Introduction
Pipelines and transactions help send multiple commands to Redis efficiently, but they work differently to improve speed or ensure all commands run together.
Pipeline: redis.pipelined do redis.set 'key1', 'value1' redis.set 'key2', 'value2' end Transaction: redis.multi do redis.set 'key1', 'value1' redis.set 'key2', 'value2' end
redis.pipelined do redis.set 'a', 1 redis.incr 'a' end
redis.multi do redis.set 'a', 1 redis.incr 'a' end
redis.pipelined do redis.set 'x', 10 redis.incr 'x' end value_after_pipeline = redis.get('x') redis.multi do redis.set 'y', 20 redis.incr 'y' end value_after_transaction = redis.get('y') [value_after_pipeline, value_after_transaction]
MULTI command, which is called as redis.multi() in clients.pipeline(), but transaction specifically uses multi() to begin.pipe = redis.pipeline()
pipe.set('a', 1)
pipe.set('b', 2)
pipe.get('a')
results = pipe.execute()
What will results contain?pipe = redis.pipeline()
pipe.set('x', 10)
pipe.set('y', 20)
pipe.execute()
But you wanted atomic execution. What is missing?multi() to start a transaction.multi(), pipeline just batches commands without atomic guarantee.Pass the test and this pattern moves unknown