How MySQL replicate the rows in a table ? and in which order
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
MySQL replication is a powerful feature that allows for data from one MySQL database server (the source) to be copied onto one or more MySQL database servers (the replicas). This process facilitates data availability, backup, and disaster recovery while enabling database scaling and offloading of reads. Understanding how replication works, specifically in terms of row replication, is critical to effectively use this feature.
How MySQL Replicates Rows in a Table
MySQL supports several forms of replication: statement-based, row-based, and mixed replication. Row-based replication, the focus here, logs changes at the individual row level, providing specific advantages in terms of data integrity.
Row-Based Replication Mechanics
In row-based replication:
- Change Detection: Any change to the rows of a table in the source database is captured.
- Logging: These changes are logged into the binary log (binlog) as individual row events, detailing original and modified values.
- Transmission: The logged events are transmitted from the source to the replicas.
- Application: On each replica, these row events are replayed to apply the same changes, ensuring the replicas have the same data as the source.
The Order of Replication
The replication process adheres to the order of change events. Thus, MySQL maintains the chronological sequence of transactions:
- Transaction Order Maintenance: Transactional modifications are applied in the order they were committed on the source, ensuring that data consistency is always maintained.
- Row Event Sequencing: With each transaction, individual row events are executed in the sequence they appear within the transaction.
- Error Handling: The replication process will halt if an error occurs in applying a change, preserving data integrity across the source and replicas.
Technical Explanation and Example
Suppose you have a table employee with the following schema:
When a row update occurs, such as increasing an employee's salary, the event is logged as a row change as follows:
In the binary log, this translates to an encoded row event indicating the id of the row, old salary, and new salary. The binary log will ensure that when the update reaches the replica, it modifies exactly the same row with the appropriate changes.
Advantages of Row-Based Replication
- Consistency: Provides consistent and exact replication especially useful when complex statements like
NOW(),UUID()are used in insertions. - Reduced Conflict: Reduces the chance of replication conflicts by applying exact changes, making it safer for distributed systems.
- Precision: Guarantees precise data reproduction at replicas down to individual row changes.
Limitations and Use Cases
However, row-based replication can sometimes lead to larger log files. In scenarios where:
- Bulk Data Changes: When operations involve large data modifications, each change needs individual logging, increasing log size, and disk I/O.
- Debugging Complexity: Understanding and troubleshooting replication issues can be more complex due to low-level change logs.
For applications like audit tracking and when using non-deterministic functions, row-based replication is preferable. It's highly beneficial when executing non-trivial logic as part of row updates.
Summary Table
| Key Aspect | Explanation |
| Replication Type | Row-based |
| Mode of Logging | Logs individual row changes as events |
| Order of Events | Maintains chronological order as changes occur and in transaction sequencing |
| Advantages | Ensures consistency and precision, reduces conflicts, ideal with non-deterministic function changes |
| Limitations | Larger log files, increased disk I/O, complex debugging |
In conclusion, utilizing row-based replication in MySQL requires an understanding of the mechanics and thoughtful consideration of the specific use case needs. Balancing replication strategy choice according to data consistency, system architecture, and performance is fundamental to harnessing MySQL's full potential in data replication.
Related reading
- How NameNode recognizes that the specific file replication is set value, than configured replication 3?
- how raft achieve strong consistency when they don't require fsync on every write
- how raft follower rejoin after network disconnected?
- How safe is totally ordered multicasting using logical clocks?
- How not persist property EF4 code first?
- How repair corrupt xampp 'mysql.user' table?
- How non-blocking API works?
- How should I log while using multiprocessing in Python?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.