Using libraries like singleton you can create applications which are the only one in the whole cluster. This application may be a GenServer which initializes an ETS table and delegates the requests to this ETS. It is like a thin wrapper to convert a local ETS into a global one.
I have this doubt because I read the following in the hammer documentation:
There may come a time when ETS just doesn’t cut it, for example if we end up load-balancing across many nodes and want to keep our rate-limiter state in one central store. Redis is ideal for this use-case, and fortunately Hammer supports a Redis backend.
If you need a shared cache for an elixir cluster, what do you prefer to use a Redis server or a singleton ETS or even a singleton cachex?
I can think advantages and disadvantages of both methods.
- With singleton ETS or Cachex you can save Elixir structs or any erlang term, in redis you have to
castdata from database to memory, unless you use very basic data structures like strings or numbers.
- Singleton ETS won’t be as performant as a local one, but I think in most of the case could be enough. Maybe is it faster than Redis anyway?
- With Redis you have to create a bigger wrapper to use it like cache.
- You lose your cache if the node dies. This can be acceptable in some scenarios.
- Less architectural dependencies.
- Redis has built-in some interesting features like TTL.
I didn’t test it, because of that my question is: Is a singleton ETS a good idea or it has his own drawbacks? Or would you prefer Redis?