# Hash Tag Co-location Patterns Force related keys to the same Redis Cluster slot using hash tags, enabling atomic multi-key operations, transactions, and Lua scripts across logically related data. Redis Cluster distributes keys across slots based on a hash of the key name. By using hash tags `{...}`, you control which part of the key determines the slot, co-locating related keys for atomic operations. ## How Hash Tags Work Redis Cluster hashes only the substring inside `{...}` to determine the slot: user:{123}:profile → hashes "123" → slot X user:{123}:settings → hashes "123" → slot X user:{123}:sessions → hashes "123" → slot X All three keys land in the same slot, enabling atomic operations across them. Without hash tags: user:123:profile → hashes entire key → slot A user:123:settings → hashes entire key → slot B (different!) ## Basic Pattern: User Data Co-location Group all data for a single entity: user:{user_id}:profile user:{user_id}:settings user:{user_id}:notifications user:{user_id}:sessions Now you can: - Update multiple fields atomically with MULTI/EXEC - Run Lua scripts touching all user keys - Use WATCH for optimistic locking across keys ## Atomic Multi-Key Operations ### Transaction Across Related Keys MULTI HSET user:{123}:profile name "Alice" updated_at "1706648400" HSET user:{123}:settings theme "dark" INCR user:{123}:stats:updates EXEC All commands execute atomically because keys share a slot. ### Lua Script Across Keys local profile = redis.call('HGETALL', KEYS[1]) local settings = redis.call('HGETALL', KEYS[2]) -- Process both atomically redis.call('SET', KEYS[3], cjson.encode({profile, settings})) return 'OK' Call with: EVAL