Concurrent Reads, Serialized Writes with GenServer and Registry

Creating a process that maintains a state and makes it available for other processes is one of the most common use cases for GenServer. And the very first optimization that is usually done here is using ETS to enable concurrent reads. In this post I show a simple Registry trick that makes it easier to do.

7 Likes

Short and sweet. Piggy-backing on Registry as a KV store is something I came up on my own and was wondering if it’s well-known.

2 Likes

Yep, I love Registry! It was a little hard to grasp at first, but once it clicked… It’s weirdly powerful.

2 Likes

Concurrent readers on the ets table are fine for single-key reads, but if you want serializable multi-key reads you’ll either have to roll your own concurrency control or go back to the GenServer. Likewise if you’re only performing single-key writes you can allow concurrent writers on the ets table and it will still serialize the operations for you (it has its own locks).

1 Like

Nice blog! Does it have an RSS feed?

Thanks! No, no RSS yet, I need to look into adding it tho