Why mysql INSERT ... ON DUPLICATE KEY UPDATE can break RBR replication on a master / master configuration
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
MySQL's INSERT ... ON DUPLICATE KEY UPDATE (IODKU) command is a versatile tool that handles the operation of inserting a row if it doesn't exist or updating it when a duplicate key conflict arises. However, in a master-master replication setup using Row-Based Replication (RBR), using this command can lead to various issues and unexpected behaviors. This article aims to delve into the technicalities of why this happens, with examples and additional insights to better comprehend the potential pitfalls of this configuration.
Why MySQL IODKU Can Break RBR Replication
Understanding Master-Master Replication
Master-master replication involves a MySQL configuration where both servers act as masters, allowing read-write operations on either node with changes being replicated to the other node. This setup is ideal for high availability and fault tolerance but comes with its share of complexities, especially when RBR is used.
Row-Based Replication (RBR)
RBR, as opposed to Statement-Based Replication (SBR), replicates individual row changes rather than the actual SQL statements. While RBR offers higher precision in replication since it doesn't depend on executing statements, it can lead to issues when the operations that affect multiple rows have complex conflict resolutions, such as IODKU.
How IODKU Works
The INSERT ... ON DUPLICATE KEY UPDATE statement attempts an INSERT, but if a duplicate key violation occurs, it performs an UPDATE instead. This dual behavior can muddy RBR replication, especially in master-master setups, where nodes might interpret operations differently, leading to data inconsistencies.
Conflict Example
Consider a table employees with a unique constraint on the emp_id column:
Assume an existing record on both master databases:
On Server A:
On Server B:
If these operations occur concurrently on both servers, they can both lead to UPDATE scenarios where each server's interpretation is different due to the timing and version of the initial record seen.
Consequences of Inconsistency
The above operations can lead to conflicting data being exchanged in replication, where row updates depend on the current row state, causing the two master databases to diverge. In RBR, the actual row changes rather than the initial SQL statements are sent over replication, making it impossible for the node to re-evaluate conditions that led to the UPDATE.
Technical Impediments
- Concurrency Conflicts: IODKU can exacerbate race conditions where operations are happening simultaneously, creating data discrepancies across nodes.
- Binlog Content: Since RBR captures row changes instead of the conditional logic (as SBR does), it cannot adjust changes based on current data, leading to deterministic issues.
- Failover Challenges: Data consistency becomes jeopardized, complicating failover tactics as discrepancy in master nodes requires additional resolutions.
Additional Considerations
Use Case Evaluation
Before embracing IODKU in master-master replication, evaluate if the same behavior can be ensured across all nodes by possibly serializing such transactions or switching to SBR when conditional logic is paramount.
Alternatives
- Conflict Resolution Tools: Incorporate tools that detect and resolve conflicts arising from such insert/update anomalies in master-master setups.
- Avoid IODKU: In high-stake consistency environments, prefer locking mechanisms like
SELECT FOR UPDATEor application-level solutions to enforce data consistency.
Table: Key Points Summary
| Aspect | Explanation |
| IODKU Function | Inserts or updates based on key conflict. |
| RBR Limitation | Cannot re-evaluate conditional logic due to row-level operation storage. |
| Conflicts in Master-Master | Simultaneous changes lead to non-deterministic outcomes across masters. |
| Concurrency Vulnerability | IODKU increases race condition occurrences, making conflict resolution essential. |
| Resolution | Alternatives include using SBR, application logic, or enhanced conflict handlers. |
Understanding the nuances of IODKU in a master-master RBR replication setup is crucial to maintaining data consistency. While master-master replication provides redundancy and uptime benefits, careful consideration of SQL command behaviors and replication models can prevent avoidable conflicts and ensure stable operations.

