The IoT Engineer connects physical devices to cloud infrastructure and manages them at scale. IoT systems have constraints that don't exist in web applications: devices run on batteries (power budget matters), connect over unreliable networks (MQTT over cellular), and must be updated remotely without physical access. Security is more critical than in web apps - a compromised device is a physical object in the real world.
MQTT Architecture
// MQTT is the IoT standard: lightweight, runs over TCP, works on 2G/3G
// Topic structure: {fleet}/{deviceId}/{type}/{direction}
// Device publishes telemetry
const topicTelemetry = `fleet/${DEVICE_ID}/telemetry/up`;
// Broker routes messages; cloud subscribes
// Cloud publishes commands to device
const topicCommands = `fleet/${DEVICE_ID}/commands/down`;
// Node.js cloud subscriber (AWS IoT or Mosquitto)
import mqtt from 'mqtt';
const client = mqtt.connect('mqtts://your-broker.example.com', {
clientId: 'cloud-processor',
username: process.env.MQTT_USERNAME,
password: process.env.MQTT_PASSWORD,
// TLS client certificate (mutual TLS - prevents rogue devices)
cert: fs.readFileSync('cloud-client.pem'),
key: fs.readFileSync('cloud-client-key.pem'),
ca: fs.readFileSync('ca.pem'),
});
client.subscribe('fleet/+/telemetry/up'); // + = wildcard for any deviceId
client.on('message', async (topic, payload) => {
const deviceId = topic.split('/')[1];
const data = JSON.parse(payload.toString());
// Write to time-series DB (TimescaleDB or InfluxDB)
await timescaleDb.insert({
time: new Date(data.timestamp),
deviceId,
temperature: data.temperature,
humidity: data.humidity,
batteryVoltage: data.batteryVoltage,
});
// Alert if threshold exceeded
if (data.temperature > 80) {
await alertingService.send({ deviceId, alert: 'temperature_critical', value: data.temperature });
}
});
Device Provisioning and OTA Updates
## Device lifecycle: provisioning → active → update → decommission
Provisioning (factory / first boot):
1. Device generates unique device certificate (private key never leaves device)
2. Factory registers certificate with IoT backend
3. Device connects to bootstrap broker → receives production broker credentials
4. Device identity stored in device registry (AWS IoT Thing)
OTA Update Flow:
1. Cloud pushes new firmware version to S3
2. Cloud publishes update available message to device:
{ "version": "2.1.0", "url": "https://...", "sha256": "..." }
3. Device downloads firmware in chunks (verify SHA-256 after each chunk)
4. Device writes to inactive partition (A/B partition scheme)
5. Device reboots into new firmware
6. If boot fails within 3 attempts → roll back to previous partition
7. Device publishes update_complete or update_failed
Security rules:
- Each device can ONLY publish to its own topic (fleet/{its-id}/...)
- Each device can ONLY subscribe to its own command topic
- Broker enforces per-device ACLs from device registry
Time-Series Query Patterns
- Use TimescaleDB (Postgres + hypertables) or InfluxDB for time-series data - regular Postgres tables degrade badly beyond 100M rows of telemetry
- Downsampling: store raw data for 7 days, 1-minute aggregates for 90 days, hourly aggregates for 2 years - 95% of storage at 99% lower cost
- For dashboards, always query pre-aggregated data - never run raw queries across millions of rows for a chart
- Alert on device silence: if a device hasn't sent telemetry in 2× its expected interval, it's likely offline - trigger an alert