Alibaba Cloud Tablestore: A Case Study on Backing up a Massive Volume of Structured Data

By Wang Zhuoran, nicknamed Zhuoran at Alibaba.

In the field of information technology and data management, the term backup implies the replication of data in a file system or database system to restore the requisite data and routine operations of the system in the event of a disaster or an error. What this all means is that, in actual backup scenarios, it is recommended that you back up important data in three or more replicas, and store them in different locations for geo-redundancy purpose.

Backup fundamentally involves two different items. The first one is to restore lost data due to mis-deletion or corruption, and the second one is to restore older data based on a custom data retention policy. Generally, the configuration of replication scope is done in the backup application.

Another important point to know is that a backup system may have high requirements for data storage as a system will contain at least one replica consisting of all the data worth storing. It may be complex to organize this storage space and manage backups. Currently, there are many different types of data storage devices available for backup on the market. These devices are configurable in several different ways to ensure geo-redundancy, data security, as well as portability.

Before sending data to a storage location, generally the processes of effective selection, extraction, and the related operation are required. You can optimize the backup process using a variety of technologies, including open file processing, real-time data source optimization, and data compression, encryption, and deduplication. Each backup solution must necessarily provide for drilling scenarios so to be able to verify the reliability of the data being backed up and, more importantly, to reveal the limits and human factors involved in the backup solution.

Now that we are on the same page when it comes to data backup and restoration, let’s discuss the backup and restoration principles involved with Tunnel Service, which is part of Alibaba Cloud Tablestore. And, in this article, we’re also going to provide you with a step-by-step guide on how to make a data backup and restoration solution using Tablestore and Tunnel Service.


  • Data Reliability of Storage Systems: Alibaba Cloud Tablestore is a serverless NoSQL multi-model database independently developed by Alibaba Cloud for storing massive structured data. It provides 99.9999999% data reliability, which is a very high standard in the industry.
  • Data Restoration After a Misoperation: A misoperation at some point is inevitable. Data backup restores data as soon as possible when a misoperation occurs. Two backup solutions are available. The first one is to deploy local disaster recovery or geo-disaster recovery. This can be costly and is primarily used for backup of basic social or financial information. The second one is to back up data to another inexpensive system for restoring the data in case of misoperation. For the latter option, users generally go with a file storage system like Alibaba Cloud Object Storage Service (OSS).

Tablestore Backup and Restoration Solution

Case Study: Tablestore Backup and Restoration Solution

1. Backup Preparation: Determine the data source to back up and the destination location. In this case study, the data source to back up is tables in Alibaba Cloud Tablestore, and the destination location is buckets in Alibaba Cloud OSS. For this case study, we will be doing the following:

  • Determine a backup plan and policy.
  • Use the Tunnel Service SDK to write code.
  • Execute the backup policy and monitor the backup process.

2. Recovery Last, we need to do the recovery step. For this step, all we need to do is restore the files.

Determining a Backup Plan and Policy

  • Full Backup: This copies all the files, folders, or data on a hard disk or database at once.
  • Incremental Backup: This backs up subsequently updated data after the last full or incremental backup.
  • Differential Backup: This backs up changed files after a full backup.
  • Selective Backup: This backs up a part of the system data.
  • Cold Backup: This acks up data when the system is in a stopped or maintenance state. The backup data and the system data from this period must necessarily be the same.
  • Hot Backup: This backs up data when the system runs properly. The system data updates at any time so that the backup data may lag behind the actual system data.

Additionally note that this case study uses the following backup plan and policy: you can customize a backup plan and policy as needed and use the Tunnel Service SDK to write related code.

  • Backup Content: Data tables in Tablestore
  • Backup Time: This includes the following:
  1. A full backup is performed periodically. The interval is adjustable and the default value is one week.
  2. Incremental backup is also performed periodically based on the configuration. Tunnel Service of Tablestore ensures a strict data order. Each incremental file is appended to the stream and is consumed at any time.
  • Backup Methods: There’s full backup, incremental backup, and hot backup.

Using the Tunnel Service SDK to Write Code

1. Create a tunnel of the full-incremental type using the SDK or within the console.

