Redis 並列処理とロック
Redisにはトランザクション機能がある
MULTIにより開始、EXECで終了
Redis Transactions allow the execution of a group of commands in a single step, they are centered around the commands MULTI, EXEC, DISCARD and WATCH.
トランザクション内でRedisに対して発行された命令は一度キューイングされ、EXECのタイミングで順次実行される
code:bash
➜ redis-cli ()
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379(TX)> incr hoge
QUEUED
127.0.0.1:6379(TX)> incr fuga
QUEUED
127.0.0.1:6379(TX)> EXEC
1) (integer) 1
2) (integer) 1
127.0.0.1:6379> get hoge
"1"
ロールバックはされないらしい
Redis does not support rollbacks of transactions since supporting rollbacks would have a significant impact on the simplicity and performance of Redis.
Rubyの場合はmutliだけでトランザクションになる
code:ruby
redis.multi do |transaction|
transaction.set "foo", "bar"
transaction.incr "baz"
end
redis-objectsによるロック
Redis::Lockが便利
ブロックを取って、処理が終わったら勝手にロックを解放してくれる