Sub topic 10
Replicating events and messages between regions
This chapter assumes you know message queues and publish/subscribe, and that you have read the chapters on data replication and write ownership. Here you will learn how events cross region boundaries and what guarantees you can rely on.
Why replicate events
Services in an active-active system often communicate through events, such as "order placed" or "profile updated". If a region fails, the other regions must still see the events it published. They also need to keep consuming events that were in flight. So events need cross-region replication, just as data does.
Two common topologies
- Replicate the broker. A tool copies messages from one region's broker to another. Examples are Kafka MirrorMaker 2, or an SNS topic that delivers to SQS queues in other regions. Consumers in each region read from a local copy.
- Publish to a global bus. The producer sends each event to a service built for multi-region delivery, such as EventBridge global endpoints. The service handles failover for you.
Replication is asynchronous in both cases. A consumer in another region sees an event after a delay, and that delay is the replication lag you will monitor later.
Delivery guarantees to plan for
Cross-region links fail and retry, so assume at-least-once delivery. A consumer may receive the same event twice. Exactly-once delivery across regions is rarely available, and it is expensive where it is. The next chapter shows how to make handlers safe under duplicates.
Ordering is also weaker than it looks. Most brokers order messages only within a partition. Two regions publishing about the same entity can produce events that arrive in different orders in different places. Route all events for one entity through one owning region where you can. That is the same idea as write ownership.
Avoiding common errors
- Replication loops. If region A copies to B and B copies to A, an event can bounce forever. Tag each event with its origin region and never re-replicate an event that came from elsewhere. MirrorMaker 2 does this by prefixing remote topic names.
- Lost events on publish. Writing to a database and then publishing to a broker are two steps, and a crash between them loses the event. Use the outbox pattern. Write the event to an outbox table in the same transaction as the data, then have a relay publish it.
- Treating lag as zero. A consumer in region B may act on state that region A has already changed. Design handlers to tolerate stale views.
Detecting stale and duplicate events
Give each event a key and a per-key sequence number. A consumer can then discard anything it has already passed.
last_seen = {}
def accept(event):
"""Return True if the event is new for its key."""
key, seq = event["key"], event["seq"]
if seq <= last_seen.get(key, 0):
return False # duplicate or stale
last_seen[key] = seq
return True
events = [
{"key": "order-1", "seq": 1},
{"key": "order-1", "seq": 2},
{"key": "order-1", "seq": 2}, # duplicate
{"key": "order-1", "seq": 1}, # arrives late
]
print([accept(e) for e in events]) # [True, True, False, False]This works when each event carries full state, so skipping an old one loses nothing. If events are deltas, a late event must be applied, not dropped. In that case you need buffering or idempotent handlers.
Exercise
A producer writes an order to its database, then publishes "order placed" to the broker. The process crashes between the two steps. Which pattern prevents the lost event, and how?
Show answer
The outbox pattern. The event is written to an outbox table in the same database transaction as the order, so both succeed or both fail. A separate relay reads the outbox and publishes to the broker, retrying until it succeeds. This yields at-least-once delivery, so consumers must tolerate duplicates.