private static void createTunnel(TunnelClient client, String tunnelName) {
CreateTunnelRequest request = new CreateTunnelRequest(TableName, tunnelName, TunnelType.BaseAndStream);
CreateTunnelResponse resp = client.createTunnel(request);
System.out.println("RequestId: " + resp.getRequestId());
System.out.println("TunnelId: " + resp.getTunnelId());

2. Familiarize yourself with the automatic data processing framework of Tunnel Service

Enter the process function and shutdown function for automatic data processing based on the instructions in the Quick Start Guide of Tunnel Service. The input parameters of the process function contain a list. StreamRecord encapsulates the data of each row in Tablestore, including the record type, primary key column, property column, and timestamp when you write records.

3. Design the persistence format for each row of data in Tablestore

This case study uses the CSV file format, which makes data fully accessible. You may also use PB or other formats. Each data row corresponds to a row in a CSV file. The persistent format is shown in the following figure. A CSV file has four columns. The TimeStamp column contains the timestamp when data is written to Tablestore (the value is 0 for a full back up or a specific timestamp for incremental backup). RecordType indicates the action type (PUT, UPDATE, or DELETE) of the data row. PrimaryKey is the JSON string of the primary key column. RecordColumns is the JSON string of the property column.

The following code snippet is the core code of conversion and processed using uniVocity-parsers (CSV) and Gson. Note the following points:

  1. Progress loss may occur when Gson deserializes the Long type into the Number type. However, it is possible to resolve this problem by using several different methods. One effective method is to serialize the Long type into the String type.
  2. Base64 encodes and decodes the binary data.
  3. Write the data directly to OSS, which reduces resource consumption during local persistence.
this.gson = new GsonBuilder().registerTypeHierarchyAdapter(byte[].class, new ByteArrayToBase64TypeAdapter())
// ByteArrayOutputStream到ByteArrayInputStream会有一次array.copy, 可考虑用管道或者NIO channel.
public void streamRecordsToOSS(List<StreamRecord> records, String bucketName, String filename, boolean isNewFile) {
if (records.size() == 0) {"No stream records, skip it!");
try {
CsvWriterSettings settings = new CsvWriterSettings();
ByteArrayOutputStream out = new ByteArrayOutputStream();
CsvWriter writer = new CsvWriter(out, settings);
if (isNewFile) {"Write csv header, filename {}", filename);
List<String[]> totalRows = new ArrayList<String[]>();"Write stream records, num: {}", records.size());
for (StreamRecord record : records) {
String timestamp = String.valueOf(record.getSequenceInfo().getTimestamp());
String recordType = record.getRecordType().name();
String primaryKey = gson.toJson(
String columns = gson.toJson(TunnelRecordColumn.genColumns(record.getColumns()));
totalRows.add(new String[] {timestamp, recordType, primaryKey, columns});
// write to oss file
ossClient.putObject(bucketName, filename, new ByteArrayInputStream(out.toByteArray()));
} catch (Exception e) {

Executing the Backup Policy and Monitor the Backup Process

public class TunnelBackup {
private final ConfigHelper config;
private final SyncClient syncClient;
private final CsvHelper csvHelper;
private final OSSClient ossClient;
public TunnelBackup(ConfigHelper config) {
this.config = config;
syncClient = new SyncClient(config.getEndpoint(), config.getAccessId(), config.getAccessKey(),
ossClient = new OSSClient(config.getOssEndpoint(), config.getAccessId(), config.getAccessKey());
csvHelper = new CsvHelper(syncClient, ossClient);
public void working() {
TunnelClient client = new TunnelClient(config.getEndpoint(), config.getAccessId(), config.getAccessKey(),
OtsReaderConfig readerConfig = new OtsReaderConfig();
TunnelWorkerConfig workerConfig = new TunnelWorkerConfig(
new OtsReaderProcessor(csvHelper, config.getOssBucket(), readerConfig));
TunnelWorker worker = new TunnelWorker(config.getTunnelId(), client, workerConfig);
try {
} catch (Exception e) {
public static void main(String[] args) {
TunnelBackup tunnelBackup = new TunnelBackup(new ConfigHelper());

2. Monitor the backup process

Monitor the backup process in the Tablestore console or use the DescribeTunnel operation to obtain client information (customizable) about each channel of the current tunnel, the total number of consumed rows, and the consumer offset.

Assume that you need to synchronize data for the period from 2 pm to 3 pm. If the DescribeTunnel operation obtains the consumer offset 2:30 pm, then half of the data is backed up. If the operation obtains the consumer offset 3:00 pm, then data from the period from 2:00 pm to 3:00 pm is backed up.

Restoring the Files

  • Download data from Alibaba Cloud OSS in streaming or multipart mode, and increase the number of concurrent downloads when necessary.
  • Write data to Tablestore through BatchWrite. The following snippet shows the Restore instance code (excluding a few details) that is used to perform the following operations in sequence:
  1. Compute the name of the file to download based on the backup policy.
  2. Read the file from OSS through streaming download.
  3. Write the file to the Restore table in Tablestore through BatchWrite.
public class TunnelRestore {
private ConfigHelper config;
private final SyncClient syncClient;
private final CsvHelper csvHelper;
private final OSSClient ossClient;
public TunnelRestore(ConfigHelper config) {
this.config = config;
syncClient = new SyncClient(config.getEndpoint(), config.getAccessId(), config.getAccessKey(),
ossClient = new OSSClient(config.getOssEndpoint(), config.getAccessId(), config.getAccessKey());
csvHelper = new CsvHelper(syncClient, ossClient);
public void restore(String filename, String tableName) {
csvHelper.parseStreamRecordsFromCSV(filename, tableName);
public static void main(String[] args) {
TunnelRestore restore = new TunnelRestore(new ConfigHelper());
restore.restore("FullData-1551767131130.csv", "testRestore");


  1. The requirements of backup through Tablestore.
  2. The principles of data backup and restoration through the Tunnel Service of Tablestore.
  3. A step-by-step process for how you can develop a data backup and restoration solution based on Tablestore by using real code snippets.

Original Source:

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