1. 9

What I liked about this post is the usage of the Elixir and OTP standard libraries to easily replace a third-party dependency (Redis) in a personal project.

  1.  

  2. 13

    If you are going to use ETS as a cache, there are a couple of points you should keep in mind:

    • There is no garbage collection for ETS. The memory used by the table will stay used unless you delete elements in the table or the process owning the table dies.
    • If you don’t want to loose the contents of the table when its owner dies, you need to set a heir to receive it. Personally, I tend to make any “important” ETS table outside of the process which uses it then, give it.This means that creating process can be the heir of the table and hold all of my logic for this scenario.
    1. 5

      This is a nice demo of how to glue GenServer and an ETS table together as simply as possible, however there are better choices for a cache used in production. Check out Cachex or ConCache.

      1. 4

        You don’t need to make a call to a process to read a from an ETS table like this. The get should just read directly from the ETS table. You’ll also want to set read_concurrency to true when creating the table.

        You can also write to the ets table concurrently, but here the important one is to not send all reads from the cache through the same process.

        1. 2

          It’s also interesting to note, that both ETS & DETS (or disk erlang term storage) is a derivative of the mnesia application. I use ETS respectfully because of this, and use mnesia 99% of the time as it’s a first class corner of OTP, whereas ETS I don’t think it was intended for anything but for a means to implement mnesia. Just food for thought.