Skip to content

Backend Services and Event Processing

GiZano edited this page Aug 5, 2026 · 1 revision

Backend Services & Event Processing

API Gateway

  • Rate Limiting: Protects against DoS via a sliding-window rate limiter in Redis (50 req/s per IP).
  • Queue Offloading: Validated payloads are non-blockingly serialized and pushed to a Redis List (seismic_events), returning a 202 Accepted immediately.

Background Worker & Persistence

A decoupled Python worker (worker.py) consumes the seismic_events queue via a blocking brpop.

  • Shares a highly optimized SQLAlchemy connection pool targeting PostgreSQL/PostGIS.
  • Evaluates the alarm logic within a single, atomic database transaction (db.commit()).

Magnitude Estimation & Deduplication

The worker estimates physical magnitude based on a MyShake-style MEMS calibration approach:

$$ M_{IoT} = \log_{10}(PGA_{calib}) + b $$

Where $PGA_{calib}$ accounts for the hardware calibration constant (K_CALIBRATION = 1.6), and $b$ is an empirical offset (B_OFFSET = 3.0).

  • Thresholding: Triggers an Alert if the magnitude reaches or exceeds 4.5.
  • Redis Deduplication: Uses an atomic check-and-set (SET nx=True, ex=60) keyed by zone_id to enforce a 60-second cooldown per zone, preventing notification spam during a swarm.
  • Outbox Pattern: Publishes the JSON payload to the quake_alerts Redis Pub/Sub channel.
  • AI Enqueue: When enabled, confirmed alerts additionally enqueue context to the AI Emergency Report Service.

Clone this wiki locally