3 ms·
Wait, how does that answer prevent only one instance of the job across the whole system? That's what I'd use Redis or something for - for locking.
by winrid 6y ago
Wait, how does that answer prevent only one instance of the job across the whole system? That's what I'd use Redis or something for - for locking.
- acdibble 6y agoIf there is only one process running this GenServer, there will only be one instance of the job in the system. An BEAM process, and therefore a GenServer, has an atomic message queue built in. Only the process itself can pop messages out of its queue, so there is no need for locking. It is not possible to get messages out of the queue concurrently. Messages can be processed concurrently, but that's after they've already been removed from the queue.
- winrid 6y agoWhat if you have 100 servers running the same application code and you just want to run this job, one time, for a customer? Usually I'd use a Cron with a distributed lock, which is served via Redis. Just curious as to how the BEAM solves it as I imagine it can.
- dnautics 6y agoYou can use tricks like the global spawn option to make processes globally unique. Doesn't scale infinitely, but for a 2->low double digits HA/load balanced system it should be fine.
- winrid 6y agoOh wow, so this will work across multiple instances of the BEAM?
- dnautics 6y agoIf they are clustered using erlang clustering, yes. https://erlang.org/doc/man/global.html https://erlang.org/doc/man/global.html Note that this works by taking a transaction lock on the entire cluster, so it's not recommended if you're making requests frequently. It also doesn't respect netsplits, unless you instruct in code to fail with a deficient cluster membership.