Explain in detail how PHP server can maximize concurrency in limited resources

Time:2022-8-3
catalogue
  • summary
  • The server
  • database
    • Sign in
    • Submit Form
  • summary

    summary

    Suppose that the application app is purchased from the supplier with 5W RMB, and a large number of candidates pour in on the day of registration, and the concurrency number soars to 30w+, resulting in system downtime, denial of service, and the candidates cannot register, then can 5W RMB support 30w+ concurrency?

    However, for us, we might as well raise the issue from an angle: “how to maximize the server concurrency in limited resources”. Suppose you are a technical leader, how will you design and architecture a project with a large amount of concurrency?

    First of all, we can outline the general idea of this project. From the above description, it is not difficult to see that the bottleneck of this project lies in “concurrent writing” rather than “reading”. Therefore, in terms of resource allocation, we can tilt to “writing”. Here I write all the data in redis. In addition, we also need to try our best to migrate MySQL read operations to redis. The work done by MySQL is more inclined to some conventional non concurrent read and write operations.

    The server

    When the user requests it, the load balancer loads it on each server

    This is a pressure measurement data from symfony, using 1 CPU, 4 GB and PHP 7 configuration.

    The data in the figure above comes from the official website of swoole. After adding the execution of our actual business logic, we can find that when we use the permanent memory startup mode, three servers with lower configuration can solve the above problem that requires 16 servers.

    database

    In fact, many people will understand that the bottleneck of many Internet projects now is more focused on database i/o, and there is no particularly large gap between languages. Including the widely criticized startup method of php-fpm, you can also use spoole and other methods to replace it. Therefore, in this project, we will focus more on the database, and we can try to use redis to solve it. Of course, in the specific code, we also need to prepare a certain number of data connection pools in advance. In addition, it is also considered that although the write speed of mongodb under the same configuration is much faster than that of MySQL, it still has obvious shortcomings compared with redis.

    Sign in

    Registration and login should be divided into two parts, which correspond to “write” and “read” respectively. In the case of high concurrency, using MySQL directly, as you expect, will explode. Therefore, in the process of building the whole project, we can cache user data into redis. The problem of “writing”: when the number of users is not clear and the concurrency is large, I prefer not to directly store user data. We can design a switch or threshold to set the user’s warehousing method. When the concurrency is large, we can asynchronously let the user log in through MQ, but usually it can log in normally.

    Submit Form

    Because this project is not a common second kill and requires immediate notification, it greatly reduces the difficulty of our project design. The function of submitting forms is also similar to that of registration. We can make data asynchronously stored and then review in the background.

    summary

    Other things like whether CDN and MySQL need master-slave will not be repeated, depending on the actual situation. Theoretically, if we use php-fpm, it will cost about 19000 yuan / month to solve the problem of the project, while when we use spool, it will cost about 4500 yuan / month. Here we don’t advocate spool. What we want to explain is that when we face large concurrent projects, especially when the business logic is relatively complex, we can solve the problem better by using resident memory, which has nothing to do with language. Finally, it should be noted that the above is only the theoretical stage, and the actual data need to be further tested. The material of this article comes from the Internet. If there is something wrong, please point it out.

    The above is a detailed explanation of how PHP server can maximize its concurrency in limited resources. For more information about how PHP server can maximize its concurrency in limited resources, please pay attention to other related articles of developeppaer!