:::: MENU ::::
Monthly Archives: January 2015

How PHP Sessions can cause Concurrency Issues?

A web application without sessions is hard to imagine. People use it very liberally to maintain session data, so do I. But what most people don’t know that it can cause issues with your application’s concurrency if not used properly. Even though it is an obvious thing, I never knew (or thought!) about it till today.

What’s the problem?

PHP Session Lock can block your applications concurrent requests from a single client. If a client is sending multiple requests concurrently and each requests involves session usage, each request will be served sequentially instead of processing them concurrently.

Why does this happen?

PHP, by default, use files to store session data. For each new session, PHP will create a file and keep writing session data to it. (hint: Blocking IO). So everytime when you are calling session_start() function, it will open your session file and it will acquire an exclusive lock on the file. So if one of your scripts is taking time to process request and your client sends another request which also requires session, it will be blocked until previous request is completed. Second request will also call session_start() but it will have to wait because first request has already acquired an exclusive lock on the session file. Once previous request is fulfilled, PHP will close session file at the end of the script execution and release the lock. Now second process will get a chance to acquire a lock on session file and proceed it’s execution.

However, this can lead to concurrency issues for same client only. Request from a client cannot block another client’s request in such case because they both will be having different sessions and hence different session files.

When can it become a bottleneck?

It will be hard to noticed this blocking period if your scripts are short (in term of execution!). But if you have slightly long running scripts, you are in trouble. This can become a bottleneck if you are working with AJAX and fetching data from several requests on the same page; which is quite common is today’s web applications.

Consider this scenario when you are fetching several data from different background AJAX requests and displaying them on UI. These requests use session. Each asynchronous request is fired immediately and together. But first requests to reach server will receive the session lock while other requests have to wait. So all these requests will be processed sequentially even though they are not dependent on each other.  Taking an example, 5 requests, each taking approximately 500ms to complete, are being sent concurrently. But because of this blocking, each request is not executed concurrently and so the last, 5th, request will start executing at 5th second and will complete execution after 5.5 seconds even though it required only 500ms to process. This can be a serious problem if some of the scripts require more processing or the number of the requests are greater.

I wouldn’t have noticed this if I hadn’t added sleep(2); in my code on local machine to simulate natural use from a slow connection. My page was sending 5 requests and each request was being severed every 2 seconds, sequentially!

So what’s the solution?

Close sessions once you are done using it!

PHP has this method to close session write: session_write_close(). Calling this method will end current session and write session data to file and release the lock to the file. So it will not block further requests even if current script is still pending processing.

Important thing to note is that once you close session using session_write_close(), you will not be use session further in the current script which is being executed.


How do I simulate this problem?

If you want to see this problem in action, try following code:

Blocking Example


Now send 5 ajax requests to this file. Example code with jQuery:

Non-Blocking Example

Now send 5 ajax requests to this file. Example code with jQuery:

You will be able to see how this small thing can greatly affect your application.

4 Chart Plotting JS Libraries That You Should Check

Plain data is boring. But visualization of the same data is cool and interesting. If your web application ever deals with even a small set of data, you may be using charts already to display them. If you aren’t, you should start using them.

Drawing charts on a canvas isn’t  an easy job and will require a lot of your time. Let me list 4 of my favorite JS library to plot graph and charts and make your life easier.

1. Chart.JS


Chart.js is a very simple and easy to use library to plot charts. It’s pretty light weight and supports following 6 types of charts:

  1. Line
  2. Bar
  3. Radar
  4. Polar Area
  5. Pie
  6. Doughnut

Chart.JS creates beautiful responsive charts and has sufficient set for APIs and a neat documentation.

Link: Chart.JS

2. Morris.JS


Morris.js is very similar to Chart.JS and creates pretty charts. Main difference between them is that Chart.JS uses canvas element to plot charts while Morris.JS uses SVG. However Morris.JS supports slightly fewer types of charts:

  1. Line
  2. Area
  3. Bar
  4. Donut

Link: Morris.JS

3. Flot



Flot is a very extensive chart plotting library that draws interactive chats and has several options and features. If you require extensive and comprehensive graphs, use nothing but Flot.

If your web app contains real-time interactive graph, this library will fit your need.

Link: Flot

4. jQuery Sparklines


This jQuery based library is the best choice when it comes plotting inline charts. It plots very beautiful inline chart. You can use it to get small charts in a text line or paragraph.

It supports following types of charts:

  1. Line
  2. Bar
  3. Stacked
  4. Discrete
  5. Pie
  6. Tristate
  7. Box
  8. Bullets

Link: jQuery Sparklines

Hello World! Welcome to the http://lokalhost.in!

I doubt there is a single geek who has never thought of writing a blog, regularly. But how many does really blog? Very few, very very few! I am no exception. I have started few blogs in the past, blogged regularly for some time and started losing motivation. Well, I am giving another shot at the blogging on this New Year.

So,, welcome to the http://lokalhost/ .  World of programming is tricky, everyday a new problem will hit you and you’ll get busy solving. But the fact is, many times we, software developers/ programmers, spend hours solving some silly “by-problem”  because we have never come across it before or the resources available for it are either unclear or not so helpful. And you may not be the only person wasting your time on it; someone else might have or may be wasting their time on it too.

I, too, come across such programming problems often and I believe I should start writing about them once I have solved them. It may save someone else’s time too. I will be writing posts on these issue and solution which I used to solve such close-to-general problems. I will also be posting Tutorials and How-to blog posts to help my fellow programmers.

We programmers face such problems quite often and I do believe to keep getting motivations to post more and more. Don’t forget to keep an eye on the blog. Stay Tuned!

And yes, Happy New Year!!