Speed up your sessions, part 2 – Performance
If it’s not already done, I suggest you to read the first part of this serie of two posts to learn how to use properly PHP sessions.
Well, now that everyone knows what to do and not do with sessions, we can try to improve performance a little more.
The rule is to determine how sessions will be used in the application and then choose a better handler than the default one : files.
SQLite : fast read / slow write
SQLite for handling sessions is used to improve significantly read accesses. On the other hand, write processing will be slown and time-consuming.
In fact, instead of using one file per session like the default “files” handler, all of them will be stored in a single file, which is a database. So any write action will lock the file resulting in other access tries to be put in a queue.
Balance the pros and the cons, if the application need huge write accesses don’t use SQLite to manage sessions. But, if writes are limited SQLite is really appropriate.
Moreover, it’s quick and simple to setup.
Install SQLite on the server if it is not already done :
apt-get install php5-sqlite
Then just tell PHP, in the php.ini file, to use SQLiteas the session handler and the file path where to store them :
session.save_handler = sqlite session.save_path = "/path/sessions.db"
You don’t even need to worry if the file exists or not, it will be automatically created. The first session_start() query will initiate the database with a table which contains 3 columns “sess_id”, “value” and “updated”.
Remember to restart your web server to apply the modifications.
Memcached : fast but RAM consuming
For people who doesn’t know about Memcached, it’s a distributed memory object caching system. Just visit the official website to learn more : http://memcached.org/. In a few words, Memcached allows to store data in memory.
Disk I/O are one of the slowest elements in applications workflow. So, if frequently used data are put in RAM, a lot of disk reads and writes will be saved. This can be applied to your application common data and to users sessions as well.
The installation is very simple, in most case rely on the package manager of the host OS is sufficient.
For example, in a debian based system :
apt-get install memcached apt-get install php5-memcache
Then, if no specific configuration has to be done in the Memcached configuration file, just change the session save handler in the php.ini :
session.save_handler = memcache session.save_path = "tcp://127.0.0.1:11211"
Restart Apache (or nginx, lighttpd, etc.) and that’s it. Now the session processing will be really fast.
But, even if using Memcached for handling sessions has good pros, there are also cons.
In fact, session data are now saved into the server RAM. That means that the storage limit is equal to the available memory on the web server. And the more RAM is used to save session data the less is available for web applications execution. So be careful in what is stored in sessions, that’s where the best practices become handful. Moreover, data stored in RAM will not survive in a power outage or reboot.
To go further, a single dedicated Memcached server can be shared between several application servers. For example, if you want to do load balancing with multiple web servers, they can use the same Memcached server to cache data. Just change the IP address in the php.ini configuration file with the cache server one and you’re done.
Other leads
Instead of using Memcached, you can set up a RAMDisk to store sessions in RAM without changing the default files handler. But this solution requires some adminsys skills.
Another possibility is to use the shared memory module of PHP. I have never tried this method and didn’t manage to find a good tutorial to implement it. So if anyone has ever done this, please leave a comment
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.