Boost the performance of your web applications with Bitnami Memcached with Multiple Instances

Raquel Campuzano Godoy

Raquel Campuzano Godoy


Passionated about IT, fencing, and books. Technical Writer @bitnami.


Share this article

Do you need to boost the performance of your web applications? In most cases, programs access the same data in applications over and over, so a caching system can be the best option to reduce the time needed. Memcached is an open source distributed memory object caching system. It is designed to cache large amounts of data. It lightens database workloads and speeds up your applications while its simplicity also facilitates quick and easy deployment.

Bitnami has packaged a three-node (default) Memcached cluster which helps developers quickly launch a set of Memcached servers. That way, you can focus on coding instead of wasting time installing and configuring complex caching systems for your external applications.

architecture diagram

Search for Bitnami Memcached with Multiple Instances in the cloud platform of your choice, and give it a try! It is available at no additional cost in both the Google Cloud Platform and the Microsoft Azure Marketplace.

Reduce costs and optimize your application memory with Memcached

Using a cache to avoid slow databases is the optimal way to ensure the efficiency of your applications. For heavy application workloads that demand a lot of requests per minute -such as social networking, gaming, or media sharing- an in-memory key value store such as Memcached is a complete solution to manage excessive data loads.

However, if you have a single Memcached server running, it is almost mandatory to increase its memory to avoid slowing down the application server. This may be a solution for improving the velocity of the application, but it is not a good idea for reducing costs.

The most cost-effective approach has a simple solution: more instances with less memory instead of one instance with a lot of GB. For example, having in total a 6GB RAM cluster comprised of three nodes, with 2GB each, is cheaper than launching a single instance with 6GB.

In addition to this, running a Memcached cluster with multiple instances alleviates the load in the web server and improves its performance. While Memcached nodes are running on the same network as the application server, small chunks of arbitrary data (results of database calls, API calls, or page rendering) are stored in multiple instances across the network, relieving disk space in the server where the application is running.

Scaling out Memcached with Bitnami

Bitnami's three-node Memcached Multiple Instances solution helps launch a production ready cluster in just one click.

Its configuration is adapted to the size of the machine, and it applies 75% of RAM to cache.

Scaling out the Bitnami Multi-Tier solution for Memcached is as easy as adding new nodes to the cluster. It can be done at deployment or even when the solution is already running. Check the Bitnami Memcached Multiple Instances documentation to learn how to add new nodes to the cluster on the Google Cloud Platform or on the Microsoft Azure.

By default, each node is provided with 3.75 GB of RAM (Google Cloud Platform) or 3.5 GB of RAM (Microsoft Azure). The default disk space of each instance is 10 GB. These values are also customizable as it is shown below:

Add nodes in Google Cloud Platform

Add nodes in Microsoft Azure

Once you have added a new node, you can check to make sure it is running smoothly by executing the telnet and stats commands:

$ telnet NEW_NODE_IP_ADDRESS 11211

You should see an output similar to this:

Check node status

More nodes mean higher failure-resistant distributed cache for your application. If a server goes down, the data is re-cached in a different Memcached node so that the speed of your application is the same as before the failure.

Bitnami recommends installing a client such as php-memcached-sasl to test your installation's efficiency. You can find further information in the Bitnami documentation for the Google Cloud Platform or in the Bitnami documentation for Microsoft Azure.

Distributing PHP sessions on the Bitnami Memcached with Multiple Instances

Memcached is perfect to reduce the number of times that the database or API needs to be read while performing operations. It caches the data in the RAM of the different nodes in the cluster, reducing the load in the web server.

A good way to see how an application would use Bitnami Memcached with Multiple Instances is, for example, having a PHP application and distributing the sessions across the Memcached servers. These are the steps you’ll follow:

  • Configure the Memcached module
  • Configure PHP to store the sessions in Memcached nodes
NOTE: Remember that you need to perform these steps in each node of your cluster and that the parameters must be exact on all servers.

Step 1: Configure the Memcached module

In your php.ini file, configure Memcached to allow distributed PHP sessions by setting the following parameters:

extension=memcache.so
memcache.maxreclevel=0
memcache.maxfiles=0
memcache.archivememlim=0
memcache.maxfilesize=0
memcache.maxratio=0
memcache.hash_strategy = consistent
memcache.allow_failover = 1
memcache.session_redundancy = 4

The main parameters to configure are:

  • extension=memcache.so: Indicates that the Memcached module is enabled.
  • memcache.allow_failover: Whether to transparently failover to other servers on errors (1 by default).
  • memcache.session_redundancy: Set how many copies of a particular session object Memcached would store for failover purposes. Important: this value needs to be set as n+1 Memcached instances. Taking the Bitnami Memcached default configuration as a reference, this number should be 4: 3 Memcached nodes+1.

Step 2: Configure PHP to store the sessions in Memcached nodes

Once the Memcached configuration is completed, it is time to indicate to PHP that Memcached servers are in charge of storing its sessions. In the php.ini file, replace the default values with the following:

session.save_handler = memcache
session.save_path = "tcp://MEMCACHED_NODE1_IP_ADDRESS:11211, tcp://MEMCACHED_NODE2_IP_ADDRESS:11211, MEMCACHED_NODE3_IP_ADDRESS:11211"

Remember to replace the MEMCACHED_NODE_IP_ADDRESS placeholder with the corresponding private IP addresses of each node of your cluster. That’s all! With this configuration, the sessions of your PHP application are stored in the Memcached servers instead of in your web server.

Start enjoying faster and optimized applications from now on!