Categories
PHP Programming Tips and Tricks Tools and Technologies

Concurrency Issues With PHP Session

Concurrent connection means how many request sending/calling at the same time. It influences the application performances rapidly. When your application can handle as much as concurrent connections your application will become more stable, high scalable and faster as well. But concurrency might be a problem when you deal with PHP Session. It is the biggest challenges to avoiding concurrency issue in PHP, especially in PHP Session.

Most of the modern web browser sent concurrent connections to a specific domain to between 4 or 6. That means if your application has a lot of static assets (JS, CSS, Images, Fonts, etc), then these will be queued until browser get the response from the server for its first set of a request (4/6 concurrent request).

Concurrency Issues With PHP Session

PROBLEM:

By default, PHP uses files to storing the Session data. PHP either create a new Session or retrieve existing Session on each request when called session_start() function. So in this case, if your user sending multiple concurrent requests to the server which involves with Session data. PHP Session will lock and block your application’s concurrent request for that particular client. And every request will be served as sequentially instead of processing them concurrently.

BEHIND THE SENCE:

For the new user, PHP sent the unique identifier number to users computer and create a new file with the same unique identifier name for handle the session for that particular user. Otherwise, PHP retrieves existing session for that user. See this article to know how does session works.

Now assume, a user sent 6 concurrent requests to server and 4 of them involved with session data. So what happened that case? PHP will take the first request and open the session file and lock it. And other 3 requests will be queued until session file will be released. So that means your application will be blocked. After your first request’s done, it will release the session file and the second request will take it. It’s will happen for rest of the request.

Now think every request taken 400-500 ms to execute, that means your last (4th) request completed after 1600 ms cause it waited 1200 ms to complete first 3 requests. So in this way PHP Session blocked the application.

However, this concurrent issue happened for the same user only. Request from a user cannot block another user’s request.

You will be able to understand how this thing can affect your application performance. To improve application performance every developer needs to avoid this issue.