Spring Boot 4 - Full Microservices Course - Home Energy Tracker - System Design (Part 1)
Welcome to the foundational system design session of the Home Energy Tracker project. This course covers building an enterprise-grade, event-driven microservices architecture using Spring Boot 4. Throughout this series, we will transition from raw mathematical requirements and capacity planning all the way to a robust distributed system design capable of ingesting millions of high-frequency telemetry events in real time.
1. Prerequisites and Technology Stack
To successfully follow along with this microservices implementation, certain baseline competencies and local development tools are required.
Core Prerequisites
- Java Object-Oriented Programming: Solid understanding of OOP principles (inheritance, polymorphism, encapsulation, and exception handling).
- Spring Boot Fundamentals: Familiarity with dependency injection, Spring MVC, REST controllers, and application configuration.
Required Tooling
- Java Development Kit (JDK): Modern LTS version compatible with Spring Boot 4.
- Build Tool: Maven or Gradle.
- Containerization: Docker and Docker Compose (for running localized infrastructure like MySQL, Apache Kafka, and time-series databases).
- Messaging Infrastructure: Apache Kafka.
- Databases: MySQL (relational data) and a Time-Series Database (e.g., TimeScaleDB or InfluxDB).
Note: The source code for this course is hosted on GitHub. Each distinct section of the course is tagged corresponding to its implementation milestone, allowing you to check out any specific commit tag if you experience issues or join the curriculum midway.
2. Electrical Energy Foundations and Calculations
Before designing database tables or writing microservices, we must understand the electrical domain we are modeling. Electrical units can often be confusing; let's break down the core equations.
Key Electrical Metrics
- Power ( or ): Indicates how much energy a device consumes per second (the instantaneous rate of energy consumption). For example, a hair dryer consumes of power per second.
- Energy (): Kilowatt-hour. This is the cumulative energy used over a specific duration of time (typically 1 hour).
- Utility Price (): The monetary rate charged by the electrical utility provider for each kilowatt-hour consumed.
Practical Calculation Example
Let's calculate the energy consumption and running cost for a hair dryer:
- 1-Hour Usage:
- 1-Minute Usage:
- Running Cost Calculation:
Assuming an electricity tariff of $0.30 per kWh:
- Cost for 1 Hour:
- Cost for 1 Minute:
These foundational formulas dictate how our Java applications will process sensor payloads and model device usage metrics.
3. System Requirements & Functional Scope
Our Home Energy Tracker microservices platform must satisfy several core functional requirements:
- Consumption Tracking: Track daily and hourly energy consumption aggregated per individual device and per room.
- Trend Analysis: Provide historical analytics and trends over customized time windows.
- Real-Time Anomaly Alerting: Allow users to configure usage thresholds, triggering instant high-consumption alerts when limits are breached.
- AI-Powered Recommendations: Integrate an AI service to deliver personalized energy-saving tips based on usage patterns.
- Real-Time IoT Ingestion: Accept high-frequency sensor readings in real time from millions of connected household appliances.
4. Capacity Planning & Load Estimation
To design an architecture that doesn't collapse under production loads, we must perform realistic back-of-the-envelope calculations based on our target scale assumptions.
Scale Assumptions
- Active Monthly Users: users
- Devices per User: Average of devices
- Telemetry Frequency: Each device sends 1 reading every minute
- Data Model: Each reading translates to 1 database write operation
Mathematical Derivations
- Total Active Devices:
- Readings Per Minute (System-Wide Ingestion Rate):
- Readings Per Day:
- Storage Footprint Calculation:
- Assume each reading record contains:
id,device_id,kilowatt_used, andtimestamp(~ per row). - Storage Required Per Day:
- Assume each reading record contains:
- Storage Required Per Month:
-
Applying a conservative 50% data compression rate reduces this to approximately .
-
Write Throughput (Write Operations Per Second):
Engineering Takeaway: Processing ~40,000 writes per second with payloads arriving every minute requires an asynchronous, decoupled event-driven streaming architecture rather than synchronous REST calls directly hitting a relational database.
5. Architectural Evolution: Naive Approach vs. Event-Driven Microservices
The Naive Monolithic / Direct-Write Trap
If our IoT devices sent HTTP requests directly to an API Gateway, which in turn executed synchronous JDBC inserts into a standard relational database (like MySQL):
- Database connection pools would exhaust almost instantly under 40,000 concurrent writes/sec.
- Network latency spikes or database locks would cause cascading failures up to the API Gateway and disconnect client IoT devices.
- Relational databases are poorly optimized for multi-billion-row time-series append workloads.
The Production-Grade Event-Driven Evolution
To solve this, we decouple our ingestion layer from our storage engine using Apache Kafka as a high-throughput distributed commit log, and leverage a specialized Time-Series Database (TSDB) for metrics persistence.
6. Comprehensive Microservices Architecture Walkthrough
Below is the complete architectural layout of our Home Energy Tracker system, illustrating how traffic flows from IoT sensors and web clients through our decoupled Spring Boot microservices.
7. Deep Dive Into Microservice Components
Let's examine the specific responsibilities of each microservice in our ecosystem:
8. Architectural Refinement: Why the Ingestion Service Matters
During initial system design, an engineer might be tempted to wire the API Gateway directly as a Kafka producer forwarding messages from IoT devices. However, this introduces architectural coupling:
- Gateway Bloat: The API Gateway should strictly handle cross-cutting concerns (routing, authentication, rate limiting, SSL termination). It should not contain data ingestion or event-publishing logic.
- Blast Radius Control: If Kafka experiences latency or broker degradation, a direct producer in the API Gateway would saturate gateway thread pools, crashing user web applications and device management endpoints alike.
- Dedicated Ingestion Tier: By isolating telemetry intake into a dedicated Ingestion Service, we can scale the ingestion tier independently from routing and management APIs.
9. The Alerting & Scheduling Workflow
The alert pipeline demonstrates how our system handles background processing and asynchronous notifications:
- Scheduled Evaluation: The
Usage Serviceruns background schedulers that periodically query aggregated time-series data against user-defined limits stored in theUser Service. - Event Dispatch: If a threshold violation is detected, the
Usage Servicepublishes an event to a dedicated Kafka alert topic. - Notification & Auditing: The
Alert Serviceconsumes the alert event, fetches recipient details from the relational database, triggers the email dispatch, and persists an immutable audit record into MySQL.
Summary and Key Takeaways
- Domain Understanding: Accurate energy tracking requires distinguishing between instantaneous power (/), cumulative energy (), and monetary tariffs ().
- Scale Realities: Supporting million devices sending telemetry every minute generates 3.6 billion rows per day, requiring robust capacity planning and high-throughput infrastructure.
- Throughput Demands: Processing 40,000 write operations per second makes synchronous relational database inserts impossible without crashing under load.
- Event-Driven Decoupling: Apache Kafka acts as a shock absorber between lightweight ingestion services and persistent data stores.
- Storage Strategy: Relational databases (MySQL) are ideal for transactional metadata (users, devices, audit logs), while Time-Series Databases (TimeScaleDB/InfluxDB) are essential for high-frequency metrics.
- Gateway Separation: The API Gateway must remain lightweight and act strictly as a reverse proxy, delegating heavy telemetry ingestion to dedicated microservices.
- Asynchronous Alerting: Background scheduler jobs combined with event topics allow decoupled alert services to evaluate thresholds and notify users without blocking real-time telemetry ingestion.