Video in progress

How do you set up Kafka across two data centers: a stretched cluster or replication? What's hard in active-active?

With low latency (zones in one region), a single stretched cluster with rack awareness is the simplest and gives RPO=0. Between distant DCs you use two separate clusters and MirrorMaker 2 or Cluster Linking; that's asynchronous, so on failover you can lose a little data and have to deal with translating offsets.

📖 Answer

Stretched cluster (3 zones, with broker.rack, RF=3, min ISR=2): every write goes synchronously to another zone. If a zone goes down, nothing is lost and clients move to another leader automatically. The requirement is a few ms of latency between zones. With two DCs this scheme doesn't work well: you need a third location for the quorum.

Two clusters + MirrorMaker 2: each DC has its own cluster, and MM2 copies topics asynchronously. Problems:

  • RPO > 0: during a failover, the messages from the last few seconds may not have reached the other DC;
  • offsets don't match: the offset numbers differ between the source and the copy. MM2 translates consumer group offsets through checkpoints (sync.group.offsets.enabled), but that's asynchronous too, so after a failover there's some reprocessing, which means consumers must be idempotent;
  • topic names: by default MM2 names the copy dc1.orders, which prevents cyclic replication.

Active-active means writes happen in both DCs. Consumers read orders and dc2.orders together (regex subscription). The hard parts: if one entity changes in both places at once, you get a conflict, and there's no global order. In practice I suggest pinning each entity to one "home" DC (by customer region, for example): writes only at home, and the other DC only reads.

The decision comes from the RPO/RTO requirement. If a payment system needs RPO=0, use a stretched cluster or a synchronous solution; for analytics, asynchronous replication is plenty. One more thing is to rehearse failover regularly: a DR plan that has never been tested doesn't work.