DEV Community

丁久
丁久

Posted on • Originally published at dingjiu1989-hue.github.io

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

This article was originally published on AI Study Room. For the full version with working code examples and related articles, visit the original post.

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-Master Replication: Conflict Resolution, CRDTs, Galera, and BDR

Multi-master replication allows writes to multiple database nodes simultaneously. Unlike primary-replica setups, there is no single point for writes. The trade-off is complexity, particularly around conflict resolution.

Why Multi-Master?

The primary reasons to consider multi-master replication are:

  • Multi-region writes : Users in the US and Europe both write with local latency.

  • Zero downtime upgrades : Any node can be taken offline without losing write availability.

  • Read scalability with local writes : Each node can serve both reads and writes with low latency.

Conflict Resolution Strategies

When two nodes concurrently modify the same row, a conflict occurs. Resolution strategies vary by system:

Last-Write-Wins (LWW)

Each row carries a timestamp. The write with the latest timestamp wins. Simple but lossy:

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-- Cassandra default

UPDATE users SET email = 'new@example.com', updated_at = now() WHERE id = 1;

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-- The LWW with the highest timestamp wins

Pros : Simple, always converges. Cons : Loses data silently.

Application-Mediated Conflict Resolution

The database detects conflicts and presents them to the application for resolution. BDR (Bi-Directional Replication) for PostgreSQL supports this:

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-- Create a conflict handler function

CREATE OR REPLACE FUNCTION resolve_conflict()

RETURNS trigger AS $$

BEGIN

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-- Custom logic: keep the row with the higher version

IF NEW.version >= OLD.version THEN

RETURN NEW;

END IF;

RETURN OLD;

END;

$$ LANGUAGE plpgsql;

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-- Apply to the replication set

SELECT bdr.conflict_handler('orders', 'resolve_conflict');

CRDT-Based Reconciliation

Conflict-free Replicated Data Types (CRDTs) mathematically guarantee convergence without coordination. Instead of storing a scalar value, you store a data structure where concurrent operations commute or combine:

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-- Counter CRDT table

CREATE TABLE page_views (

page_id INTEGER PRIMARY KEY,

counter INTEGER DEFAULT 0

);

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\-- Each replica increments independently

UPDATE page_views SET counter = counter + 1 WHERE page_id = 42;

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


Read the full article on AI Study Room for complete code examples, comparison tables, and related resources.

Found this useful? Check out more developer guides and tool comparisons on AI Study Room.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.