Pipeline vs individual commands performance in Redis - Performance Comparison
When working with Redis, sending commands one by one or using a pipeline affects how long operations take.
We want to see how the total time grows when using pipelines versus individual commands.
Analyze the time complexity of these two ways to send commands:
// Individual commands
for i in range(1, n+1) {
redis.set('key' .. i, 'value' .. i)
}
// Pipeline commands
pipeline = redis.pipeline()
for i in range(1, n+1) {
pipeline.set('key' .. i, 'value' .. i)
}
pipeline.exec()
The first sends each command separately; the second sends all commands together in one batch.
Look at what repeats as input grows:
- Primary operation: Sending a command to Redis server.
- How many times: Once per command for individual; once for all commands in pipeline.
As the number of commands (n) grows, the time to send them changes differently:
| Input Size (n) | Approx. Operations (Individual) | Approx. Operations (Pipeline) |
|---|---|---|
| 10 | 10 network trips | 1 network trip |
| 100 | 100 network trips | 1 network trip |
| 1000 | 1000 network trips | 1 network trip |
Pattern observation: Individual commands grow linearly with n; pipeline stays almost constant because it batches commands.
Time Complexity: O(n)
This means sending commands one by one takes time that grows directly with the number of commands.
[X] Wrong: "Using pipeline always makes the total time zero or constant no matter how many commands."
[OK] Correct: Pipeline reduces network trips but still processes each command, so total work grows with n, just network overhead is less.
Understanding how batching commands affects performance shows you can think about real-world system speed and efficiency.
"What if we used pipelines but sent multiple smaller batches instead of one big batch? How would the time complexity change?"