{"id":5430,"date":"2022-04-06T17:25:24","date_gmt":"2022-04-07T00:25:24","guid":{"rendered":"https:\/\/tdengine.com\/?p=5430"},"modified":"2025-11-03T08:09:00","modified_gmt":"2025-11-03T16:09:00","slug":"migrating-from-mongodb-to-tdengine-to-significantly-reduce-total-cost-of-ownership-for-a-livestock-iot-platform","status":"publish","type":"post","link":"https:\/\/tdengine.com\/migrating-from-mongodb-to-tdengine-to-significantly-reduce-total-cost-of-ownership-for-a-livestock-iot-platform\/","title":{"rendered":"Migrating from MongoDB to TDengine Significantly Reduces TCO"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<h2 class=\"gb-headline gb-headline-505a1303 gb-headline-text\">Architecture and Implementation<\/h2>\n\n\n\n<p>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 <a href=\"https:\/\/tdengine.com\/what-is-a-time-series-database\/\" target=\"_blank\" rel=\"noreferrer noopener\">time series databases<\/a> (TSDB) such as InfluxDB and TDengine. After building the test environment, we tested the two respectively, and finally settled on TDengine. <strong>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.<\/strong><\/p>\n\n\n\n<p>Based on the design principle of the <a href=\"https:\/\/tdengine.com\/supertable\/\">supertable<\/a>, 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 subtable, and the device&#8217;s measurements are used as columns in the subtable.<\/p>\n\n\n\n<p>The supertable schema is shown in Figure 1 below. Figures 2 and 3 show the supertable 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.<\/p>\n\n\n\n<figure class=\"gb-block-image gb-block-image-525ce188\"><img decoding=\"async\" width=\"871\" height=\"466\" class=\"gb-image gb-image-525ce188\" src=\"https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-01-schema-1.jpg?strip=all&sharp=1\" alt=\"\" srcset=\"https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-01-schema-1.jpg?strip=all&amp;sharp=1 871w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-01-schema-1-300x161.jpg?strip=all&amp;sharp=1 300w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-01-schema-1-768x411.jpg?strip=all&amp;sharp=1 768w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-01-schema-1.jpg?strip=all&amp;sharp=1&amp;w=174 174w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-01-schema-1.jpg?strip=all&amp;sharp=1&amp;w=522 522w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-01-schema-1.jpg?strip=all&amp;sharp=1&amp;w=696 696w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-01-schema-1.jpg?strip=all&amp;sharp=1&amp;w=450 450w\" sizes=\"(max-width: 871px) 100vw, 871px\" \/><\/figure>\n\n\n\n<figure class=\"gb-block-image gb-block-image-2a220298\"><img decoding=\"async\" width=\"861\" height=\"239\" class=\"gb-image gb-image-2a220298\" src=\"https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-02-schema-2.png?strip=all&sharp=1\" alt=\"\" srcset=\"https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-02-schema-2.png?strip=all&amp;sharp=1 861w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-02-schema-2-300x83.png?strip=all&amp;sharp=1 300w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-02-schema-2-768x213.png?strip=all&amp;sharp=1 768w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-02-schema-2.png?strip=all&amp;sharp=1&amp;w=172 172w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-02-schema-2.png?strip=all&amp;sharp=1&amp;w=516 516w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-02-schema-2.png?strip=all&amp;sharp=1&amp;w=688 688w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-02-schema-2.png?strip=all&amp;sharp=1&amp;w=450 450w\" sizes=\"(max-width: 861px) 100vw, 861px\" \/><\/figure>\n\n\n\n<figure class=\"gb-block-image gb-block-image-f3e6bcc6\"><img decoding=\"async\" width=\"889\" height=\"231\" class=\"gb-image gb-image-f3e6bcc6\" src=\"https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-03-schema-3.png?strip=all&sharp=1\" alt=\"\" srcset=\"https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-03-schema-3.png?strip=all&amp;sharp=1 889w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-03-schema-3-300x78.png?strip=all&amp;sharp=1 300w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-03-schema-3-768x200.png?strip=all&amp;sharp=1 768w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-03-schema-3.png?strip=all&amp;sharp=1&amp;w=177 177w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-03-schema-3.png?strip=all&amp;sharp=1&amp;w=355 355w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-03-schema-3.png?strip=all&amp;sharp=1&amp;w=533 533w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-03-schema-3.png?strip=all&amp;sharp=1&amp;w=711 711w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-03-schema-3.png?strip=all&amp;sharp=1&amp;w=450 450w\" sizes=\"(max-width: 889px) 100vw, 889px\" \/><\/figure>\n\n\n\n<p>At present, all the IoT data tables of our company are designed based on the TDengine supertable. The core livestock supertable, 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 supertable design also allows us to trace possible processing delays and other problems. <\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<figure class=\"gb-block-image gb-block-image-e4290c7b\"><img decoding=\"async\" width=\"1292\" height=\"992\" class=\"gb-image gb-image-e4290c7b\" src=\"https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-04-architecture.png?strip=all&sharp=1\" alt=\"\" srcset=\"https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-04-architecture.png?strip=all&amp;sharp=1 1292w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-04-architecture-300x230.png?strip=all&amp;sharp=1 300w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-04-architecture-1024x786.png?strip=all&amp;sharp=1 1024w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-04-architecture-768x590.png?strip=all&amp;sharp=1 768w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-04-architecture.png?strip=all&amp;sharp=1&amp;w=516 516w, https:\/\/eujqw4hwudm.exactdn.com\/wp-content\/uploads\/22.022-04-architecture.png?strip=all&amp;sharp=1&amp;w=450 450w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure>\n\n\n\n<h2 class=\"gb-headline gb-headline-1fd2a084 gb-headline-text\">Data Migration and Ramifications<\/h2>\n\n\n\n<p>Since we were replacing MongoDB, we had to perform a data migration as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Newly generated collector data was written into MongoDB as well as TDengine.<\/li>\n\n\n\n<li>Historical data was gradually migrated into TDengine.<\/li>\n\n\n\n<li>The data source for AI analysis was changed from MySQL data warehouse, to TDengine.<\/li>\n<\/ul>\n\n\n\n<p>During the migration process, we also encountered two small issues:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>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.<\/li>\n\n\n\n<li>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.<\/li>\n<\/ul>\n\n\n\n<p>The ramifications after the migration are very clear. When we used MongoDB, the self-built cluster used six, 4-core, 32G servers<strong>. 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.<\/strong> 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 subtables, and only some businesses need to view aggregated operational data. When querying group by+last_row(*) on the supertable, the data can be returned within 1.5s, and querying on the subtable in about 0.1 seconds (select * from son_table limit 10). This meets our business requirements.<\/p>\n\n\n\n<h2 class=\"gb-headline gb-headline-56216660 gb-headline-text\">Closing Thoughts<\/h2>\n\n\n\n<p>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&#8217;s stream computing and <a href=\"https:\/\/tdengine.com\/high-performance\/\" target=\"_blank\" rel=\"noreferrer noopener\">high-performance<\/a> 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!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how TDengine replaced MongoDB and delivered cost reduction and increased efficiency in a livestock management scenario.<\/p>\n","protected":false},"author":81,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[23],"tags":[],"ppma_author":[167],"class_list":["post-5430","post","type-post","status-publish","format-standard","hentry","category-case-studies"],"authors":[{"term_id":167,"user_id":81,"is_guest":0,"slug":"chait","display_name":"Chait Diwadkar","avatar_url":{"url":"https:\/\/tdengine.com\/wp-content\/uploads\/29.03-05-cdiwadkar.jpg","url2x":"https:\/\/tdengine.com\/wp-content\/uploads\/29.03-05-cdiwadkar.jpg"},"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/posts\/5430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/comments?post=5430"}],"version-history":[{"count":12,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/posts\/5430\/revisions"}],"predecessor-version":[{"id":29455,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/posts\/5430\/revisions\/29455"}],"wp:attachment":[{"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/media?parent=5430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/categories?post=5430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/tags?post=5430"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/tdengine.com\/wp-json\/wp\/v2\/ppma_author?post=5430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}