Video in progress
A user is connected to a gateway in Frankfurt, and the event they care about appeared in a service in Singapore. How do you build routing in a multi-region real-time system?
Each region has its own gateways and routing table, and the event is routed to the user's "home" region or to the region they're currently connected to. Global routing is done either by sending to all regions, or through a light global index that stores the user's location.
📖 Answer
There are two main approaches, and the choice depends on how much users move around.
Home region. Every user has a home region (set at sign-up or by data location). DNS/GeoDNS usually connects them there. Wherever the event appears, it's sent to the delivery service in the home region. It's simple and also fits data residency requirements. The problem: a user who travels to Singapore connects to Frankfurt and latency is high.
By connected region. The user connects to the nearest region. A global index userId -> region is kept (a KV store replicated across regions, with some lag). The region where the event originates looks at the index and forwards it to the right region. The index may be stale, so if the receiving region sees the user isn't there, it writes to the inbox, and the client gets it anyway when it reconnects.
The third and simplest option is to replicate every event to all regions. With 3 regions and moderate traffic this works and has the fewest moving parts. As the number of regions and the traffic grow, the waste becomes noticeable.
I usually decide like this:
- 2 to 3 regions, moderate traffic: replicate everywhere, each region delivers locally;
- a data residency requirement (for example, data must not leave Uzbekistan): a home region is mandatory;
- a global, mobile audience: connected region plus a global index.
In any variant, the inbox (events stored with a seq) is the source of truth and the real-time push is an optimization. Then cross-region lag or a routing mistake causes only delay, not loss.
When a region goes down: clients fail over to another region, but its inbox may be a few seconds behind because of replication lag. The client should notice that through seq and backfill later.
Sign up to mark studied questions and save your progress.