Do we need a process for ETS tables?

10.3.2: You might wonder why GenServer is still used in the ETS-based key/value store. The sole purpose of this process is to keep the table alive. Remember, an ETS table is released from memory when the owner process terminates. Therefore, you need to have a distinct, long-running process that creates and owns the table.

The GenServer simply acts as the owner process. When the owner process is killed by the supervisor the ETS table goes with it. Then the supervisor restarts the GenServer which in turn creates a fresh ETS table.

if the table crashes

How would a table crash??? Granted the contents can get corrupted which can lead to processes that rely on it to crash. If that can happen then it makes sense to run the owner process and client process under a :one_for_all supervisor which would also kill the owner process to create a fresh table.

But there are other use cases where the ETS table acts as a backing store to bridge the termination of one process when it is replaced with a new one - in that case it makes sense to have the supervisor own the table (Don’t loose your ETS tables).

Article: The Hitchhiker’s Guide to the Unexpected

4 Likes