Redis Message Queue

Source: Internet
Author: User

Redis Message Queue

Redis message queue is easy to use and has no configuration. It is much lighter than ActiveMQ and has simple functions. If you only need simple subscription and release, you can consider using it.

Subscription operation

The command is subscribe [channel] [channel]..., as shown in [Code 1]. The channel [redis. blog] is successfully subscribed to.

Release Operation

The command is publish [channel] [message], as shown in code 2. Figure 1 shows the effect of the subscribed client.
[Code 1 ]:

subscribe "redis. blog"

Code 2]

publish "redis.blog" "hello redis"

Figure 1]

Unsubscribe operation <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Rotate/L + cq + oaMgPGJyPgqhvrT6wuszob88L3A + CgoKCjxwcmUgY2xhc3M9 "brush: SQL;"> unsubscribe "redis.blog"

Mode subscription

Command: psubscribe [pattern] [pattern]…, As shown in code 4, if the published message conforms to the current subscription mode, a Message notification is also received, as shown in figure 2.
Code 4]

psubscribe "redis.*"

Figure 2]

Unsubscribe
Command: punsubscribe [pattern] [pattern]…, As shown in code 5.
Code 5]

punsubscribe "redis.*"

Data Structure
For channels and modes, redis uses two struct implementations, which are defined in redisServer as follows:

struct rediServer{ dict *pubsub_channels; list *pubsub_patterns;}

A channel is a dict. In pubsub_channels, the key is the channel name and the value is a list. The list stores all the client machines that subscribe to the current channel;
Subscription: If the current channel does not exist, the subscribe operation is a dictAdd operation. If yes, it is equivalent to append the current client to the value list corresponding to the current channel;
Unsubscribe: Get the list from the get (channel) in dict and delete the current client from the list. If the list is empty after deletion, the current channel has no subscribers, the current channel is deleted.

The mode is a list. each node in the list is a pubsubPattern structure, which is defined as follows:

typedef struct pubsubPattern{redisClient *client;robj *pattern;} 

Subscription Mode: Inserts a pubsubPattern at the end of the current list team;
Unsubscribe Mode: This is the process of traversing the list to delete matching nodes.
Even if different clients subscribe to the same mode, they also have two different nodes, or two different modes subscribed to by the same client. This can be seen from the data structure of pubsubPattern.

Send message
When a message is published, the redis server performs two operations:

Send a message to all subscribers of the corresponding channel. The specific operation is as follows:
Take the current channel as the key and find the corresponding value in the current pubsub_channels dictionary. value is a subscriber list. traverse the list and send the message to all subscribers; send the message to all modes that match the current channel. The specific operation is as follows:
Use the current channel as the key to traverse all nodes in the current pubsub_patterns list. If the pattern value of a node (pubsubPattern) matches the key, the message is sent to the client of the current node, the traversal ends.

The other three commands

Pubsub channels: view all current channels. You can also use the wildcard to return all matched channels (pubsub channels "redis. * "); pubsub numsub: receives any number of channels as input parameters and returns the number of subscribers for these channels; pubsub numpat: returns the number of subscribers in the current subscription mode on the server;

As for how to timely feedback published messages to all subscribers, redis operates through file events on the server, not just the Message Queue function, all get, all set operations are driven by file events, which will be described separately.

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.