PHP allows multiple users to access PHP at the same time in an instant. How to implement a process that is similar to an exclusive process?

Source: Internet
Author: User
Tags flock
Scenario: 1. multiple users access a page (including the program) at the same time, or one page simultaneously requests the program multiple times. the time between two requests is so short that it is too late to use flock or use the database to determine whether the program is being executed. 3. only run in a common virtual host ,... scenario:
1. Multiple users access a page (including the program) at the same time, or one page simultaneously requests the program multiple times
2. the time between two requests is so short that it is too late to use flock or use the database to determine whether the program is being executed.
3. Only run in a common virtual host. exec and other functions must be disabled. redis and other functions are not provided.

Problem:
How can we ensure that this program is only executed once in such a short interval?

Reply content:

Scenario:
1. Multiple users access a page (including the program) at the same time, or one page simultaneously requests the program multiple times
2. the time between two requests is so short that it is too late to use flock or use the database to determine whether the program is being executed.
3. Only run in a common virtual host. exec and other functions must be disabled. redis and other functions are not provided.

Problem:
How can we ensure that this program is only executed once in such a short interval?

I think it can be implemented in two ways.

The first approach is to minimize the time needed for read/write locks. This can be done using nosql and memcache. But you need to figure out the exact time unit.

The second type is to convert the program into a resident background program, such as a command line program, and use socket to exchange data with conventional business logic. We recommend that you use swoole extension.


  

The lock mechanism is used to solve the conflict between optimistic locks and pessimistic locks.

After access, users can use redis or memcache to write relevant data (this step does not perform operations, but only records and users need to perform operations), and then relies on the queue function, the server Asynchronously processes the data just written (once the execution is successful, all subsequent queues fail to be executed)

I personally think that the process reusability of multiple requests should not be considered. The reason is that the data of dynamic websites may be different, is it because the efficiency of loading a lot of codes is low or your business data efficiency is low, it is generally because the business data is slow, rather than the code parsing is slow. If cache is used for business data, you can use opcache for php code.

For fpm, each request is processed by a process. no matter how short your interval is and how many people there are at the same time, the rule uses a process to process a request at a time. After all, it is stateless. if you want to be in a state and make sure that the program that generates the page is executed once, you can use the cache. You can use the cache lock to compete to generate the cache, for example, add of memcached, make sure that the generated cache is executed only once. If no memcached is available, you can use apc or apcu in a single-host environment. if memcached does not exist, nginx also has a module for page generation to ensure that the cache is generated.

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.