Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle concurrent updates to redis key?

I searched in google on the same but I couldn't find a clear cut answer.

The use case I want to solve is following. Multiple systems sends out the process completion status all associated to the same identifier. I want to send out a notification when all processes associated with the same identifier gets completed. I thought of using redis as my data store with identifier as the key. The notification system is multi threaded and will process the status completion events coming from different systems.

Scenario: Identifier 123 has two processes p1 and p2 associated to it. Say one of the notification system thread processes p1 completion status. It pulls the data associated with 123 from redis. It sees that p2 is not yet completed. It adds p1 completion status to the redis value and updates the key. Say the same thing happens in parallel for P2 completion status. At the end none of my two threads emit the final completion status. How to go about solving this issue?

I see from redis documentation that redis is single threaded. Does that mean that when one thread performs an update for a key, no other thread can perform any read/write operation for the same key?

like image 740
karthik.zorfy Avatar asked Apr 20 '18 13:04

karthik.zorfy


Video Answer


1 Answers

I see from redis documentation that redis is single threaded. Does that mean that when one thread performs an update for a key, no other thread can perform any read/write operation for the same key?

Yes, but that does mean that another thread won't read the value before the current thread updates it with a new value. It also doesn't mean that the other thread, not knowing about the update that current thread made, will not overwrite the update with something of its own.

Read about Redis' transactions and specifically make sure that you understand how WATCH works.

like image 79
Itamar Haber Avatar answered Nov 06 '22 22:11

Itamar Haber