-
Notifications
You must be signed in to change notification settings - Fork 2
Backend Services and Event Processing
GiZano edited this page Aug 31, 2026
·
2 revisions
The backend is engineered to handle massive telemetry spikes (firehosing) during seismic swarms by decoupling ingestion from processing via Redis.
-
Rate Limiting: Sliding-window rate limiter in Redis (
ZADD/ZREMRANGEBYSCORE, 50 req/s per IP). -
Queue Offloading: Validated payloads are
XADDed to a Redis Stream (readings:stream), returning202 Acceptedin milliseconds (no blocking DB write).
The queue is a Redis Stream (not a List), enabling consumer groups:
-
Horizontal scale: Any number of producers
XADD; any number of workersXREADGROUP(docker compose scale worker=N). -
At-least-once:
XAUTOCLAIMreclaims pending entries after crashes. -
Dead Letter Stream:
readings:dlqparks poisoned messages. -
Backpressure:
MAXLEN ~200000, batch 64 with 500 ms block, singledb.commit()per batch. - Multi-tenancy: All keys (stream, group, DLQ) are env-repointable.
A decoupled worker.py loops XREADGROUP + XAUTOCLAIM:
- Shares an optimized SQLAlchemy pool for PostgreSQL.
- Stages a whole batch in-memory; on DB error routes the batch to the DLQ.
readings is a TimescaleDB hypertable on recorded_at (PostGIS still enabled via unified postgres-timescale image). timescale.py DDL is idempotent and fails closed on plain PostGIS. Continuous aggregate readings_minute serves dashboard rollups; compression + retention policies.
-
Zone management:
GET /zones,POST /zones/; seed of 8 global macro-regions +Unknown Region. -
Auto-assignment:
resolve_zonefirst checks Redis fast-pathzoneindex:<geohash>(prec 3), falls back to PostGISST_Containsordered byST_Area ASC. The cache is purely an optimization. -
Zone detection:
GET /zones/locate?latitude=&longitude=for mobile “Detect my zone”. -
Zone-scoped retrieval:
GET /zones/{zone_id}/readings,DELETE /zones/{zone_id}/readings,GET /zones/{zone_id}/alerts.
PGA_calib uses K_CALIBRATION=1.6, b=B_OFFSET=3.0 (shared with mobile).
-
Thresholding: Triggers
AlertifM ≥ 4.5. -
Per-area cooldown (v1.2.1): Atomic
SET nx=True ex=60keyed by area —alert_cooldown:<geohash>prec 4 (~39×19 km) whenlat/lonpresent, else zone. Stops overlapping macro-regions from silencing independent quakes.Reading.lat/loncaptured at ingestion. -
Outbox: First worker acquiring the lock persists
Alertand publishesquake_alertsto Redis Pub/Sub. -
AI enqueue: When
AI_REPORT_ENABLED=true, confirmed alerts push context toai_report_queue— see AI Emergency Report Service.
- Previous: Data Plane & Message Broker (MQTT)
- Next: Mobile Client & Live Telemetry
- Back to: Home