How does Redis handle atomic operations like INCR?
Learn why Redis INCR is atomic — single-threaded execution, no lost updates, and how MULTI/EXEC and Lua extend atomicity across multiple commands.
Expected Interview Answer
Redis executes commands like INCR atomically because it processes commands one at a time on a single main thread — INCR reads the current number, adds one, and writes it back as an indivisible unit, so concurrent clients can never interleave and lose an update.
Since only one command runs at any instant, there is no read-modify-write race the way there is with application-side GET then SET. INCR also parses the stored string as an integer, increments it, and stores the result, erroring if the value is not a valid integer or overflows 64-bit range. For multi-step atomicity across several keys, Redis offers MULTI/EXEC transactions and Lua scripts via EVAL, both of which run without interruption from other clients.
- No lost updates under high concurrency
- Single-threaded command execution removes race conditions
- INCR/DECR/INCRBY cover counters in one round trip
- MULTI/EXEC and Lua extend atomicity to multiple commands
- Simpler than application-side locking for counters
AI Mentor Explanation
Imagine a single scorer who is the only person allowed to touch the run tally. Each time a run is scored they read the total, add it, and write the new number before anyone else can look. Because only one hand ever updates the board, two scorers can never both read 120 and both write 121 — every run is counted exactly once, just as Redis serializes each INCR.
Step-by-Step Explanation
Step 1
Command arrives
A client sends INCR counter; it joins Redis's single command queue.
Step 2
Single-threaded execution
Redis runs one command at a time, so INCR cannot interleave with another client's command.
Step 3
Read-modify-write in one step
Redis parses the stored integer, adds one, and writes the result atomically.
Step 4
Validation
If the value is not a valid 64-bit integer, INCR returns an error instead of corrupting data.
Step 5
Return the new value
The client receives the post-increment number, useful for counters and ID generation.
Step 6
Extend with MULTI or Lua
For multi-key atomicity, wrap commands in MULTI/EXEC or a Lua script that also runs uninterrupted.
What Interviewer Expects
- Understanding Redis's single-threaded command model
- Why INCR avoids the GET-then-SET race
- Knowledge of the integer parsing and overflow error
- Awareness of MULTI/EXEC and Lua for multi-command atomicity
- Practical uses like counters and rate limiting
Common Mistakes
- Implementing counters with GET then SET in the app
- Assuming Redis needs locks for INCR
- Confusing single-threaded command execution with no I/O concurrency
- Ignoring the integer-only requirement of INCR
- Thinking MULTI/EXEC provides rollback like SQL transactions
Best Answer (HR Friendly)
“Redis handles one command at a time, so when INCR adds one to a counter, no other request can sneak in halfway. That means many users can increase the same counter at once without ever losing a count, which is why it is great for things like page-view counters.”
Code Example
SET views 0
INCR views # -> 1
INCR views # -> 2
INCRBY views 10 # -> 12
# Two clients calling INCR concurrently
# still produce 13 then 14 — never a lost update# MULTI/EXEC runs queued commands without interruption
MULTI
INCR orders:count
SET orders:last "job42"
EXECFollow-up Questions
- Why does single-threaded execution make INCR atomic?
- How does INCR differ from an application-side GET then SET?
- How would you build a rate limiter using INCR and EXPIRE?
- What does MULTI/EXEC guarantee and what does it not guarantee?
- When would you reach for a Lua script instead of INCR?
MCQ Practice
1. Why is INCR atomic in Redis?
Redis executes commands serially on its main thread, so an INCR's read-modify-write cannot interleave with another command.
2. What happens if you INCR a key holding a non-integer string?
INCR requires a value parseable as a 64-bit integer; otherwise it returns an error rather than corrupting the value.
3. Which feature extends atomicity across multiple Redis commands?
MULTI/EXEC transactions and Lua scripts run their commands without interruption from other clients.
Flash Cards
Why doesn't INCR need a lock? — Redis runs commands one at a time on a single thread, so there is no race.
What does INCR return? — The value of the key after the increment.
What if the value isn't an integer? — INCR returns an error instead of modifying the key.
How do you increment by more than one? — Use INCRBY key amount.
How do you make several commands atomic? — Wrap them in MULTI/EXEC or run a Lua script via EVAL.