Read-only replicas in Redis - Time & Space Complexity
When using read-only replicas in Redis, it's important to understand how the time to get data changes as the amount of data grows.
We want to know how fast reading data from replicas happens as we ask for more data.
Analyze the time complexity of the following Redis commands on a read-only replica.
READONLY
GET user:1001
GET user:1002
GET user:1003
...
GET user:n
This code sets the connection to read-only mode and then reads multiple user keys from the replica.
Look at what repeats when reading from the replica.
- Primary operation: Each GET command reads one key from the replica.
- How many times: Once per key requested, so n times for n keys.
Each key read takes about the same time, so more keys mean more total time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 GET operations |
| 100 | 100 GET operations |
| 1000 | 1000 GET operations |
Pattern observation: The total work grows directly with the number of keys read.
Time Complexity: O(n)
This means the time to read keys from the replica grows in a straight line as you ask for more keys.
[X] Wrong: "Reading from a replica is instant no matter how many keys I ask for."
[OK] Correct: Each key read still takes time, so asking for more keys means more total time.
Understanding how reading from replicas scales helps you explain system behavior clearly and shows you know how data size affects performance.
"What if we used pipelining to send all GET commands at once? How would the time complexity change?"