PHP Queue processing: PHP Message Queuing Implementation principle (graphic)

Source: Internet
Author: User
What does the PHP queue mean? How did it come true? What are the application scenarios? The following is a detailed explanation of how PHP Message Queuing is implemented by using MySQL to decouple cases and use Redis for traffic clipping cases.

Concept of the queue:

1, is the queue structure of the middleware

2. no immediate processing is required after the message is placed

3. Processed by subscribers / consumers in sequence

Queue principle:

is also the decoupling principle: business systems and queue processing systems are not related

A write (business system), a read (Queue management system).

write , just go to the queue to write , other don't worry, read can read and write not related

Similarly, read from the queue to read , to work on the job, no job rest

Application Scenarios:

Redundancy: persistent storage in the queue , followed by the order handler, after processing is completed, the record is deleted

Decoupling: The solution to these 2 sets of systems (business and queue processing), we usually do is a set of systems. There is no direct relationship between the team and the system, one of them jumps, and the other system will not be affected because the queue is separated.

Traffic cutting front: seconds to kill and snapped. Traffic spikes, with the cache using Message Queuing (which is limited, such as only allow 10, the queue after the storage of 10 will not be saved, in the prompt to kill the second end)

Asynchronous communication: because the message itself enables the queued system to return directly, the asynchronous operation of the program is implemented

Scenario Extensibility: such as now only the order system and the financial system, follow-up I want to add a distribution system, directly to the distribution system to subscribe to this queue can be

Sort Guarantee: Some scenarios, such as The order in which the bank data is processed is important , because the queue itself can be made single-threaded, single-input out of this system

Implement Media:

1, using MySQL: High reliability, easy to implement, slow speed

2, using Redis: Fast, single big message packet efficiency is low

3, the use of more professional third-party library: Professional, reliable, high cost of learning.

Message processing trigger mechanism:

1, Dead Loop mode read: Easy to implement, failure can not be timely reply ( for seconds to kill this short time )

2, timed tasks: Pressure sharing, there is a processing limit (regardless of the system before you queue, peak how unstable, the queue system will still be timed execution )

Note: Timing time is a key: Do not finish the last scheduled task, the next scheduled task will start

Case: Order system, after the order information is written to the queue, immediately returned to the next order success. The distribution system periodically reads the queue every few minutes, and the order is summarized and processed.

3, daemon: Similar to PHP-FPM and CGI, Need Shell Foundation (use this process to detect, whether there is content in the queue, the content, the start of the Team System for processing)


Using MySQL to implement decoupling cases:

Why decoupling: If the schemas are together. Order system pressure, and logistics information is not required to return immediately. Orders collapsed, logistics jumped, so to decouple

Design of the table:

Code that the order system writes to the queue:

The distribution system reads the queue code:

Perform timed Tasks crontab-e


Note: This log file should be created on its own


1. Access via browser order.php Add Order

2, go to the database to see the results, in the shell log in the results ( no successful execution, it may be that the sh file needs to be placed in a feature directory bar, and so on after the review run )


Use Redis for traffic-cutting cases



Ideas:

1, Seconds Kill program request write to Redis

2, check the length of the redis stored data, above the upper limit directly discarded (return to kill the second end)

3, the death cycle processing data deposited into Redis and storage

Design of the table:


Seconds to kill code:


Inbound Code:


Direct Browser access to user.php

use php-f savetodb.php in the shell

Go to the database to view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.