DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR
  • Database Integration Tests With Spring Boot and Testcontainers
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • How Agile Works at Tesla [Video]

Trending

  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR
  • Database Integration Tests With Spring Boot and Testcontainers
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • How Agile Works at Tesla [Video]
  1. DZone
  2. Coding
  3. Languages
  4. Highly Available PHP sessions using memcached 4 Coherence

Highly Available PHP sessions using memcached 4 Coherence

Steve Millidge user avatar by
Steve Millidge
·
Jul. 22, 14 · Interview
Like (0)
Save
Tweet
Share
6.27K Views

Join the DZone community and get the full member experience.

Join For Free

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.

PHP Memcached Coherence (units of measurement) Session (web analytics)

Opinions expressed by DZone contributors are their own.

Trending

  • Seven Steps To Deploy Kedro Pipelines on Amazon EMR
  • Database Integration Tests With Spring Boot and Testcontainers
  • Alpha Testing Tutorial: A Comprehensive Guide With Best Practices
  • How Agile Works at Tesla [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: