Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Highly Available PHP sessions using memcached 4 Coherence

DZone's Guide to

Highly Available PHP sessions using memcached 4 Coherence

· Cloud Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

In this blog we will show you step-by-step how to use PHP memcached support, combined with our new library to store PHP sessions directly in Oracle Coherence using the memcached protocol.

We  (C2B2) announced in our blog a new github project memcached-4-coherence that provides a memcached protocol interface to the Oracle Coherence In-memory Data Grid. One advantage of putting a memcached interface onto Oracle Coherence is that many different web technologies and languages support the memcached protocol for storing session state and we can use that fact to store sessions directly into Oracle Coherence.

Installing memcached support on Ubuntu

We will assume you have php5 and apache installed on Ubuntu.

The simplest way to add memcached support to PHP is to run;

sudo apt-get install php5-memcached

This will install and configure the php memcached extension and place a configuration file at 

/etc/php5/conf.d/20-memcached.ini

  Configuring PHP to use memcached to store session state

Edit the file /etc/php5/conf.d/20-memcached.ini and add the following lines

session.save_handler = memcached

session.save_path = "localhost:11211"

memcached.sess_binary = 1

this configures PHP to use a memcached server on the local machine listening on port 11211. This also configures PHP to use the binary protocol as our memcached library for Coherence only supports the binary protocol.

Restart your apache server

sudo service apache2 restart

To get true resilience you can add a comma separated list of Coherence 4 memcached nodes in the session.save_path. However unlike with normal memcached you don't need to set any hash or replica settings as this is handled by Coherence.

 Start a Coherence Memcached Server

We'll assume you've followed the build instructions from our github project  memcached-4-coherence and you have a jar file coherence-memcached-server-1.0.0-SNAPSHOT.jar in the target directory. Also copy coherence-3.7.1.jar, netty-3.6.6.Final.jar, gson-2.2.4.jar into the target directory.

Then run

java -jar coherence-memcached-server-1.0.0-snapshot.jar 11211 test

You may get a bind error. If so ensure you don't already have a memcached server running on port 11211 by running;

sudo service memcached stop

For true session resilience you need to start a second coherence node on a second physical machine and modify the  session.save_path as described above.

Create a Test PHP File

Now PHP should be able to create sessions that are stored into your Oracle Coherence cluster for high availability. So let's test this by creating a simple PHP file to test session storage sessions.php.

<?php

session_start();

if (!isset($_SESSION['count'])) {

  $_SESSION['count'] = 0;

} else {

  $_SESSION['count']++;

}

echo "Session Value is ";

echo $_SESSION['count'];

?>

If you run this a few times @ http://127.0.0.1/sessions.php you will see the output below.

Session Value is 0

if you refresh the page a few times you should see the number increasing as expected.

The great thing is that your PHP session data is now stored in a highly resilient HA data grid.

Using Containers? Read our Kubernetes Comparison eBook to learn the positives and negatives of Kubernetes, Mesos, Docker Swarm and EC2 Container Services.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}