Edge & Cloud Engineer Guide: Resilient Linux IoT Daemons & SQLite WAL
Edge computing appliances—running headless Debian/Ubuntu or Yocto Linux on Raspberry Pi CM4, NVIDIA Jetson, or industrial x86 gateways—operate in harsh environments with sudden power loss and intermittent LTE/Starlink connectivity. A Python daemon that buffers telemetry in RAM or writes unjournaled JSON files to an SD card will corrupt its state on the first power cycle. As a Senior Cloud, Backend, and Edge Engineer, here is how I build self-healing Linux edge daemons with SQLite WAL local spooling, `systemd` hardware watchdogs, and idempotent cloud synchronization.
1. Power-Loss-Safe Local Telemetry Spooling with SQLite `WAL` Mode
By persisting sensor readings into a local SQLite database configured with `PRAGMA journal_mode=WAL;` and `PRAGMA synchronous=NORMAL;`, the edge daemon can buffer hundreds of thousands of events during a 48-hour internet outage and flush them in compressed batches once the cloud link recovers:
import sqlite3
import json
import time
class EdgeTelemetrySpool:
def __init__(self, db_path: str = "/var/lib/edge-agent/spool.db") -> None:
self.conn = sqlite3.connect(db_path, isolation_level=None)
self.conn.execute("PRAGMA journal_mode=WAL;")
self.conn.execute("PRAGMA synchronous=NORMAL;")
self.conn.execute(
"""
CREATE TABLE IF NOT EXISTS telemetry_outbox (
seq_id INTEGER PRIMARY KEY AUTOINCREMENT,
created_at_ms INTEGER NOT NULL,
payload_json TEXT NOT NULL
)
"""
)
def enqueue(self, payload: dict) -> int:
cur = self.conn.execute(
"INSERT INTO telemetry_outbox (created_at_ms, payload_json) VALUES (?, ?)",
(int(time.time() * 1000), json.dumps(payload, separators=(",", ":"))),
)
return int(cur.lastrowid or 0)
def pop_batch(self, limit: int = 200) -> list[tuple[int, str]]:
cur = self.conn.execute(
"SELECT seq_id, payload_json FROM telemetry_outbox ORDER BY seq_id ASC LIMIT ?",
(limit,),
)
return list(cur.fetchall())
def ack_up_to(self, max_seq_id: int) -> None:
self.conn.execute("DELETE FROM telemetry_outbox WHERE seq_id <= ?", (max_seq_id,))
2. `systemd` `sd_notify` Watchdogs & Read-Only Root Filesystems
Our daemon pings the Linux `systemd` watchdog (`WATCHDOG_USEC`) only after verifying that both the sensor acquisition loop and SQLite writer are healthy—automatically restarting a hung process within 15 seconds without human intervention.
3. Hire a Senior Cloud, Backend & Edge Systems Engineer
Deploying headless Linux gateways, IoT fleets, or edge-to-cloud synchronization pipelines? Explore my Cloud Engineer Pillar Guide, review my Edge Gateway Integration Testing Guide, or book a 1-Day Edge & Cloud Sync Sprint.