Migrating from MongoDB to TDengine significantly reduces TCO

Original Author: Yu Dong, Dongguan Zhongrong Digital

XiaoT Introduction: Most Chinese breeding enterprises generally use traditional plastic ear tags + manual periodic analysis + veterinary on-site diagnosis to prevent disease in livestock. Although there are other solutions on the market, there are still problems such as livestock identification errors and high latency, and it is impossible to monitor livestock in real time. To resolve these problems we use emerging technologies to create an AIoT platform for livestock “feature collection + AI analysis”. This leads to early detection, early warning, and early prevention of livestock abnormalities.

We designed an application with the following general workflow. The application is used to enter the IoT device number in advance and then the device is affixed to the animal. The device collects data 20 times per minute, and the collected data types include temperature, humidity, latitude and longitude, movement, pulse etc. The collected data is analyzed at the Edge and the summary results are sent to the cloud server through the 4G network. In the cloud, the corresponding variables, such as weather based on longitude and latitude are obtained and combined with the device data. AI is then used to comprehensively evaluate current health conditions in livestock, such as loss of appetite, fever trends, etc.

1. Architecture and Implementation

Like traditional IoT projects, this project had high performance requirements for data writing as well as certain aggregate queries. Specifically there are significantly more writes and fewer reads, which is a typical high-concurrency writing scenario. MongoDB was used before but aggregate queries were very inefficient and it was generally inconvenient. Cassandra was given a try but it did not meet the requirements. By chance, we learned about time series databases such as InfluxDB and TDengine. After building the test environment, we tested the two respectively, and finally settled on TDengine. In addition to the clear performance gap between TDengine and InfluxDB, the table data TTL mechanism, data compression, stream computing and other functions provided by TDengine made it a clear winner for our stringent high-performance requirements.

Based on the design principle of the super table, we use the business related information of livestock as tags, which is also convenient to associate with data in MySQL. Each device exists as a sub-table, and the device’s measurements are used as columns in the sub-table.

The super table schema is shown in Figure 1 below. Figures 2 and 3 show the super table schema for environmental sensors installed in fixed positions on the farm. The temperature and humidity table, in addition to the table that stores the original message data, are not listed here.

Figure 1 – Super table schema for devices on livestock
Figure 2 – Super table schema for Temperature Sensors
Figure 3 – Super table schema for Humidity Sensors

At present, all the IoT data tables of our company are designed based on the TDengine super table. The core livestock super table, has more tags than other tables. It should be noted that, in order to ensure that the tags in TDengine are consistent with MySQL, whenever the basic attributes of livestock are modified for business reasons, tags need to be modified synchronously in TDengine. The super table design also allows us to trace possible processing delays and other problems.

The collect_time in the table is the collection time, and the insert_time is the time when data is inserted into TDengine. If the time difference between the two is large, there may be network problems, collection device failures or insufficient server throughput etc. Having this data, allows us to investigate. The whole process of livestock data collection to data placement is shown in the figure below. The data collected by the collection device is transmitted through the 4G network. The device gateway performs preliminary processing on the data and then pushes it to MQ (Message Queue). The data is then transmitted to subscribers/consumers on MQ and finally to TDengine.

Figure 4 – Livestock data collection to data writing into TDengine

2. Data Migration and Ramifications

Since we were replacing MongoDB, we had to perform a data migration as follows:

  • Newly generated collector data was written into MongoDB as well as TDengine.
  • Historical data was gradually migrated into TDengine.
  • The data source for AI analysis was changed from MySQL data warehouse, to TDengine.

During the migration process, we also encountered two small issues:

  • Since the MySQL+MongoDB solution was used before, all MongoDB statements had to be rewritten as TDengine SQL. Although TDengine supports standard SQL, there are minor differences in details which have to be accounted for.
  • Since we provide many services, I initially thought about creating a middleware layer to provide data query to other services in the system. However, since TDengine is a relatively new open source project I decided to let each service connect to TDengine by themselves. All the services can be integrated after the migration and routine operations are stable.

The ramifications after the migration are very clear. When we used MongoDB, the self-built cluster used six, 4-core, 32G servers. After migrating to TDengine, the self-built cluster only used only two, 8-core, 32G machines. This had led to a significant operational cost decline. In terms of specific performance, I simulated the data of more than 6,000 collectors, and the table data totaled about 300 million records. Most of our queries are based on sub-tables, and only some businesses need to view aggregated operational data. When querying group by+last_row(*) on the super table, the data can be returned within 1.5s, and querying on the sub-table in about 0.1 seconds (select * from son_table limit 10). This meets our business requirements.

3. Closing Thoughts

With the development of emerging technologies such as the Internet of Things and artificial intelligence, AIoT has become a major trend that cannot be ignored. To store the massive amounts of data, requires both warehouses and transactional databases. However, with a purpose-built product like TDengine, designed with IoT, time-series data in mind, this complexity can be avoided. In a sense, TDengine with it’s stream computing and high-performance query engine, helped save a lot of intermediate steps and resulted in significant cost reduction and increased efficiency. So far, TDengine has been outstanding in cost reduction and performance improvement. We are looking forward to the day when it can support calls to AI models and output the results directly. That will be perfect!