Persistence Strategies

Understand the importance of persistence in distributed Elixir and also get some valuable points from MOZ's case study.

In the previous chapters, we learned about Elixir’s excellent tools for reasoning about the state. In this chapter, we’ve begun to explore some of the abstractions for building distributed systems. At this point, the question of when to use Elixir abstractions and when to instead rely on off-the-shelf solutions may arise. Sometimes the lines between creating a database and a GenServer may blur.

This is a common question. The team at Plataformatec heard similar questions from the community and different clients around the world. Those conversations often involved different technologies, ranging from databases to messaging systems. To address such inquiries, the first topic Plataformatec engineers would bring up was about persistence.

To frame any persistence application, we need to answer one question first: “Can we afford to lose the data?” Often, an application’s data is essential, and losing it is catastrophic, but sometimes, such as in a cache, the data is disposable. We call such data ephemeral or being in an ephemeral state. If losing data is not an option, the choice is usually easy. Don’t reinvent the wheel. Instead, choose a database that fits individual constraints.

Dealing with ephemeral data

If the data is ephemeral, you’re in for a treat. Elixir is great at dealing with such problems. In such cases, we can likely keep the information in memory, and use all the tooling Elixir provides. Let’s view a classic example of an ephemeral state.

Imagine that we want to show the number of users connected to our ...