Amazon ElastiCache – Redis

Nowadays, performance is one of the most important and challenging metric that you need to consider when developing a web service. The applications are evolving so the services we are going to build need to evolve too.
One method to improve the performance of our web service is to introduce a cache layer. Recently I have been experimenting Amazon ElastiCache.
Amazon ElastiCache is a web service that makes it easy to deploy, operate, and scale an in-memory data store or cache in the cloud. The service improves the performance of web applications by allowing you to retrieve information from fast, managed, in-memory data stores, instead of relying entirely on slower disk-based databases.
ElastiCache supports two different memory engines:

  • Redis
  • Memcached

It automatically detects and replaces failed nodes, reducing the overhead associated with self-managed infrastructures and provides a resilient system that mitigates the risk of overloaded databases, which slow website and application load times. You can find more information here.

In this post we will create a Redis Amazon ElastiCache cluster and will see how interact with it programmatically.
Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. You can find the full documentation here.

To create a new cache cluster, from the Amazon AWS services dashboard, select Amazon ElastiCache (Database section).
Click on the button Create Cluster and select Redis as cache engine.
You can now select the number of replica (eventually the number of shard) and the node type (up to cache.r3.8xlarge with 237GB or RAM).
Once the cluster has been created you can see it in the cluster list (note the number of node, in this example is just one).

By clicking on the cluster name, you can see the node details such as the status, the port and endpoint (it looks likes

Now that our ElastiCache Redis cluster is up and running we can start to interact with it to store our contents and resources.
Notice that, the service is designed to be accessed exclusively from within AWS. However, if the ElastiCache cluster is hosted inside a VPC, you can use a Network Address Translation (NAT) instance to provide outside access. You can read more about accessing ElastiCache resources from Outside AWS here.

I created an EC2 (Ubuntu Server 14.04) instance and placed it in the same VPC (Virtual Private Cloud) of the previously created ElastiCache cluster.

Redis client is available in several programming language (from C to Common Lisp), you can find the list here.
For this example I am going to use the redis-py client for Python.
You can install redis-py using pip.

To connect to the Redis cluster:

We can now use the set method to store a resource.

and the get method to read it (the get method returns None is the key has not been found).

Here you can find the list of all the Redis commands.

We can can connect to our cluster using the Redis command line interface too.
redis-cli is the Redis command line interface, a simple program that allows to send commands to Redis, and read the replies sent by the server, directly from the terminal.
From the redis-cli tool you have to specify the cluster host and port.

You can now run you Redis command directly from the command line.

The use of Amazon ElastiCache is easy and with a high level of abstraction (another reason why I love it). You can easily integrate Amazon ElastiCache within your application (i.e. caching the Rest API calls) and exploit the advantages of use a distributed (you can scale up to several nodes) sharded and replicated cluster of Redis instances running in the cloud.