Replication basics in MySQL - Time & Space Complexity
When using replication in MySQL, it's important to understand how the process scales as data changes grow.
We want to know how the time to copy data from one server to another changes as the amount of data increases.
Analyze the time complexity of this simple replication setup.
-- On master server
CREATE TABLE orders (id INT PRIMARY KEY, details TEXT);
-- Insert many rows
INSERT INTO orders VALUES (1, 'details1'), (2, 'details2');
-- On slave server
START SLAVE;
-- Slave reads and applies changes from master
This code shows a master server with a table and inserts, and a slave server that reads and applies these changes.
Look at what repeats during replication.
- Primary operation: The slave reads each change (insert/update/delete) from the master's log and applies it.
- How many times: Once for every change made on the master.
As the number of changes on the master grows, the slave has more work to do.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 changes | 10 apply operations |
| 100 changes | 100 apply operations |
| 1000 changes | 1000 apply operations |
Pattern observation: The work grows directly with the number of changes to replicate.
Time Complexity: O(n)
This means the time to replicate grows in a straight line with the number of changes made on the master.
[X] Wrong: "Replication time stays the same no matter how many changes happen."
[OK] Correct: Each change must be copied and applied, so more changes mean more work and more time.
Understanding how replication time grows helps you design systems that stay fast as data grows, a key skill in real projects.
"What if the slave applied changes in batches instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand replication concept
Replication means copying data from one database (called master) to another (called slave) automatically.Step 2: Identify the main purpose
The main goal is to keep data synchronized between servers without manual copying.Final Answer:
To copy data automatically from one database server to another -> Option AQuick Check:
Replication = automatic data copy [OK]
- Confusing replication with backup
- Thinking replication speeds up queries on one server
- Assuming replication encrypts data
Solution
Step 1: Recall replication start command
In MySQL, the command to start the slave replication process isSTART SLAVE.Step 2: Check other options
Other options likeSTART REPLICATION,BEGIN SLAVE, orRUN SLAVEare not valid MySQL commands.Final Answer:
START SLAVE -> Option AQuick Check:
Start replication = START SLAVE [OK]
- Using incorrect commands like START REPLICATION
- Confusing with transaction commands
- Forgetting to start the slave after setup
CHANGE MASTER TO MASTER_HOST='192.168.1.10', MASTER_USER='replica', MASTER_PASSWORD='pass'; executed on a slave, what does it do?Solution
Step 1: Understand CHANGE MASTER TO command
This command configures the slave server to connect to a master server with specified host and user credentials.Step 2: Analyze given parameters
It sets the master host IP to 192.168.1.10 and uses the user 'replica' with password 'pass' for connection.Final Answer:
Sets the slave to connect to the master at IP 192.168.1.10 using user replica -> Option DQuick Check:
CHANGE MASTER TO = configure slave connection [OK]
- Thinking it starts replication immediately
- Assuming it creates a new master server
- Believing it deletes master config
START SLAVE; but replication does not start. Which of these is a likely cause?Solution
Step 1: Check slave configuration
If the slave is not configured withCHANGE MASTER TO, it does not know which master to connect to.Step 2: Understand replication start requirements
Without proper master info,START SLAVEcannot initiate replication, even if master is up or password is set.Final Answer:
The slave has not been configured with CHANGE MASTER TO -> Option CQuick Check:
Missing CHANGE MASTER TO blocks replication start [OK]
- Assuming START SLAVE works without configuration
- Blaming MySQL version without checking config
- Ignoring master server status
sales. Which command helps you filter replication to this database?Solution
Step 1: Understand replication filtering
MySQL uses replication filters likeREPLICATE_DO_DBto replicate only specific databases.Step 2: Identify correct syntax
The correct syntax usesCHANGE REPLICATION FILTER REPLICATE_DO_DB = ('sales');with parentheses around the database name.Final Answer:
CHANGE REPLICATION FILTER REPLICATE_DO_DB = ('sales'); -> Option BQuick Check:
Use REPLICATE_DO_DB with parentheses for filtering [OK]
- Using non-existent commands like SET REPLICATION FILTER
- Omitting parentheses around database name
- Trying to filter with CHANGE MASTER TO
Ready to prove it?
Pass the test and this pattern moves unknown
