Redis with Node.js (ioredis) - Time & Space Complexity
When using Redis with Node.js through ioredis, it's important to understand how the time to run commands grows as data size increases.
We want to know how the number of operations changes when we run commands like fetching or setting many keys.
Analyze the time complexity of the following Redis commands using ioredis.
const Redis = require('ioredis');
const redis = new Redis();
async function getMultipleKeys(keys) {
const pipeline = redis.pipeline();
keys.forEach(key => pipeline.get(key));
const results = await pipeline.exec();
return results.map(result => result[1]);
}
This code fetches multiple keys from Redis using a pipeline to send all requests at once.
Look for repeated actions that affect time.
- Primary operation: Sending a GET command for each key in the list.
- How many times: Once for each key in the input array.
As the number of keys grows, the number of GET commands grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 GET commands sent |
| 100 | 100 GET commands sent |
| 1000 | 1000 GET commands sent |
Pattern observation: The number of operations grows directly with the number of keys.
Time Complexity: O(n)
This means the time to get all keys grows in a straight line as you add more keys.
[X] Wrong: "Using pipeline makes the time constant no matter how many keys we get."
[OK] Correct: Pipeline reduces network delays but still sends one command per key, so time grows with the number of keys.
Understanding how Redis commands scale helps you explain your choices clearly and shows you know how to handle data efficiently in real projects.
"What if we replaced pipeline with individual get calls without batching? How would the time complexity change?"