Cache and ETS

Moz fetched all of their data up front using a databaseless strategy. Another company called Ministry of Games kept mutable data in-memory and persisted it to the database at specific moments and time intervals.[61] Those are great examples of leveraging the power of Elixir to design optimal solutions.

You can also leverage the tooling provided by the VM machine on simpler problems, such as caching. A cache allows you to store the result of a computation and re-use it several times, perhaps by multiple entities. Caches are a classic example of ephemeral data. If you lose the cache, you can just rebuild it again.

In Elixir, when you need to store shared data across multiple processes, you can use ETS,[62] which is the Erlang Term Storage. ETS provides a high-level mechanism for storing data in-memory, and it’s often the tool of choice for caches. For example, Ecto uses ETS to cache the compilation of Ecto queries, leading to great performance. Then each application needs to compile its query once. There’s one ETS table per node, and a single Elixir node scales very well, so ETS makes a highly efficient cache.

If you can build the cache cheaply and you have multiple instances of the Erlang VM deployed across multiple servers, it’s better for each instance to have its own cache rather than using some network cache. We call such a cache local. Remember: accessing the data stored in-memory is orders of magnitude faster than using any external service. That’s why Elixir developers rarely resort to Redis or Memcached. ETS, or ETS abstraction such as con_cache,[63] is almost always a better solution.

If a local ETS cache is not enough, developers can use the tools described in this chapter to provide more sophisticated solutions. For example, imagine that building the cache is slightly expensive and, if another node in the network has already built the cache, you would rather get a copy of the cache than rebuild it. You can use a process group implementation to keep a list of processes across your cluster that hold certain caches. When you need to access the cache and the list of caches is not empty, you can ask one of those processes to send the cache to you.

In the worst-case scenario, where computing of the cache on demand is infeasible, you may want to resort to a more robust solution that builds the cache in the background and updates a global storage, such as a database or even S3, in a solution quite similar to the Moz application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset