Quick revision
The short answer
When a key contains a non-empty {...}, Redis Cluster hashes only the text inside the first pair of braces, so {cart:42}:items and {cart:42}:meta land in the same slot and can be used together in MULTI/EXEC, Lua scripts and multi-key commands. We check it with CLUSTER KEYSLOT, tag at the unit that must change atomically (one cart, never {cart} or a tenant), and from Spring prefer a Lua script that receives both keys in KEYS. Renaming the keys is a small data migration that needs a plan.
What to remember
In this answer, we saw that cart:42:items and cart:42:meta fall into different slots, so MULTI/EXEC fails with CROSSSLOT. A hash tag makes Redis hash only the first non-empty {...}, so {cart:42}:items and {cart:42}:meta share a slot, which we proved with CLUSTER KEYSLOT. From Java we used a Lua script with both keys in KEYS, tagged at the cart rather than a broad value, and planned the rename as a migration with dual reads or expiry.
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.
When a key contains a non-empty {...}, Redis Cluster hashes only the text inside the first pair of braces, so {cart:42}:items and {cart:42}:meta land in the same slot and can be used together in MULTI/EXEC, Lua scripts and multi-key commands. We check it with CLUSTER KEYSLOT, tag at the unit that must change atomically (one cart, never {cart} or a tenant), and from Spring prefer a Lua script that receives both keys in KEYS. Renaming the keys is a small data migration that needs a plan.
Why this question is asked
Moving from a single Redis to Redis Cluster is mostly transparent until the first transaction over two keys fails with CROSSSLOT. The cart keys cart:42:items and cart:42:meta look related to us, but the cluster hashes the whole key name, and those names land in different slots. Hash tags are the tool that fixes this, and they are easy to misuse in ways that hurt the whole cluster. Interviewers ask this coding question to see whether we can apply the fix precisely and avoid the trap. In this answer, we will reproduce the failure, add the hash tag and prove it, write the atomic update in redis-cli and in Java, look at misuse, and plan the key migration.
1Reproducing the Problem
Redis Cluster computes a key's slot as CRC16 of the key modulo 16384. With the full names, the two cart keys hash to unrelated slots, and the server refuses to run a transaction that touches both.
If the second slot lives on another node, the error is a MOVED redirect instead, and the wording varies slightly between versions, but either way EXEC aborts and the meaning is the same: a transaction, a Lua script or a multi-key command must stay within one slot, because one slot is the largest unit a single node is guaranteed to own.
2Adding the Hash Tag
A hash tag is a part of the key in curly braces. When a key contains a { followed later by a }, with at least one character between them, Redis hashes only the text between the first { and the next }. So we put the part that identifies the cart in braces: {cart:42}:items and {cart:42}:meta. Both hash only cart:42, so both land in the same slot.
- Look for the first {none: hash the whole key
- Is there a } after it?YesCheck what is between themNoHash the whole key
- At least one character between?YesHash only that textNoHash the whole key
- Slot = CRC16(hashed part) mod 16384{cart:42}:items ->
CRC16(cart:42)
4 steps in order. Play them, or step through them one at a time.
Notice that CLUSTER KEYSLOT is the proof, not the naming convention. A test that asserts the same slot for every pair of keys a cart uses catches a typo such as {cart:42}:items next to {cart: 42}:meta before it reaches production.
3Doing the Atomic Update From Java
Spring Data Redis does not support MULTI/EXEC on its cluster connection, so from Java the clean way to update both hashes atomically is a Lua script. A script runs atomically on the node that owns the slot, and it must receive every key it touches in KEYS, so the cluster client can route it and the server can check that all keys share a slot.
Never build key names inside the script, for example by concatenating 'cart:' .. ARGV[1]. The cluster client routes the script by the keys in KEYS, and the server checks only those; a key built inside the script may live in a different slot, and the script would fail on a cluster or, worse, work by accident in tests on a single instance.
4How Not to Use Hash Tags
The tag decides which keys share a slot, and a slot lives on exactly one primary. Tag at the unit that must change atomically: one cart. If we tagged every cart key with {cart}, every cart in the system would hash to the same slot and the same shard, which then carries all the cart load while the other shards idle, and resharding cannot help because a slot cannot be split. The same goes for {tenant}: a large tenant would become one shard's problem.
| Key design | Slots used | Effect |
|---|---|---|
| cart:42:items, cart:42:meta | two unrelated slots | CROSSSLOT for transactions |
| {cart:42}:items, {cart:42}:meta | one slot per cart | atomic per cart, carts spread out |
| {cart}:42:items | one slot for all carts | one hot shard, cannot be resharded |
5Renaming the Keys Is a Migration
Existing carts are stored under the old names, so switching code to the new names would make every cart appear empty. Two common plans exist. If carts are short-lived, say with a 7-day TTL, write new carts under the new names and read the new keys first, falling back to the old ones, until the old keys have expired. If carts must be kept, add a backfill job that copies each old cart to the new keys, with dual reads during the switch, then delete the old keys. In both cases watch CROSSSLOT errors and cart read misses during the rollout.
6Follow-up questions
What happens to a key named foo{}{bar}?
The first { is immediately followed by }, so the hash tag is empty, and Redis then hashes the whole key foo{}{bar}. The second pair of braces is never considered, because only the first { and the next } count. So it does not share a slot with bar or with {bar}.
Can I use WATCH with hash-tagged keys?
Yes, WATCH works on a cluster as long as all watched and modified keys are in the same slot and the commands go to the same node. Spring Data Redis still lacks MULTI on its cluster connection, which is why a Lua script is the simpler choice from Java.
Does the tag have to be at the start of the key?
No, it can be anywhere: cart:{42}:items works too. Putting it at the start is a readable convention, but what matters is that all related keys contain exactly the same tag text.
7Conclusion
In this answer, we saw that cart:42:items and cart:42:meta fall into different slots, so MULTI/EXEC fails with CROSSSLOT. A hash tag makes Redis hash only the first non-empty {...}, so {cart:42}:items and {cart:42}:meta share a slot, which we proved with CLUSTER KEYSLOT. From Java we used a Lua script with both keys in KEYS, tagged at the cart rather than a broad value, and planned the rename as a migration with dual reads or expiry.
Open with
The keys fail because Redis Cluster hashes the whole key name, so cart:42:items and cart:42:meta land in different slots, and a transaction, a Lua script or a multi-key command must stay within one slot.
Then, if they want more
The fix is a hash tag. When a key contains a non-empty part in curly braces, only the text inside the first pair of braces is hashed, so if I rename the keys to {cart:42}:items and {cart:42}:meta, both hash only cart:42 and land in the same slot.
I would prove it with CLUSTER KEYSLOT on both names, which must return the same number, and I would put that check in a test. Then MULTI, HSET on the items key, HSET on the meta key and EXEC works as before.
From Java, Spring Data Redis does not support MULTI on a cluster connection, so I would use a Lua script that receives both keys in KEYS and never builds key names inside the script, because the cluster routes and checks the script by the keys it is given.
The important rule is to tag at the unit that must change atomically, one cart. Tagging with something broad like {cart} or a tenant would put every cart into one slot on one shard, and a slot can never be split.
Finally, renaming keys is a data migration: either write new keys and read new then old during the switch, or, since carts are short-lived, let the old keys expire into the new layout.
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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
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 "The update now fails with CROSSSLOT. Show how to rename the keys with a hash tag so the...": https://scrivio-interview-library.netlify.app/databases/redis-scaling/2703bf85b6/