Your app is booming, you need more web servers and you need to serve users and keep their user experience. When you had a single server you just used session for that, but now, How do you keep sessions across multiple web servers?
Using a session stickiness load balancer is a good solution, but what happens when a web server needs a restart?
Session Off Loading
Many of you are familiar with the concept of session off loading. The common configuration for it is using Memcached to store the session object, so it is accessible from all web servers. However, this solution lacks the features of high availability by replication and persistence.
The Next Step: High Availablability
Offloading web servers sessions to MongoDB looks like a great solution: key-value store, lazy persistency and built in replication and auto master failover.
Step by Step Solution for LAMP Session Off Loading using MongoDB
- Install MongoDB (preferably in a replica set with at least 3 nodes)
- Install MongoDB driver for PHP:
- Configure the php.ini: extension=mongo.so
- Download and integrate Apache session handler implementation using MongoDB.
- Configure your MongoDB setting in the module:
- Set the list of nodes in the connectionString setting: 'connectionString' => 'mongodb://SERVER1:27017, mongodb://SERVER2:27017',
- Do the same to the list of servers (see at the bottom of this post)
- Configure the cookie_domain (or just place a null string there to support all): 'cookie_domain' => ''
- Don't forget to enable replica set if you are using one: 'replicaSet' => true,
- Add the MongoSession.php to your server and require it: require_once('MongoSession.php');
- Replace the session_start() function with $session = new MongoSession();
And that's all. Now you can continue using the session objects as you used to create new great features:
if (!isset($_SESSION['views'])) $_SESSION['views'] = 0;
$_SESSION['views'] = $_SESSION['views'] + 1;
echo $_SESSION['views'];
Bottom Line
Smart work can tackle every scale issue you are facing off...
Keep Performing,
Appendix:
'servers' => array(array(
'host' => 'SERVER1',
'port' => Mongo::DEFAULT_PORT,
'username' => null,
'password' => null,
'persistent' => false
),
array(
'host' => 'SERVER2',
'port' => Mongo::DEFAULT_PORT,
'username' => null,
'password' => null,
'persistent' => false
)