Message Notification Task Queue Benefits of using task queues
- Loose coupling. Producers and consumers do not need to know each other's implementation details, only the description format of the task is agreed. This enables producers and consumers to be written by different teams using different programming languages
- Easy to scale. Consumers can have multiple, and can be distributed across different servers, this can easily reduce the load on a single server
Using the Lpush and Rpop commands to implement the queue, here is a new command Brpop and Blpop brpop key [key ...] timeout (s) brpop command similar to the Rpop command, the only difference When there are no elements in the list, the Brpop command blocks the connection until a new element is added
Priority Queue
Use the Brpop command to detect multiple key functions. If multiple keys have elements, an element in the first key is taken from left to right. Therefore, to implement the priority queue, put the priority key to the front is OK.
Publish/Subscribe mode
- The Publish/Subscribe mode contains two roles, publisher and Subscriber, respectively.
- Subscribers can subscribe to one or a number of channels (channel)
- The publisher can send a message to the specified channel, and all subscribers who subscribe to the channel will receive this message.
Command |
Description |
PUBLISH Channel msg |
Publish the message. The number of subscribers who received this message. Messages sent out will not be persisted, that is, when a client subscribes to the channel and receives only subsequent messages that are posted to that channel, it will not receive the |
SUBSCRIBE Channel [Channel ...] |
Subscribe to channels and subscribe to multiple channels at the same time |
Unsubscribe [Channel ...] |
Unsubscribe to the specified channel, and if no channel is specified, all channels will be unsubscribed |
Psubscribe pattern [pattern ...] |
Subscribe to one or more channels that match a given pattern |
Punsubscribe [pattern [pattern ...]] |
Unsubscribe from the specified rules, and unsubscribe from all rules if no parameters are set |
PUBSUB subcommand [argument [argument ...] |
Viewing subscription and publishing system status |
Attention
- After executing the subscribe command, the client enters the subscription state, in which case the client cannot use a command other than the Subscribe/unsubscribe/psubscribe/punsubscribe 4 commands that belong to the Publish/subscribe mode. Otherwise it will be an error
- The Punsubscribe command can only be used to unsubscribe from the rules subscribed by the Psubscribe command, without affecting the channels subscribed directly through the Subscribe command, nor does the unsubscribe command affect the rules subscribed by the Psubscribe command. Another error is that when you use the Punsubscribe command to unsubscribe from a rule, the wildcard character is not expanded, but a strict string match is performed, so Punsubscribe cannot unsubscribe from the channel. Rules, but must use Punsubscribe channel.* to unsubscribe
Clients may receive three types of replies after entering the subscription status. Each type of reply contains 3 values,
Message type (first value) |
a second value |
a third value |
Subscribe: A feedback message indicating a successful subscription |
Channel name for subscription success |
Number of channels subscribed by the current client |
Message: Indicates the messages received |
The name of the channel that generated the message |
Content of the message |
Unsubscribe: Indicates successful unsubscribe to a channel |
The corresponding channel name |
Number of channels subscribed by the current client |
The first value is unsubscribe, and when the third value is 0 o'clock the client exits the subscription state and can then execute other commands that are not in the Publish/subscribe mode.
Example:
Redis A>"subscribe""channel.1"3) (integer) 1
# Psubscribe by regular subscriptionRedis C>Psubscribe Channel.?*reading messages ... (Press Ctrl-c to quit) 1) "psubscribe" 2) "channel.? *" 3) (integer) 1redis B>PUBLISH CHANNEL.1 Hi!(integer) 2#此时, C received a reply1) "pmessage" 2) "channel.? *" # Note The reply has one more value, the second value is the wildcard character used when subscribing3) "channel.1" 4) "hi! "
Regular channel.? * Can match CHANNEL.1 and channel.10, but will not match channel.
If you find an error, please tap, welcome message exchange, thank you
REDIS Message Notification