×
Community Blog Getting Started with Alibaba Cloud ApsaraDB for Memcache SDK with NodeJS

Getting Started with Alibaba Cloud ApsaraDB for Memcache SDK with NodeJS

In this tutorial, we will set up Alibaba Cloud's ApsaraDB for Memcache with the Alibaba Cloud SDK and NodeJS.

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.

1

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

2

3

Create an ECS instance on the VPC.

4

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

5

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

6

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

7

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.

8

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

9

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.

10

You should be successfully logged in now.

11

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.

12

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.

13

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

14

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

15

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.

16

Create the Instance.

17

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

18

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

19

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 .

20

Go to Whitelist Settings.

21

Click Add a Whitelist Group.

22

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

23

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.

24

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.

25

Fill out the details.

26

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

27

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.

28

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.

0 0 0
Share on

Alibaba Clouder

2,599 posts | 762 followers

You may also like

Comments