REPLICAOF command in Redis - Time & Space Complexity
When using the REPLICAOF command in Redis, it's important to understand how the time it takes to set up replication changes as the data grows.
We want to know how the work Redis does scales when making one server copy another.
Analyze the time complexity of the following Redis command usage.
# Make this Redis server a replica of another server
REPLICAOF 192.168.1.100 6379
# This command tells Redis to start syncing data from the master
# and keep replicating changes.
This command sets the current Redis instance to copy data from the specified master server and keep in sync.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Redis copies all keys and their values from the master to the replica during initial sync.
- How many times: Once for each key in the master database during initial synchronization.
As the number of keys on the master grows, the amount of data to copy grows too.
| Input Size (n keys) | Approx. Operations |
|---|---|
| 10 | Copy 10 keys |
| 100 | Copy 100 keys |
| 1000 | Copy 1000 keys |
Pattern observation: The work grows directly with the number of keys to copy.
Time Complexity: O(n)
This means the time to complete the initial replication grows linearly with the number of keys on the master.
[X] Wrong: "REPLICAOF instantly copies data regardless of size."
[OK] Correct: The initial sync copies every key, so more keys mean more time before the replica is fully caught up.
Understanding how replication time grows helps you explain system behavior and design better data setups in real projects.
"What if the master database size doubles? How would that affect the time for REPLICAOF to complete initial sync?"