PHP Session using Sharedance in Apache Web Cluster

Our new online shopping cart site is run on 3 Apache servers which mount the same document root in all nodes. With a load balancer in front of it to distribute the HTTP/HTTPS connections equally using weigh round-robin algorithm, we are facing big problem in session handling for the site; When user’s session is not exist in the current server, they need to authenticate once more if load balancer redirect the user to another server. We are considering following solutions:

  • Mount the same partition of session.save_path directory in all nodes using GFS2:
    • Serious IO issue due to high write in single directory
    • Increase server load especially on the GFS2 locking (dlm) process
  • Use memcached server:
    • Need to have simple modification on the PHP code
    • The sessions are saved in memory instead of disk. It is fast but session ID will be gone if server rebooted or service restarted.
  • Set session to be stored in MySQL database:
    • Need to modify the PHP code especially on how it handle locking. If session stored in file format, the file system will automatically handling the locking part.
    • Increase the database server workload due to high read/write and it can generate to millions row within months.

At the end of the day, we are planning to use Sharedance because it is super simple. You just need to install the server, do some changes in php.ini, restart Apache and done! You are then set to have a session server which will be lookup by all web cluster nodes.

OS: CentOS 6.3 64bit
Server IP:
Session directory: /var/sharedance
Webserver #1:
Webserver #2:
Webserver #3:

Session Server (Sharedance)

1. We will use RPMforge to make our life easier:

$ rpm --import
$ rpm -Uhv

2. Install Sharedance using yum:

$ yum install sharedance -y

3. I want Sharedance to listen to the main IP and cache expiration should be 6 hours. Open /etc/sysconfig/sharedance via text editor and make sure you have following line:

SHAREDANCE_OPTIONS="--ip= --expiration=21600"

You can use following command to check the complete list of options available:

$ sharedanced --help

4. Make sure it is auto start on boot and start the Sharedance service:

$ chkconfig sharedance on
$ service sharedance start

5. You can check in the process list this 2 processes should exist and listening to port 1042:

$ ps aux | grep sharedanced
496    2621    0.0   0.0   111520  568  ?  Ss   11:44   0:00   sharedanced [SERVER] 
496    2622    0.0   0.0   111520  352  ?  SN   11:44   0:00   sharedanced [CLEANUP]
$ netstat -tulpn | grep sharedanced
tcp   0   0*       LISTEN       2621/sharedanced [SERVER]

To allow firewall rules in iptables for Sharedance port:

$ iptables -I INPUT -p tcp --dport 1042 -j ACCEPT

6. We need to copy the PHP session handler and sharedance PHP file provided by Sharedance which is located under /usr/share/doc/sharedance-0.6/php/ direcotry to all web cluster nodes so they can prepend in the php.ini. I will copy them to /etc/php.d directory in all nodes:

$ cd /usr/share/doc/sharedance-0.6/php/
$ scp session_handler.php sharedance.php
$ scp session_handler.php sharedance.php
$ scp session_handler.php sharedance.php

Web Servers

1. Change the php.ini value of your server as below:

auto_prepend_file = /etc/php.d/session_handler.php
session.save_handler = user

2. Edit /etc/php.d/session_handler.php using text editor and make sure the first line contain the IP address of Sharedance server:


3. Restart Apache web server to apply the changes:

$ service httpd restart


I download this file: and execute it from the web server to get following result:

We should see the same session ID exist in Sharedance session directory at /var/lib/sharedance:

$ ls -al | grep tqsncjk23k78cm747n4b1eq5l4
-rw------- 1 sharedance sharedance 24 Aug 1 12:17 tqsncjk23k78cm747n4b1eq5l4