What is a hash tag in Redis Cluster, and why would you use one to control which slot certain keys are assigned to?
Short answer
A hash tag is a portion of a key wrapped in curly braces, and when present, Redis Cluster only uses that specific portion for the hash slot calculation rather than the entire key, letting you deliberately force multiple related keys to land on the same slot and therefore the…
Advertisement
AModel answer
A hash tag is a portion of a key wrapped in curly braces, and when present, Redis Cluster only uses that specific portion for the hash slot calculation rather than the entire key, letting you deliberately force multiple related keys to land on the same slot and therefore the same node, which is required for operations that need to work with multiple keys together.
-- Both keys will be assigned to the same hash slot
-- because of the shared {user:123} hash tag
redis-cli set '{user:123}:profile' 'data'
redis-cli set '{user:123}:settings' 'data'Real-world example
An application storing both a user's profile and settings under related keys uses hash tags to guarantee both keys always land on the same node, allowing them to be safely used together in a single multi key operation.
Likely follow-ups
- What operations in Redis Cluster require keys to be on the same node?
- What happens if you forget to use a hash tag for keys that need to be grouped together?
Related topics