Quick revision
The short answer
We use consistent hashing with bounded loads: each server's capacity is ceil((1 + epsilon) x average load), for example epsilon 0.25, and a room goes to its ring position unless that server is full, in which case it walks clockwise to the next server with spare capacity. The assignment is kept in a shared store or decided by a small routing tier so every router agrees, load is measured in connections and messages rather than room counts, and joins and leaves move only nearby rooms plus a few overflow rooms. A single room bigger than a server must be split in the application.
What to remember
In this answer, we saw that plain consistent hashing balances room counts, not load, so a few large rooms overload one server. Consistent hashing with bounded loads caps every server at ceil((1 + epsilon) x average) and sends a room clockwise to the next server with spare capacity, keeping most affinity. Because placement now depends on load, routers must share one assignment map or route through a small decision tier. Joins and leaves move nearby rooms plus a few overflow rooms, epsilon trades evenness against movement, and a room larger than any server must be split across servers with pub/sub.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We use consistent hashing with bounded loads: each server's capacity is ceil((1 + epsilon) x average load), for example epsilon 0.25, and a room goes to its ring position unless that server is full, in which case it walks clockwise to the next server with spare capacity. The assignment is kept in a shared store or decided by a small routing tier so every router agrees, load is measured in connections and messages rather than room counts, and joins and leaves move only nearby rooms plus a few overflow rooms. A single room bigger than a server must be split in the application.
Why this question is asked
Room affinity is what makes the chat service simple: all members of a room share in-memory state on one server, so a message is fanned out without any network hop. Plain consistent hashing gives affinity, but it does not care how busy each room is, and in the evening a few huge rooms leave one server at 2.3 times the average. Interviewers ask this design question because the textbook algorithm needs a specific extension, and because the extension creates new questions about agreement between routers and about what happens when servers come and go. In this answer, we will see why plain consistent hashing is unbalanced here, add bounded loads, decide where the assignment lives, look at joins and leaves, and handle the room that is too big for any server.
1A Quick Recap and Why Plain Hashing Is Unbalanced
Consistent hashing places servers at points on a ring (usually several virtual points each) and assigns each room to the first server clockwise from hash(room_id). Its strengths are that every router computes the same answer without coordination, and adding or removing a server moves only the rooms near its points.
But it balances the number of rooms, not the load. Room sizes are wildly uneven: most rooms have a handful of people, and a few, such as a live event or a popular community, have thousands of members sending messages every second. If two of those land on the same server, that server carries far more connections than the others, no matter how evenly the room ids are spread. Virtual nodes cannot fix this, because the imbalance comes from the rooms, not from the ring.
2Consistent Hashing With Bounded Loads
The extension, described by Mirrokni, Thorup and Zadimoghaddam and used in production load balancers, adds a capacity to each server. With total load L spread over n servers, the average is L / n, and every server may carry at most ceil((1 + epsilon) x L / n). Epsilon is the allowed overshoot; with epsilon 0.25, no server goes above 125 percent of the average.
To place a room, we start at its ring position as usual. If the server there has room under its cap, the room goes there. If not, we keep walking clockwise and take the first server that still has spare capacity. Most rooms land on their natural server, so affinity and predictability are mostly preserved, but no server can exceed the cap.
- Hash the room id onto the ringroom-918 -> position p
- Take the first server clockwise from pserver 7
- Server load + room weight within the cap?YesAssign the room thereNoTry the next server clockwise
- Record room -> server in the shared mapall routers read the same answer
4 steps in order. Play them, or step through them one at a time.
The room's weight is its real cost, such as its current connection count or messages per second, not 1. Notice that a room heavier than the remaining capacity of its natural server simply moves on to the next one; very heavy rooms are the case we handle separately later.
3Making Every Router Agree
Plain consistent hashing needs no coordination: the answer depends only on the room id and the server list. Bounded loads break that, because the answer now also depends on current load, and two routers that see slightly different load numbers could place the same room on different servers. Then half the members would join the room on server 7 and half on server 8, and they would not see each other's messages.
So the assignment must be decided once and shared. One way is a shared assignment map, for example in Redis or etcd: when a room's first member arrives, a router computes the placement and writes room-918 -> server 7 with a compare-and-set, so a concurrent router either wins or reads the winner's answer. Every later member's router reads the map. The other way is to route through a small set of routing nodes that own the placement decision, for example one leader with a standby. The map also gives us a place to see and override assignments during incidents.
4What Happens When Servers Join or Leave
When a server joins, it takes over the ring ranges next to its virtual points. Rooms in those ranges move to it, plus a few rooms that had overflowed past a full server and can now land closer to their natural position. When a server leaves, its rooms move to the next servers clockwise, subject to the cap, which may push a few rooms one step further. Most rooms are untouched, so most affinity survives.
Bounded loads cost a little more movement than plain consistent hashing, because a change in the average also changes the cap and can shift overflow rooms. The paper bounds the expected number of extra moves per change by a constant that grows as epsilon shrinks, roughly with 1 / epsilon squared. Smaller epsilon gives a tighter balance but more rooms that overflow and move; larger epsilon moves less but allows more imbalance. Values between 0.1 and 0.25 are common, and we tune by watching both the max-to-mean ratio and the number of room moves per change.
Moving a room means moving its state: the new server loads the room's recent history and membership from the durable store, and clients reconnect to it, so moves should be rate limited and preferably done when a room is quiet.
5The Room That Is Bigger Than a Server
No hashing scheme can place a room whose own load is larger than one server's capacity. If a live event room has 15,000 connections and the cap is 6,250, it does not fit anywhere. For such rooms, the application must split the room itself. Members are spread over several servers, each holding a shard of the room's connections, and messages for the room are published once to a pub/sub channel (Redis pub/sub or a Kafka topic) to which every server holding members subscribes. Each server then fans the message out to its local members. We would switch a room to this mode automatically when its weight passes a threshold, such as half the cap, so ordinary rooms keep the simple in-memory path.
6Follow-up questions
Why does rendezvous hashing alone not solve this either?
Rendezvous (highest random weight) hashing picks, for each room, the server with the highest hash of room and server together. It spreads room ids evenly and moves few rooms on changes, like consistent hashing, but it is equally blind to how heavy each room is. Two huge rooms can still pick the same server. It can be combined with a cap, by taking the highest-scoring server that has spare capacity, which is the same bounded-load idea with a different ordering.
Why measure connections and messages rather than rooms?
Because the cost of a server is the work it does: memory per connection and CPU per message fanned out. A room with 5,000 members posting every second costs thousands of times more than a two-person room. Balancing room counts would leave exactly the imbalance we started with.
How quickly should the cap react when a quiet room suddenly becomes busy?
Not instantly, or rooms would bounce between servers. We place on the room's weight when it starts, re-evaluate periodically with hysteresis, and only move a room if its server stays above the cap for a while, with a limit on moves per minute.
7Conclusion
In this answer, we saw that plain consistent hashing balances room counts, not load, so a few large rooms overload one server. Consistent hashing with bounded loads caps every server at ceil((1 + epsilon) x average) and sends a room clockwise to the next server with spare capacity, keeping most affinity. Because placement now depends on load, routers must share one assignment map or route through a small decision tier. Joins and leaves move nearby rooms plus a few overflow rooms, epsilon trades evenness against movement, and a room larger than any server must be split across servers with pub/sub.
Open with
Plain consistent hashing balances room ids, not load, so a couple of huge rooms on the same server give us 2.3 times the average. I would use consistent hashing with bounded loads.
Then, if they want more
Each server gets a capacity of ceil((1 + epsilon) times the average load), say epsilon 0.25, so nobody goes above 125 percent of average. A room goes to its normal ring position, and if that server is full, we walk clockwise to the next one with spare capacity.
Load means real cost, connections and messages per second, not number of rooms. Because placement now depends on load, routers could disagree, which would split a room across servers, so the assignment is decided once and stored in a shared map like Redis or etcd with compare-and-set, or decided by a small routing tier.
When servers join or leave, only rooms near the changed ring positions move, plus a few overflow rooms, so most affinity survives; the cap costs a bit more movement than plain hashing, and epsilon trades evenness against movement. Moves are rate limited and the new server reloads room state from the durable store.
Finally, a room bigger than one server cannot be fixed by any hashing; that room has to be split in the application, with several servers sharing it through pub/sub.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Design a routing scheme that keeps room affinity but caps how far above average any...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/f0b8150bcf/