Scrivio Interview library
DebuggingFoundations6 min read · updated 13 November 2026

A nightly Spring Boot job loads 2 million product summaries into Redis with one SET key value EX 86400 call per item. It takes 40 minutes, Redis CPU stays under 5 percent, and the job runs in another availability zone with a round trip of about 1.1 ms.

Explain why it is slow and how you would make it finish in a minute or two.

The job is bound by network latency, not by Redis: each SET waits a full 1.1 ms round trip, and 2,000,000 times 1.1 ms is about 37 minutes, which is why Redis sits idle. Pipelining the SET key value EX 86400 commands in batches of a few hundred to a thousand, for example with RedisTemplate.executePipelined, sends many commands per round trip and brings the load down to well under a couple of minutes. MSET is not an option because it cannot set a TTL, and a few parallel pipelined workers can help further if online traffic allows.

Why this question is asked

A job that takes 40 minutes while Redis does almost nothing is a classic sign of a round-trip problem, and it appears in interviews because the fix is simple once the cause is understood, but the details, such as TTLs, batch sizes and Cluster behaviour, separate a careful answer from a quick one. In this answer, we will do the arithmetic that explains the 40 minutes, rewrite the loop with pipelining, see why MSET is the wrong shortcut, look at parallel workers and Redis Cluster, and make the job safe to rerun.

1Doing the Arithmetic

The job calls SET key value EX 86400 once per product, waiting for each reply before sending the next. Each call costs one round trip between availability zones, about 1.1 ms. Redis executes the SET in a few microseconds, so almost all of that time is the network.

2,000,000 calls times 1.1 ms is 2,200 seconds, about 37 minutes. Add some serialization and client overhead, and we arrive at the observed 40 minutes. Redis CPU stays under 5 percent because it spends most of its time waiting for the next command to arrive. The job is bound by network latency, and no tuning on the Redis side can change that.

Step throughOne command per round trip
Batch jobNetwork (1.1 ms RTT)Redis
SET p:1 ... EX 86400
OK after 1.1 ms
SET p:2 ... EX 86400
OK after 1.1 ms
repeat 2,000,000 times: about 37 minutes

5 messages in order. Play them, or step through them one at a time.

Redis works for microseconds and waits for milliseconds.

2Pipelining the Writes

With pipelining, the client sends a whole batch of commands without waiting, then reads all the replies. With batches of 1,000, the job needs 2,000 round trips instead of 2,000,000, which is about 2.2 seconds of waiting. The rest of the time is spent sending the data and executing the SETs, so the job becomes bound by bandwidth and Redis CPU, and typically finishes in tens of seconds to a minute or two, depending on value sizes.

In Spring Data Redis, executePipelined does this for us: everything we call on the connection inside the callback is queued and flushed together, and the replies are collected at the end.

Java
@Component
class ProductSummaryLoader {
private final StringRedisTemplate redis;
ProductSummaryLoader(StringRedisTemplate redis) { this.redis = redis; }
void load(Stream<ProductSummary> all) {
Iterators.partition(all.iterator(), 1000).forEachRemaining(batch -> // Guava, or any batching helper
redis.executePipelined((RedisCallback<Object>) conn -> {
StringRedisConnection c = (StringRedisConnection) conn;
for (ProductSummary p : batch) {
c.set("product:" + p.id(), p.toJson(),
Expiration.seconds(86400), SetOption.upsert()); // SET key value EX 86400
}
return null;
}));
}
}
// Before: 2,000,000 round trips x 1.1 ms -> about 37 minutes
// After: 2,000 batches x (1.1 ms + send and execute time) -> about 1 minute

Notice that we keep the exact same command, SET with EX, inside the pipeline. The pipeline changes how commands travel, not what they do. Batches of a few hundred to a thousand keep the reply buffer on the server and the result list in the client small; a single pipeline of 2 million commands would hold 2 million replies in memory on both sides.

3Why Not Just Use MSET?

