You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This gem supports [Redis transactions](https://redis.io/topics/transactions), including atomicity with `MULTI`/`EXEC`,
174
174
and conditional execution with `WATCH`. Redis does not support cross-node transactions, so all keys used within a
175
-
transaction must live in the same key slot. To use transactions, you must thus "pin" your client to a single connection using
176
-
`#with`. You can pass a single key, in order to perform multiple operations atomically on the same key, like so:
175
+
transaction must live in the same key slot. To use transactions, you can use `#multi` method same as the [redis-client](https://github.com/redis-rb/redis-client#usage):
177
176
178
177
```ruby
179
-
cli.with(key:'my_cool_key') do |conn|
180
-
conn.multi do |m|
181
-
m.call('INC', 'my_cool_key')
182
-
m.call('INC', 'my_cool_key')
183
-
end
184
-
# my_cool_key will be incremented by 2, with no intermediate state visible to other clients
178
+
conn.multi do |tx|
179
+
tx.call('INCR', 'my_key')
180
+
tx.call('INCR', 'my_key')
185
181
end
186
182
```
187
183
188
-
More commonly, however, you will want to perform transactions across multiple keys. To do this, you need to ensure that all keys used in the transaction hash to the same slot; Redis a mechanism called [hashtags](https://redis.io/docs/reference/cluster-spec/#hash-tags) to achieve this. If a key contains a hashag (e.g. in the key `{foo}bar`, the hashtag is `foo`), then it is guaranted to hash to the same slot (and thus always live on the same node) as other keys which contain the same hashtag.
184
+
More commonly, however, you will want to perform transactions across multiple keys. To do this,
185
+
you need to ensure that all keys used in the transaction hash to the same slot;
186
+
Redis a mechanism called [hashtags](https://redis.io/docs/reference/cluster-spec/#hash-tags) to achieve this.
187
+
If a key contains a hashag (e.g. in the key `{foo}bar`, the hashtag is `foo`),
188
+
then it is guaranted to hash to the same slot (and thus always live on the same node) as other keys which contain the same hashtag.
189
189
190
-
So, whilst it's not possible in Redis cluster to perform a transction on the keys `foo` and `bar`, it _is_ possible to perform a transaction on the keys `{tag}foo` and `{tag}bar`. To perform such transactions on this gem, pass `hashtag:` to `#with` instead of `key`:
190
+
So, whilst it's not possible in Redis cluster to perform a transction on the keys `foo` and `bar`,
191
+
it _is_ possible to perform a transaction on the keys `{tag}foo` and `{tag}bar`.
192
+
To perform such transactions on this gem, use `hashtag:
191
193
192
194
```ruby
193
-
cli.with(hashtag:'user123') do |conn|
194
-
# You can use any key which contains "{user123}" in this block
195
-
conn.multi do |m|
196
-
m.call('INC', '{user123}coins_spent')
197
-
m.call('DEC', '{user123}coins_available')
198
-
end
195
+
conn.multi do |tx|
196
+
tx.call('INCR', '{user123}coins_spent')
197
+
tx.call('DECR', '{user123}coins_available')
199
198
end
200
199
```
201
200
202
-
Once you have pinned a client to a particular slot, you can use the same transaction APIs as the
> Errors happening after EXEC instead are not handled in a special way: all the other commands will be executed even if some command fails during the transaction.
223
+
> It's important to note that even when a command fails, all the other commands in the queue are processed - Redis will not stop the processing of commands.
224
+
251
225
```
226
+
$ telnet 127.0.0.1 6379
227
+
set key3 a
228
+
+OK
229
+
multi
230
+
+OK
231
+
set key3 b
232
+
+QUEUED
233
+
incr key3
234
+
+QUEUED
235
+
exec
236
+
*2
237
+
+OK
238
+
-ERR value is not an integer or out of range
239
+
get key3
240
+
$1
241
+
b
242
+
```
243
+
244
+
The `SET` command was processed because the `INCR` command was queued.
245
+
246
+
```
247
+
multi
248
+
+OK
249
+
set key3 c
250
+
+QUEUED
251
+
mybad key3 d
252
+
-ERR unknown command 'mybad', with args beginning with: 'key3' 'd'
253
+
exec
254
+
-EXECABORT Transaction discarded because of previous errors.
255
+
get key3
256
+
$1
257
+
b
258
+
```
259
+
260
+
The `SET` command wasn't processed because of the error during the queueing.
> Redis does not support rollbacks of transactions since supporting rollbacks would have a significant impact on the simplicity and performance of Redis.
252
265
253
-
Because `RedisClient` from the redis-client gem implements `#with` as simply `yield self` and ignores all of its
254
-
arguments, it's possible to write code which is compatible with both redis-client and redis-cluster-client; the `#with`
255
-
call will pin the connection to a slot when using clustering, or be a no-op when not.
266
+
It's hard to validate them perfectly in advance on the client side.
267
+
It seems that Redis aims to prior simplicity and performance efficiency.
268
+
So I think it's wrong to use the transaction feature by complex ways.
269
+
To say nothing of the cluster mode because of the CAP theorem. Redis is just a key-value store.
256
270
257
271
## ACL
258
272
The cluster client internally calls [COMMAND](https://redis.io/commands/command/) and [CLUSTER NODES](https://redis.io/commands/cluster-nodes/) commands to operate correctly.
0 commit comments