Skip to content
Sign inGet all access
Chapter 9·12 minFree

Atomicity

The four ways to avoid a race.

Redis runs one command at a time, so a single command is safe. The danger is two commands with a gap.

The read-then-write race

Twenty clients each read a counter, add one, and write it back:

terminal
redis-cli GET counter
Output$ # 20 clients: GET counter, then SET counter (value + 1)$ redis-cli GET counter"1"$ # 20 clients: INCR counter$ redis-cli GET counter"20"

Atomic single commands

The fix is to stop reading first. Each of these is one command:

INCR hits
INCR hits
LPUSH q a
LMOVE q done LEFT RIGHT
ZINCRBY board 5 ada
Output(integer) 1(integer) 2(integer) 1"a""5"

A lock

When no single command fits, SET with NX and EX claims a lock and gives it a lifetime in one step:

SET lock 1 NX EX 30
SET lock 1 NX EX 30
TTL lock
OutputOK(nil)(integer) 30

OK means you hold it, (nil) means someone else does. The EX is not optional:

SETNX lock 1
TTL lock
Output(integer) 1(integer) -1

Releasing it safely

DEL lock is wrong: your lease may have expired. Store a token and delete only if it matches:

terminal
redis-cli SET lock token123 EX 30
redis-cli EVAL "if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) else return 0 end" 1 lock wrongtoken
redis-cli EXISTS lock
Output$ redis-cli SET lock token123 EX 30OK$ redis-cli EVAL "if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) else return 0 end" 1 lock wrongtoken(integer) 0$ redis-cli EXISTS lock(integer) 1$ redis-cli EVAL "if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) else return 0 end" 1 lock token123(integer) 1$ redis-cli EXISTS lock(integer) 0

MULTI and EXEC

MULTI queues commands and EXEC runs them with nothing interleaved:

MULTI
INCR a
INCR a
EXEC
GET a
OutputOKQUEUEDQUEUED1) (integer) 12) (integer) 2"2"

What MULTI is not

Not a transaction in the SQL sense. A runtime failure does not stop the others, and nothing rolls back:

SET name ada
MULTI
INCR name
SET ok yes
EXEC
GET ok
OutputOKOKQUEUEDQUEUED1) (error) ERR value is not an integer or out of range2) OK"yes"

A command Redis cannot even parse is different — the whole block is refused:

MULTI
NOSUCHCOMMAND x
SET ok yes
EXEC
EXISTS ok
OutputOK(error) ERR unknown command 'NOSUCHCOMMAND', with args beginning with: 'x' QUEUED(error) EXECABORT Transaction discarded because of previous errors.(integer) 0

You also cannot branch. A GET inside the block returns its value only in the EXEC reply, too late to use:

SET n 5
MULTI
GET n
INCR n
EXEC
OutputOKOKQUEUEDQUEUED1) "5"2) (integer) 6

WATCH

WATCH closes that gap: EXEC runs only if the key has not changed. Nothing touched it here:

SET n 5
WATCH n
MULTI
INCR n
EXEC
OutputOKOKOKQUEUED1) (integer) 6

If another client writes to a watched key, EXEC abandons everything:

terminal
redis-cli
Output$ # connection A127.0.0.1:6379> SET n 5OK127.0.0.1:6379> WATCH nOK127.0.0.1:6379> MULTIOK127.0.0.1:6379> INCR nQUEUED$ # connection B, before A commits: SET n 99127.0.0.1:6379> EXEC(nil)127.0.0.1:6379> GET n"99"

EVAL

A Lua script runs as one command, so everything inside it is atomic:

EVAL "return redis.call('INCR', KEYS[1])" 1 counter
GET counter
Output(integer) 1"1"

Choosing between them

Use when Cost
One atomic command there is one that fits none — always try this first
SET NX EX you need a lock around other work must handle expiry and release
MULTI/EXEC several writes must land together no branching, no rollback
WATCH you must read before deciding a retry loop
EVAL the logic needs branching Lua, and it blocks the server