Video in progress

A microservice system has to run active-active in two regions. What are the main difficulties and how do you solve them?

The main problem is data: writing in both regions at the same time leads to conflicts. Usually each record has a "home" region (sharding by user or tenant), cross-region replication is asynchronous, and operations that can't tolerate conflicts are routed to a single region.

📖 Answer

Replicating stateless services into two regions is easy. The hard part is each service's DB, cache, Kafka topics and the consistency between them.

Latency between regions is tens to hundreds of ms. With synchronous replication every write slows down by that much, and if the link between regions breaks, writes stop. With asynchronous replication, if one record changes in both regions at the same time, you get a conflict.

This is how I usually approach it:

  1. A home region. Every user/tenant/account is tied to one region. Its writes happen only there. The gateway or DNS routes the user to their home region, and the second region keeps their data as a read-only replica. That is an "active-active system, but active-passive for every write". Most of the conflicts disappear.

  2. Things that must be globally unique. For example, a unique username or the last unit of an item in the warehouse. For these, one region is the "owner", or you use a global consensus database (Spanner, CockroachDB), at the price of latency.

  3. Conflict-tolerant data. Carts, likes, counters: CRDTs or "last write wins" (with care about clocks) are acceptable.

  4. Events. MirrorMaker 2 or Confluent Replicator for Kafka; topic names with a region prefix, and consumers must be ready for duplicates and disorder (idempotency is mandatory here).

  5. Failover. When a region goes down its users move to the other one, but because replication is asynchronous, writes from the last seconds may be lost (RPO > 0). You have to agree on this with the business in advance and rehearse failover regularly.

In many cases the honest answer is that the effort real active-active takes is big. First you should ask whether active-passive + fast failover (an RTO of minutes) is enough.