Skip to content

Commit 46e852f

Browse files
authored
docs: fix readme about MGET, MSET and DEL commands (#373)
1 parent 981c020 commit 46e852f

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

README.md

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -138,41 +138,66 @@ The `#pubsub` method supports sharded subscriptions.
138138
Every interface handles redirections and resharding states internally.
139139

140140
## Multiple keys and CROSSSLOT error
141-
A subset of commands can be passed multiple keys. But it has a constraint the keys are in the same hash slot.
142-
The following error occurs because keys must be in the same hash slot and not just the same node.
141+
A subset of commands can be passed multiple keys.
142+
In cluster mode, these commands have a constraint that passed keys should belong to the same slot
143+
and not just the same node.
144+
Therefore, The following error occurs:
143145

144-
```ruby
145-
cli = RedisClient.cluster.new_client
146+
```
147+
$ redis-cli -c mget key1 key2 key3
148+
(error) CROSSSLOT Keys in request don't hash to the same slot
149+
150+
$ redis-cli -c cluster keyslot key1
151+
(integer) 9189
146152
147-
cli.call('MGET', 'key1', 'key2', 'key3')
148-
#=> CROSSSLOT Keys in request don't hash to the same slot (RedisClient::CommandError)
153+
$ redis-cli -c cluster keyslot key2
154+
(integer) 4998
149155
150-
cli.call('CLUSTER', 'KEYSLOT', 'key1')
151-
#=> 9189
156+
$ redis-cli -c cluster keyslot key3
157+
(integer) 935
158+
```
152159

153-
cli.call('CLUSTER', 'KEYSLOT', 'key2')
154-
#=> 4998
160+
For the constraint, Redis cluster provides a feature to be able to bias keys to the same slot with a hash tag.
155161

156-
cli.call('CLUSTER', 'KEYSLOT', 'key3')
157-
#=> 935
158162
```
163+
$ redis-cli -c mget {key}1 {key}2 {key}3
164+
1) (nil)
165+
2) (nil)
166+
3) (nil)
159167
160-
Also, you can use the hash tag to bias keys to the same slot.
168+
$ redis-cli -c cluster keyslot {key}1
169+
(integer) 12539
161170
162-
```ruby
163-
cli.call('CLUSTER', 'KEYSLOT', '{key}1')
164-
#=> 12539
171+
$ redis-cli -c cluster keyslot {key}2
172+
(integer) 12539
165173
166-
cli.call('CLUSTER', 'KEYSLOT', '{key}2')
167-
#=> 12539
174+
$ redis-cli -c cluster keyslot {key}3
175+
(integer) 12539
176+
```
168177

169-
cli.call('CLUSTER', 'KEYSLOT', '{key}3')
170-
#=> 12539
178+
In addition, this gem works multiple keys without a hash tag in MGET, MSET and DEL commands
179+
using pipelining internally automatically.
180+
If the first key includes a hash tag, this gem sends the command to the node as is.
181+
If the first key doesn't have a hash tag, this gem converts the command into single-key commands
182+
and sends them to nodes with pipelining, then gathering replies and returning them.
171183

172-
cli.call('MGET', '{key}1', '{key}2', '{key}3')
184+
```ruby
185+
r = RedisClient.cluster.new_client
186+
#=> #<RedisClient::Cluster 127.0.0.1:6379>
187+
188+
r.call('mget', 'key1', 'key2', 'key3')
189+
#=> [nil, nil, nil]
190+
191+
r.call('mget', '{key}1', '{key}2', '{key}3')
173192
#=> [nil, nil, nil]
174193
```
175194

195+
This behavior is for upper libraries to be able to keep a compatibility with a standalone client.
196+
You can exploit this behavior for migrating from a standalone server to a cluster.
197+
Although multiple times queries with single-key commands are slower than pipelining,
198+
that pipelined queries are slower than a single-slot query with multiple keys.
199+
Hence, we recommend to use a hash tag in this use case for the better performance.
200+
176201
## Transactions
177202
This gem supports [Redis transactions](https://redis.io/topics/transactions), including atomicity with `MULTI`/`EXEC`,
178203
and conditional execution with `WATCH`. Redis does not support cross-node transactions, so all keys used within a

0 commit comments

Comments
 (0)