redis / redis-rb

A Ruby client library for Redis
MIT License
3.96k stars 1.03k forks source link

Keep compatibilities as possible for the transaction feature between a standalone client and a cluster client #1267

Closed supercaracal closed 4 months ago

supercaracal commented 4 months ago

There are some differences of the interface for the transaction feature between redis gem and redis-client gem. It seems that the redis-client gem is designed to be more simpler than the redis gem.

redis gem: https://github.com/redis/redis-rb/blob/cdfe17264f1a67b35eb60d1532cee103c18692f1/lib/redis/commands/transactions.rb#L38-L49

redis-client gem: https://github.com/redis-rb/redis-client?tab=readme-ov-file#transactions

redis.multi(watch: %w[key]) do |tx|
  next unless redis.call('GET', 'key') == 'some value'

  tx.call('SET', 'key', 'other value')
  tx.call('INCR', 'counter')
end

On the redis-clustering gem side, I fixed some bugs for the transaction feature, but several compatibilities were broken.

So I tried to implement to keep the compatibility as possible by using an adapter. The adapter fills the interface gap between the redis-clustering gem and the redis-cluster-client gem. Because the cluster client implementation for the transaction feature tends to be complex for the sharding, we are hard to keep the same behaviors between the redis gem and the redis-clustering gem. But I thought it is possible up to a point.

redis.watch('{my}key') do |client|        # The client is an instance of the adapter
  if redis.get('{my}key') == 'some value' # We can't use the client passed by the block argument
    client.multi do |tx|                  # The tx is the same instance of the adapter
      tx.set('{my}key', 'other value') 
      tx.incr('{my}counter') 
    end 
  else 
    client.unwatch 
  end 
end