;

Memcached with the Repcached patch to achieve High Availability – Ubuntu 14

Try it in our public cloud & Get $5 Credit
CLAIM NOW

Memcached with the Repcached patch to achieve High Availability – Ubuntu 14

Memcached is an open source program under the BSD license that performs distributed memory caching. It is ideal for web applications using databases, as it will cache data into the server’s RAM to reduce how often the database must be accessed. This cache can be distributed amongst multiple servers.

But if a server fails, then all data associated with its cache is lost. That’s where Repcached comes in. It’s a free patch for Memcached that uses redundancy to allow normal operation even in the event of node failure.

Getting Started

To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) running Ubuntu 14.
• All commands should be run as the root user

For the purposes of this guide, both nodes will have IP addresses that are on the same local network. The first server has 10.0.0.77 and the second server has 10.0.0.78. Keep in mind that repcached is not a broadly used patch. Ensure that you have thoroughly tested it in your environment before adding it into production.

Step-by-step guide

Start off with two identical servers running Ubuntu 14. Make sure both are fully up to date before beginning.

apt-get update && apt-get -y upgrade

Get the patched memcached package from our repositories and install it onto both servers.

cd /root
wget https://launchpad.net/~niedbalski/+archive/ubuntu/memcached-repcached/+files/memcached_1.4.14-0ubuntu10repcache3_amd64.deb
dpkg -i  memcached_1.4.14-0ubuntu10repcache3_amd64.deb

After Memcached has been installed, it may start automatically. On both servers, look for and kill any active memcached instances.

killall -9 memcached

Prepare the configuration file for the server at 10.0.0.77 first.

cp /etc/memcached.conf /etc/memcached_server1.conf

Don’t alter memcached.conf, but instead modify the new file:

nano /etc/memcached_server1.conf

The new file’s contents are not needed, so delete the entirety. Add the following into the file, making sure to set IP addresses appropriately for your setup (-l is for the local node IP and -x is for the remote node IP):

# Run as daemon
-d
# Logfile location
logfile /var/log/memcached.log
# Memory Amount
-m 64
# Connection Port TCP
-p 11211
# User under which the daemon will run
-u memcache
# Bind on IP
-l 10.0.0.77
# Connections Limit
-c 4096
# Return error when memory is exhausted (rather than removing items)
-M
# Slave Memcached Server
-x 10.0.0.78

Start Memcached on the server at 10.0.0.77.

service memcached start

Next, prepare the configuration file for the other server at 10.0.0.78.

cp /etc/memcached.conf /etc/memcached_server2.conf

Again, leave the original memcached.conf intact and just modify the new file:

nano /etc/memcached_server2.conf

Delete the new file’s contents and add in the following, taking care to set the correct IP addresses:

# Run as daemon
-d
# Logfile location
logfile /var/log/memcached.log
# Memory Amount
-m 64
# Connection Port TCP
-p 11211
# User under which the daemon will run
-u memcache
# Bind on IP
-l 10.0.0.78
# Connections Limit
-c 4096
# Return error when memory is exhausted (rather than removing items)
-M
# Slave Memcached Server
-x 10.0.0.77

Start Memcached on the server at 10.0.0.78.

service memcached start

Now it’s time to test replication using the two nodes.

First, telnet to the Memcached server on the first node at 10.0.0.77 and add some keys to Memcached.

telnet 10.0.0.77 11211
set besthost 0 0 10
globo.tech
get besthost
VALUE besthost 0 10
globo.tech
quit

On the second node at 10.0.0.78, verify that same key.

telnet 10.0.0.78 11211
get besthost
VALUE besthost 0 10
globo.tech
quit

If the keys for besthost return the same value, your two replicated Memcached instances are now fully functional and replicate data between them. Taking either server (but not both) offline will have no affect on the availability of data from the cache.

Conclusion

Memcached with the Repcached patch is a powerful tool in use by sites such as Reddit, Twitter, Facebook, and many more. Consider adding it to your tool suite if you have a dynamic database-driven web application to improve data reliability and reduce errors resulting from node failure. If this guide was helpful to you, kindly share it with others who may also be interested.