The PHP implementation of common locking and locking under concurrent
http://www.cnblogs.com/scotoma/archive/2010/09/26/1836312.html
Apache + PHP concurrent access
http://www.cnblogs.com/WestContinent/archive/2013/03/25/2981667.html
1 2 3 |
|
Lock and unlock PHP implementation
http://blog.csdn.net/topasstem8/article/details/6735240
PHP does not have complete thread support, and even deployment to a httpd server based on the threading model will cause some problems. However, even with PHP under the multi-process model, it is inevitable that multiple processes will access the same resource. For example, the data cache shared by the entire program, or a specific process must be queued due to limited resources, and a unique identifier is generated for each user. The PHP language itself does not provide process mutual exclusion and locking mechanisms, which makes programming in these situations difficult. The currently understood alternative methods are as follows:
- Use MySQL's locking mechanism to achieve mutual exclusion. The disadvantage is that it increases the connection burden on the database server and makes the program dependent on the database service to work properly.
- Use file lock mechanism. That is, the flock function is used to realize the locking and mutual exclusion mechanism through the file to simulate the working mode of the locking primitives under the general programming model. This method became an essential element to protect data integrity in the era when plain text files were used as storage engines. Now it is quite common to use text files as cache media. PmWiki should also use this mechanism to remind multiple people editing a page at the same time. However, the file lock mechanism will more or less call the file lock feature on the host operating system, so you must check whether the server operating system provides a complete and reliable file lock mechanism for the PHP environment when using it.
- Use shared memory space to count. PHP can use the shmop_open function to open up a memory space and share data between service processes. In order to ensure mutually exclusive and secure access to shared data, you can use the group of functions sem_get, sem_acquire, and sem_release to implement a shared counting lock mechanism. This method is actually implemented by calling the system's ipc service in the background.
- I can add that you can also use redis to achieve locking
What should I do to write a website that supports high concurrency in PHP?
http://www.zhihu.com/question/20049768
- Webserver (Nginx): This layer can be easily distributed and deployed. Combined with intelligent DNS resolution, it can easily prevent single points of failure and achieve regional access acceleration. Combined with LVS, it is easy to achieve load balancing. This layer is mainly responsible for processing static requests and forwarding PHP requests to the PHP processing node of the second layer. As for the static resource address ( http://misc.xxxx.com ), it can be deployed separately or directly using commercial cloud storage. Service (Qiniu is good at home, Amazon S3 is available abroad)
- PHP processing node: A node is actually a system process that listens to a specific port. The webserver requests are distributed through a load balancer (I use AWS loadbalancer), which is well distributed and load balanced. I still use php-fpm that comes with php. In fact, the performance of hhvm from facebook is very powerful, but it still cannot pass the unit test of my project 100%. It can be replaced smoothly when hhvm matures.
- Cache: Memcached is used. The function of this layer is mainly to reduce database IO and speed up hot data access. The caching strategy and the program are highly coupled, so I won’t go into details, but simply speaking, there are two ways, one is in the program Add a cache processing at the global level. This method has low code coupling, but the effective hit rate is not high. Some projects may not adapt to it. The other is to add cache processing at the specific data access point. This method has a higher program coupling. High, but the cache hit rate is very high, almost no invalid cache exists, I use this.
- Database: My current project data scale is not large, I only use a single database for the time being, but the program logic is ready for linear expansion of the database. In fact, the expansion of the database layer is a cliché. The common method is to sub-database and sub-table. This piece needs to lay the foundation in the early code. In addition, the smoother method is to use middleware, such as 360's Atlas, Alibaba's cobar, and Taobao's. TDDL, middleware can be extended without extensive code changes, but the specific usage scenarios are still limited, and specific projects need to be investigated separately.
- Others: According to different projects, the architecture can also selectively use queues. The beanstalkd and Redis I use now are also a good choice. The commonly used environment for queues is mail sending and in-site message push, but in some scenarios it can also be used as a buffer for the core database, and it is also a good choice for large concurrent or sudden traffic.