Saltar al contenido principal

Two Regions, Both Writable: Conflict Resolution with Hybrid Logical Clocks

· 11 min de lectura
Clustron Team
Ingeniería de Sistemas Distribuidos

Geographic redundancy starts as an availability requirement and quickly becomes a consistency problem. The availability part is easy to state: if a region goes dark, the application should keep serving from another one. The naive implementation is a primary region that owns all writes and a secondary that replicates and serves reads. It survives a read-region outage gracefully, but it has two sharp edges. Every writer, wherever they are, pays the latency to reach the one primary — a user in Sydney writing to a primary in Virginia feels every one of those milliseconds. And when the primary fails, you are not serving degraded; you are promoting a standby under pressure, hoping the last few writes made it across before the lights went out.

Active-active removes both edges by making every region a first-class writer. A user writes to the region nearest them, reads from the region nearest them, and the regions reconcile in the background. No write pays an intercontinental round-trip, and no failover needs a promotion ceremony, because there was never a single primary to lose. The price of this is the problem active-active exists to solve and single-primary setups get to pretend does not exist: two regions can write the same key at the same time, and the system must decide, deterministically and identically everywhere, which write wins.

Zaris runs active-active as two independent clusters — not one cluster stretched across an ocean — linked by asynchronous replication, with conflicts resolved by a hybrid logical clock and last-writer-wins. This post is about why those three choices go together, and what "last writer" has to mean when there is no clock everyone can trust.

Why two clusters, not one stretched across the ocean​

The tempting design is one logical cluster whose nodes happen to sit in two regions. It is tempting because it looks like less to operate, and it is wrong because a cluster is a consensus system, and consensus hates latency. Every quorum decision, every ownership handoff, every heartbeat now carries the cross-region round-trip, and worse, a network partition between the regions — which on the public internet is a when, not an if — forces the cluster to choose between stalling (no quorum) and splitting (two halves each thinking they are in charge). You have taken the availability you were trying to buy and made it hostage to the link you least control.

Two independent clusters invert that. Each region is a complete, self-sufficient cluster that forms quorum locally, serves locally, and survives locally. The link between them is not in any critical path: it carries a replication stream, asynchronously, and if it breaks, both regions keep serving their own users without noticing. The stream is a resumable change feed — an ordered log of mutations each cluster can replay to the other, picking up exactly where it left off after any interruption, so a partition that lasts a minute or a day heals by replaying the backlog rather than by a full resync. The cost of independence is that replication is asynchronous, which is precisely what creates conflicts. Those conflicts are not a flaw in the design; they are the design's honest admission that you cannot have both regions writable and instantly consistent and independent of the link between them. You pick two, and active-active picks writable-and-independent, then cleans up the consistency afterward.

The hard part: what does "last writer wins" even mean?​

Last-writer-wins is the obvious conflict rule: if Virginia and Sydney both write key k, keep the later write and discard the earlier. The trouble is the word "later." Compare wall-clock timestamps and you are trusting that two machines on two continents agree on the time — and they do not, not to the precision you need. Clock skew of even a few milliseconds means the write that happened second can carry an earlier timestamp, and LWW-by-wall-clock will silently keep the stale value. Worse, the decision is non-deterministic: two sites reconciling the same pair of writes could pick differently if they read the clocks differently, and now your two regions permanently disagree about what k is. A conflict rule that does not converge is not a conflict rule.

A hybrid logical clock (HLC) is the fix, and it is a genuinely elegant one. An HLC timestamp is a pair: a physical-time component and a logical counter. It tracks wall-clock time closely enough to be meaningful to humans and to time-based features, but it is adjusted by a simple rule — on every event, take the max of your current physical clock and the physical component of the last timestamp you have seen, and if they tie, bump the logical counter. The effect is that HLC timestamps form a total order that respects causality: if write B happened after write A and could have known about it, B's HLC timestamp is strictly greater, regardless of what the raw wall clocks said. And because the order is total and derived by a deterministic rule, every site that compares the same two timestamps reaches the same verdict.

That is what makes LWW safe here. "Last writer" means "higher HLC timestamp," and because HLC is a deterministic total order, Virginia and Sydney independently reconciling the same conflict will always keep the same write. The regions converge without talking to each other about the decision — they only have to exchange the writes and their timestamps, and the arithmetic does the rest.

Region Westindependent clusterwrite k=A hlc=102:0Region Eastindependent clusterwrite k=B hlc=102:1async change feed (resumable)exchange writes + HLC timestampsboth converge → k = Bhigher HLC (102:1) wins,same verdict on both sides

Turning it on, and the number to watch​

Active-active is opt-in, because it changes the semantics of a write — you are choosing eventual convergence over immediate global consistency, and that is a decision, not a default. Enabling it tells each cluster to stamp writes with HLC timestamps and to run the replication stream to its sibling site; from the application's point of view, writes and reads look exactly as they did before, served at local latency.

The number that matters operationally is replication lag, usually framed as RPO — the recovery point objective, or "how much recently-written data would I lose if a region vanished right now." Because replication is asynchronous, there is always some window of writes that have been acknowledged locally but not yet replicated to the other region. A write committed in West a hundred milliseconds ago may not be in East yet. If West disappears in that window, those writes are gone from the surviving side. The lag is normally tiny, but it is never zero, and it grows when the inter-region link is slow or saturated — so Zaris exposes it as a first-class metric. Watching replication lag is how you know your geographic redundancy is actually redundant: a lag that is climbing is a quiet warning that the two regions are drifting apart, and that an outage right now would cost you more than you think.

What LWW does and does not give you​

It is worth being precise about the guarantee, because last-writer-wins is often oversold. What you get is convergence: after replication catches up, every region holds the identical value for every key, chosen deterministically. What you do not get is merge: when two writes conflict, one is kept whole and the other is discarded whole — there is no field-level reconciliation, no "add both," no application-defined resolver in the base model. For the vast majority of cache and key-value workloads that is exactly right; the records are whole values and the newest one is the one you want. But if your data is a set that two regions are concurrently adding different elements to, LWW will keep one region's set and drop the other's additions, which is not what you meant. That is the signal to model the value as something conflict-free, or to route writes for that particular key to a single region, rather than to reach for active-active and expect it to merge intent it was never told about. Active-active with HLC-LWW is a precise tool: it converges last-writer-wins values across regions without a single clock everyone trusts, and it is honest about being exactly that and not a general-purpose merge engine.

Wrap-up​

Single-primary geo-redundancy makes every writer pay for one region's location and turns a primary outage into a promotion scramble. Active-active fixes both by letting every region write locally and reconciling afterward — and the reconciliation is the whole game. Zaris runs it as two independent clusters so neither region's availability is hostage to the cross-ocean link, replicates between them over a resumable change feed that heals a partition by replaying its backlog, and resolves the conflicts that asynchronous replication inevitably creates with a hybrid logical clock, so "last writer wins" means a deterministic total order that respects causality rather than a race between two wall clocks. Enable it per workload, watch the replication-lag metric so you know how much an outage would really cost, and keep in mind that it converges whole values rather than merging them. Within those lines, both regions are writable, all the time, and they agree on the result — which is the thing single-primary never could.