Alibaba Cloud’s ApsaraDB for Redis is a key-value pair storage database that offers in-memory caching and high-speed access to applications hosted on the cloud. The ApsaraDB stack is part of Alibaba Cloud’s wider suite of database services. The ApsaraDB suite includes support for RDS, Memcache, MongoDB and Redis. Alibaba Cloud’s ApsaraDB frees you from common system admin tasks associated with managing large web application in-memory caches and corporate databases. ApsaraDB for Redis includes an automatic backup system as standard.
In this tutorial, you’ll get started using Alibaba Cloud ApsaraDB for Redis, and explore how you can migrate and back up a local database with Alibaba Cloud ApsaraDB for Redis.
Setting up the Backup File
ApsaraDB for Redis allows you to migrate with the EOF file. Make sure you set appendonly on your Redis database with the following command:
config set appendonly yes
Creating an Alibaba Cloud ECS Instance
Now, let’s setup Redis on Alibaba Cloud’s ApsaraDB for Redis. To do this, we will have to first create an Alibaba Cloud ECS Instance on which you will then attach the Redis service. Let’s create an ECS instance installed with Ubuntu 16.04. Choose the region based on your requirements.
Don’t forget to set a password so you can connect to the instance.
Agree to the terms and then create the instance.
Setting up the Instance and Redis
Now, let’s install everything we need on the instance. First, we need to do the required Ubuntu updates:
Then, now that that’s finished, let’s install Redis on the ECS instance:
apt install redis-tools
Creating an ApsaraDB for Redis Instance
Pick the Redis version you require.
Notice that Alibaba Cloud auto-populates the VPC/vSwitch details with the defaults just created. When you’re ready, click Buy Now, then agree to the terms and click Activate.
Connecting to ApsaraDB Redis
Now let’s connect to the ApsaraDB for Redis instance on the ECS instance we have just created. We will use the SSH protocol for this.
Next, in the ApsaraDB for Redis details page, we have to whitelist a network CIDR block in order to access the Redis instance. The default whitelist blocks all access with 127.0.0.1.
For this tutorial, we will modify the whitelist setting to allow access from anywhere. Remember that, in a real production scenario, you will want to whitelist just a section of a VPC CIDR block for Redis.
After the whitelist has been set, the Connection Address appears on the ApsaraDB for Redis Instance Information tab. Make a note of it.
Now, from the terminal connected to the ECS instance, you can access the ApsaraDB for Redis instance with the following command:
redis-cli -h [Connection Address] -a [password]
Getting ApsaraDB for Redis Ready for Migration
Follow these steps to make sure that your installation is in proper working order. To do this, you’ll need to follow these steps:
First, start Redis with the
Then, run the command line interface with
redis-cli and check the server is running with a
ping. And, set and get a test key-value pair and save it to the backup file.
You can also connect to a Redis instance using other client software such as Jedis for Java, for example.
Migrating Redis to Alibaba Cloud
In a terminal window,
cd to the local directory in which your
appendonly.aof file resides and run the following
scp appendonly.aof firstname.lastname@example.org:/home
Now, find the copied
appendonly.aof file in the ECS instance.
appendonly.aof to the ApsaraDB for Redis instance with the following command in the ECS terminal.
redis-cli -h [Connection Address] -p 6379 -a [password] --pipe < appendonly.aof
Now, log in to your ApsaraDB for Redis service and with the
get command, check the data by getting something you set on the database locally. You will see that your data has been successfully migrated over to the Alibaba Cloud.
Connecting with Jedis
You can also connect to your Alibaba Cloud ApsaraDB for Redis instance with Jedis for Java in Eclipse. Before you can access the Redis instance in the cloud you have to open some ports with network configuration changes on the ECS instance.
rinetd on the Ubuntu instance. Note that the details given here will be different for other operating systems.
wget http://www.boutell.com/rinetd/http/rinetd.tar.gz&&tar -xvf rinetd.tar.gz&&cd rinetd
sed -i 's/65536/65535/g' rinetd.c
mkdir /usr/man&&make&&make install
Next, add the following configuration details to the
rinetd configuration file:
0.0.0.0 6379 r-7go1a73cf051c984.redis.rds.aliyuncs.com 6379
Then, start the
rinetd is running and working properly.
Also, you have to make sure to open the Redis port in the Security Group details page of the ECS instance. Go to the ECS Management Console Security Groups page and select your Security Group. Click Configure Rules, and then click Add Security Group Rules.
Open port range 6379/6379 to the IP range you wish to have access to your Redis instance. We have used 0.0.0.0/0 for this tutorial.
Now you can code directly to the Redis instance from your local development environment. For Jedis, create a new class and add the appropriate configuration details to match your cloud instance.
Setting up Backup Configurations
As with all the other Alibaba Cloud ApsaraDB services, in the ApsaraDB for Redis instance details, you can set backup configurations for your Redis instance.
To summarize what we did in this tutorial, we first had set up our Redis database and made sure it was working, and we set an example key-value pair. Then, we made sure we saved our local Redis database with the appendonly option. Next, after that, we created an Alibaba Cloud ECS Instance on which to host our ApsaraDB for Redis instance. We configured the instance to have Ubuntu 16.04 as our operating system and setup Redis on the instance. Next, we created an Alibaba Cloud ApsaraDB for Redis instance, which was attached to the ECS instance we had just created. We migrated our local database to the Alibaba Cloud instance and checked whether it had migrated successfully. Finally, we performed some network administration in order to be able to access our Alibaba Cloud ApsaraDB for Redis instance from outside the cloud console. We chose the Java Jedis client and wrote a test class to check that we could connect to the cloud database.
Enjoyed this tutorial? Learned something? Don’t forget to check out our other Alibaba Cloud ApsaraDB tutorials for setting up ApsaraDB for Memcache, ApsaraDB for RDS and ApsaraDB for MongoDB, along with several other tutorials for several other products and services offered by Alibaba Cloud.