TYPO free


fighting for TYPO free code

Storing PHP sessions in MemCached

10 Jul 2010

Memcached is a Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

It has been explained in many blog entries already but it does not hurt to report once again that PHP sessions can be stored in this key-value store in memory. This is a lot quicker than using the default session storage mechanism of PHP: files.

Installing MemCached

Installation instructions for MemCached differ per platform. On Debian:

apt-get install memcached
apt-get install php5-memcache

On MacOS:

port install memcached
port install php5-memcache

Start memcached. You can check if it is running by telnetting into it:

$ telnet localhost 11211 
Connected to localhost.
Escape character is '^]'.
get foo
VALUE foo 0 2
STAT pid 8861

Change your php.ini to use memcache for session storage:

; Handler used to store/retrieve data.
session.save_handler = memcache

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
session.save_path = "tcp://"

Restart your apache or cgid and you're done.

Now that you have memcached up and running it's time to learn how to move some of your TYPO3 cache tables to memcached.

Steffen 10 Jul 2010, 20:19
Nice idea. But what about performance?

Is it faster/slower on a single server than the default handler?
How does it scale in comparison to default session handler?
Michiel 10 Jul 2010, 23:44
Writing sesion information to memory is faster than writing it to disk. It does not scale very well. You can define more servers and setup a configuration that will failover, but that's not scaling. If you want scaling, then create a core patch that does it's own session handling using a memcached server pool, just like the cacheing framework can use a pool of memcached servers.
Commenting is closed for this item