In this article I will compare the four different built-in methods for Magento session management. These are: file-storage (the default), database, memcached and tmpfs-backed file-storage.
Admittedly, the tmpfs-backed file-storage method is only tenuously ‘built-in’, to use it you have to mount an in-memory filesystem but I wanted to benchmark it here anyway, and cover some of the benefits and limitations it offers.
So we’ll look firstly at each of the four methods being compared – particularly what they are good for (and not good for), and then I’ll get straight in to some benchmarks.
Not at all coincidentally, I’ll be using my new Magento Speed Testing tool to benchmark each session storage option. <plug> I just launched the premium subscription last week, you can now performance test your own Magento store from multiple data-centers globally for only $5/month. Give it a try on your own store, the results may surprise you. </plug>
30 Second Introduction to Sessions (and Session Storage)
When you go back to a Magento store a few days after browsing it, and the items you added to cart are still in the cart – that’s sessions!
Sessions are user-specific data that is stored on the server for each client. The server maps clients to their sessions with a cookie (or in dire, cross-domain cases, by a SID in the url).
How quickly data can be put in and got out of the session storage, how volatile it is, and how it is distributed in a clustered environment are all important considerations when choosing how to store sessions.
Thankfully Magento provides multiple options out-of-the-box. Let’s take a look at those…
Filesystem Session storage
Remember that innocuous setting during Magento install that asks you if you want to store your sessions on disk or in the database? Well if you are like me, you normally choose filesystem, not knowing any better and having never bothered to test or research the consequences (until now)
The benefits of file-based sessions for Magento are simplicity, stability and durability. It’s simple because you don’t have to do anything to set it up. It’s stable because most modern filesystems use journaling to make sure that in the event of a system crash your session data is consistent. It’s also durable, because a system restart will not cause you to lose your session data. Coming back to a webstore a week later and not having your items in your cart is annoying, right?
To enable file-based storage, choose it during installation or in your
app/etc/local.xml file simply make sure you have a
session_save tag like this:
Note: Look in
app/etc/local.xml.additional for examples but ignore the comment about empty defaulting to files, you need the word
files in there.
I have performance benchmarked filesystem storage in the Benchmark section below.
Database Session storage
Database session storage is the other storage type, the one you probably didn’t choose during install, and forgot to circle back round and find out if you should have. With database session storage the session data is stored in the Magento MySQL database. The connection used is the same as the core connection.
One of the key benefits of using database sessions is the clustered environment support. In a filesystem based session storage scheme, if you have more than one Magento frontend node in a cluster, they will need to share session data (unless you use a loadbalancer with sticky sessions) and the database gives you that capability quite easily.
Databases also have good stability and durability during crashes and reboots.
One of the problems with database session storage is that it adds more load to the database. On large catalog sites with many SKUs, or busy sites with lots of sessions, this can hurt the database performance.
To use the database for session storage simply have this in your local.xml:
Check out the benchmarks below to see how well database session storage performs.
Memcached Session storage
Memcached session storage takes a bit more setup than either of the previous two options, which is probably why it’s not considered a ‘normal’ option during Magento install. For starters you need a Memcached server running.
Once you have it up and running, the memcached session storage offers a number of benefits. Firstly it is very cluster friendly. The session data can be shared by any number of webnodes, and to make things even better you can easily add more memcached server nodes so that even your session storage can be scaled to handle many 1000’s of concurrent sessions*. Secondly, it is (or can be) separate of the database and web node entirely, which offloads the work of storing sessions from busy nodes in a high-traffic environment.
The memcached server is not tolerant to failures, if your system crashes or dies the data in memory will be lost. Being a separate server does allow the session storage to be kept running during a web or database node restart though – something you cannot do with the tmpfs storage option below.
In terms of speed, the memcached keeps all it’s data in memory (think a giant hashtable), so it should be fast. However, there is the overhead of an extra tcp connection, so we’ll see in the benchmark section if that has an effect or not.
Quick tip: sharing one memcached server with multiple stores is tricky, you need to use different ports or unix sockets. So if you need multiple stores and are not comfortable with that sort of socket setup, assume you may need to run a separate memcached instance for each store.
To use a memcached session store in Magento you’ll need to have this in your app/etc/local.xml:
Note: Adjust the
server:port in the url above to match your environment. I’m assuming you have memcached running.
(*then all you need is to own a webstore that has 1000’s of concurrent shoppers…)
tmpfs-backed Filesystem Session storage
This is not really a good idea for storing sessions, it’s highly volatile and unstable two things you probably don’t want for ecommerce store sessions. It’s also not scalable either as it relies on the machine’s physical memory, and you can only stick so much of that in – you might need it for the web server too. It will also not help at all in a clustered environment as it is machine specific.
Despite all it’s limitations I really wanted to test tmpfs sessions more out of curiosity than anything, I have a hunch that it will make quite a performance boost. Let’s see how it does in the benchmark section below.
If you’re keen to try this, I’ll assume you know what you’re doing on the system side of things. Mount
var/sessions) as a
tmpfs and set Magento to use file based session storage like in the first option above.
Here are the results of each sessions storage for varying numbers of concurrent users.
Session Storage Transaction throughput (Transactions/s) for a varying number of concurrent users
Session Storage Average Response Time (s) for a varying number of concurrent users
I’ve looked at the benefits and limitations of each Magento session storage option, and tested the performance of each with varying numbers of concurrent users.
I was a quite surprised at how little each option affected the store’s performance. I guess it shows you that session storage is not so much about performance as it is about stability, and suitability for your requirements (cluster vs non-cluster, large catalog database etc).
I am working on a follow up article that simulates the effect of a week’s worth of sessions (in a long-lived session setup) because I have a hunch that the filesystem begins to slow down as the number of sessions being stored gets higher. It will be interesting to see if that is true and if it is also true for the DB or memcached options.
I hope this article will help you to make an informed decision which option to choose during install, and understand what can be achieved by tweaking the settings.
On an unrelated note; my latest round of changes including the ‘Run this test again’ button on Magento Speed Test make repeatably testing small performance tweaks like this on a store really easy. Try it out on your own site for free!
Lastly, it’s been a while since I posted an article here at Magebase, I hope this article is on the mark for our many new readers, any feedback most welcome.
Originally published on magebase.com. Copyright © 2011 Magebase - All Rights Reserved.