Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

A sharded system uses hash(user_id) across 32 shards. One shard runs at 95 percent CPU while the others are at 20 percent. Traffic analysis shows a single celebrity account generates 30 percent of reads. What do you do short term and long term?

In the short term we take the read load off the hot shard: cache the celebrity's data aggressively (an application cache, and a CDN for public content), add read replicas to that shard, and rate-limit any automated traffic. In the long term we treat known hot keys specially, by splitting a hot entity across sub-keys (key salting) and merging on read, or by giving it a dedicated shard, and we monitor load per key, not only per shard. Adding more shards does not help, because one key always maps to one shard.

Why this question is asked

Hash sharding promises even load, and it delivers it for keys, not for traffic. When one key carries a large share of all requests, the hash faithfully sends all of it to one shard. Social networks know this as the celebrity problem, and the same thing happens with a viral product, a giant tenant or a popular stream. Interviewers ask this debugging scenario to see whether we can separate the short-term fire fighting from the structural fix, and whether we understand why the obvious fix, more shards, cannot work. In this answer, we will confirm the diagnosis, take the pressure off quickly, fix it structurally, and set up monitoring so we see the next one coming.

1Confirming the Diagnosis

The numbers already tell the story. With hash(user_id) over 32 shards, a typical user's traffic is about 1/32 of the total, a little over 3 percent per shard. One account generating 30 percent of all reads means its shard carries roughly 30 percent plus its normal 3 percent, about ten times the average. That matches one shard at 95 percent CPU while the others sit at 20 percent.

It is worth noticing why more shards do not help. The hash of the celebrity's user_id is a single number, so it maps to a single shard whether we have 32 shards or 320. Going to 64 shards would lower the load on every other shard and leave the hot one exactly as hot. The hot spot is caused by one key, so the fix must be about that key.

2Short Term: Take Reads Off the Hot Shard

Since the load is reads, caching is the fastest relief. The celebrity's profile, recent posts and follower count are read by millions and change rarely, so even a short time to live, such as 5 to 30 seconds, removes almost all of those reads from the database. We put a local in-process cache, such as Caffeine, in the service for the hottest keys, in front of a shared cache such as Redis, and serve public content through the CDN with a cache header so most requests never reach our servers.

Java
@Cacheable(cacheNames = "profiles", key = "#userId") // Caffeine with expireAfterWrite = 10s
public ProfileView profile(long userId) {
return repository.loadProfile(userId); // runs about once per 10 s per instance
}
// before: about 30 percent of all reads hit shard 17
// after: shard 17 sees one query per key per instance every 10 s

