A stateful service keeps per-user state in memory on 200 servers, assigned by hashing the user id. A few tenants generate 100 times the traffic of a typical one, and load shifts through the day. Hashing leaves some servers hot. What would you design instead?
Sketch your own answer first, then compare it with the one below.
A strong answer
Hashing balances keys, not load, and no number of virtual nodes sees the traffic. When load is skewed and moving, move from a fixed function to an assignment service.
Divide the key space into many slices, which are ranges of hash space. A controller maps slices to servers and watches load reports. It splits a slice that is hot, merges cold neighbours, and moves slices off overloaded or unhealthy servers. Clients cache the assignment and route directly, so the controller is off the request path and a controller outage freezes the assignment rather than stopping traffic. A very hot slice can be assigned to several servers if the state allows replication.
There are costs. Ownership must be exact while a slice moves, so that two servers do not both accept writes, which means a handoff protocol. The new owner starts with cold in-memory state. Clients can hold a stale assignment, so servers must reject requests for slices they no longer own. Movement has a cost, so the controller should move as little as possible.
The Slicer sharding service takes this approach, and its authors report that it cut resource use for the median workload by 63 percent compared with static sharding.
A single tenant above the capacity of one server still needs its own answer, such as splitting by sub-key.
Sources
- Atul Adya, Daniel Myers, Jon Howell, Jeremy Elson, Colin Meek, Vishesh Khemani, Stefan Fulger, Pan Gu, Lakshminath Bhuvanagiri, Jason Hunter, Roberto Peon, Larry Kai, Alexander Shraer, Arif Merchant, Kfir Lev-Ari (2016). Slicer: Auto-Sharding for Datacenter Applications.