How to use Redis to realize message queue in PHP
This article is about how to use Redis to achieve message queue in PHP. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
The advantage of Redis as message queue lies in its lightweight, high concurrency and delay sensitivity. Application scenarios include real-time data analysis, second kill counter, cache and so on.
Problems to be solved when Redis does message queuing:
1. Message reliability: there is no corresponding mechanism to guarantee message consumption. When consumer consumption fails, the message body is lost and needs to be handled manually. The producer simply inserts data into the queue, regardless of whether the consumer is successful or not.
2. The consumer hang up message will not be lost, but it needs to be triggered again before the consumer can continue to consume the message.
The code is as follows:
Lib.php is a tool file that contains database connections and Redis connections: