## Web Cluster Architecture

Overall architecture:

<img src="../../../../cluster/v2/images/web-cluster-architecture/web-cluster-1.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" loading="lazy" alt="Web cluster architecture overview diagram" />

Detailed architecture:

<img src="../../../../cluster/v2/images/web-cluster-architecture/web-cluster-2.png" style="border:1px solid #ccc;border-radius:6px;margin:8px 0;" loading="lazy" alt="Web cluster architecture detailed diagram" />

## 1. Client Layer

* Browsers, mobile apps, and third-party systems access the platform through HTTP/HTTPS.

## 2. Load Balancing Layer

* Nginx cluster:
  * Reverse proxy and backend load balancing.
  * SSL termination and static resource caching.
  * Horizontal scaling for high availability.

## 3. Application Service Layer

* Spring Boot service cluster:
  * Provides RESTful APIs and handles business logic.
  * Multiple instances are deployed horizontally and balanced through Nginx.

## 4. Message Queue Layer

* Kafka cluster:
  * Topics can be split into multiple partitions, such as `Partition 1` and `Partition 2`.
  * Consumes device data from the FMQ cluster, including MQTT/TCP/UDP service clusters.
  * Device gateway services write data into Kafka.
  * Spring Boot services consume Kafka data and process device commands, alarms, and other business events.

## 5. Data Processing Layer

* Rule engine:
  * LiteFlow executes scene-automation rules such as device linkage and alarm triggers.
  * Ignite stores and manages LiteFlow rule configuration and supports dynamic updates.
* Distributed scheduling:
  * Quartz cluster runs Modbus polling jobs and coordinates cluster tasks through database locks.

## 6. Cache and Metadata Layer

* Distributed cache:
  * Ignite cluster caches device metadata such as device IDs, status, and configuration.
  * Integration with Spring Boot provides low-latency metadata access.
* Redis primary-replica architecture:
  * The primary node handles writes such as session state and real-time counters.
  * Replica nodes handle reads and can be combined with Sentinel for automatic failover.

## 7. Data Storage Layer

* Relational database:
  * MySQL primary-replica architecture, where the primary handles transactional writes and replicas handle queries.
* Time-series database:
  * TDengine stores historical device data such as temperature and humidity and supports efficient time-series queries.

## 8. Device Access Layer

* Protocol gateway:
  * Supports MQTT/TCP/UDP protocol access.
  * Forwards device data to the Kafka cluster.

## Key Design Points

1. High availability: critical components such as Nginx, Kafka, MySQL, and Redis use cluster or primary-replica deployment.
2. Data flow: device data -> MQTT/TCP/UDP gateway -> Kafka -> Spring Boot services -> TDengine/MySQL.
3. Performance: Ignite caches device metadata to reduce MySQL pressure, and TDengine optimizes time-series storage and queries.
4. Scalability: Kafka partitions and Nginx load balancing provide horizontal scaling.
