Scrivio Interview library
ConceptFoundations6 min read · updated 13 November 2026

Every Redis command is a request and a reply over the network, so for small commands the round-trip time, not Redis itself, usually limits how fast one client can go.

Compare pipelining, multi-key commands such as MGET, MULTI/EXEC transactions and Lua scripts as ways to cut round trips. Which are atomic, and when would you choose each?

Pipelining sends many commands without waiting for each reply, which saves round trips but is not atomic. Multi-key commands such as MGET and MSET are one command, so they are one round trip and atomic, but cover only one kind of operation. MULTI/EXEC runs a queued group atomically but cannot use a read result to decide a later command, and Lua scripts or functions run atomically on the server with logic between commands, at the cost of blocking the server while they run. I choose pipelining for bulk independent work, MGET-style commands for simple batches, MULTI/EXEC for atomic groups of writes, and Lua for read-decide-write logic.

Why this question is asked

For small commands, the network round trip usually costs far more than Redis's own work: a GET executes in about a microsecond, while a round trip inside a data centre takes 0.2 to 1 ms. So the way we group commands decides how fast a client can go. Interviewers ask this comparison because the four options look similar but differ in what they guarantee, and choosing wrongly gives either slow code or subtle race conditions. In this answer, we will see why round trips dominate, then go through pipelining, multi-key commands, MULTI/EXEC and Lua with an example of each, and finish with how to choose.

1Why Round Trips Dominate

Every Redis command is a request and a reply. If a client sends one command, waits for the reply, then sends the next, its speed is limited by the round-trip time (RTT). With an RTT of 0.5 ms, one client can do at most 2,000 commands per second, however fast Redis is. Redis, meanwhile, could execute hundreds of thousands. Each command also costs a read and a write system call on both sides.

All four techniques reduce the number of round trips. What differs is whether the group of commands is atomic, meaning no other client's command can run in the middle, and whether logic can run between the commands.

2Pipelining: Many Commands, One Trip

With pipelining, the client writes many commands to the socket without waiting, then reads all the replies. Redis processes them in order as they arrive and queues the replies. Three hundred commands then cost roughly one round trip plus the execution time.

Java
// Spring Data Redis: 300 GETs in one round trip
List<Object> prices = redis.executePipelined((RedisCallback<Object>) conn -> {
StringRedisConnection c = (StringRedisConnection) conn;
for (String id : ids) c.get("price:" + id);
return null; // results are collected by executePipelined
});
// prices.size() -> 300, in request order

But pipelining is not atomic. Redis reads the commands from this client in chunks and may execute other clients' commands between them. It is also not all-or-nothing: if the client disconnects halfway, some commands ran and some did not. We should send pipelines in batches, often hundreds to a few thousand commands, so the server's reply buffer and the client's memory stay bounded.

3Multi-Key Commands: One Command, Many Keys

MGET, MSET, HMGET, DEL with several keys and similar commands do several operations as a single command. That makes them one round trip and atomic, because one command always runs alone on the main thread.

Text
MSET price:1 10 price:2 12 price:3 9 -- OK (all three set at once)
MGET price:1 price:2 price:3 -- 1) "10" 2) "12" 3) "9"
DEL tmp:1 tmp:2 tmp:3 -- (integer) 3

Their limit is that each covers only one kind of operation: MSET cannot set a TTL, and there is no command that does a GET on one key and an INCR on another. In Redis Cluster, all the keys in one multi-key command must hash to the same slot, otherwise Redis replies with a CROSSSLOT error; hash tags such as price:{eu}:1 put related keys in one slot.

4MULTI/EXEC: An Atomic Group

MULTI starts a transaction. The following commands are queued, not executed, and each returns QUEUED. EXEC runs all of them back to back, with no other client's command in between. If the client uses a pipeline for the whole block, it is one round trip too.

Text
WATCH stock:42
GET stock:42 -- "5" (read before MULTI)
MULTI
DECRBY stock:42 1 -- QUEUED
RPUSH orders:42 "o-9001" -- QUEUED
EXEC -- 1) (integer) 4 2) (integer) 1
# if another client changed stock:42 after WATCH, EXEC returns (nil) and nothing runs

