PHP session may cause concurrency problems

Source: Internet
Author: User
Tags php session

PHP session may cause concurrency problems

In Web application development, 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.

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, it is very common to use AJAX technology to send multiple requests for data within the same page. 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.

void Session_write_close (void)
End the current session and store session data.
Session data is usually stored after your script terminated without the need to call Session_write_close (), but as session Data is locked to prevent concurrent writes only one script could operate on a session at any time. When using framesets together with sessions you'll experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables AR E done.

Case:

The corresponding session file after Session_Start () is locked until the end of the current script is unlocked.
Session_Start () will not start until a process has access to the same session ID as the file is unlocked during the lock.
The workaround is to call Session_write_close () to write the data to the file and end the session after the session has set the data.

For example, the following code

PHP Code
<?php
Session_name ("Stest");
session_id ("TestID");
Session_Start ();
$_session["Count"]=0;
Session_write_close ();
Sleep (10);
echo "Success";
?>
If you do not join Session_write_close ();
Concurrently 3 processes access the page, the first process executes 10 seconds, the second executes 20 seconds, and the third executes 30 seconds.
After joining Session_write_close ()
Concurrent 3 processes will be completed in 10 seconds at a time

PHP session may cause concurrency problems

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.