Scrivio Interview library
DebuggingIntermediate6 min read · updated 13 November 2026

To give each region local writes, a team set up bidirectional MySQL replication between two regions, each primary replicating from the other. Within a week replication in one direction stopped with error 1062 (duplicate entry), and users report that some profile edits were silently undone.

Explain both problems, what you would change, and whether this setup should stay at all.

The duplicate key error came from both primaries handing out auto-increment ids from the same sequence, so concurrent inserts in the two regions got the same id and one side failed when the other's row arrived. The undone edits came from concurrent updates to the same row: each change replicated and overwrote the other, so whichever arrived last won on each side, and the two sides can even end with different values. Interleaved auto_increment settings fix the ids but not other unique keys or update conflicts, so the safer design gives every row a home region that receives all its writes, and keeps bidirectional replication, if at all, only for data that is partitioned by region or append-only.

Why this question is asked

Setting up two MySQL primaries that replicate from each other takes an afternoon, and it seems to give every region fast local writes. Within a week, it usually shows why multi-leader replication is hard. Interviewers ask this debugging question because it contains both classic failures of multi-leader setups and tests whether we can recommend structural change rather than another configuration tweak. In this answer, we will recap how the setup works, explain the duplicate key error and the silently undone edits, look at the auto-increment fix and its limits, and decide what the setup should become.

1A Quick Recap of the Setup

Each region has a MySQL primary. Primary A is a replica of B and primary B is a replica of A, so every write in either region flows to the other. MySQL avoids loops by tagging each event with the originating server's id, so a server ignores events that started on itself. Applications in each region write to their local primary. There is no coordination between the two when they accept a write; each commits locally and replicates asynchronously.

2Problem 1: The Duplicate Key Error

Both primaries started with the same table and the same auto-increment counter. When users in both regions sign up at about the same time, each primary assigns the next id from its own counter, and both choose the same number.

SQL
-- region A -- region B, the same second
INSERT INTO profiles(name) VALUES ('Ana'); INSERT INTO profiles(name) VALUES ('Bo');
-- id 50211 -- id 50211
-- B applies A's row: ERROR 1062 (23000): Duplicate entry '50211' for key 'profiles.PRIMARY'
-- replication from A to B stops; B's applier waits for someone to fix it

With row-based replication, B receives an insert for id 50211, finds that id taken by Bo, and stops. Every later change from A then queues behind the error, so region B drifts further and further behind until someone intervenes.

3Problem 2: Edits That Were Silently Undone

The second problem is quieter. A user edits their profile in region A while a background job or the same user on another device edits the same row in region B. Each primary commits its own update and replicates it. Row-based events carry the new row image, so when A's update arrives at B, B overwrites its own newer edit with it, and vice versa. On each side, whichever update arrives last wins.

Notice what that can mean: A ends with B's value and B ends with A's value. The two primaries now disagree permanently about this row, and no error was raised. From the user's point of view, an edit appeared to save and then vanished on the next read.

Step throughConcurrent updates crossing in bidirectional replication
User in APrimary APrimary BJob in B
SET city = 'Lyon'
SET city = 'Paris'
ok
ok
replicate city = 'Lyon'
replicate city = 'Paris'
now city = 'Paris'
now city = 'Lyon'

8 messages in order. Play them, or step through them one at a time.

Both writes succeeded, both were overwritten, and the two sides now disagree.

4Interleaving Ids: Necessary but Not Enough

The standard fix for the ids is to make the counters never collide. With auto_increment_increment = 2 on both servers, auto_increment_offset = 1 on A and 2 on B, A hands out odd ids and B even ids. That stops the 1062 on primary keys.

But it fixes only that one key. A unique index on email still breaks: two people registering the same address in both regions both succeed locally, and replication fails on the unique key instead. And interleaving does nothing for the lost updates, which are not about keys at all. Fixing those requires deciding, for every row, what should happen when both sides change it, and MySQL's built-in replication has no conflict detection to hook into.

5What the Setup Should Become

I would not keep this as a general-purpose write path. The safest design keeps one writer per row: every row has a home region, recorded with the row or derived from the user's account, and all writes for it are routed to that region's primary, even when the user is travelling. Replication still copies the data both ways for local reads, but since only one region ever writes a given row, conflicts cannot occur. When a region fails, its rows are reassigned deliberately, not written from both sides.

Bidirectional replication can stay for data that is naturally partitioned by region, such as orders placed in a region's store, or that is append-only, such as event logs with globally unique ids, because such data cannot conflict. If true multi-region writes to the same rows are a requirement, a database designed for it is the better tool, either one with conflict handling or a consensus-based distributed SQL database that orders writes across regions at the cost of cross-region latency.

Whatever remains needs monitoring in both directions: alert on any replication error or growing lag on either side, compare checksums of tables between the regions regularly, and make it a rule that nobody restarts replication by skipping transactions. A skipped transaction is a change permanently missing from one side.

6Follow-up questions

How would you detect that the two sides now hold different values for the same row?

Compare them systematically. For each table, compute a checksum per primary key range, for example a CRC of the concatenated columns over 1,000 ids, on both primaries at a quiet moment or for rows with updated_at older than the current replication lag, and list the ranges whose checksums differ, then the rows. Tools that checksum through replication, such as pt-table-checksum, assume one source, so in a circular setup it is safer to compute both sides independently and compare. Run it on a schedule and alert on any difference.

Does GTID-based replication prevent these conflicts?

No. GTIDs identify transactions and prevent applying the same transaction twice, but they do nothing about two different transactions that change the same row or claim the same key.

What about MySQL Group Replication in multi-primary mode?

It certifies transactions across members and rolls back the later of two conflicting ones, so lost updates become errors instead. But it is designed for low-latency networks, so across distant regions the commit latency usually makes single-primary mode with routing the better choice. Check your version's guidance.

7Conclusion

In this answer, we saw that two MySQL primaries replicating from each other share neither a counter nor a lock. Concurrent inserts took the same auto-increment id and stopped replication with 1062, and concurrent updates crossed in flight so the last one to arrive won on each side, leaving the regions in silent disagreement. Interleaved auto-increment settings fix only the id collisions. The lasting fix is one writer per row through home regions, with bidirectional replication kept only for regional or append-only data, plus monitoring and checksums in both directions.

What to say in the interviewAbout 90 seconds aloud

Open with

Both problems come from two primaries accepting writes with no coordination. The duplicate key error happened because both primaries used the same auto-increment sequence, so two inserts at the same moment in different regions got the same id.

Then, if they want more

When one side's row replicated to the other, the id was taken, the applier stopped with 1062, and everything behind it queued up. The undone edits are update conflicts. Two updates to the same row committed in both regions, each replicated to the other side and overwrote the local version, so the last one to arrive won on each side.

The two sides can even end with each other's values, and no error is raised. The id part can be patched with auto_increment_increment 2 and offsets 1 and 2, but that does nothing for other unique keys like email, or for lost updates. So I would not keep this as a general write path.

I would give every row a home region and route all its writes there, so each row has a single writer, and replication just provides local reads. Bidirectional replication could stay for data that cannot conflict, like region-local orders or append-only events with unique ids.

If we truly need multi-region writes to the same rows, I would pick a database built for it. And whatever remains needs alerts on replication errors in both directions, regular checksum comparisons, and a firm rule against skipping transactions.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain both problems, what you would change, and whether this setup should stay at all.": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/ab1d954b78/