Sub topic 2
Keeping Compute Stateless in Multi-Region Active-Active Systems
Assume you understand basic load balancing, DNS, web services, and database replication. This chapter focuses on how application instances share work across regions without depending on local memory.
Keep compute stateless
A stateless service does not require a particular server to remember a user's state. Any healthy instance can handle the next request. This makes global routing and regional failover simpler.
Store state outside the process:
- Put durable data in a replicated database.
- Put short-lived session data in a shared, region-aware cache.
- Store larger files in replicated object storage.
- Use signed tokens for small, non-sensitive session claims.
A token-contained session can reduce database reads, but revocation is harder. A shared session store supports immediate changes, but adds replication and availability concerns.
Replicate data across regions
Choose replication based on the data's consistency needs. Synchronous replication can provide stronger read-after-write behavior, but cross-region latency may make it impractical. Asynchronous replication improves latency and availability, but creates replication lag and possible data loss during a failure.
Separate data by ownership where possible. A user's profile might have one home region that accepts writes, while other regions serve replicated reads.
Avoid conflicts with ownership and partitioning
Write ownership assigns each record or partition to one region. Requests routed elsewhere forward the write or redirect the client. This prevents concurrent updates from competing.
Partitioning by tenant, user, or account can limit the scope of a conflict. Record the owner and replication version so a failover process knows which writes are safe to accept.
Resolve unavoidable conflicts
When multiple regions can write the same record, use an explicit rule. Last-writer-wins is deterministic when versions include a tie-breaker, but it can silently discard a valid update.
def lww_merge(a, b):
return a if (a[0], a[1]) >= (b[0], b[1]) else b
eu = (1700000005.2, "eu-west-1", "blue")
us = (1700000005.2, "us-east-1", "green")
print(lww_merge(eu, us))Version vectors preserve more history and can detect concurrent writes, but require more metadata. A CRDT is a data structure designed to merge concurrent updates. For example, a grow-only counter merges by taking the maximum value for each region, so increments are not lost.
Exercise
Why should a multi-region service avoid storing a user's session only in local process memory?
Show answer
A later request may reach another instance or region, where that memory does not exist.