Kiến trúc hệ thống

Tổng quan kiến trúc phân tán của VieLang IoT — actor model, clustering, consistent hashing, và các thành phần cốt lõi

Kiến trúc hệ thống

VieLang IoT được xây dựng trên kiến trúc phân tán, horizontal scaling, sử dụng mô hình actor cho xử lý đồng thời cao, không có single point of failure.

Sơ đồ tổng quan

┌─────────────────────────────────────────────────────┐ │ VieLang IoT Node │ │ │ │ ┌──────────────┐ ┌──────────────────────────────┐ │ │ │ Transport │ │ Core Services │ │ │ │ ─────────── │ │ ────────────────────────── │ │ │ │ MQTT │ │ Device Management │ │ │ │ HTTP │ │ Rule Engine │ │ │ │ CoAP │ │ Tenant/Customer │ │ │ │ LwM2M │ │ Dashboard/Widget │ │ │ │ SNMP │ │ Alarm Management │ │ │ └──────┬───────┘ └──────────────┬───────────────┘ │ │ │ │ │ │ ┌──────▼─────────────────────────▼───────────────┐ │ │ │ Actor System (Tokio) │ │ │ │ App Actor → Tenant Actors → Device Actors │ │ │ └─────────────────────────────────────────────────┘ │ └────────────────────────┬────────────────────────────┘ │ ┌────────────────┼────────────────┐ ▼ ▼ ▼ PostgreSQL Redis Kafka/Queue (Relational) (Cache) (Message Queue)

Actor Model

Mỗi entity chạy như một actor độc lập — xử lý message bất đồng bộ, không chia sẻ state:

App Actor └── Tenant Actor (cho mỗi tenant đang hoạt động) ├── Device Actor (cho mỗi thiết bị đang kết nối) │ ├── Session Manager (kết nối MQTT/HTTP) │ └── Attribute Cache (L1 cache) ├── Rule Chain Actor (xử lý rule chain) │ └── Rule Node Actors (mỗi node là một actor) └── Alarm Actor (quản lý alarm state)

Lợi ích:

  • Không deadlock (không shared mutable state)
  • Fault isolation (actor lỗi không ảnh hưởng actor khác)
  • Backpressure tự nhiên (message queue per actor)

Clustering

Consistent Hashing

Khi có nhiều node, device được phân phối bằng consistent hashing dựa trên device ID:

Device-A → Hash → Node 1 Device-B → Hash → Node 2 Device-C → Hash → Node 1 Device-D → Hash → Node 3

Tất cả message từ một thiết bị luôn được route đến cùng một node → cache hit cao, không race condition.

Service Discovery

Nodes đăng ký và khám phá nhau qua etcd (hoặc Zookeeper):

Node 1: 192.168.1.10:8080 → registered Node 2: 192.168.1.11:8080 → registered Node 3: 192.168.1.12:8080 → registered

Khi node mới join hoặc node cũ crash → cluster tự rebalance.

RPC giữa các Node

Khi Device A (ở Node 1) nhận RPC request nhưng device target (Device B) đang ở Node 2:

Client → Node 1 (nhận request) Node 1 → Node 2 (gRPC forward) Node 2 → Device B (gửi RPC) Device B → Node 2 (response) Node 2 → Node 1 (gRPC return) Node 1 → Client (HTTP response)

Transport Layer

Mỗi giao thức chạy trên transport riêng:

ProtocolPortSecurity
MQTT1883Plain
MQTT + TLS8883TLS 1.3
HTTP8080Plain
HTTPS443TLS 1.3
CoAP5683Plain
CoAP + DTLS5684DTLS 1.3
LwM2M5685Plain
LwM2M + DTLS5686DTLS
WebSocket8080/wsUpgrade

Storage Layer

PostgreSQL (Primary DB)

Entities: devices, assets, tenants, customers, users Config: rule chains, dashboards, device profiles Events: alarms, audit logs, notifications Timeseries: telemetry (partitioned by time)

Cassandra (Optional — High-Scale Timeseries)

Dùng khi cần lưu hàng tỷ điểm dữ liệu:

Cluster: 3-5 nodes Replication factor: 3 Write throughput: ~1M events/sec per cluster

Redis (Cache)

Session tokens (JWT) Device attribute cache Rate limit counters Pub/Sub cho WebSocket

Rule Engine Architecture

MQTT Message Arrives ↓ Transport Service (parse, auth) ↓ Tenant Actor (routing) ↓ Device Actor (session state) ↓ Rule Chain Actor ├── Filter Node Actor ├── Enrichment Node Actor (đọc DB/cache) ├── Transform Node Actor └── Action Node Actor (ghi DB, gọi API) ↓ Storage (PostgreSQL/Cassandra)

Deployment Modes

Monolithic (Single Node)

docker-compose up

Phù hợp: development, demo, small production (<10K devices).

Microservices

core-service: Quản lý entity, REST API transport-service: MQTT, HTTP, CoAP rule-engine-service: Rule chain processing web-ui-service: Frontend

Scale từng service độc lập theo nhu cầu.

Kubernetes

# vielang-core: 3 replicas # vielang-transport: 5 replicas (xử lý MQTT) # vielang-rule-engine: 3 replicas

High Availability

Load Balancer (HAProxy / AWS ALB) ↓ Node 1 Node 2 Node 3 (VieLang IoT) ↓ PostgreSQL (Primary-Replica) + Redis Cluster

Không có single point of failure — mất 1 node, cluster tiếp tục hoạt động (thiết bị reconnect trong vài giây).