can multiple clients access same list concurrently

2019-03-31 04:43发布

问题:

Suppose there are two clients which are accessing same redis list datastructure. One is doing LPOP and other is doing RPUSH on the same list. Will there be a any contention between these two clients if they are running in parallel ? Will Redis lock mylist (below) when one client is accessing it,even if the clients running in parallel are accessing different ends of mylist?

Client 1
RPUSH mylist a
RPUSH mylist b

Client 2
LPOP mylist
LPOP mylist

Client 1 and Client 2 are running in parallel. Let me know if there will be contention in such a scenario.

回答1:

Redis is single-threaded, so every command that comes to it is (guaranteed to be) executed atomically. There is no parallel/concurrent access to redis' data structures, so in your scenario you cannot tell who's gonna execute first.



回答2:

one won't block the other, unless you use MULTI/EXEC to create a transaction, to avoid race conditions where one client POPS before the other client finishes to PUSH N values.



标签: redis