PHP Sessions in Memcached

PHP Sessions Clustering with Memcached

PHP session clustering improves application availability by storing session data in Memcached. In a multi-server PHP environment, another application server can recover the user session from Memcached if the server that originally handled the session becomes unavailable.
Application Tier Use two or more PHP application servers, such as multiple Apache instances.
Session Storage Memcached acts as the distributed session backup used during application-server failover.

Create the PHP Cluster Environment

Build an environment that contains an NGINX load balancer, two or more PHP application servers, and a Memcached node. The source example uses two Apache servers.

1Open the environment topology wizard

Sign in to the platform and click Create environment.

2Add multiple PHP application servers

Select two or more application-server instances. The source demonstrates the setup with Apache.

3Add Memcached and create the environment

Enable a Memcached node, enter the environment name, and click Create.

PHP environment with Memcached
Create a multi-server PHP environment with Memcached enabled.

Memcached provides a shared location for the session information generated by the application servers. This supports failover when traffic is moved from one PHP server to another.

NGINX Apache Memcached PHP cluster architecture
NGINX distributes requests between PHP servers while Memcached provides shared session backup.

How Session Failover Works

When a user request completes, the application stores the updated session in Memcached while the originating application server can continue serving the same session normally.

Normal Operation The selected PHP server handles the request and updates the corresponding session data in Memcached after the request is completed.
Server Failure If that server becomes unavailable, NGINX sends the next request to another PHP server, which retrieves the required session from Memcached.

The replacement server identifies the required session using the session identifier, retrieves it from Memcached, serves the request, and updates the shared session again. This allows the user’s application session to continue despite the failure of the original server.

The source combines this Memcached session storage with NGINX traffic distribution across the application-server cluster.

Configure PHP Sessions for Memcached

1Open Apache configuration

Locate the environment in the dashboard and click Config for the Apache application server.

2Open php.ini

In the configuration file manager, navigate to etc > php.ini.

3Enable the Memcached PHP extension

Add the following line to the Dynamic Extensions section:

extension=memcached.so
Enable memcached PHP extension
Enable the memcached.so extension in php.ini.

4Configure the PHP session handler

In the [Session] section of php.ini, set Memcached as the session handler and point PHP to the Memcached server on port 11211.

session.save_handler = memcached
session.save_path = "<server>:11211"

Replace <server> with the Memcached node IP address or URL. The source instructs users to obtain this value through the Memcached node’s Info action in the environment dashboard.

PHP session save path for Memcached
Set the PHP session handler to Memcached and specify the Memcached server on port 11211.
Configuration scope: In a multi-node PHP layer, ensure the required PHP configuration is applied consistently to every application-server instance that may handle user requests.

Save and Restart Apache

Save the php.ini changes and restart the Apache node so the Memcached extension and session settings are loaded.

Result: if one PHP application-server instance becomes unavailable, users can be routed to another instance and their session can be recovered from Memcached, helping avoid visible interruption to the application.
For production use, test actual session persistence and failover behavior with your application before relying on the cluster configuration for high availability.

What’s next?