How does php solve the global cumulative count problem under high concurrency?

Source: Internet
Author: User
There is such a requirement that a high-concurrency website wants to intercept requests from browsers, record certain information of each request, and write data into files (not to write data into databases, if the data is written to the database, it can be directly incremental by ID. In fact, it is not). For each request, there must be...

There is such a requirement that a high-concurrency website wants to intercept requests from browsers, record certain information of each request, and write data into files (not to write data into databases, if the data is written to the database, it can be directly incremental by ID. In fact, it is not). Each request must have a number. From 0000 ~ 9999 loops, because the website contains a large amount of traffic, there will be a high concurrency problem, how should I record this number?

Think about these solutions

  • Using global variables is dangerous and unstable.

  • When the database is used to count, the number can be accumulated. Of course, a transaction lock must be applied to the database. If the database is not added, a deadlock may occur. However, in the highly concurrent state, this will cause the pressure on the database to increase, resulting in response latency and even crash.

  • Using files to record numbers is similar to the above database record method, and there are the same problems

  • Redis is introduced for record. In redis cache technology, variable increments are atomic operations, which can accumulate numbers without deadlocks and are fast. It is the best way to record them, however, we have encountered some problems in using redis.

So here, I would like to ask you what better solutions do you have for the global digital accumulative records of highly concurrent websites?

Reply content:

There is such a requirement that a high-concurrency website wants to intercept requests from browsers, record certain information of each request, and write data into files (not to write data into databases, if the data is written to the database, it can be directly incremental by ID. In fact, it is not). Each request must have a number. From 0000 ~ 9999 loops, because the website contains a large amount of traffic, there will be a high concurrency problem, how should I record this number?

Think about these solutions

  • Using global variables is dangerous and unstable.

  • When the database is used to count, the number can be accumulated. Of course, a transaction lock must be applied to the database. If the database is not added, a deadlock may occur. However, in the highly concurrent state, this will cause the pressure on the database to increase, resulting in response latency and even crash.

  • Using files to record numbers is similar to the above database record method, and there are the same problems

  • Redis is introduced for record. In redis cache technology, variable increments are atomic operations, which can accumulate numbers without deadlocks and are fast. It is the best way to record them, however, we have encountered some problems in using redis.

So here, I would like to ask you what better solutions do you have for the global digital accumulative records of highly concurrent websites?

  1. Memory databases, such as redis and memcache, can also be the best and fastest choice. It is unclear what technical problems the landlord has encountered.

  2. There is no doubt that pure php cannot solve this problem. Introduce swoole extension. Using swoole's atomic class can achieve multi-process atomic lock-free cumulative count.

  3. Develop your own...

Each request must have a number.

From 0000 ~ 9999 is a loop required? If not, we recommend that you useuuid

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.