Sub topic 8
Avoiding conflicts with write ownership and partitioning
This chapter assumes you know basic cloud networking and DNS, stateless web services behind load balancers, and relational or NoSQL databases. It also assumes you have read the chapter on replicating data across regions.
Why prevent conflicts instead of resolving them
When two regions accept writes to the same record at the same time, the copies diverge. You can resolve that later with last-writer-wins or a CRDT (a data type built so concurrent updates always merge). Resolution is hard to get right, and it can lose data. Write ownership avoids the problem: for each piece of data, exactly one region accepts writes. Other regions hold read-only replicas.
The system is still active-active. Every region serves live traffic and reads locally. Only the write path for a given record is pinned to one place.
Partitioning strategies
You decide which region owns which data by choosing a partition key. Three common choices:
| Strategy | Owner is chosen by | Fits | Main risk |
|---|---|---|---|
| Home region per user | Where the user signed up or lives | Consumer apps, profiles, carts | Traveling users see slower writes |
| Key hash | Hash of the record ID mapped to a region | Evenly spread keys with no geography | Writes are often far from the user |
| Entity affinity | Region of the parent entity, such as a tenant | B2B and multi-tenant systems | One large tenant overloads its region |
A home region is stored as data, usually in a directory table that is replicated to every region. Because it changes rarely, it is safe to replicate widely.
Routing writes to the owner
A region that receives a write for data it does not own has two options. It can proxy the request to the owner, which costs one cross-region round trip. Or it can reject the request and tell the client where to go. Proxying keeps clients simple. The following sketch shows the decision.
HOME = {"alice": "eu-west-1", "bob": "us-east-1"} # replicated directory
THIS_REGION = "us-east-1"
def handle_write(user, apply, forward):
"""Apply locally if this region owns the user, else forward to the owner."""
owner = HOME[user]
if owner == THIS_REGION:
return apply()
return forward(owner)
print(handle_write("bob", lambda: "written locally", lambda r: f"forwarded to {r}"))
print(handle_write("alice", lambda: "written locally", lambda r: f"forwarded to {r}"))Common errors
- Reading your own write from a replica. After a forwarded write, the local replica is stale until replication catches up. Read from the owner, or return the written value in the response.
- Changing the owner without a handoff. If you move a user to a new home region while writes are in flight, both regions may accept writes. Pause writes, wait for replication to drain, then update the directory.
- Failover without a rule. If the owner region fails, another region must take over. If the old owner comes back and still believes it owns the data, you have split brain. Use a fencing token or a lease so only one owner is valid at a time.
- Hot partitions. A skewed key, such as one huge tenant, concentrates writes in one region. Split the key further, for example by tenant and account.
Exercise
A user lives in Europe, and their home region is eu-west-1. They travel to the US and update their profile, and the request reaches us-east-1. Name two ways the system can handle the write, and one cost of each.
Show answer
The US region can proxy the write to eu-west-1, which adds a cross-region round trip to the write latency. Or it can reject the write and redirect the client to eu-west-1, which pushes the retry logic onto the client. In both cases, reads from the US replica may be stale until replication completes.