Queue message implementation in redis applies decoupling method

  • 2020-06-12 10:57:52
  • OfStack

The scenario of a message queue

As we all know, a message is a unit of data that is passed between two computers, and this "message" can be very simple, such as just a text string, or more complex, possibly containing embedded objects. A "message queue" is a container that holds messages during their transmission. In web programs, maybe we will need the user's request data updated or added to the database, but in the case of Gao Bing hair, although we don't know what is the background reasons of the user, but still complain about slow or poking fun at the program, such as in the past few years, it is difficult to have joked about 12306 grab your ticket? Slow reaction? Have you ever received "service is too busy" when you were using a program that was overloaded with concurrent access that exceeded the maximum load on the system? Of course we can increase the load capacity of the application by adding the number of servers in the server cluster.

Disadvantages of excessive coupling: In php applications, excessive coupling makes the system extensibility weak and linkage error.

Today I'm going to introduce decoupling between modules using redis's queued messages.

Application scenario: The user orders a batch of goods in the procurement module and then adds the corresponding inventory in the warehouse module when signing for the goods. The common practice is to push data to the warehouse module through the interface to carry out warehousing operation when signing for the goods is completed. However, if the warehouse module is sent incorrectly, no successful warehousing will lead to the failure of signing for the purchase module, which is the disadvantage of coupling to the system.

Solution: apply techniques of php + redis queue messages when the user to sign for success, put the data into the queue, shown successful procurement module is returned to sign for prompt to the user, the subsequent operations don't need to worry about purchasing module, then the warehouse system can be released through redis subscription mode to listen, when you have data, implement the logic code inventory warehouse system

Run.

Key points:

1. What if the redis server goes down?

Make a judgment in the sign-in logic code of the purchase module. If the sign-in succeeds, the sign-in is returned; if the sign-in fails, the traditional method is run, and the method is put into the library through the interface. What if the redis dies and the warehouse module dies? Pack up and go home.

2. How to realize real-time storage in the traditional way?

redis's subscription publishing model can be used to solve this problem. The subscribed code is in the warehouse module, and the published code is in the procurement module.

Subscribe to sub.php


<?php
$redis = new Redis();
$redis->connect('localhost', 6379);
$redis->subscribe(['ruku'], function ($redis, $chan, $msg) {// Subscribe to inbound channel if($msg==2500){   // If you listen for a published message then you take the message from the queue and put it in the store      .   }  
});

Publish the code ES43en.php


<?php
$redis = new Redis();
$redis->connect('localhost', 6379);
$order = ['id' => 1, 'name' => ' millet 6', 'price' => 2499, 'created_at' => '2017-07-14'];$redis->lpush('order',$order);
$redis->publish("ruku", 2500);

conclusion


Related articles: