I am facing concurrency problem with redis, my API is build on Nodejs Fastify and i am using fastify-redis in my API call.
I am using two simple methods of redis ZRANGEBYSCORE and ZINCRBY
The problem is under high concurrency ZINCRBY is executed late which results into giving me same value on multiple request.
How can i prevent this under high concurrency is there any method to lock the KEY which was previously executed.
Here is the example of my code
numbers = await redis.zrangebyscore(
`user:${req.query.key}:${state}`, //key
0, // min value
50, // max value
"LIMIT",
0, // offset
1 // limit
);
if (numbers.length > 0) {
await redis.zincrby(`user:${req.query.key}:${state}`, 1, numbers[0]);
res.send(numbers[0]);
}