Getting Started with Alibaba Cloud ApsaraDB for Memcache SDK with NodeJS

In this tutorial, we’ll show you how to get started with Alibaba Cloud’s ApsaraDB for Memcache SDK with NodeJS.

What Is ApsaraDB for Memcache?

ApsaraDB is Alibaba Cloud’s suite of database products that includes support for RDS, MongoDB, Redis, and Memcache. In this tutorial, we will set up Alibaba Cloud’s ApsaraDB for Memcache with the Alibaba Cloud SDK and NodeJS.

Alibaba Cloud ApsaraDB for Memcache is an in-memory caching service that supports high-speed access to queries and data – which is often web application session data. It improves the response of dynamic websites or applications by relieving the load on the backend database as cached data is stored in-memory.

Memcache SDK with NodeJS

The Memcache SDK can be installed with the NodeJS package manager (npm) along with the Alibaba Cloud SDK.

We will install and run a Hello World program on the Alibaba Cloud ApsaraDB for Memcache on an ECS instance within an Alibaba Cloud VPC using the NodeJS package manager.

Preparatory Steps

• You'll need an Alibaba Cloud VPC and VSwitch CIDR block.

• You'll need an ECS instance on the VPC where you can install an ApsaraDB instance.

Create ApsaraDB Memcache Instance

Create a VPC with VSwitch in your favorite region.

Make a note of the CIDR block you will use to whitelist access for the Alibaba Cloud ApsaraDB Memcache application.

Create an ECS instance on the VPC.

For the tutorial’s purposes, we will create an ECS instance with an Ubuntu Linux OS.

Set a password for the root user so we can access the instance on the command line.

Check the configuration details, agree to the terms, and create the instance.

You can access the instance by binding a public IP address to the ECS, or you can access the instance directly in the Alibaba Cloud user interface. In the ECS instance details page, click Connect.

A new browser window opens, and you will see a pop-up window with your connection password. Make a note of it.

You will be prompted for the password to access the UI terminal window. Once you are in, login with the username and password you set when you created the ECS instance.

You should be successfully logged in now.

For those who find working on the Alibaba Cloud ECS UI terminal too slow, the best way to access the instance is to create a public Elastic IP Address and bind it to the instance.

Bind the EIP to the ECS instance and make a note of the IP address. Once the EIP is bound to the ECS instance we can access it from the terminal window.

Setup Ubuntu OS, npm, and the Alibaba SDK

Let’s install everything we need on the Ubuntu OS ECS instance. First, let’s do the requisite Ubuntu OS updates:

apt-get update
apt-get upgrade
apt-get dist-upgrade

Now let’s install nodejs and npm on the ECS instance.

apt-get install nodejs
apt-get install npm

Now we can use the node package manager to install the Alibaba Cloud SKD.

npm install aliyun-sdk

Setup the Memcache Instance

Now we’re ready to set up the ApsaraDB Memcache instance on the ECS instance we have just created. Go to ApsaraDB for Memcache in the Products page.

Create Instance.

In the configuration details page, attach the instance to the VPC you created previously and click Buy Now.

Agree to terms and Activate.

In the ApsaraDB for Memcache page you will see the ApsaraDB instance being created.

Now you have to whitelist the CIDR IP range so that you can access the Memcache application on the instance. However, for testing purposes you can whitelist all IP addresses with 0.0.0.0/0.

To specify specific IPs or CIDR blocks, go into the ApsaraDB instance details.

Go to Whitelist Settings.

Click Add a Whitelist Group.

In the whitelist group, add the IP address or CIDR segment from the VPC.

This will allow us to connect to the ApsaraDB Memcache instance running on the ECS. After you click OK, you will be able to see the ApsaraDB instance Connection Information on the instance details page. Make a note of the Connection Address.

Memcache Hello World

In a terminal window, cd to the node_memcached directory on the ECS instance. Create a new JavaScript file in the directly. Here, I’ve called the file helloworld.js.

Fill out the details.

Run helloworld.js with node and note the successful addition of the key/value pair hello/world via the memcached.add function.

Check the online documentation for more information.

Backup Memcache

In the Alibaba Cloud ApsaraDB for Memcache instance details, there is a tab for backing up your Memcache instances. You have the option to backup immediately by clicking Create Backup, or there are default Backup Settings which back up your instances on a daily basis.

Summary

To summarize, we first set up a VPC and VSwitch in order to run an ECS instance. We installed an ApsaraDB Memcache application on the ECS instance.

We created the ECS instance with an Ubuntu OS. We were quickly able to install the relevant packages to run the Alibaba Cloud memcached_node SDK.

We wrote a helloworld.js that set one key/value pair and ran it.