Skip to Content
🚀 KalamDB v0.3.0-alpha2 is out — Learn more
Use Cases & ExamplesIoT & Monitoring

IoT Sensor Data & Monitoring

This pattern handles bursty event streams and immediate alerting without long-lived hot retention.

Core pattern

  • STREAM for short-lived raw readings (TTL_SECONDS)
  • SHARED for persisted rollups/aggregates
  • live subscriptions for threshold detection
  • optional topic worker for downstream alert pipelines

Example schema

CREATE TABLE iot.sensor_data ( event_id BIGINT PRIMARY KEY DEFAULT SNOWFLAKE_ID(), sensor_id TEXT NOT NULL, temperature DOUBLE, humidity DOUBLE, timestamp TIMESTAMP DEFAULT NOW() ) WITH (TYPE = 'STREAM', TTL_SECONDS = 10); -- Aggregated metrics (persisted) CREATE TABLE iot.metrics ( sensor_id TEXT PRIMARY KEY, avg_temp DOUBLE, max_temp DOUBLE, min_temp DOUBLE, hour TIMESTAMP ) WITH (TYPE = 'SHARED', FLUSH_POLICY = 'interval:3600');

Example implementation

await client.subscribeWithSql( 'SELECT * FROM iot.sensor_data WHERE temperature > 80 OR humidity > 95', (event) => { if (event.type === 'change' && event.change_type === 'insert') { triggerAlert(event.rows); } } );

Code references

Last updated on