MSET sets many keys in one command, which looks even simpler. But MSET has no TTL option. To expire the keys we would need a separate EXPIRE per key, which brings back half the round trips unless those are pipelined too, and creates a window in which keys exist without a TTL; if the job dies in that window, the keys never expire. In Cluster, MSET also requires all keys in one slot. Keeping SET with EX inside a pipeline gives one command per key with its TTL attached, which is exactly what we want.

4Parallel Workers and Redis Cluster

If one pipelined worker is still slower than we want, a few workers in parallel can help, each loading a different range of products. Two to four is usually enough. More workers move the bottleneck to Redis CPU, and this Redis also serves online traffic, so while the job runs we should watch the main thread's CPU and the online services' latency, and keep a cap on the number of workers.

With Redis Cluster, a pipeline cannot go to a single server, because the keys belong to different nodes. A cluster-aware client splits the batch by node and sends each part to its primary. That still works well, but a batch of 1,000 keys over 6 primaries becomes six smaller pipelines, so it is worth keeping batches bounded per node and checking that the client library supports pipelining in cluster mode as we use it.

Load time by approach (2 million keys, 1.1 ms RTT)
ApproachRound tripsApproximate time
One SET per round trip2,000,000about 37-40 minutes
Pipelined batches of 10020,000about 25-40 seconds plus transfer
Pipelined batches of 1,0002,000under a minute, bandwidth bound
Batches of 1,000, 4 workers2,000 totaltens of seconds, watch online latency
Times are estimates; measure on a sample run.

5Follow-up questions

How would you make the load safe to rerun if it fails halfway?

SET with EX is idempotent: writing the same product again just overwrites the value and resets its TTL, so rerunning the whole job is already safe. To avoid redoing finished work, the job can record the last completed batch in a checkpoint key and resume from there. If readers must never see a mix of old and new data, the job can write into versioned keys, such as product:v20261112:42, and switch a small pointer key to the new version only after the whole load succeeds.

Would moving the job into the same availability zone help?

Yes, a round trip inside a zone might be 0.2 to 0.3 ms instead of 1.1, which cuts the unpipelined time to under 10 minutes. But pipelining removes the dependency on the round trip almost entirely, so it is the bigger win and works wherever the job runs.

Why not use a Lua script to load a batch?

It would also be one round trip per batch, but the script blocks the main thread while it writes a whole batch, and the values would have to be passed as arguments anyway. A pipeline gets the same throughput while letting online traffic interleave between commands.

6Conclusion

In this answer, we saw that 2 million sequential SETs at 1.1 ms each take about 37 minutes, so the job was bound by network latency and Redis stayed idle. Pipelining the same SET key value EX 86400 commands in batches of a few hundred to a thousand cuts the round trips by a factor of a thousand and brings the load to around a minute. MSET is the wrong shortcut because it cannot set TTLs, parallel workers help within the limits of online traffic, Cluster clients split pipelines by node, and idempotent SETs make the job safe to rerun.

What to say in the interviewAbout 80 seconds aloud

Open with

The job is latency-bound. It sends one SET with EX, waits for the reply, then sends the next, so every item costs a full 1.1 ms cross-zone round trip.

Then, if they want more

Two million times 1.1 ms is about 37 minutes, which matches the 40 minutes, and it explains why Redis CPU is under 5 percent: it executes each SET in microseconds and then waits for the next one. The fix is pipelining.

I would batch the writes in groups of a few hundred to a thousand, for example with RedisTemplate.executePipelined, so one round trip carries a whole batch. Then 2 million items need about 2,000 round trips, a couple of seconds of latency, and the job becomes limited by bandwidth and Redis CPU, so it should finish in a minute or two.

I would keep SET with EX inside the pipeline rather than switching to MSET, because MSET cannot set a TTL. If needed, a few pipelined workers in parallel can go faster, but I would watch Redis CPU and online latency while the job runs.

And with Redis Cluster, the client splits each pipeline by node and sends each part to its primary, so I keep batches bounded per node.

About this answer

Last updated
13 November 2026
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 "Explain why it is slow and how you would make it finish in a minute or two.": https://scrivio-interview-library.netlify.app/databases/redis-internals/60da917319/