lcabrini
What would be the “proper” way to implement a simple persistent key/value store? Settings that are read far more than they are written. In Go I used a single postgres table and on application start-up I read it into to a mutex-backed struct. The write method would persist the setting in the database.
I wouldn’t necessarily need to use Postgres as a backing store, but that way the settings get backed up with the rest of the data. That being said, I’m not against using a non-postgres backing store, if that makes sense.
I’ve looked a bit into ETS, Mnesia, I also read somewhere that it would be a good idea to build a GenServer around a key/value store. I feel a bit overwhelmed by the choices and would like a pointer in the right direction. Thank you.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
derek-zhou
ETS is not persistent, DETS and Mnesia are. As you said, there are many choices, so your usage pattern and what you feel more comfortable play a big part.
If I were you I will just use Postgres, especially if you are going to need a relational db down the road anyway.
Schultzer
GenServers are great if you don’t need to scale, as they are notorious for being a bottleneck, the best persistent key value store is DETS if you only need it for a single node or persistent_term.
The Erlang docs are pretty comprehensive, especially if you know your constraints.
lcabrini
Thanks. I think you are right, I feel comfortable with postgres so I I’ll stick to that. So now I just need to figure out how to cache the table in memory and trigger a write to db only when the data has been modified.
lcabrini
I see. I saw something about DETS, but assumed it was probably distributed ETS, hence overkill for my needs and still not persistent. I guess I was wrong. Thank you! I’ll look deeper into DETS, this time without prejudice.
l00ker
This would be a good fit for cachex or nebulex if you don’t want to roll your own solution.
garrison
If you’re using Postgres, use Postgres. Otherwise use SQLite. Create a table with binary keys/values and then use ETS as a write-through cache.
Always write to the DB first and then the cache before returning from the write function. Always read from the cache.
Load the rows from the DB into the cache at startup. If you want to use arbitrary terms just encode them with
term_to_binary().Edit: the
put()function above is only correct for a single writer, meaning it cannot be used from multiple processes without synchronization. See @Asd 's more thorough answer below for an example that uses a GenServer as a single writer to serialize writes.garrison
Given the number of times “disk” is mentioned, I’m pretty sure it’s “disk ets”. Distributed ets sure would be nice, though.
Asd
I’d do this like this if you want it backed by postgres table
But if you’re okay with just a file on disk, I’d consider using a
detsHowever, I am going to release much more performant persistent LVM KV db in the upcoming months, so I will reply here again once it’s ready
Asd
This code is very wrong and buggy.
Consider two processes calling the
putfunction at the same time. If one doesput("key", 1)and other doesput("key", 2), it is possible that the order of operations would beAnd postgres database would have
2written while ets would have1. Wrapping it into GenServer (or any other locking mechanism) is a mustgarrison
You are 100% correct, the OP mentioned settings so I assumed a single writer without even thinking about it.