Redis pipeline Pipeline

Source: Internet
Author: User
Tags redis server

Purpose of the pipeline function:Improve program execution efficiency by reducing the number of communications between the client and the server.

1. Communication

In general, each time a user executes a redis command, the client and the server need to communicate with each other: the client sends the command request to the server, the server returns the result of executing the command to the client.

When the program executes some complex operations, the client may need to execute multiple commands and communicate with the server multiple times.

Suppose we are building a website that tags books. Each book on this website can be tagged with any number of tags. To record which tags the books are read by the most people, we create a click counter for each tag. When a user browses a book on the website, the program adds an operation to the click counter of each label in the book.
For example, for the redis in action book, users may tag it with four tags: "computer", "programming", "Database", and "redis, every time the redis in action page is accessed, the program will perform an additional operation on the click counters of the four tags:

Incr Tag: Computer: click_counterincr Tag: programming: click_counterincr Tag: Database: click_counterincr Tag: redis: click_counter

 

For this click counting program, the number of incr commands to be executed by the client depends on the number of tags in the book. The more tags, the more incr commands to be executed.
Displays the communication between the client and the server when the redis in action page is viewed.

Because the redis server has very high performance, when a command request arrives at the server, the server usually finishes executing the command quickly and returns the command execution result to the client.
In most cases, the client spends most of its waiting time sending command requests and receiving command responses when executing redis commands.

 

Ii. Pipeline Functions

For the program that needs to execute multiple commands, such as the click counter shown above, if we can reduce the number of communications between the client and the server during program execution, it can effectively improve the program performance, and redis's Pipeline Function (pipeline) is set for this.
Redis pipeline allows clientsSend multiple command requests to the server at a time, and return all the results of the executed command requests to the client in a command response.This function can effectively reduce the number of times the client needs to communicate with the server when executing multiple commands.

The click counter listed above is used as an example. We can wrap multiple incr commands into one pipeline for execution, reduce the number of communications required by the program to execute n incr commands from N to 1.
Demonstrate how the pipeline function reduces the number of times the click counter of each tag in redis in action from four to one.

 

Iii. Pipeline functions in the python Client

Different redis clients use different pipeline functions. The following code shows how to use a python client, how to enable the Pipeline Function to update the click counter of each tag in redis in action:

From redis import redis # create redis client = redis () # create a pipeline object and wrap four incr commands # transaction = false to disable the transaction function, only use the Pipeline Function # for transaction details, we will introduce pipeline = client later. pipeline (transaction = false) pipeline. incr ('tag: Computer: click_counter ') pipeline. incr ('tag: programming: click_counter ') pipeline. incr ('tag: Database: click_counter ') pipeline. incr ('tag: redis: click_counter ') # Send the four commands wrapped in a streamline ()

 

The results of multiple commands executed in pipeline mode will be returned in a command response. The following is the execution result of the above counter update code:

# The results of multiple commands will be returned in the form of a list # The first item in the list 10087 is the value of Tag: Computer: click_counter # the second item 5001 Is Tag: programming:: click_counter value # the third item 3421 is the value of Tag: Database: click_counter # The fourth item 1001 is the value of Tag: redis: click_counter [10087,500 1, 3421,100 1]

 

 

Iv. Summary

In general, each time a user executes a redis command, the client and the server need to communicate with each other: the client sends a command request to the server, the server returns the command obtained from the command execution to the client;
In most cases, most of the time spent in executing commands is the process of sending commands and receiving replies. Therefore, the number of communications between the client and the server is reduced, it can effectively improve the program execution efficiency;
The pipeline can Package Multiple commands and send them together, and receive the results of all executed commands in one response. This function can effectively improve the efficiency of the program when executing multiple commands;

 

Redis pipeline Pipeline

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.