Two more measures help while the cache warms up. We add read replicas to the hot shard and send its read queries there, accepting a few hundred milliseconds of replication lag for this content. And we look at who is reading: if part of the 30 percent comes from scrapers or bots polling the account every second, rate limiting per client at the gateway removes load that has no business value. One caution with caching a hot key: when the entry expires, thousands of requests can miss at once and all go to the database. Request coalescing, where only one request per key reloads and the others wait for it (Caffeine's loading cache does this per instance), prevents that.

3Long Term: Treat Hot Keys Specially

Caching handles reads of mostly static data. For hot data that changes often, or for hot writes, we need the data itself spread out. The technique is key salting: instead of storing all of the hot entity's data under one key, we store it under several sub-keys, such as user 42 followed by a salt from 0 to 15, each of which hashes to a different shard. Writes pick one sub-key, for example at random or by hashing the item id, and reads fetch from all sub-keys and merge.

Java
int SALTS = 16; // only for keys on the hot-key list
String writeKey(long userId, long postId) {
return hotKeys.contains(userId) ? userId + "#" + Math.floorMod(postId, SALTS) : String.valueOf(userId);
}
writeKey(42, 1001); // "42#9" -> some shard
writeKey(42, 1002); // "42#10" -> another shard
writeKey(7, 5000); // "7" normal user, no salting
// read for user 42: query "42#0" .. "42#15" in parallel, merge and sort by time
Options for a hot key
OptionHelps withCost
Application cache + CDNHot reads of slowly changing dataStaleness for a few seconds; stampedes on expiry
Read replicas on the hot shardHot readsReplication lag; more servers
Rate limitingAutomated or abusive trafficMust not block real users
Key saltingHot reads and writesFan-out and merge on every read of that key
Dedicated shard via overrideAny load of one big entityRouting exceptions to maintain
More shardsNothing for one keyOne key always maps to one shard

Notice that salting is applied only to a short list of known hot keys. Applying it to everyone would multiply the read cost for every user to save load on a few. The other long-term option is placement: move the celebrity to a dedicated shard, or a dedicated set of replicas, sized for its load. That requires a routing layer that allows overrides, such as a small directory of exceptions consulted before the hash. Many systems combine both: a directory override for the few biggest entities and salting for data that is both hot and write-heavy.

4Monitor Load per Key, Not Only per Shard

This incident was visible only as one shard at 95 percent CPU, which tells us something is wrong but not what. To catch the next hot key early, we measure load per key: count requests per user_id or tenant_id in the service, with a sampling approach or a count-min sketch so it stays cheap, and export the top keys as a metric. An alert fires when any single key passes a share of traffic, say 2 percent, long before it saturates a shard. The same list feeds the hot-key set that salting and caching use, so special handling is applied automatically rather than during an incident.

5Follow-up questions

What does key salting cost on the read path?

Every read of a salted key becomes a fan-out: with 16 salts, one logical read turns into 16 queries to up to 16 shards, which run in parallel and then must be merged, sorted and possibly paginated in the application. Latency becomes that of the slowest of the 16, and a failure of any one shard degrades the result. So salting costs read latency, read amplification and code complexity, which is why we use it only for the few keys that need it and combine it with a cache so most reads never fan out.

Why not shard by post id instead of user id?

That would spread the celebrity's posts evenly, but every timeline and profile query is per user, so they would all become scatter-gather queries across all shards. We would trade one hot shard for load on every shard for almost every request.

How do you choose the number of salts?

From the load: if the hot key carries ten times the capacity of one shard's share, we need at least ten sub-keys, and we round up for headroom, for example 16. More salts spread better but make each read fan out further.

6Conclusion

In this answer, we diagnosed a hot shard caused by a single celebrity key and saw why more shards cannot help, since one key always maps to one shard. Short term, we cache the hot data in the application and the CDN, add read replicas to the hot shard and rate-limit automated traffic. Long term, we split known hot keys with salting and merge on read, or give them dedicated placement through a routing override, and we monitor load per key so the next hot key is caught before it saturates a shard.

What to say in the interviewAbout 100 seconds aloud

Open with

The cause is a hot key, not a bad hash. Hashing spreads keys evenly, but one key always maps to one shard, so a celebrity generating 30 percent of reads puts roughly ten times the average load on its shard.

Then, if they want more

That is also why adding shards would not help at all: the hot key stays on one shard whatever N is. In the short term I would take reads off that shard.

I would cache the celebrity's profile and recent content in the application with a short TTL, using Caffeine with request coalescing so expiry does not cause a stampede, plus Redis and the CDN for public content.

I would add read replicas to the hot shard and send its reads there, and look at whether part of the traffic is bots or scrapers that we can rate-limit at the gateway. In the long term, I would handle known hot keys specially.

One option is key salting: split the hot user's data across sub-keys like user 42 hash 0 to 15, which land on different shards, and fan out and merge on read. That costs read amplification and latency, so it is only for a short list of keys.

The other option is a routing override that puts that entity on a dedicated shard or replica set. Finally, I would add per-key load metrics, the top keys by request share with an alert at a threshold like 2 percent, so we spot hot keys before a shard hits 95 percent.

About this answer

Last updated
12 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 documentation below. 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 "A sharded system uses hash(user_id) across 32 shards. One shard runs at 95 percent CPU...": https://scrivio-interview-library.netlify.app/databases/partitioning-and-sharding/93909b0ead/