Saltar al contenido principal

One Core, One Shard: The Thread-per-Core Architecture

· 11 min de lectura
Clustron Team
Ingeniería de Sistemas Distribuidos

The default way to build a server is a pool of worker threads that all reach into one shared set of data structures, with locks to keep them from corrupting each other. It is the model almost everyone starts with, and it works beautifully on four cores. The trouble arrives quietly as the core count climbs. The locks that were uncontended start to contend; threads spend time waiting for each other instead of working. The cache lines holding the shared state bounce between cores and between CPU sockets on every write, and that coherence traffic is pure overhead. Each core you add returns a little less than the one before, until adding cores stops helping and throughput flattens — you have paid for a 32-core machine and are getting the work of twelve.

Two distinct costs are hiding in that flattening, and they compound. One is lock contention: a shared structure needs mutual exclusion, and mutual exclusion is, by definition, cores taking turns rather than running in parallel. The other is quieter and often larger — false sharing and cache-coherence traffic, where the mere act of one core writing to shared state invalidates that cache line everywhere else, forcing other cores to re-fetch it across the memory interconnect. Even a lock-free shared structure pays the second cost. The common root is the word shared.

The thread-per-core, shared-nothing architecture removes the sharing. Pin one thread to each core, split the data into shards with one shard per core, and route every request to the single core that owns the key it touches. That core does the whole operation on its own data, from its own queue, with no lock — because nothing else ever reaches into its shard.

Partition the data, not just the work​

The pivotal move is that the data is partitioned, not merely the threads. In the shared-pool model any thread can handle any request, which is why the data has to be shared and therefore locked. In the thread-per-core model each core is the sole owner of a slice of the keyspace, so a request for a key is not handed to whichever thread is free — it is delivered to the one core that owns that key. Because that core has exclusive access to its shard, it needs no lock to read or mutate it, and because it keeps touching the same shard over and over, that shard's hot data stays resident in the core's private L1 and L2 caches instead of being evicted and re-fetched.

Coordination does not vanish; it changes form. Where the shared model coordinates through locked memory, the thread-per-core model coordinates through message passing: a request that lands on the wrong core is placed on the owning core's queue, a lock-free single-producer handoff rather than a contended mutex. Message passing between cores is cheaper than shared-memory synchronization because it moves a small pointer once instead of bouncing a cache line back and forth, and it scales because each core's queue is independent.

shared poolthread-per-corecorecorecorecoreshared dataone lockcores take turns;cache lines bounce between themcorecorecorecoreshardshardshardshardeach core owns its shard —no lock, data stays hot in cachethe same request rate; the right side keeps scaling as cores are added because nothing is shared

Routing is the new placement problem​

Delivering a request to the right core is the same problem as placing a key on the right node, moved inside a single machine. The key is hashed to a shard, the shard maps to a core, and the request is enqueued there. The best implementations push this all the way down to the network: with receive-side scaling or SO_REUSEPORT, each core drives its own NIC receive queue and its own event loop, so even the interrupt handling is sharded and a packet for a given connection tends to arrive on a core that can handle it without a handoff. The write path looks like this from the owning core's side:

// Each core runs its own loop over its own shard — no shared state, no lock.
void CoreLoop(int coreId, Shard shard, Queue inbox) {
while (running) {
var req = inbox.Dequeue(); // SPSC handoff from whoever received it
var resp = shard.Apply(req); // exclusive access: no lock needed
req.Reply(resp);
}
}

// Dispatch: route to the owning core, or hand off if we are not it.
int owner = ShardFor(req.Key) % coreCount;
if (owner == CurrentCore()) cores[owner].Shard.Apply(req); // fast path, in place
else cores[owner].Inbox.Enqueue(req); // cross-core message

The cost moves, it does not disappear​

Shared-nothing is not free; it relocates the hard part. Two costs in particular deserve respect.

The first is the cross-core handoff. A request that arrives on a core other than its shard's owner must be message-passed, which adds a queue hop and, worse, can pull the request's data into a second core's cache. A workload that is mostly single-key stays on the fast path, but one with heavy multi-key or cross-shard transactions pays a handoff on nearly every operation, and at that point the message passing can cost more than the locks it replaced.

The second is load imbalance, and it is the sharp edge. In a shared pool, a burst of traffic for one key is absorbed by whatever threads are free. In thread-per-core, that key lives on exactly one core, so a hot shard is a hot core running flat out while its neighbors sit idle — and you cannot lend it their capacity, because they do not have its data. The mitigations are the same ones used across nodes: create many more shards than cores so the scheduler can migrate shards off a hot core, and replicate genuinely hot read keys so more than one core can serve them. Neither fully erases the fact that shared-nothing trades the shared pool's natural load balancing for raw per-core throughput.

When a shared design is still right​

If your workload is dominated by operations that span many keys, or your core count is modest, or the data simply is not large enough to shard meaningfully, the shared-pool model is simpler and may well be faster — you avoid the handoff on every cross-shard op and the imbalance risk, and a few cores rarely contend hard enough to matter. Thread-per-core earns its complexity when the core count is high, the workload is mostly per-key, and the shared model has already hit the wall where more cores stop buying more throughput. That is exactly the regime a high-throughput key-value store lives in, which is why it is the architecture of choice there and overkill almost everywhere else.

Wrap-up​

The shared-pool server hits a ceiling because shared means both lock contention and cache-coherence traffic, and both get worse with every core. Thread-per-core removes the sharing: one thread per core, one shard per core, every request routed to its owner, no locks, and data that stays hot in private caches — so throughput scales nearly linearly with cores. The costs are real and they move rather than vanish: cross-core handoffs for requests that miss their shard, and a load-balancing problem where a hot shard pins a single core. Shard far more finely than you have cores, replicate the hottest keys, and reserve the whole design for the high-core, mostly-per-key workloads where the shared model has already stopped scaling.