Explaining and Using Hash Tags
Hash Tag Design
Multi-key operations, such as those using the MSET command or Lua scripts, are atomic. All specified keys are executed at the same time. However, in a cluster, each key is hashed to a given shard, and multi-key operations are no longer atomic. The keys may be allocated to different slots. As a result, some keys are updated, while others are not. If there is a hash tag, the cluster determines which slot to allocate a key based on the hash tag. Keys with the same hash tag are allocated to the same slot.
Using Hash Tags
Only the content between the first left brace ({) and the following first right brace (}) is hashed.
For example:
- In keys {user1000}.following and {user1000}.followers, there is only one pair of braces. user1000 will be hashed.
- In key foo{}{bar}, there is no content between the first { and the first }. The whole key foo{}{bar} will be hashed as usual.
- In key foo{{bar}}zap, {bar (the content between the first { and the first }) is hashed.
- In key foo{bar}{zap}, bar is hashed because it is between the first pair of { and }.
Hash Tag Example
When the following operation is performed:
EVAL "redis.call('set',KEYS[1],ARGV[1]) redis.call('set',KEYS[2],ARGV[2])" 2 key1 key2 value1 value2
The following error is displayed:
ERR 'key1' and 'key2' not in the same slot
You can use a hash tag to solve this issue:
EVAL "redis.call('set',KEYS[1],ARGV[1]) redis.call('set',KEYS[2],ARGV[2])" 2 {user}key1 {user}key2 value1 value2
Redis Usage FAQs
- Can I Change the AZ for an Instance?
- Can I Change the VPC and Subnet for a DCS Redis Instance?
- Can I Customize or Change the Port for Accessing a DCS Instance?
- Can I Modify the Connection Addresses for Accessing a DCS Instance?
- Why Does It Take a Long Time to Start a Cluster DCS Instance?
- What Should I Do If an Error Occurs in redis_exporter?
- Does DCS for Redis Provide Backend Management Software?
- Can I Recover Deleted Data of a DCS Instance?
- How Do I Check Redis Memory Usage?
- Why Is the Capacity or Performance of a Shard of a Redis Cluster Instance Overloaded When That of the Instance Is Still Below the Bottleneck?
- Why Does an OOM Error Occur During a Redis Connection?
- What Clients Can I Use for Redis Cluster in Different Programming Languages?
- Why Do I Need to Configure Timeout for Redis Cluster?
- Why Am I Seeing a Timeout Error When Reading Data from Redis?
- Explaining and Using Hash Tags
- Why Does a Key Disappear in Redis?
- Will Cached Data Be Retained After an Instance Is Restarted?
- How Do I Know Whether an Instance Is Single-DB or Multi-DB?
- What Are the Constraints on Implementing Multi-DB on a Proxy Cluster Instance?
- How Do I Create a Multi-DB Proxy Cluster Instance?
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.
more