The catch is that a command inside the transaction cannot use the result of an earlier one, because nothing executes until EXEC. To make a decision based on a read, we read before MULTI and protect it with WATCH, an optimistic check: if a watched key changes before EXEC, the transaction is discarded and we retry. Also note that Redis does not roll back: if one queued command fails at runtime, for example a type error, the others still run.

5Lua Scripts and Functions: Logic on the Server

A Lua script, sent with EVAL or EVALSHA, or a function registered with FUNCTION LOAD in Redis 7, runs entirely on the server, atomically, and can read, decide and write in one round trip. This is the tool when the next step depends on a value we just read.

Java
-- decrement stock only if enough is left; returns the new stock or -1
local left = tonumber(redis.call('GET', KEYS[1]) or '0')
if left < tonumber(ARGV[1]) then return -1 end
return redis.call('DECRBY', KEYS[1], ARGV[1])
-- EVAL <script> 1 stock:42 2 -> 3
-- EVAL <script> 1 stock:42 5 -> -1 (not enough, nothing changed)
Choosing a way to cut round trips
TechniqueRound tripsAtomicLogic between commands
Pipeliningone per batchnono
Multi-key command (MGET, MSET)oneyesno, one operation type
MULTI/EXEC (+ WATCH)one if pipelinedyesno; WATCH gives optimistic checks
Lua script or functiononeyesyes: read, decide, write
Atomic means no other client's command runs in the middle.

The cost is that the script blocks the main thread while it runs, like any command, so it must stay short. A script that loops over 100,000 elements stalls every client. In Cluster, all keys a script touches must be passed in KEYS and be in one slot.

6Follow-up questions

Can you pipeline several MULTI/EXEC blocks?

Yes. A pipeline is just a way of sending commands without waiting, so we can send MULTI, the queued commands, EXEC, then another MULTI block, all in one write. Each block is atomic on its own; other clients' commands may run between the blocks but never inside one. This is a common way to apply many small atomic updates efficiently.

When would you choose MULTI/EXEC over a Lua script?

When the group is a fixed set of writes that does not depend on values read inside it, such as updating a counter and appending to a log together. It needs no script management, and each command is a normal, visible command. When a decision depends on a value, Lua is simpler and avoids WATCH retries.

Does pipelining help if Redis is already at 100 percent CPU?

It reduces the syscall and parsing work per command, so it often lowers CPU too. But if the CPU is spent executing expensive commands, pipelining them does not make them cheaper.

7Conclusion

In this answer, we saw that round trips, not Redis's execution, limit a client issuing small commands. Pipelining batches any commands into one trip without atomicity, multi-key commands are atomic but limited to one operation type and one slot in Cluster, MULTI/EXEC runs a group atomically without using intermediate results, and Lua scripts and functions add server-side logic atomically at the cost of blocking while they run. We pipeline in bounded batches and pick the others by whether we need atomicity and logic.

What to say in the interviewAbout 80 seconds aloud

Open with

All four cut round trips, but they guarantee different things. Pipelining sends many commands without waiting for each reply and reads the replies afterwards. It saves round trips and syscalls, but it is not atomic: other clients' commands can run between mine.

Then, if they want more

Multi-key commands like MGET, MSET, HMGET or a DEL with several keys are a single command, so one round trip and atomic, but they cover one kind of operation, MSET cannot set TTLs for example, and in Cluster all the keys must be in one slot.

MULTI/EXEC queues commands and runs them together atomically; WATCH adds an optimistic check on keys I read before, but a read result cannot decide a later command inside the transaction, and there is no rollback if a command fails.

Lua scripts and functions run atomically on the server with logic in between, so read, decide, write in one round trip, at the cost of blocking the server while they run, so they must stay short.

So I pipeline bulk independent work in batches of hundreds to a few thousand commands so buffers stay bounded, use MGET-style commands for simple batches, MULTI/EXEC for an atomic group of writes, and Lua when the logic depends on a value.

About this answer

Last updated
13 November 2026
Versions it names
Redis 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Compare pipelining, multi-key commands such as MGET, MULTI/EXEC transactions and Lua...": https://scrivio-interview-library.netlify.app/databases/redis-internals/8680cafe3e/