Pipeline examples#

This example show quickly how to use pipelines in redis-py.

Checking that Redis is running#

import redis

r = redis.Redis(decode_responses=True)

Simple example#

Creating a pipeline instance#

pipe = r.pipeline()

Adding commands to the pipeline#

pipe.set("a", "a value")
pipe.set("b", "b value")


Executing the pipeline#

[True, True, 'a value']

The responses of the three commands are stored in a list. In the above example, the two first boolean indicates that the set commands were successfull and the last element of the list is the result of the get("a") comand.

Chained call#

The same result as above can be obtained in one line of code by chaining the opperations.

pipe = r.pipeline()
pipe.set("a", "a value").set("b", "b value").get("a").execute()
[True, True, 'a value']

Performance comparison#

Using pipelines can improve performance, for more informations, see Redis documentation about pipelining. Here is a simple comparison test of performance between basic and pipelined commands (we simply increment a value and measure the time taken by both method).

from datetime import datetime

incr_value = 100000

Without pipeline#

r.set("incr_key", "0")

start = datetime.now()

for _ in range(incr_value):
res_without_pipeline = r.get("incr_key")

time_without_pipeline = (datetime.now() - start).total_seconds()
print("Without pipeline")
print("Time taken: ", time_without_pipeline)
print("Increment value: ", res_without_pipeline)
Without pipeline
Time taken:  21.759733
Increment value:  100000

With pipeline#

r.set("incr_key", "0")

start = datetime.now()

pipe = r.pipeline()
for _ in range(incr_value):
res_with_pipeline = pipe.execute()[-1]

time_with_pipeline = (datetime.now() - start).total_seconds()
print("With pipeline")
print("Time taken: ", time_with_pipeline)
print("Increment value: ", res_with_pipeline)
With pipeline
Time taken:  2.357863
Increment value:  100000

Using pipelines provides the same result in much less time.