Redis practice (12) subscribing to and Publishing messages

Source: Internet
Author: User
Tags redis server

Pub/sub is a message communication mode. The main purpose is to decouple
Coupling, which is similar to the observer pattern in the design pattern. Pub/sub not only solves the Direct Problem of publishers and subscribers
Code-level coupling also solves the coupling between the two in physical deployment. As a pub/sub server, redis
And the publisher. The subscriber can use the subscribe and psubscribe commands
Server subscribes to the Message type that you are interested in. redis calls the message type channel ). When the publisher passes
When the publish Command sends a specific type of message to the redis server. All clients that subscribe to this message type will receive
This message. Here, messages are delivered in many-to-many ways. A client can subscribe to multiple channels or
Send a message.
The following is an experiment. Here we use 3 different clients. client1 is used to subscribe to messages of the channel tv1, client2
It is used to subscribe to the two chanel messages, tv1 and tv2, and client3 is used to publish messages of tv1 and tv2.

Client1:

650) this. width = 650; "title =" QQ20131214203048.png "src =" http://www.bkjia.com/uploads/allimg/131229/22131I0J-0.png "alt =" 212155349.png"/>

Client2:
650) this. width = 650; "title =" QQ20131214203048.png "src =" http://www.bkjia.com/uploads/allimg/131229/22131I011-1.png "alt =" 212226969.png"/>

Client3:

650) this. width = 650; "title =" QQ20131214203048.png "src =" http://www.bkjia.com/uploads/allimg/131229/22131K410-2.png "alt =" 212314958.png"/>

The following describes the receipt of Client1 and Client2:

650) this. width = 650; "title =" QQ20131214203048.png "src =" http://www.bkjia.com/uploads/allimg/131229/22131La5-3.png "alt =" 212435865.png"/>

650) this. width = 650; "title =" QQ20131214203048.png "src =" http://www.bkjia.com/uploads/allimg/131229/22131LX3-4.png "alt =" 212504435.png"/>

Both Client1 and Client2 receive a message from tv1, and then use Client3 to publish a message:

650) this. width = 650; "title =" QQ20131214203048.png "src =" http://www.bkjia.com/uploads/allimg/131229/22131K5F-5.png "alt =" 212623854.png"/>

Check the receipt status of Clien1 and Client2.

650) this. width = 650; "title =" QQ20131214203048.png "src =" http://www.bkjia.com/uploads/allimg/131229/22131M0T-6.png "alt =" 212728278.png"/>

650) this. width = 650; "title =" QQ20131214203048.png "src =" http://www.bkjia.com/uploads/allimg/131229/22131K946-7.png "alt =" 212801287.png"/>

The example above will be explained in detail below
1. client1 subscribes to messages from the channel tv1, and client2 subscribes to messages from the channels tv1 and tv2.
Message
2. client3 is the message publisher used to publish the tv1 and tv2 channels.
3. Next we will publish a message "publish tv1 program1" in client3. You can see that this message is
Sent to the tv1 Channel
4. client1 and client2 of course receive messages from this channel.
5. Then, client3 publishes a message "publish tv2 program2", which is sent to tv2 because
Client1 does not subscribe to tv1, so no result is displayed in the result of client1, but client2 subscribes to this
Channel, so client2 will return results.
You can also use psubscribe TV * to subscribe to the content of a channel starting with TV in batches.
After reading this small example, we should have a perceptual knowledge of the pub/sub function. Note that when a connection passes
Subscribe or psubscribe subscription channel, and then enter the subscription mode. In this mode, in addition to subscribe to additional channels or
The unsubscribe or punsubscribe command can be used to exit the subscription mode, and other commands cannot be sent. In addition
The psubscribe command subscribes to multiple wildcard channels. If a message matches multiple channel modes, the message is received multiple times.
To the same message.

 

Batch send requests using Pipeline

Redis is a cs-mode tcp server that uses a request response protocol similar to http. A client can use
Multiple socket connections initiate multiple request commands. After each request command is sent, the client usually blocks and waits for the redis service.
After redis completes processing, the Request command returns the result to the client through the Response Message. The basic communication process is as follows:

Client: INCR XServer: 1Client: INCR XServer: 2Client: INCR XServer: 3Client: INCR XServer: 4

 

Basically, four commands require eight tcp packets. Due to the network delay of communication, assume that
The packet transmission time between them takes 0.125 seconds. It takes at least one second to complete the eight packets of the preceding four commands.
In this way, even if redis can process 100 commands per second, our client can only issue four commands in one second. This
How can I solve this problem if I fail to make full use of redis's processing capabilities? We can use the pipeline method.
Package Multiple commands from the client and send them together. You do not need to wait for the response of a single command, but the redis server will process the response.
After multiple commands are completed, the processing results of these commands are packaged and returned to the client. The communication process is as follows:

Client: INCR XClient: INCR XClient: INCR XClient: INCR XServer: 1Server: 2Server: 3Server: 4

 

It is assumed that the tcp packet will not be split because it is too long. Two tcp packets can complete four commands, and the client can
The incr commands are sent together in a tcp packet, and the server can put the processing results of the four commands into a tcp
Message returned. When a large number of operations are performed using the pipeline method, we can save a lot of time when the network delay is wasted.
It is too late. You need to note that the command is sent by using pipeline. redis must be slowed down before processing all the commands.
Saves the processing results of all commands. The more packaged commands, the more memory consumed by the cache. Therefore, it is not a package command.
The more, the better. Tests are required based on actual conditions.

Whether or not to use pipeline must be based on
Network conditions are determined, and everything cannot be implemented based on the latest and best technology, because it may not be the most suitable for you.

 

 

This article is from "phper-a little bit every day ~" Blog, please be sure to keep this source http://janephp.blog.51cto.com/4439680/1340466

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.