• Implement task queues using lists
  • Priority queue
  • Subscribe by rule

Redis can also be used as a task queue. A task queue, as its name implies, is a queue that delivers tasks. What is the difference between a task queue and a message queue? Task queue is a logical model, while message queue is a communication model. They are different levels of abstraction. Message queue can be used to realize task queue.

There are two types of entities interacting with the task queue: producer and consumer. The producer puts the tasks that need to be processed into the task queue, and the consumer continuously reads the task information from the task queue and executes it.

A loose-coupling effect can be achieved with task queues, where producers and consumers do not need to know each other’s implementation details, just agree on a task description format. This allows producers and consumers to be written by different teams using different programming languages.

Moreover, under this model, consumers can be easily extended and distributed among different servers, reducing the load on a single server.

Implement task queues using lists

Redis’s list type supports PUSH from one side and POP from the other, making it ideal for a queue. The producer simply adds tasks to the list via LPUSH, and the consumer keeps fetching them from the list using RPOP. But do not want consumers to keep RPOP, can be improved with BRPOP. BRPOP When there are no elements in the list the BRPOP command blocks the connection until a new element is added.

The BRPOP command accepts two parameters, the first is the key name and the second is the timeout in seconds. Nil is returned when no new element has been retrieved after that time. When the timeout is set to 0, the list will block forever if no new elements are added.

Enable multiple clients

To test message notifications, you can open multiple instances of Redis-CLI. If you are using a Redis docker image, you can start a Redis container as a Redis server:

docker run --name redisserver -d -p 6379:6379 redis redis-server
Copy the code

Using the redis-server command, you can start the Redis server. Then you can open several terminals, perform the following command to start the redis container, and connect redisserver before startup, so that it can be used as a redis client USES:

docker run -it --link redisserver:redis --rm redis redis-cli -h redisserver -p 6379
Copy the code

In example A, enter:

 BRPOP queue 0
Copy the code

Instance A will then remain blocked. Then add elements to the queue in instance B:

 LPUSH queue taskk
Copy the code

You can immediately see the result returned in instance A and verify that the newly added element in the queue has been removed. Corresponding to BRPOP, there is the BLPOP command.

Priority queue

The BRPOP command can accept multiple keys at the same time, for example:

 BRPOP queue:1 queue:2 queue:3 0
Copy the code

It checks for all keys at the same time, blocks if all keys have no elements, and ejects elements from one of them if it has elements. If multiple keys have elements, one element of the first key is taken from left to right.

Queue :1 queue:2 queue:3 Queue :1 has the highest priority and the tasks in this queue are consumed first.

For example, on a website, if the account activation messages sent to users have a higher priority than the campaign promotion messages, you can put the two messages in different queues and use BRPOP to send the activation messages first.

Publish/subscribe mode

In addition to implementing task queues, Redis provides a set of commands that allow developers to implement a Publish /subscribe mode. The Publish/subscribe pattern also enables inter-process messaging. In the task queue implemented with list type, a task can only be consumed by one consumer, and multiple producers can push tasks to the queue. In publish/subscribe mode, a message can be received by all subscribers to a channel, and multiple publishers can push messages to a given channel.

Publish A message in redis-CLI instance A:

 PUBLISH channel.1 msg1
Copy the code

The return value of the PUBLISH command represents the number of subscribers that received the message. The sent messages are not persisted. A client subscribing to channel.1 can only receive subsequent messages published to the channel, not those sent previously.

Subscribe to channel.1 in redis- CLI instance B

 SUBSCRIBE channel.1
Copy the code

The SUBSCRIBE command allows you to SUBSCRIBE to multiple channels at the same time.

SUBSCRIBE command after the client will enter a state of subscription, the client can only use the conditions belong to “publish/SUBSCRIBE pattern” command, the command has the SUBSCRIBE/UNSUBSCRIBE/PSUBSCRIBE/PUNSUBSCRIBE.

However, after entering the subscription state in redis-CLI, you cannot exit to the non-subscription state. You can only close the CLI.

If instance B, which has subscribed to channel.1, posts msg1 from instance A to channel.1, instance B receives the following response:

1) "message"
2) "channel.1"
3) "msg1"
Copy the code

The first line is the message types, message type values have the subscribe/message/unsubscribe. When the message type is message, the second line indicates the channel name and the third line indicates the message content. When the message type is SUBSCRIBE, it indicates the feedback about the success of the subscription. The second line is the name of the channel successfully subscribed, and the third line is the number of channels currently subscribed by the client. When the message type is unsubscribe, the second line is the corresponding channel name, and the third line is the number of channels currently subscribed by the client. When this value is 0, the client will exit the subscription status.

Subscribe by rule

In addition to subscribing to a channel with a given name, you can SUBSCRIBE to multiple channels with the PSUBSCRIBE command according to a specified rule. Such as:

> PSUBSCRIBE channel.*
 Reading messages... (press Ctrl-C to quit)
 1) "psubscribe"
 2) "channel.*"(3)integer1)"
Copy the code

Subscription rules support the Glob style wildcard format, and any number of characters can be matched in channel. With the PSUBSCRIBE command, you can repeatedly subscribe to a channel. For example, a client executes the PSUBSCRIBE channel.? channel.? *, the client will receive two messages after Posting to channel.2.

The PUNSUBSCRIBE command unsubscribes a specified rule

PUNSUBSCRIBE [pattern ...]
Copy the code

If pattern is not specified, all rules subscribed by PSUBSCRIBE are unsubscribed.

Using the PUNSUBSCRIBE command, you can unsubscribe only the rules subscribed by the PSUBSCRIBE command, and it does not affect the channels subscribed by the SUBSCRIBE command directly. Similarly, the UNSUBSCRIBE command does not affect the rules for subscribes via the PSUBSCRIBE command.