Implementing Energy Management with TDengine: Architecture, Connector Choice, and Performance Evaluation
This article presents a detailed case study of using TDengine as the core time‑series database for a private‑cloud energy‑management platform, covering project background, overall architecture, connector selection, Java RESTful implementation, performance testing, deployment strategy, and future technical expectations.
The project addresses Shuanghui Development's need for fine‑grained energy monitoring across multiple factories by building a private‑cloud platform that collects, stores, and analyzes meter data in real time, reducing per‑unit energy consumption and improving economic efficiency.
The overall architecture consists of IaaS, PaaS, and SaaS layers, with the energy‑collection subsystem at its core. TDengine serves as the central time‑series engine, accessed via RESTful APIs for high‑throughput data insertion and fast query capabilities, supporting dashboards, alerts, and predictive analytics.
TDengine Key Applications
1. Connector Selection – Although JDBC offers strong performance, the team chose the RESTful connector for its simplicity, container‑friendly deployment, and adequate throughput for the expected ~7,000 messages per minute.
2. RESTful Code Implementation
① ThreadPoolExecutor thread pool
ExecutorService pool = new ThreadPoolExecutor(150, 300, 1000, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue
(100), Executors.defaultThreadFactory(),
new ThreadPoolExecutor.DiscardOldestPolicy());② OKHttp thread pool
public ConnectionPool pool() {
return new ConnectionPool(20, 5, TimeUnit.MINUTES);
}③ Asynchronous RESTful insertion
public void excuteTdengineWithSqlAsync(String sql, Callback callback) {
try {
MediaType mediaType = MediaType.parse("application/octet-stream");
RequestBody body = RequestBody.create(mediaType, sql);
Request request = new Request.Builder()
.url(tdengineHost)
.post(body)
.addHeader("Authorization", "Basic cm9vdDp0YW9zZGF0YQ==")
.addHeader("cache-control", "no-cache")
.build();
mOkHttpClient.newCall(request).enqueue(callback);
} catch (Exception e) {
logger.error("执行tdengine操作报错:" + e.getMessage());
}
}③ Java Docker image
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-XX:MaxRAM=2000m","-Xms1024m","-jar","/app.jar"]3. Performance Testing
In production, the RESTful interface achieved sub‑millisecond insert latency for single rows and sub‑10 ms response for LAST_ROW queries on billions of records; batch inserts of 100–200 rows per request comfortably handled 2,000 requests per second. Simulated tests confirmed similar performance under higher loads.
4. Implementation Plan
The solution deploys separate TDengine supertables for water, electricity, steam, and gas meters across six factories, each with tags for factory ID, meter level, process, and meter ID. Supporting clusters include EMQX for MQTT, Redis for connection‑state caching, and the TDengine cluster for storage.
5. Technical Expectations
TDengine’s low resource footprint and built‑in messaging and stream‑processing make it suitable for small‑to‑medium IoT scenarios without additional Kafka/Flink stacks, while still coexisting with them in larger deployments. Future work aims to integrate edge‑side protocols (Modbus, OPC‑UA) and tighter Kubernetes edge ecosystems.
6. System UI
Energy consumption is calculated by querying cumulative columns with time‑windowed aggregations, e.g.:
select last(cumulative) as max_val, first(cumulative) as min_val from supertable
where tags ... and ts >= now-10h interval(1h) group by meter_id;The responsive PC and H5 mobile dashboards demonstrate sub‑100 ms query latency even with billions of rows.
Conclusion
Adopting TDengine simplified the stack, reduced operational complexity, and delivered superior insertion and query performance for Shuanghui’s energy‑management use case, confirming its suitability for similar industrial IoT projects.
DataFunSummit
Official account of the DataFun community, dedicated to sharing big data and AI industry summit news and speaker talks, with regular downloadable resource packs.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.