Using Key Tags to Perform Multi-Key Operations in Redis Cluster
This guide explains how to use Redis cluster key tags to align multiple keys to the same slot, enabling multi-key commands like MGET, and includes step‑by‑step examples of connecting, setting keys, checking slots, encountering CROSSSLOT errors, and resolving them with tagged keys.
The article demonstrates how to perform multi-key operations in a Redis cluster by using key tags to ensure keys map to the same slot.
First, it shows how to start a Redis instance, create a cluster, and connect to it using the redis-cli command with the -c (cluster mode) and -a (password) options:
redis-cli -h 127.0.0.1 -p 7000 -c -a 123456Next, two keys are set and their slot locations are inspected. The set commands and cluster keyslot queries reveal that the keys belong to different slots, which leads to an error when attempting a multi-key mget :
127.0.0.1:7000> set hello world
OK
127.0.0.1:7000> set slogan full-stack
-> Redirected to slot [16191] located at 127.0.0.1:7002
OK
127.0.0.1:7000> cluster keyslot hello
(integer) 866
127.0.0.1:7002> cluster keyslot slogan
(integer) 16191
127.0.0.1:7002> mget slogan hello
(error) CROSSSLOT Keys in request don't hash to the same slotThe article explains that single-key commands are automatically routed to the correct node, but multi-key commands require all keys to reside in the same slot; otherwise Redis returns a CROSSSLOT error.
To solve this, the author introduces key tags: by enclosing a common substring in curly braces (e.g., {star} ), Redis calculates the slot based only on the tagged part, forcing the keys into the same slot.
127.0.0.1:7000> cluster keyslot hello:{star}
(integer) 15279
127.0.0.1:7000> cluster keyslot slogan:{star}
(integer) 15279After adding the tag, the mget command succeeds and returns the values of both keys:
127.0.0.1:7002> mget hello:{star} slogan:{star}
1) "hello"
2) "world"The article concludes with a reminder to like and comment, and provides links to related Redis tutorials.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.