Skip to content

Commit 59db4e3

Browse files
authored
Explain patterns with hash tags in cluster spec (redis#2646)
Adding to the cluster spec documentation of the following Redis features: redis/redis#12754 (SCAN) redis/redis#12536 (KEYS) redis/redis#12728 (SORT, SORT_RO)
1 parent e54befc commit 59db4e3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

docs/reference/cluster-spec.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,23 @@ Examples:
199199
* For the key `foo{bar}{zap}` the substring `bar` will be hashed, since the algorithm stops at the first valid or invalid (without bytes inside) match of `{` and `}`.
200200
* What follows from the algorithm is that if the key starts with `{}`, it is guaranteed to be hashed as a whole. This is useful when using binary data as key names.
201201

202+
#### Glob-style patterns
203+
204+
Commands accepting a glob-style pattern, including `KEYS`, `SCAN` and `SORT`, are optimized for patterns that imply a single slot.
205+
This means that if all keys that can match a pattern must belong to a specific slot, only this slot is searched for keys matching the pattern.
206+
The pattern slot optimization is introduced in Redis 8.0.
207+
208+
The optimization kicks in when the pattern meets the following conditions:
209+
210+
* the pattern contains a hashtag,
211+
* there are no wildcards or escape characters before the hashtag, and
212+
* the hashtag within curly braces doesn't contain any wildcards or escape characters.
213+
214+
For example, `SCAN 0 MATCH {abc}*` can successfully recognize the hashtag and scans only the slot corresponding to `abc`.
215+
However, the patterns `*{abc}`, `{a*c}`, or `{a\*bc}` cannot recognize the hashtag, so all slots need to be scanned.
216+
217+
#### Hash slot example code
218+
202219
Adding the hash tags exception, the following is an implementation of the `HASH_SLOT` function in Ruby and C language.
203220

204221
Ruby example code:

0 commit comments

Comments
 (0)