Mysql Replication, 2 databases, 2 ways?
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 you to create one or more copies of a database, facilitating redundancy, load distribution, and disaster recovery. Understanding the various approaches to MySQL replication can empower you to design a robust and scalable architecture. This article explores two commonly used databases for MySQL replication and two methods of implementing it.
MySQL Replication Basics
Replication in MySQL involves copying data from a primary database server (also known as the "master") to one or more replica servers (or "slaves"). The replica maintains a copy of the master database, allowing your systems to distribute read operations, enhance fault tolerance, and enable easier scaling of applications.
Key Concepts
- Master: The primary database that accepts all writing operations. It logs all changes to its data.
- Slave: The database server that copies data from the master and keeps an up-to-date replica.
- Binary Logs: The logs that record all changes to the database. These are essential for replication.
- Relay Logs: Logs maintained by the slave, which replicate the binary logs from the master.
- Replication Lag: The delay between an update on the master and its propagation to the slave.
Databases Supporting MySQL Replication
MySQL Community Server
The open-source MySQL Community Server is the foundation of MySQL replication. It offers a reliable and feature-rich platform that can be deployed on various operating systems. Key features include:
- Asynchronous Replication: Provides eventual consistency, with potential lag between master and slave.
- Synchronous Replication (using Galera Cluster for MySQL): Ensures real-time data consistency, suitable for critical applications.
MariaDB
MariaDB, a fork of MySQL, also supports replication and is fully compatible with MySQL protocols. It offers several enhancements:
- Galera Cluster: Native multi-master synchronous replication that ensures high availability.
- Parallel Replication: Improved replication performance by executing multiple replication threads.
Methods of Replication
When setting up MySQL replication, choosing the right strategy is crucial. Below are two common methods:
1. Statement-Based Replication (SBR)
In SBR, SQL statements executed on the master are also executed on the slave. This method works well for deterministic queries.
- Advantages:
- Generally smaller binlogs, as they store statements instead of data changes.
- Straightforward setup with less storage overhead.
- Disadvantages:
- Non-deterministic queries can lead to data inconsistency.
- Potential issues with specific data types and time-related functions.
2. Row-Based Replication (RBR)
RBR logs actual data changes rather than the SQL query that caused the change. This ensures that data remains consistent between master and slave.
- Advantages:
- Deterministic replication, avoiding non-deterministic issues.
- Best suited for applications with numerous transactions.
- Disadvantages:
- Generally larger binlogs due to detailed row information.
- More disk and network usage.
Challenges in MySQL Replication
- Replication Lag: Leading to outdated reads on the slave.
- Replication Conflicts in Multi-Master Configurations: Particularly challenging in write-heavy applications.
- Network Bandwidth: Larger binlogs or high-frequency updates can consume increased resources.
Summary Table
| Feature | MySQL Community | MariaDB | Statement-Based | Row-Based |
| Replication Type | Asynchronous Synchronous | Asynchronous Synchronous | Uses SQL statements | Uses actual data changes |
| Main Advantage | Open-source community support | Enhanced features performance | Smaller binlogs less storage overhead | Deterministic avoids non-deterministic issues |
| Main Disadvantage | Non-deterministic queries incur consistency risk in SBR mode | Larger binlogs high network usage in RBR mode | Issues with specific data types non-deterministic issues | Consumes more disk and network due to detailed row logs |
Conclusion
MySQL replication provides significant benefits in terms of scalability, load distribution, and data redundancy. Understanding the differences between statement-based and row-based replication, as well as the capabilities of MySQL and MariaDB, can assist in selecting the best solution for your environment. Careful planning and understanding of potential pitfalls can enhance the reliability and performance of your database infrastructure.
Related reading
- MySQL Replication 3 masters, 1 Slave
- MySQL replication monitor - Seconds_Behind_Master
- mysql replication phpmyadmin windows version
- MySQL replication Slave_SQL_Running fails after inserting data
- MySQL Replication SQL Thread, which privileges?
- MySQL replication Tungsten vs. Galera
- MySQL replication vs other techniques
- MySQL sharding and partition in distributed system

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.