Replication strategies in DBMS Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Replication strategies in databases help copy data across servers to improve availability and reliability.
We want to understand how the time to replicate data grows as the amount of data increases.
Analyze the time complexity of this simple replication process.
-- Pseudocode for synchronous replication
BEGIN TRANSACTION;
INSERT INTO master_table VALUES (...);
FOR EACH replica IN replicas LOOP
SEND data TO replica;
WAIT for replica ACKNOWLEDGEMENT;
END LOOP;
COMMIT TRANSACTION;
This code inserts data into the master database and sends it to each replica, waiting for confirmation before finishing.
Look for repeated actions that affect time.
- Primary operation: Sending data and waiting for acknowledgement to each replica.
- How many times: Once per replica, so the number of replicas determines repetitions.
As the number of replicas grows, the time to complete replication grows roughly in direct proportion.
| Input Size (number of replicas) | Approx. Operations |
|---|---|
| 10 | 10 sends and waits |
| 100 | 100 sends and waits |
| 1000 | 1000 sends and waits |
Pattern observation: Doubling replicas roughly doubles the replication time.
Time Complexity: O(r)
This means the replication time grows linearly with the number of replicas.
[X] Wrong: "Replication time stays the same no matter how many replicas there are."
[OK] Correct: Each replica requires sending data and waiting, so more replicas mean more total time.
Understanding how replication time grows helps you design systems that stay fast and reliable as they scale.
What if replication was asynchronous and did not wait for acknowledgements? How would the time complexity change?
Practice
Solution
Step 1: Understand Master-Slave replication
In this strategy, one server (master) handles all write operations, and other servers (slaves) copy data from it.Step 2: Compare with other strategies
Master-Master allows multiple masters; Peer-to-Peer is decentralized; Snapshot copies data at intervals.Final Answer:
Master-Slave replication -> Option AQuick Check:
One main write server = Master-Slave [OK]
- Confusing Master-Slave with Master-Master
- Thinking slaves can write data
- Mixing snapshot with continuous replication
Solution
Step 1: Define Master-Master replication
Both servers can accept writes and replicate changes to each other to keep data synchronized.Step 2: Eliminate incorrect options
Data is copied only once at setup describes snapshot; One server writes, others read only describes Master-Slave; Servers do not communicate is not replication.Final Answer:
Two servers both accept writes and replicate changes to each other -> Option AQuick Check:
Master-Master means both write and sync [OK]
- Thinking only one server writes in Master-Master
- Confusing snapshot with replication
- Assuming no communication means replication
Solution
Step 1: Understand replication delay
Slaves replicate data from master with a 2-second delay, so data on slaves lags behind master by 2 seconds.Step 2: Analyze options
Immediate consistency means no delay, which is incorrect. 100 seconds delay is unrelated to request rate. Slaves do not write directly.Final Answer:
2 seconds delay -> Option CQuick Check:
Replication delay = 2 seconds [OK]
- Confusing request rate with delay time
- Assuming slaves write directly
- Thinking slaves are always immediately consistent
Solution
Step 1: Identify cause of conflicts
Simultaneous writes cause conflicts in Master-Master replication because both servers can change the same data.Step 2: Apply conflict resolution
Using rules like timestamps or last-write-wins helps resolve conflicts automatically.Final Answer:
Implement conflict resolution rules or use timestamps -> Option DQuick Check:
Conflict resolution fixes simultaneous writes [OK]
- Thinking disabling replication solves conflicts
- Switching to Master-Slave without need
- Ignoring conflict resolution mechanisms
Solution
Step 1: Analyze requirements
High availability and multiple write servers require a strategy where more than one server can accept writes.Step 2: Match strategy to needs
Master-Master replication allows multiple write servers but may have conflicts; Master-Slave does not allow multiple writes.Final Answer:
Master-Master replication -> Option BQuick Check:
Multiple writes + availability = Master-Master [OK]
- Choosing Master-Slave for multiple writes
- Ignoring conflict tolerance
- Thinking snapshot replication supports writes
