Module F-7·18 min read

Why sequential redis.get() in a loop is the most common Redis performance mistake, what RESP looks like on the wire, how pipelining eliminates per-command RTT, and when to use MGET/MSET vs explicit pipelines.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

F-7 — Pipelining and the RESP Protocol

Who this module is for: You use Redis in a loop — fetching a list of keys one by one, or updating a batch of records sequentially. Your Redis operations work, but you have not thought about what is happening at the network level. This module explains why that pattern is slow, what the RESP protocol looks like on the wire, and how pipelining eliminates the per-command network round-trip to make batch operations 10–100x faster.


The Network Round-Trip Problem

Every Redis command you issue pays a cost that has nothing to do with Redis itself: the time for bytes to travel from your application to the Redis server and back. This is the Round-Trip Time (RTT).

On a typical cloud setup — application and Redis in the same region, connected via a private network — RTT is 0.5–2ms. On a local machine (Redis and app on the same host via TCP loopback), it is < 0.1ms.

Now consider this pattern — common in codebases everywhere:

javascript

With 1,000 keys and 1ms RTT, this takes at minimum 1,000 × 1ms = 1 second. Redis itself processes each GET in microseconds. You are spending 99.9% of your time waiting for network round-trips.

This is the problem pipelining solves.


What the RESP Protocol Looks Like

Before diving into pipelining, it helps to understand the protocol Redis uses. RESP (REdis Serialization Protocol) is a text-based protocol designed to be simple to implement and fast to parse.

Every Redis command is serialized as a RESP Array. For SET mykey "hello":

text

The server's response for OK:

+OK\r\n ← Simple string

For a GET response returning "hello":

text

For (nil):

$-1\r\n ← Null bulk string (length -1)

For an integer (e.g., INCR returning 42):

:42\r\n ← Integer

For an error:

-ERR wrong number of arguments for 'get' command\r\n

Why this matters for pipelining: RESP is a streaming protocol. Redis reads commands from a socket buffer, processes them, and writes responses back. It does not require request-response pairing — you can send 100 commands before reading any response, and Redis will process them in order and write 100 responses in order.


What Pipelining Is

Pipelining is the technique of sending multiple commands to Redis in a single network write — without waiting for a response between each command. Redis processes them in order and sends all responses back together.

Without pipelining (naive):

text

With pipelining:

text

One network write, one network read, 1,000 results. RTT paid once instead of 1,000 times.


Pipelining in Node.js (ioredis)

javascript

The exec() call sends all queued commands to Redis in a single write and waits for all responses.

Benchmarking the difference:

javascript

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.