Master-replica architecture in Redis - Time & Space Complexity
When using Redis master-replica setup, it is important to understand how the time to process commands grows as data or replicas increase.
We want to know how the system handles more data or more replicas in terms of speed.
Analyze the time complexity of replicating data from master to replicas.
# Setup on each replica (once):
REPLICAOF master_host master_port
# Master writes data
SET key value
# Master automatically sends updates to all connected replicas
# Replica receives and applies updates
# This happens continuously as master changes
This code shows a master writing data and replicas copying those changes continuously.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Master sends each write command to all replicas.
- How many times: For each write, the master sends data to every replica.
As the number of replicas grows, the master must send the same data to more places.
| Input Size (n = replicas) | Approx. Operations |
|---|---|
| 1 | 1 send per write |
| 10 | 10 sends per write |
| 100 | 100 sends per write |
Pattern observation: The work grows directly with the number of replicas.
Time Complexity: O(n)
This means the time to replicate data grows linearly as you add more replicas.
[X] Wrong: "Adding more replicas does not affect the master's speed because replicas work independently."
[OK] Correct: The master must send data to each replica, so more replicas mean more work for the master.
Understanding how replication scales helps you design systems that stay fast as they grow. This skill shows you can think about real system limits and plan accordingly.
"What if the master batches updates before sending to replicas? How would the time complexity change?"