Resilience Layer
Failure Scenarios
RAFIQ is designed to fail gracefully. Every failure mode has a documented detection mechanism, an immediate fallback, and a recovery path. The system never enters an undefined state — every error is a known, handled scenario.
Internet / Cloud Failure
HTTP timeout to Supabase health endpoint (5s timeout, 3 retries)
Sync engine switches to offline queue mode. All writes continue locally. Cloud push paused.
Auto-reconnect with exponential backoff. On reconnect: drain queue in priority order (Critical → Low).
RTO: < 30s to detect, instant fallback RPO: 0 — no data lost
MQTT Broker Failure
Client disconnect callback fired. Core engine loses MQTT connection.
Core engine switches to direct API polling mode. Wearable data buffered at gateway. HA commands via direct REST.
Mosquitto auto-restarts via systemd. Client reconnects with clean session. Re-subscribes all topics.
RTO: < 10s (systemd auto-restart) RPO: Low — buffered at gateway
AI Inference Timeout
AI orchestrator enforces 5s timeout per inference call. Alert if >3 consecutive timeouts.
Rules engine takes over health analysis. Predefined threshold rules replace LLM reasoning. Simpler but reliable.
Ollama watchdog restarts the process. Model reloaded from disk (GGUF). Inference resumes in ~30s.
RTO: < 35s to restart RPO: 0 — rules engine covers the gap
SQLite Corruption
PRAGMA integrity_check on startup and hourly. Any non-"ok" result triggers alert.
Read-only mode from WAL snapshot. Writes buffered in memory. Emergency data preserved.
Restore from last Supabase full dump. Apply local WAL changes on top. Re-sync from cloud.
RTO: < 5min with recent cloud backup RPO: Seconds to minutes depending on last sync
Wearable BLE Disconnect
BLE RSSI drop below -90dBm + connection timeout (5s). LWT message on MQTT.
Last known vitals cached and used for 5 minutes. Emergency engine continues with cached values.
BLE gateway auto-scans for device every 5s. Reconnects and re-subscribes GATT notifications.
RTO: < 15s to reconnect if in range RPO: 0 — no reads lost, stale data clearly flagged
Home Assistant Crash
WebSocket connection to HA drops. HTTP health check fails.
Direct MQTT device control bypasses HA. Core publishes to device topics directly.
HA service auto-restarts via systemd. Core reconnects WebSocket and re-syncs device states.
RTO: < 30s (systemd restart) RPO: 0 — direct MQTT control active
ESP32 Emergency MCU Offline
MQTT LWT message from Raqeeb MCU. No heartbeat for 30s.
Core engine polls sensors directly via alternative I2C bus. Emergency engine continues with direct hardware access.
ESP32 watchdog timer auto-resets the MCU (10s). MQTT LWT cleared on reconnect.
RTO: < 15s (watchdog reset) RPO: 0 — direct sensor polling active
Memory Pressure (RAM > 85%)
System monitor checks RAM every 60s. Alert at 75%, action at 85%.
Evict LLM model cache. Reduce AI context window. Restart non-critical services (voice pipeline, caches).
Services restart with lower memory profile. Swap space used as emergency buffer if needed.
RTO: < 60s to restore headroom RPO: 0 — no data affected
Failure Matrix
All 8 scenarios are auto-recovering — no manual intervention required
| Failure | Severity | RTO (Recovery Time) | RPO (Data Loss) |
|---|---|---|---|
| Internet / Cloud Failure | High | < 30s to detect, instant fallback | 0 — no data lost |
| MQTT Broker Failure | High | < 10s (systemd auto-restart) | Low — buffered at gateway |
| AI Inference Timeout | Medium | < 35s to restart | 0 — rules engine covers the gap |
| SQLite Corruption | Critical | < 5min with recent cloud backup | Seconds to minutes depending on last sync |
| Wearable BLE Disconnect | Medium | < 15s to reconnect if in range | 0 — no reads lost, stale data clearly flagged |
| Home Assistant Crash | Medium | < 30s (systemd restart) | 0 — direct MQTT control active |
| ESP32 Emergency MCU Offline | Critical | < 15s (watchdog reset) | 0 — direct sensor polling active |
| Memory Pressure (RAM > 85%) | Medium | < 60s to restore headroom | 0 — no data affected |