0
0
Redisquery~5 mins

REPLICAOF command in Redis - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: REPLICAOF command
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the number of keys on the master grows, the amount of data to copy grows too.

Input Size (n keys)Approx. Operations
10Copy 10 keys
100Copy 100 keys
1000Copy 1000 keys

Pattern observation: The work grows directly with the number of keys to copy.

Final Time Complexity

Time Complexity: O(n)

This means the time to complete the initial replication grows linearly with the number of keys on the master.

Common Mistake

[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.

Interview Connect

Understanding how replication time grows helps you explain system behavior and design better data setups in real projects.

Self-Check

"What if the master database size doubles? How would that affect the time for REPLICAOF to complete initial sync?"