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?"
Practice
Solution
Step 1: Understand the role of read-only replicas
Read-only replicas copy data from the master Redis server and serve read requests to reduce load on the master.Step 2: Identify the correct purpose
They do not handle writes or delete data; their main job is to share read load.Final Answer:
To copy data from the master and handle read requests -> Option BQuick Check:
Read-only replicas = copy data + read requests [OK]
- Thinking replicas handle writes
- Believing replicas delete data
- Assuming replicas do not sync data
Solution
Step 1: Recall the correct Redis command for replication
The command to set a Redis server as a replica isREPLICAOFfollowed by the master's IP and port.Step 2: Verify other options
Other commands like SETREPLICA, COPYFROM, SYNCREPLICA do not exist in Redis.Final Answer:
REPLICAOF <master-ip> <port> -> Option DQuick Check:
Replica setup command = REPLICAOF [OK]
- Using non-existent commands
- Confusing REPLICAOF with SYNC commands
- Misspelling the command name
REPLICAOF 192.168.1.10 6379, what happens if you try to run SET key value on the replica?Solution
Step 1: Understand replica behavior on write commands
Redis replicas are read-only and reject write commands like SET.Step 2: Identify the outcome of running SET on a replica
Running SET on a replica causes an error because it cannot accept writes.Final Answer:
An error occurs because replicas are read-only -> Option AQuick Check:
Write commands on replicas = error [OK]
- Assuming writes succeed on replicas
- Thinking commands auto-forward to master
- Believing replica disconnects on write
REPLICAOF 192.168.1.10 6379 but it is not syncing data. What is the most likely cause?Solution
Step 1: Check configuration details
If the replica is not syncing, the most common cause is incorrect master IP or port in the REPLICAOF command.Step 2: Evaluate other options
Older Redis versions may cause issues but less likely; REPLICAOF is the correct command; master being a replica can cause sync issues but less common.Final Answer:
The master IP or port is incorrect -> Option CQuick Check:
Incorrect master address = no sync [OK]
- Using wrong command name
- Ignoring IP/port typos
- Assuming version mismatch is always cause
Solution
Step 1: Recall how to remove replication
To stop replication temporarily, useREPLICAOF NO ONEwhich makes the replica a standalone server.Step 2: Check other options
Commands like REPLICAOF NONE, STOPREPLICA, DISABLEREPLICA do not exist in Redis.Final Answer:
REPLICAOF NO ONE -> Option AQuick Check:
Stop replication = REPLICAOF NO ONE [OK]
- Using incorrect command variants
- Trying to shut down replica to stop replication
- Confusing NO ONE with NONE
Ready to prove it?
Pass the test and this pattern moves unknown
