Saltar al contenido principal

Keeping Related Keys Together: Hash Tags and Slot Co-location

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

A partitioned key-value store earns its throughput by refusing to keep everything in one place. Every key is hashed, the hash picks a slot, the slot belongs to one owner, and the owners spread across the cluster. Spread the keys evenly and you spread the load evenly — that is the entire point, and it works precisely because keys are hashed independently of each other. user:42:profile and user:42:cart are, as far as the hash is concerned, two unrelated strings, and they will very likely land on two different owners.

That independence is a feature right up until you have two keys that are not actually independent. You want to read a user's profile and cart in one round trip, or update a balance and append to its ledger in one atomic transaction, or move a set of related keys together. Every one of those operations needs a single node that owns all the keys involved — because a transaction cannot be half-executed on one machine and half on another, and a multi-key read that fans out to two owners is no longer one operation with one consistent snapshot. The keys you think of as a unit have been scattered by the very mechanism that makes the store fast.

Zaris handles this the way Redis Cluster does, and the design is worth understanding before you hit it as a CROSSSLOT error in production: it does not silently stitch a multi-key op back together across owners, and it gives you one precise lever — the hash tag — to put related keys back on the same owner on purpose.

Keys, slots, and owners​

The mapping from a key to its owner has a deliberate indirection in the middle. A key does not hash straight to a node; it hashes to a slot, and slots are assigned to nodes. Zaris uses the Redis-compatible scheme: there are 16384 slots, and a key's slot is CRC16(key) mod 16384.

slot(key)   = CRC16(key) mod 16384
owner(key) = node currently assigned slot(key)

The indirection is what makes rebalancing cheap. When you add a node, the cluster hands it a block of slots and migrates just those slots' keys — it never rehashes the whole keyspace, because the key→slot map is fixed by CRC16 and only the slot→node map moves. A client that knows the slot map can compute slot(key) locally and go straight to the owner, which is how partition-aware routing avoids a redirect hop on the hot path.

But notice what this does to a pair of related keys. CRC16("user:42:profile") and CRC16("user:42:cart") are two different 16-bit values, so they land in two different slots, which are almost certainly owned by two different nodes. Nothing about sharing the substring user:42 brings them together — CRC16 hashes the whole key, and a one-character difference scatters the result. The relationship that is obvious to you is invisible to the hash.

Why cross-slot is refused, not split​

When a multi-key command arrives whose keys do not all live in the same slot, Zaris rejects it rather than quietly doing something that looks like success. A MGET across two owners, or a transaction touching keys on three nodes, returns a cross-slot error. This feels unhelpful the first time you see it, and it is the correct behaviour.

The alternative — transparently fanning the operation out to each owner and merging the results — breaks the promise the operation was making. A transaction's whole value is that its keys change together or not at all; split it across two independent owners and you have two separate transactions that can partially fail, which is exactly the outcome the transaction existed to prevent. A multi-key read split across owners returns values snapshotted at different instants on different machines, so the "atomic read of a related set" you asked for is now a collection of unrelated point reads wearing a trenchcoat. Refusing the cross-slot op is the store telling you the truth: these keys are not co-located, so I cannot give you the guarantee you asked for. The fix is to co-locate them, and the store gives you a way to say so.

The hash tag: hashing part of the key​

A hash tag is a span of the key wrapped in braces, {...}. When a key contains one, Zaris hashes only the bytes inside the first brace pair, not the whole key. Everything outside the braces is still part of the key's identity — it is a distinct key, stored and retrieved under its full name — but it is ignored for the purpose of choosing a slot.

# No hash tag — hashed whole, scattered across slots:
user:42:profile → slot(CRC16("user:42:profile"))
user:42:cart → slot(CRC16("user:42:cart")) # different slot

# Hash tag {42} — only "42" is hashed, so both share a slot:
user:{42}:profile → slot(CRC16("42"))
user:{42}:cart → slot(CRC16("42")) # SAME slot → same owner

Now both keys compute the same slot, land on the same owner, and a transaction or multi-key read across them is a single-owner operation again — allowed, atomic, one round trip. You did not pin anything to a specific node or fight the balancer; you declared, in the key name, which keys belong together, and let the ordinary slot machinery keep them together through every rebalance. Migrate that slot to a new node and all the {42} keys move as one unit, because they are one slot's worth of keys.

Whole key hashed — scattereduser:42:profileuser:42:cartOwner Aslot 9213Owner Cslot 2871MGET / MULTI across them → CROSSSLOT (refused)Hash tag 42 — only "42" hashed → co-locateduser:42:profileuser:42:cartOwner Bslot = CRC16("42")one owner → multi-key op allowed & atomic

The tradeoff you are choosing​

Co-location is a lever, not a default, because it cuts directly against the thing partitioning is for. Every key that shares a hash tag shares a slot, and a slot is owned by exactly one node. Tag ten million keys with {global} and you have hand-built a hotspot: one node owns all of them, serves all their traffic, and holds all their memory, while the rest of the cluster sits idle. The store will do exactly what you told it to, which is the problem.

So the rule of thumb is to scope the tag to the smallest unit that actually needs atomicity together. {user:42} groups one user's keys — a few keys, touched by one user's requests, a natural and well-distributed unit, because different users hash to different slots. A per-tenant tag is usually fine; a single global tag almost never is. The question to ask of any hash tag is simply: how big is the hottest group this creates, and is one node happy to own all of it? Answer that honestly and hash tags give you cross-key atomicity exactly where you need it, while the rest of the keyspace keeps spreading the way it should.

Wrap-up​

Independent hashing is what lets a partitioned store scale, and it is also what quietly separates keys you meant to keep together. Zaris does not paper over that with a transparent fan-out that would break the guarantees a multi-key operation is supposed to deliver; it refuses the cross-slot op and hands you a precise way to prevent it. A hash tag hashes only the braced span of the key, so everything sharing that span shares a slot and an owner — related keys moved and mutated as a unit, atomic multi-key ops back on the table, and no node pinned by hand. Spend the lever where atomicity is worth a shared slot, keep the groups small enough that no owner drowns, and the store keeps the keys you care about together without giving up the spread that made it fast.