Because new applications require to be reliable and super fast, caching is one of the solutions you should implement in an application to guarantee no time is spent without any reason. Suppose you have developed an application that is distributed on a few different servers. Your application implements sessions for each user, so you need to store and handle this session somewhere. Because your application is distributed, you can’t store the sessions on the local machines, but you need to find another solution. Well, a caching solution is what you are looking for. In particular, Memcached is an object caching system. It allows you to store key-value fields where the key needs to be unique, and it’s used to set and then retrieve data; the value is the object you want to store (such as the session object in the previous example).
Because of this importance, Google offers an in-memory caching solution through Google Memorystore. It is a scalable, secure, and highly available in-memory service for both Redis and Memcached.
In this lab, you will create a Memcached cluster, you will connect to it and perform some operations, and you will finally scale it out.
Upon completion of this lab you will be able to:
- Create a Memcached cluster
- Connect to a Memcached cluster and perform caching operations
- Scale out a Memcached cluster
Creating a Google Memorystore Memcached Cluster
A Memcached cluster represents an in-memory caching cluster solution that allows you to store and perform operations over objects (such as string and session objects). A caching system is a small and super fast in-memory store that lets you build caching solutions to avoid spending time performing useless queries on a DB.
In this lab step, you will create a Memcached cluster.
1. Look at the upper part of the Google Cloud dashboard and locate the search bar. Enter memorystore and select Memcached:
You will be displayed the list of Memcached clusters you have in Memorystore:
2. Click on CREATE INSTANCE.
3. Fill the cluster creation form as described below (leave the unmentioned fields at their default value):
- Instance ID: ca-lab-memcached-cluster
- Display name: ca-lab-memcached-cluster
- Region: us-central1
- Zone: us-central1-a
- Nodes: 1 (Make sure to select just one node, otherwise you will be banned)
- Memory per node: 1 GB
- Cores per node: 1 vCPU
- Private service connection: Click on SET UP CONNECTION
- Click on Continue under the Enable Service Networking API section
- Check the Use an automatically allocated IP range under the Allocate an IP range section. Click on Continue
- Click on CREATE CONNECTION
You need to set up a private connection to be able to access the Memcached cluster from resources within the same network.
4. Click on CREATE.
5. Wait until the Memcached cluster is ready:
Note: It can take up to 5 minutes. Just regularly refresh the page.
Connecting and Performing Operations to the Memcached Cluster
Memcached lets you build an in-memory caching solution to store and handle objects.
In this lab step, you will connect to the Memcached cluster through the Compute Engine that has been deployed in your environment, and you will perform some basic Memcached operations.
1. Look at the upper part of the Google Cloud dashboard and enter compute engine in the search bar. Click on Compute Engine:
2. Move on the right part of the instance and click on SSH:
Note: It should take a minute or so to establish the SSH connection.
3. Enter the following command to install the telnet client, which will be used to connect to the Memcached cluster:
sudo apt-get install telnet -y
4. Move back to the Memorystore Memcached dashboard and click on the name of the Memcached cluster you created.
The Instance details section will be displayed:
5. Scroll down to the Connection properties and copy the Discovery endpoint removing the 11211 port:
6. Execute the following command to log into the Memcached cluster you previously created:
telnet <discover_endpoint> 11211
7. Execute the following command to store your first object:
set test_value 0 900 10 test_value
The set command is composed as the following:
- key: is the key associated to the value
- flags: flags you want to attach to the command
- expiration: the expiration time related to the key-value field (in seconds)
- byte: the maximum amount of bytes to store
8. Execute the following command to retrieve the object you previously stored:
You will see something like below:
In this lab step, you connected to the Memcached cluster through the Compute Engine that has been deployed in your environment, and you performed some basic Memcached operations.
Scaling the Memorystore Memcached Cluster
Because of growth reasons, you should need to scale out your Memcached cluster, Google lets you simply scale it up and down if needed.
In this lab step, you will scale out your Memcached cluster.
1. Move back to the Memorystore Memcached dashboard and click on the name of the Memcached cluster you created.
2. From the Instance details section of your Memcached cluster, click on EDIT:
3. Scroll down to the Nodes, and set it to 2:
4. Click on SAVE.
It could take up to 15 minutes to scale up. You will see Nodes count has been set to 2
In this lab step, you scaled out your Memcached cluster.