PHP session may cause concurrency problems

Source: Internet
Author: User
Tags php session

In the development of Web applications, people often use the session to store data. However, some people may not know that in PHP, the use of the session may cause concurrency problems. Plus91 Technologies Senior Engineer Kishan Gor, a software solutions provider for the medical industry in India, explained the issue on a personal blog.

If the same client sends multiple requests concurrently, and each request uses a session, then the presence of a PHP session lock causes the server to respond serially to those requests, rather than parallel. This is because, by default, PHP uses files to store session data. For each new session,php, a file is created and the data is continuously written to it. Therefore, each time the Session_Start () method is called, the session file is opened and the file's exclusive lock is obtained. Thus, if the server script is processing a request and the client sends a request that also needs to use the session, then the latter request will block until the previous request processing completes releasing the exclusive lock on the file. However, this is limited to multiple requests from the same client, which means that requests from one client do not block requests from another client.

If the script is short, this usually does not matter. However, if the script runs longer, it can cause problems. In modern Web application development, there is a very common situation, is the use of Ajax technology in the same page to send multiple requests to obtain data. If these requests require a session, then the first request arrives at the server and the session lock is reached, and the other requests must wait, and all requests are processed serially, even if they do not have a dependency relationship. This will greatly increase the response time of the page.

One way to avoid this problem is to call the Session_write_close () method to close the session immediately after you have finished using the session. This will release the session lock, even if the current script is still waiting to be processed. It is important to note that the current script cannot further manipulate the session after the method is called.

In particular, the questions and ideas presented in this article apply only to the PHP default session management mode using the Session_Start () method. For example, there are users who point out that if the application is hosted on AWS EC2 and the Dynamodb,session lock issue is properly configured, it will not occur.

PHP session may cause concurrency problems

Related Article

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: 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.