MySQL
Replication
Database
Data Synchronization
Table Rows

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.

Practice system design

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:

  1. Change Detection: Any change to the rows of a table in the source database is captured.
  2. Logging: These changes are logged into the binary log (binlog) as individual row events, detailing original and modified values.
  3. Transmission: The logged events are transmitted from the source to the replicas.
  4. 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:

  1. Transaction Order Maintenance: Transactional modifications are applied in the order they were committed on the source, ensuring that data consistency is always maintained.
  2. Row Event Sequencing: With each transaction, individual row events are executed in the sequence they appear within the transaction.
  3. 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:

sql
1CREATE TABLE employee (
2    id INT PRIMARY KEY,
3    name VARCHAR(50),
4    salary DECIMAL(10,2)
5);

When a row update occurs, such as increasing an employee's salary, the event is logged as a row change as follows:

sql
UPDATE employee SET salary = salary * 1.10 WHERE id = 1234;

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 AspectExplanation
Replication TypeRow-based
Mode of LoggingLogs individual row changes as events
Order of EventsMaintains chronological order as changes occur and in transaction sequencing
AdvantagesEnsures consistency and precision, reduces conflicts, ideal with non-deterministic function changes
LimitationsLarger 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design