Redis can not only be used as a cache server, but also as a message queue. Its list type inherently supports use as a message queue.
Since the Redis list is implemented using a doubly linked list and saves the head and tail nodes, it is very difficult to insert elements on both sides of the list. Fast. (Recommended learning: Redis video tutorial)
So you can directly use Redis's List to implement the message queue, with just two simple instructions: lpush and rpop or rpush and lpop.
But there is a problem with message consumers, that is, they need to constantly call the rpop method to check whether there are pending messages in the List. Each call will initiate a connection, which will cause unnecessary waste. Maybe you will use Thread.sleep() and other methods to let the consumer thread consume again after a period of time, but there are two problems in doing so:
1) If the producer speed is greater than the consumer consumption speed, the message queue The length will keep increasing and will occupy a lot of memory space over time.
2) If the sleep time is too long, some time-sensitive messages cannot be processed. If the sleep time is too short, it will also cause relatively large overhead on the connection.
So you can use the brpop command. This command will only return if there is an element. If there is no element, it will block until the timeout returns null. Then you can run Customer and clear the console. You can see that the program has no output and is blocked. Got brpop here. Then open the Redis client and enter the command client list to see that there are currently two connections.
In addition to providing support for message queues, Redis also provides a set of commands to support the publish/subscribe mode.
1) Publish
The PUBLISH instruction can be used to publish a message, the format is PUBLISH channel message
The return value indicates the number of subscribers to the message.
2) Subscription
The SUBSCRIBE instruction is used to receive a message in the format SUBSCRIBE channel
It can be seen that the subscription mode was entered after using the SUBSCRIBE instruction, but the publish was not received. message, this is because the subscription will not receive it until the message is sent. For other commands in this mode, only replies can be seen.
Replies are divided into three types:
1. If it is subscribe, the second value indicates the subscribed channel, and the third value indicates which subscription it is. Channel? (understood as a serial number?)
2. If it is message, the second value is the channel that generated the message, and the third value is the message
3. If For unsubscribe, the second value represents the unsubscribed channel, and the third value represents the current number of client subscriptions.
You can use the command UNSUBSCRIBE to unsubscribe. If no parameters are added, all channels subscribed to by the SUBSCRIBE command will be unsubscribed.
Redis also supports wildcard-based message subscription, using the command PSUBSCRIBE (pattern subscribe).
You can see that the publish command returns 2, and the subscriber received the message twice. This is because the PSUBSCRIBE command can subscribe to the channel repeatedly. Channels subscribed using the PSUBSCRIBE command must also be unsubscribed using the PUNSUBSCRIBE command. This command cannot unsubscribe from channels subscribed by SUBSCRIBE. Similarly, UNSUBSCRIBE cannot unsubscribe from channels subscribed by the PSUBSCRIBE command. At the same time, the PUNSUBSCRIBE instruction wildcard will not be expanded.
Summary:
Using the List data structure of Redis can easily and quickly create a message queue. At the same time, the BRPOP and BLPOP instructions provided by Redis solve the problem of frequent calls to Jedis. Resource waste caused by rpop and lpop methods. In addition, Redis provides instructions for the publish/subscribe mode, which can realize message passing and inter-process communication.
For more Redis-related technical articles, please visit the Introduction to Using Redis Database Tutorial column to learn!
The above is the detailed content of Can redis be used as a message queue?. For more information, please follow other related articles on the PHP Chinese website!