emoragaf
Best way to generate sequential ids
Hello, we have this situation right now:
Every time we make a sale we need to emit a document with a unique and sequential document number.
These numbers belong to a sequence based on the product sold (each document is for one product), we have a reserved range of numbers for each product, the ranges are big enough that we do not need to worry about running out of numbers right now.
We are looking at some options to generate these numbers (postgres sequences, Kafka events, Zookeeper sequence nodes), and I thought that maybe this is a good fit for Elixir.
I’m thinking of spawning a GenServer for each product configured with the sequence range and current value, so I can use it to generate the subsequent numbers.
I would persist the updated values to a DB to restore the processes in case of failure, and in case of needing some redundancy I could use something like libcluster + swarm to spread my GenServers across a cluster
Does this look right? Maybe I’m missing an obvious alternative, any pitfalls I might not be taking into account?
Most Liked
lalo2302
In my company we had the exact problem you have and we solved it using Postgres advisory locks:
PostgreSQL: Documentation: 9.4: Explicit Locking Point 13.3.5
So you don’t deal with the concurrency on your elixir application, but on the database level.
So let’s say you want to have a sequential number for product “A”, and 2 concurrent requests come
Request 1 and 2 concurrently:
-
- Comes in
-
- Comes in
-
- Sets advisory lock with id
:erlang.crc32("A")
- Sets advisory lock with id
-
- Sets advisory lock with id
:erlang.crc32("A")
- Sets advisory lock with id
-
- Lock is being used, I’ll wait until it is free
-
- Query count “A” => returns 2
-
- Inserts new record with 3
-
- Finishes transaction and releases lock
-
- Lock is available, I can proceed
-
- Query count “A” => returns 3
-
- Inserts new record with 4
I hope I was explicit enough. Using postgres takes away you a lot of headaches. You don’t need to worry about if your new GenServer is a bottle neck, or if you deploy on a cluster how should you manage your processes.
shanesveller
Do the counters / sequences need to be durable across BEAM restarts, or internally consistent across multiple BEAM nodes? Both of those are challenging with any of the ideas above. Some other questions that come to mind:
Do they need to be monotonic across all machines?
Are gaps permissible?
Are collisions disastrous?
Can you overflow a given product’s range?
Most of these point me towards solutions on the persistent storage rather than application code, i.e. a specialized use of Postgres sequences or similar.
I’d also be curious what real-world or business constraints are at play here since the described technical limitations are unusual.
kokolegorille
ksuid generate unique, sortable id.
You could (post/)prepend with your product id if You want.
Now your solution with a Genserver per product could work… it depends how much do You sell, but if You sell too much You might have a bottleneck. It will not work that well in a distributed mode.
It depends also if You just want an increasing counter, or uuid style id.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








