Rust MQTT ingestion worker that batches writes into PostgreSQL.
- Subscribes to configurable MQTT topic filters.
- Parses payload into JSON (
payloadcolumn). - Extracts simple values into
value_numericorvalue_textwhen possible. - Batches inserts into
mqtt_messages. - Exposes
/healthz,/readyz, and/metricsfor operations.
- MQTT broker -> Rust worker -> Postgres
- Internal bounded queue for backpressure.
- Timer-based and size-based batch flush.
- Retry with exponential backoff for transient SQL errors.
Copy and edit .env.example.
Required:
MQTT_URLMQTT_TOPICSMQTT_NAME_PROPERTY(defaults toname)MQTT_SENSOR_VALUE_PROPERTY(defaults tosensor_value)DATABASE_URL(orPG_HOST,PG_PORT,PG_DB,PG_USER,PG_PASSWORD)
Optional:
MQTT_USERNAME,MQTT_PASSWORD,MQTT_CLIENT_IDPG_SCHEMA(defaultpublic)PG_TABLE(defaultmqtt_messages)BATCH_SIZE(default100)BATCH_INTERVAL_MS(default500)QUEUE_CAPACITY(default10000)MAX_INSERT_RETRIES(default5)METRICS_ADDR(default0.0.0.0:8080)
Create the target table first:
psql "$DATABASE_URL" -f schema.sqlcargo run --releaseGET /healthz-> process aliveGET /readyz-> MQTT connected and Postgres reachableGET /metrics-> Prometheus text format
The ingestor looks for a sensor name field and a sensor value field in each topic payload. Messages that do not contain the configured sensor value field are skipped. On first startup, retained messages are used to build an allowlist of sensor IDs, where the sensor ID is the last path segment of the topic.
docker build -t mqtt-postgres:latest .
docker run --rm -p 8080:8080 --env-file .env mqtt-postgres:latestPush a tag like v1.2.3 to publish a new image to GHCR at:
ghcr.io/<owner>/mqtt-postgres
The workflow also publishes latest for tagged releases.
Manifest is in k8s/deployment.yaml.
kubectl apply -f k8s/deployment.yamlReplace the image and secret values for your environment.
Heavy use of copilot was used to write this code.