MQ prophet
MQ(Message queue) is the middleware commonly used by backend developers. The first-in, first-out (FIFO) message processing mechanism can help decoupler backend services to adapt to the trend of microservices architecture. Currently, the mainstream MQ in the industry includes Ali MQ, IBM WebSphere MQ, Kafka, RabbitMQ. Developers can choose according to their own business scenarios and comparison reports.
Delay queue
A delayed queue does not process messages in time like a normal queue, but instead waits for a period of time before processing them. Application scenarios, for example, refund after 24 hours or notification of requirement development after 3 days.
What is special about RabbitMQ
RabbitMQ itself does not support delay queuing, but it has two features: Time To Live (TTL) and dead letter queuing. If a message is not consumed in MQ and stays longer than TTL, it will be put into a dead letter queue. The dead-letter queue here can be understood as a new MQ that can be listened on and consumed.
Example code to set a message to an expiration time of 6 seconds:
if err := channel.Publish( exchange, routKey, false, false, amqp.Publishing{ Expiration: ContentType: "text/plain", Body: [] Byte (Body), Timestamp: time.now (),},); err ! = nil { errMsg := fmt.Sprintf("Failed to msg Publish! err = %v", err) return errors.New(errMsg) }Copy the code
transform
Using RabbitMQ’s TTL and dead-letter queue, we can set the waiting time to TTL, deliberately not consume it, wait for it to expire, dead-letter queue, asynchronous processing, as follows: