The 1.2M Events/Sec Ingestion Challenge #
Fleet management and smart vehicle platforms generate continuous streams of high-frequency GPS, engine diagnostics (OBD-II), fuel temperature, and speed telematics. Traditional relational databases crumble under this write velocity due to transaction log lock contention and B-Tree index rebalancing.
To sustain continuous **1.2 million events per second write throughput** while supporting sub-50ms fleet analytics dashboards, we implemented a three-tier architecture:
1. **Edge Ingestion Gateway (Go):** Stateless WebSocket and MQTT listener clusters terminating connections and parsing binary protocol buffers. 2. **Distributed Message Backbone (Apache Kafka):** 24-partition topic cluster grouped by `device_region_id` with LZ4 compression. 3. **Columnar Analytical Store (ClickHouse):** Sharded `ReplicatedReplacingMergeTree` cluster with memory buffers.
---
Zero-Loss Kafka Partitioning & Buffer Architecture #
The Go ingestion microservice batches incoming telemetry frames in memory buffers for 100ms before publishing vectorized payloads to Kafka:
package main
import (
"context"
"time"
"github.com/segmentio/kafka-go"
)
type TelemetryBatcher struct {
writer *kafka.Writer
buffer chan kafka.Message
}
func (b *TelemetryBatcher) FlushLoop(ctx context.Context) {
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
var batch []kafka.Message
for {
select {
case msg := <-b.buffer:
batch = append(batch, msg)
if len(batch) >= 5000 {
_ = b.writer.WriteMessages(ctx, batch...)
batch = batch[:0]
}
case <-ticker.C:
if len(batch) > 0 {
_ = b.writer.WriteMessages(ctx, batch...)
batch = batch[:0]
}
case <-ctx.Done():
return
}
}
}
---
ClickHouse AggregatingMergeTree Table Optimization #
ClickHouse stores data column-by-column on disk, enabling up to 90% compression ratios and multi-gigabyte per second sequential scan speeds:
CREATE TABLE default.vehicle_telemetry_raw (
tenant_id UUID,
vehicle_id LowCardinality(String),
timestamp DateTime64(3, 'UTC'),
latitude Float64,
longitude Float64,
speed_kmh Float32,
fuel_level_pct UInt8,
engine_temp_c Int16
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/telemetry', '{replica}')
PARTITION BY toYYYYMM(timestamp)
ORDER BY (tenant_id, vehicle_id, timestamp);
This architecture powers live geofencing alerts, idle-time analytics, and route playback across 45,000 active commercial trucks with zero server strain.