How to Implement Data Replication Using Tunnel Service for Data in Tablestore

Image for post
Image for post

By Wang Zhuoran, nicknamed Zhuoran at Alibaba.


The term data replication implies a process that involves storing data replicas on multiple servers through an interconnected network. A data replication solution is important because it will helps to achieve the following:

  • Reduce access latency by moving data geographically closer to users.
  • Improve system availability by allowing the system to work even when some components fail.
  • Improve throughput by scaling up the system to multiple servers and provide data access.

Data replication is relatively easy to implement when the concerned data remains unchanged. This is because such data is only required to be replicated each node only once and that’s it. However, it is very challenging to correctly and effectively complete the replication of data that is constantly changing.

Using DataX for Data Replication in Tablestore

Alibaba Cloud Tablestore is a NoSQL multi-model database of Alibaba Cloud. It provides the storage necessary for a huge volume of structured data, as well as fast queries, and related analysis services. The distributed storage and powerful index engine of Alibaba Cloud Tablestore provide petabytes worth of storage, can support tens of millions of transactions per second (TPS), and latency as low as only a few milliseconds.

DataX is an offline data synchronization tool widely used within the Alibaba Group. As a data synchronization framework, DataX abstracts the synchronization of different data sources into a Reader plug-in that reads data from various data sources and a Writer plug-in that writes data to the destination database.

The following figure shows how to use DataX for completing data replication of a table in Alibaba Cloud Tablestore. The OTSreader plug-in reads data from Tablestore and extracts incremental data from the specified data extraction scope. The OTSstreamreader plug-in exports incremental data from Alibaba Cloud Tablestore. The otswriter plug-in writes data to Tablestore. Configure Tablestore-related Reader and Writer plug-ins in DataX to complete the data replication process of the tables in Tablestore.

Image for post
Image for post

Using Tunnel Service for Data Replication in Tablestore

Alibaba Cloud’s Tunnel Service is an integrated service for full-incremental data consumption based on the Tablestore API. It provides real-time consumption tunnels for distributed data, including incremental data, full data, and full and incremental data. Creating tunnels for a table, allows consuming historical and incremental data in the table.

Image for post
Image for post

Using a full-incremental Tunnel Service, build an efficient and elastic data replication solution. This article describes how to use Tunnel Service for data replication in Tablestore. Refer to the tablestore-examples of GitHub for the complete open-source code. The practice demonstrated in this article is based on the Java SDK of Tunnel Service. Read the Overview and Quick Start documents for more details.

1. Configure the Replication Scope

To begin, you’ll need to specify the data replication scope. This step helps to complete the table data synchronization before the specified time that is either the current or a future time point. The following snippet shows the specific configuration where the OTSreader plug-in records the configuration of the source table, and the OTSwriter plug-in records the configuration of the target table.

Parameters of the OTSreader plug-in are as follows:

  • endpoint: The endpoint of the Tablestore service, for example, . Before data replication, run the curl command to check the connectivity.
  • instanceName: The name of a Tablestore instance.
  • tableName: The name of a table in Tablestore.
  • accessId: The AccessKey ID of an Alibaba Cloud account used to access Tablestore.
  • accessKey: The AccessKey secret of an Alibaba Cloud account used to access Tablestore.
  • tunnelName: The name of a tunnel in Tablestore.
  • endTime: The end time for data replication. The format corresponding to SimpleFormat in Java is yyyy-MM-dd HH:mm:ss.

Parameters of the otswriter plug-in (with the same parameters omitted):

  • batchWriteCount: The number of records written to Tablestore in a batch. The maximum value is 200.

Note: More functions will be available in the future, such as data replication within a specified time range.

2. Compile the Main Logic

The main logic of data replication consists of four steps. It is critical to note that all the steps execute during the first run, while only the third and fourth steps execute upon program restart or resumable upload.

1. Create a Destination Table for Data Replication

For this step, you’ll need to call the DescribeTable operation to obtain the schema of the source table and then create a destination table. Set the valid version offset of the destination table to a sufficiently large value (86,400 seconds by default) since the server checks the version numbers in the attribute column while processing write requests, and the version numbers are written only within a certain range. For inventory data in the source table, the timestamp is usually small and filtered out by the server, causing synchronized data loss.

2. Create a Tunnel in the Source Table

Call the CreateTunnel operation of Tunnel Service to create a tunnel. The following snippet shows an example to create a tunnel of the full-incremental type (TunnelType.BaseAndStream).

3. Start a Scheduled Task to Monitor the Backup Progress

Now, call the DescribeTunnel operation to monitor the backup progress. The DescribeTunnel operation obtains the latest consumption time. Comparing the time with the configured backup end time helps to obtain the current synchronization progress. After the end time arrives, the server exits backup.

4. Start Data Replication

Start the automatic consumption framework of the Tunnel Service to initiate automatic data synchronization. OtsReaderProcessor parses data in the source table and writes data to the destination table. The processing logic will be described later in this article.

3. Compile the Data Synchronization Logic (OtsReaderProcessor)

To use Tunnel Service, you’ll need to compile the Process logic and Shutdown logic of data. The core of data synchronization is to parse data and write it to the destination table. The following snippet shows the complete code for data processing. The main logic checks whether the data timestamp is within a reasonable time range, converts StreamRecord into the corresponding row in BatchWrite, and then writes data to the destination table in series.

4. Technical Annotation

Let’s take a look at the most common issues pertaining to the practice of using Tunnel Service for data replication in Alibaba Cloud Tablestore.

  • How can I ensure the backup performance?

Backup is divided into full (inventory) data backup and incremental data backup. For full data backup, Tunnel Service logically splits all data in the table into several shards with the size similar to the specified size. The overall parallelism of full data synchronization relates to the number of shards, which effectively ensures throughput. For incremental data backup, process data in a single partition in series to ensure data orderliness. The performance for incremental data backup is directly proportional to the number of partitions (refer to the Incremental Synchronization Performance White Paper). To speed up (add partitions), contact the customer service representatives of Alibaba Cloud Tablestore.

  • How can I scale out data synchronization?

Assume that multiple Tunnel Workers (clients) consume data on the same tunnel (with the same tunnel ID). When Tunnel Workers execute heartbeat messages, the tunnel server automatically redistributes channel resources, so that active channels are evenly distributed for each Tunnel Worker, achieving resource load balancing. Add Tunnel Workers to scale out. Tunnel Workers run on one or more instances. For more information, see Description of the data consumption framework

  • How can I ensure data consistency?

Data consistency is based on the order-preserving protocol of the Tunnel Service. The idempotence of full and incremental data synchronization ensures the consistency of backup data.

  • How can I implement resumable upload?

The client of Tunnel Service regularly sends the time points of synchronized (consumed) data to the server for persistence. After the failover or program restart, the next data consumption starts from the recorded checkpoint, without experiencing data loss.

The Road Ahead

This tutorial has outlined the practice of how you can use Tunnel Service to complete a simple and effective data replication solution, which implements table data replication at a specified time. Using the sample code in this practice helps to set parameters for the source and destination tables to efficiently replicate and migrate table data.

In future evolution, Tunnel Service will also support the creation of tunnels in a specified time range. This will allow more flexibility to develop a data backup plan and implement more functions such as continuous backup and time-based recovery.

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