Reputation: 9133
I have a node backend REST API that will be deployed as multiple containers.
I have an array of users = ['u1', 'u2', 'u3', 'u4'] that can be stored in mongoDb or redis
I have a get endpoint that returns next user from this array.
Eg: for 1st request it should return u1, 2nd request should return u2, 5th request returns u1.
once this API is deployed as multiple containers, Lets say there are 10 containers running and 10 requests are concurrently made, how do I make sure that they are returned in order.
Im looking for a standard approach to handle this scenario. Following is an approach I have thought of
Store the array as list on redis.
Do LPOP and RPUSH on each request So that list after the first request would be
['u2', 'u3', 'u4', u1]
Concern here is that if 5 requests comes in parallel, there could be a scenario, where 5LPOP could be executed and array could be empty and RPUSH could be executed out of order.
What is the best approach to handle this issue?
Upvotes: 0
Views: 183
Reputation: 25929
In Redis you'd need to use MULTI to get a transaction-like behavior
"All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served in the middle of the execution of a Redis transaction. This guarantees that the commands are executed as a single isolated operation" https://redis.io/topics/transactions
note that Redis transactions does not mean strict serializability see https://jepsen.io/analyses/redis-raft-1b3fbf6
Upvotes: 2