Command Palette

Search for a command to run...

Hectal
Phase 3Intermediate4 of 17 in Redis

Atomicity: Transactions, Lua, Functions & Pipelining

What's atomic in Redis and what's merely batched: MULTI/EXEC with WATCH, Lua scripts, Redis Functions, and pipelines, with the problem each one solves.

Single commands are atomic. The moment a feature needs "read, decide, write" across one or more keys, you need one of four tools: MULTI/EXEC, WATCH (optimistic locking), Lua scripts, or Redis Functions. Pipelining looks similar but gives no atomicity at all.

Interviewers love this area because it separates people who've used Redis from people who understand it: "Is a pipeline a transaction?", "Why can't MULTI make a decision?", "What happens if a Lua script runs for 10 seconds?"

0/5 · 0%
5 topics ~57 min 14 code blocks & diagrams
Start with the first topic
1
3.1

The Atomicity Ladder: Command, MULTI, Lua, Functions

Redis offers four levels of atomicity: a single command, MULTI/EXEC (a queued batch that runs without interleaving), Lua scripts (logic that runs without interleaving), and Functions (Lua libraries stored on the server). None of them roll back, and pipelines aren't on the ladder at all.

10 min 1 diagram 1 code practice

2
3.2

MULTI/EXEC and WATCH: Optimistic Concurrency

MULTI queues commands and EXEC runs them together without interleaving. WATCH adds optimistic check-and-set: if any watched key changes before EXEC, the transaction aborts and the client retries. It solves the classic lost-update race, at the cost of retries under contention.

11 min 1 diagram 3 code practice

3
3.3

Lua Scripting: Atomic Logic on the Server

EVAL runs a Lua script atomically on the server: read keys, branch, write, return, in one round trip. It's the standard tool for rate limiters, safe unlocks and conditional updates, as long as scripts stay short, declare their keys, and are loaded efficiently with EVALSHA.

17 min 3 code practice

4
3.4

Redis Functions: Server-Side Logic as a Deployed Artifact

Redis Functions (7.0+) are named Lua functions grouped into libraries, loaded once with FUNCTION LOAD, called with FCALL, and persisted and replicated like data. They fix the script-cache problems of EVALSHA and give server-side logic a versioned deployment lifecycle.

9 min 2 code practice

5
3.5

Pipelining: Paying the Round Trip Once

Pipelining sends many commands without waiting for each reply, turning N round trips into roughly one. It's the single biggest throughput win for bulk work, but it's not atomic, and very large pipelines cost memory on both sides.

10 min 1 diagram 2 code practice