Getting Started with Alibaba Cloud ApsaraDB for Memcache SDK with NodeJS

Image for post
Image for post

ApsaraDB is a suite of database products on Alibaba Cloud 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.

Installing Memcache SDK with NodeJS

The Memcache SDK can be installed with the NodeJS package manager (npm) along with the Alibaba Cloud SDK. In this tutorial, we will be specifically installing and running a Hello World program on Alibaba Cloud ApsaraDB for Memcache using an Alibaba Cloud 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 Alibaba Cloud ECS instance on the VPC where you can install an ApsaraDB instance.

Creating an ApsaraDB Memcache Instance

Create a VPC with VSwitch in your favorite region.

Image for post
Image for post

This is the CIDR block you will use to provide whitelist access for the Alibaba Cloud ApsaraDB Memcache application.

Image for post
Image for post

Create an ECS instance on the VPC.

Image for post
Image for post

In this tutorial, we will create an ECS instance with an Ubuntu Linux OS.

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

You should be successfully logged in now.

Image for post
Image for post

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 (EIP) Address and bind it to the instance.

Image for post
Image for post

Associate 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.

Image for post
Image for post

Setup Ubuntu OS, npm, and the Alibaba SDK

Now let’s install everything we need on your ECS instance installed with Ubuntu. First, let’s do the required Ubuntu OS updates:

apt-get update
apt-get upgrade
apt-get dist-upgrade
Image for post
Image for post

Then, 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. This simplifies things.

npm install aliyun-sdk
Image for post
Image for post

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.

Image for post
Image for post

Create the Instance.

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

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 IP addresses or CIDR blocks, go into the ApsaraDB instance details page, and click the instance ID/Name .

Image for post
Image for post

Go to Whitelist Settings.

Image for post
Image for post

Click Add a Whitelist Group.

Image for post
Image for post

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

Image for post
Image for post

This will allow us to connect to the ApsaraDB Memcache instance running on the ECS server instance. 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.

Image for post
Image for post

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.

Image for post
Image for post

Fill out the details.

Image for post
Image for post

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

Image for post
Image for post

Check the online documentation for more information.

Backup Memcache

In the Alibaba Cloud ApsaraDB for Memcache instance details page, 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.

Image for post
Image for post

Summary

To sum up, we first set up a VPC and VSwitch in order to run an ECS instance. Then, we installed an ApsaraDB Memcache application on the ECS instance. The ECS instance we created was installed with Ubuntu. We were able to quickly install the relevant packages to run the Alibaba Cloud memcached_node SDK. Last, we wrote a helloworld.js that set one key/value pair and ran it.

Original Source:

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store