Video in progress

Staff 🌱 Spring

In a Spring system running across several regions, what architectural decisions do you make about data consistency?

First I separate which data needs strict consistency (balances, stock) and which is fine with eventual consistency (catalog, profile). Writes go to a "home region" or a single leader, reads come from local replicas, and there's an explicit rule for resolving conflicts. Every decision is a tradeoff between latency, availability and cost.

📖 Answer

The core tension (CAP/PACELC): the network between regions is 50-150 ms. If every write waits for the other region's acknowledgment, it's slow; if it doesn't wait, the data diverges when a region gets cut off.

The options:

  1. Active-passive. One primary region writes, and the second stands by with an asynchronous replica. Simple, but on failover the last few seconds of writes may be lost (RPO > 0) and the switch takes time (RTO).

  2. A "home" region for each piece of data (sharding by geography). A user and their accounts are written in one region, and the others keep copies for reading. In many cases this is the most practical option.

  3. Active-active, multi-leader. Both regions write, and conflicts are resolved with last-write-wins, CRDTs or business rules. Only for data where conflicts can be resolved: a cart, settings. Almost never for money.

  4. Global consensus databases (Spanner, CockroachDB): strict consistency, but every write pays cross-region latency, and the price is high.

On the Spring side: routing the user to their home region at the gateway; cross-region replication with idempotent, versioned events; for "read-your-writes", keep the latest write version in the session. Practice failover regularly, because a failover that hasn't been tested won't work.

Most important is to agree on the requirements in numbers: RPO, RTO and the acceptable latency for each operation.