Commands and Syntax of Redis Pipelining
With Redis pipelining, it is as easy as firing off a sequence of commands in succession without waiting for responses. Here’s a basic outline of how it works:
- First, it starts the TP transaction.
- Redis commands are sent one after the other.
- Finally, execute ‘EXEC’ for all queued transactions.
- Command sent, and received responses in return.
Here is an example sequence in Python using the popular `redis-py` library:
In our instance:
- We start with pipe = r.pipeline()
- Chain two commands (SET and GET)
- Then execute the pipeline through pipe.execute().
- The we can decode and interpret these responses.
Below is the implementation of the example:
Python3
import redis # Connect to Redis server r = redis.Redis(host = 'localhost' , port = 6379 ) # Start a pipeline pipe = r.pipeline() # Queue multiple commands pipe. set ( 'name' , 'Alice' ) pipe.get( 'name' ) # Execute the pipeline responses = pipe.execute() # Retrieve responses name = responses[ 1 ].decode( 'utf-8' ) print (f "Name: {name}" ) |
Output:
Name: Alice
Explanation of the output:
- The SET command sets the key ‘name’ with the value ‘Alice’.
- The GET command retrieves the value associated with the key ‘name’.
- The pipeline is executed with pipe.execute(), which sends both commands to the Redis server.
- The responses are stored in the responses list.
- The value retrieved from the GET command is decoded as a UTF-8 string and stored in the variable name.
- Finally, it prints the name, which should be “Alice” based on the input provided.
Redis Pipelining Commands
Here are some commonly used Redis Pipelining commands along with their explanations, syntax, and examples:
1. MULTI: Begins a transaction block for Redis Pipelining.
Syntax: `MULTI`
2. EXEC: Executes all the commands in the transaction block.
Syntax: `EXEC`
Below is an example Using MULTI and EXEC to perform a transaction with Redis Pipelining:
Python3
import redis # Create a Redis connection r = redis.StrictRedis(host = 'localhost' , port = 6379 , db = 0 ) # Start a pipeline pipe = r.pipeline() # Queue up commands within the transaction pipe.multi() pipe. set ( 'name' , 'Alice' ) pipe.get( 'name' ) # Execute the transaction pipe. exec () # Retrieve and print the result result = pipe.execute() print ( "Name:" , result[ 1 ].decode( 'utf-8' )) |
3. SET: Sets the value of a key.
Syntax: `SET key value`
4. GET: Retrieves the value associated with a key.
Syntax: `GET key`
Below is an example Using SET and GET commands in a Redis Pipelining:
Python3
import redis # Create a Redis connection r = redis.StrictRedis(host = 'localhost' , port = 6379 , db = 0 ) # Start a pipeline pipe = r.pipeline() # Queue up SET and GET commands pipe. set ( 'name' , 'Alice' ) pipe.get( 'name' ) # Execute the pipeline responses = pipe.execute() # Decode and print the result name = responses[ 1 ].decode( 'utf-8' ) print ( "Name:" , name) # This will print "Name: Alice" |
5. DISCARD: Cancels the transaction block created by MULTI, discarding all queued commands.
Syntax: DISCARD
Below is an example Using DISCARD to cancel a transaction:
Python3
import redis # Create a Redis connection r = redis.StrictRedis(host = 'localhost' , port = 6379 , db = 0 ) # Start a pipeline pipe = r.pipeline() # Queue up commands within the transaction pipe.multi() pipe. set ( 'name' , 'Alice' ) pipe.discard() # Cancel the transaction # Execute the pipeline (no need for EXEC after DISCARD) pipe.execute() # Attempt to retrieve the value (it won't be set) value = r.get( 'name' ) print ( "Name:" , value) # This will print "Name: None" |
6. HSET: Sets the field in a hash stored at the given key to the specified value.
Syntax: HSET key field value
7. HGET: Retrieves the value of a field in a hash stored at the given key.
Syntax: HGET key field
Below is an example Using `HSET` and `HGET` commands in Redis Pipelining:
Python3
import redis # Create a Redis connection r = redis.StrictRedis(host = 'localhost' , port = 6379 , db = 0 ) # Start a pipeline pipe = r.pipeline() # Queue up HSET and HGET commands pipe.hset( 'user:id1' , 'username' , 'Alice' ) pipe.hget( 'user:id1' , 'username' ) # Execute the pipeline responses = pipe.execute() # Decode and print the result username = responses[ 1 ].decode( 'utf-8' ) print ( "Username:" , username) # This will print "Username: Alice" |
8. RPUSH: Appends one or more values to the end of a list.
Syntax: RPUSH key value [value …]
9. LPOP: Removes and returns the first element from a list.
Syntax: LPOP key
Below is an example Using `RPUSH` to add elements to a list and `LPOP` to remove and retrieve elements from the list in Redis Pipelining:
Python3
import redis # Create a Redis connection r = redis.StrictRedis(host = 'localhost' , port = 6379 , db = 0 ) # Start a pipeline pipe = r.pipeline() # Queue up RPUSH and LPOP commands pipe.rpush( 'tasks' , 'Task 1' , 'Task 2' ) pipe.lpop( 'tasks' ) # Execute the pipeline responses = pipe.execute() # Print the results # This will print "Task removed: Task 1" print ( "Task removed:" , responses[ 1 ].decode( 'utf-8' )) |
Complete Guide to Redis Pipelining
Redis is unbeatable when it comes to storing and retrieving data. It is open source and functions as an in-memory data structure store that provides performance, low latency data to different applications. Among others, one feature to effectuate this is Redis Pipelining – so stated by Redis. Here we analyze the concept of Redis pipelining
Important Topics for Redis Pipelining
- What is Redis Pipelining?
- Request/Response Protocols and Round Trip Time (RTT)
- Commands and Syntax of Redis Pipelining
- Examples of Redis Pipelining
- Pros of Redis Pipelining
- Cons of Redis Pipelining
- Conclusion
Contact Us