Sub topic 12
Routing requests: coordinators and client-side routing
This chapter assumes you know how the hash ring and preference lists work, and that gossip gives each node a view of cluster membership. It covers the last step before replication: how a request reaches the right nodes.
The routing problem
A client wants to run get("user:42"). The key lives on the N nodes in its preference list. Someone has to hash the key, find those nodes, send the request to them, and collect the replies. That someone is the coordinator for the request. There are three common ways to pick it.
| Strategy | Who routes | Extra hops | Client complexity |
|---|---|---|---|
| Any-node coordinator | Any node the client reaches, often via a load balancer | 1 if the node is not a replica | Low |
| Partition-aware client | The client library, using a copy of the ring | 0 | High |
| Dedicated routing tier | A proxy layer that holds the ring | 1 | Low |
Server-side coordination
In the simplest design the client sends the request to any node. That node looks up the preference list. If it is a replica, it coordinates and also serves one of the reads or writes itself. If it is not, it forwards the request to a replica, which becomes the coordinator.
The coordinator does the quorum work from the previous chapters. For a write, it sends the value to all N replicas and waits for W acknowledgements. For a read, it asks the replicas and waits for R responses. It then merges the versions it received and returns the result.
Clients stay simple because they only need a list of addresses. The cost is the extra network hop when the first node is not a replica.
Client-side routing
A partition-aware client keeps its own copy of the ring and sends each request straight to a replica. This removes the extra hop and lowers tail latency.
The ring copy can become stale. Nodes join, leave, and fail. The client therefore refreshes its view in one of two ways:
- It polls a node periodically for the current membership.
- It learns of changes from the responses. A node that receives a request for a key it does not own forwards it and sends back the updated ring version.
Forwarding is the safety net. A stale client still gets a correct answer, only more slowly. Routing is a performance optimization and never decides correctness.
Choosing the coordinator
Whoever coordinates, the choice affects latency and load.
- Pick a replica. A replica coordinator saves a hop and can serve one read locally.
- Spread the load. If every client picks the first node in the preference list, that node becomes a hot spot. Choose among the N replicas, for example the one that answered fastest recently.
- Avoid failed nodes. Use gossip state to skip nodes marked down. If gossip is wrong, the request times out and the client retries on another node.
The sketch below shows a client that routes to a preference-list replica and falls back on failure. It reuses the Ring class from the partitioning chapters.
def route_request(ring, key, send, n=3):
"""Try each replica in the preference list until one coordinates the request."""
last_error = None
for node in ring.preference_list(key, n):
try:
return send(node, key)
except ConnectionError as err:
last_error = err
raise RuntimeError(f"no replica reachable for {key}") from last_errorRetries make requests repeat, so writes must be safe to repeat. Including a version or a request ID lets replicas ignore a duplicate.
Common errors
- Sending every request through one fixed node, which makes it a bottleneck and a single point of failure.
- Retrying a write without a way to detect duplicates.
- Treating a client's cached ring as authoritative. Membership changes constantly.
Exercise
A client library caches the ring and sends requests directly to a replica. A new node joins and takes over part of the key range. What happens to a request for a key in that range from a client that has not refreshed its ring, and why is this acceptable?
Show answer
The client sends the request to the old owner. That node is no longer in the key's preference list, so it forwards the request to the correct replica and returns the newer ring version. The request costs one extra hop, but the answer is still correct. This is acceptable because routing only affects latency. The quorum protocol on the actual replicas decides correctness.