# NOTIFY/LISTEN ARCHITECTURE DIAGRAM ## Channel Routing Flow ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ PostgreSQL NOTIFY/LISTEN │ │ Hot-Reload Configuration System │ └─────────────────────────────────────────────────────────────────────────────┘ ┌──────────────┐ │ config_ │ │ settings │ │ table │ └──────┬───────┘ │ ┌────────────────┴────────────────┐ │ INSERT / UPDATE / DELETE │ │ (tr_config_settings_notify) │ └────────────────┬────────────────┘ │ ▼ ┌────────────────────────┐ │ notify_config_change() │ │ (TRIGGER FUNCTION) │ └────────┬───────────────┘ │ ┌──────────────────┼──────────────────┐ │ │ │ ▼ ▼ ▼ Extract category Build JSON Determine from config_key payload service name (split on '.') (CASE logic) │ │ │ └──────────────────┴──────────────────┘ │ ▼ ┌──────────────────────┐ │ Route to Channel(s) │ └──────────┬───────────┘ │ ┌──────────────────────────────┼──────────────────────────────┐ │ │ │ ▼ ▼ ▼ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │ config_changed│ │ config_changed│ │ config_changed│ │ _trading │ │ _backtesting │ │ _ml_training │ └───────┬───────┘ └───────┬───────┘ └───────┬───────┘ │ │ │ ▼ ▼ ▼ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐ │ Trading │ │ Backtesting │ │ ML Training │ │ Service │ │ Service │ │ Service │ │ (Rust) │ │ (Rust) │ │ (Rust) │ └───────────────┘ └───────────────┘ └───────────────┘ - Risk mgmt - Strategy sim - Model training - Execution - Backtest runs - Inference - Compliance - Model cache │ ▼ │ ▼ ┌───────────────┐ ▼ ┌───────────────┐ │ config_changed│ ┌───────────────┐ │ config_changed│ │ _api_gateway │ │ config_changed│ │ _global │ └───────┬───────┘ │ _global │ └───────┬───────┘ │ └───────┬───────┘ │ ▼ │ │ ┌───────────────┐ │ │ │ API Gateway │ │ │ │ Service │ │ │ │ (Rust) │ │ │ └───────────────┘ │ │ - Auth/JWT │ │ - Rate limiting │ │ - RBAC │ │ │ └────────────────────────┬───────────────────────────────┘ │ ▼ ┌───────────────┐ │ Monitoring │ │ Dashboard │ │ (All changes) │ └───────────────┘ ``` --- ## RBAC NOTIFY Flow ``` ┌─────────────────────────────────────────────────────────────────────────────┐ │ RBAC Permission Change Events │ └─────────────────────────────────────────────────────────────────────────────┘ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ roles │ │ permissions │ │ role_ │ │ user_roles │ │ table │ │ table │ │ permissions │ │ table │ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │ │ │ │ │ INSERT/UPDATE/ │ INSERT/UPDATE/ │ INSERT/UPDATE/ │ INSERT/UPDATE/ │ DELETE │ DELETE │ DELETE │ DELETE │ │ │ │ └──────────────────┴──────────────────┴──────────────────┘ │ ▼ ┌─────────────────────────┐ │ notify_permission_ │ │ change() │ │ (TRIGGER FUNCTION) │ └─────────┬───────────────┘ │ │ Build JSON payload: │ - operation (INSERT/UPDATE/DELETE) │ - table name │ - role_id, permission_id, user_id │ - timestamp │ ▼ ┌─────────────────────────┐ │ pg_notify() │ │ 'permissions_changed' │ └─────────┬───────────────┘ │ ┌───────────────────┴───────────────────┐ │ │ ▼ ▼ ┌───────────────┐ ┌───────────────┐ │ API Gateway │ │ config_changed│ │ Service │ │ _global │ └───────┬───────┘ └───────┬───────┘ │ │ │ Actions: │ │ 1. Invalidate permission cache │ │ 2. Reload RBAC rules │ │ 3. Update in-memory ACL │ │ ▼ │ ┌───────────────┐ │ │ Monitoring │ │ │ Dashboard │ └──────────────────────────────┴───────────────┘ │ ▼ ┌──────────────────┐ │ Audit Log │ │ - Who changed │ │ - What changed │ │ - When changed │ └──────────────────┘ ``` --- ## Channel Routing Logic ### Config Key → Channel Mapping | Config Key Prefix | Channel | Service | Example Keys | |-------------------|---------|---------|--------------| | `risk.*` | `config_changed_trading` | Trading Service | `risk.max_daily_loss` | | `compliance.*` | `config_changed_trading` | Trading Service | `compliance.sox_enabled` | | `execution.*` | `config_changed_trading` | Trading Service | `execution.slippage_tolerance` | | `circuit.*` | `config_changed_trading` | Trading Service | `circuit.activation_threshold` | | `strategy.*` | `config_changed_backtesting` | Backtesting Service | `strategy.lookback_days` | | `backtest.*` | `config_changed_backtesting` | Backtesting Service | `backtest.data_window` | | `ml.*` | `config_changed_ml_training` | ML Training Service | `ml.model_cache_ttl` | | `training.*` | `config_changed_ml_training` | ML Training Service | `training.batch_size` | | `model.*` | `config_changed_ml_training` | ML Training Service | `model.mamba2.lr` | | `auth.*` | `config_changed_api_gateway` | API Gateway | `auth.jwt_expiry` | | `api.*` | `config_changed_api_gateway` | API Gateway | `api.rate_limit_rpm` | | `mfa.*` | `config_changed_api_gateway` | API Gateway | `mfa.enforce_for_roles` | | `system.*` | `config_changed_global` | All Services | `system.latency_target_ns` | | `s3.*` | `config_changed_global` | All Services | `s3.bucket_name` | | `*` (unknown) | `config_changed_global` | All Services | Fallback for unknown | --- ## Payload Structure Examples ### Config Change Payload ```json { "operation": "UPDATE", "table": "config_settings", "key": "risk.max_daily_loss", "value": "100000", "old_value": "50000", "category": "risk", "timestamp": 1730000000.123, "id": "550e8400-e29b-41d4-a716-446655440000" } ``` ### Permission Change Payload ```json { "operation": "INSERT", "table": "role_permissions", "timestamp": 1730000000.123, "role_id": "550e8400-e29b-41d4-a716-446655440001", "permission_id": "550e8400-e29b-41d4-a716-446655440002", "user_id": null } ``` --- ## Multi-Service Notification Example ### Model Configuration Change ``` ┌──────────────┐ │ model_config │ │ table │ └──────┬───────┘ │ UPDATE is_active = true │ WHERE name = 'mamba2' │ ▼ ┌────────────────────────┐ │ notify_model_config_ │ │ change() │ └────────┬───────────────┘ │ ├──────────────────────────────────┐ │ │ ▼ ▼ ┌─────────────────────┐ ┌─────────────────────┐ │ config_changed_ │ │ config_changed_ │ │ ml_training │ │ trading │ └─────────┬───────────┘ └─────────┬───────────┘ │ │ ▼ ▼ ┌─────────────────────┐ ┌─────────────────────┐ │ ML Training Service │ │ Trading Service │ │ - Update model list │ │ - Reload model │ │ - Mark as active │ │ - Invalidate cache │ └─────────────────────┘ └─────────────────────┘ ``` --- ## Service Integration Points ### Trading Service **Listens On**: `config_changed_trading`, `config_changed_global` **Reloads**: - Risk limits (max_daily_loss, position_limit_pct) - Execution settings (slippage_tolerance, order_timeout) - Compliance rules (sox_enabled, mifid2_enabled) - Circuit breaker thresholds ### Backtesting Service **Listens On**: `config_changed_backtesting`, `config_changed_global` **Reloads**: - Strategy parameters (lookback_days, indicators) - Historical data windows - Simulation settings (commission, slippage) ### ML Training Service **Listens On**: `config_changed_ml_training`, `config_changed_global` **Reloads**: - Model configurations (learning_rate, batch_size) - Training parameters (epochs, early_stopping) - Model cache TTL - Inference timeouts ### API Gateway **Listens On**: `config_changed_api_gateway`, `permissions_changed`, `config_changed_global` **Reloads**: - JWT expiry and signing keys - Rate limits (per-minute, per-hour) - MFA enforcement rules - RBAC permission cache --- ## Performance Characteristics | Metric | Value | Notes | |--------|-------|-------| | **NOTIFY Latency** | <10ms | PostgreSQL internal | | **Service Receive** | <50ms | Network + deserialization | | **Cache Invalidation** | <1ms | In-memory operation | | **Config Reload** | <100ms | Parse + apply new config | | **Total Hot-Reload Time** | <200ms | End-to-end | --- ## Monitoring and Debugging ### Global Channel Monitoring All configuration changes are sent to `config_changed_global` for centralized monitoring: ```sql -- Terminal: Listen to all config changes LISTEN config_changed_global; -- Payload received for any config update: -- {operation, table, key, value, old_value, category, timestamp, id} ``` ### Permission Change Monitoring ```sql -- Terminal: Listen to permission changes LISTEN permissions_changed; -- Payload received for RBAC updates: -- {operation, table, timestamp, role_id, permission_id, user_id} ``` --- ## Security Considerations 1. **NOTIFY payload size**: Limited to 8KB - Config values should be reasonably sized - Large configs should use references 2. **No authentication on NOTIFY**: - All database users can receive notifications - Services must validate config before applying 3. **Idempotency**: - Services should handle duplicate NOTIFY events - Use timestamp/version for de-duplication 4. **Graceful degradation**: - Services cache last-known-good config - Continue operating if NOTIFY fails - Periodic config refresh as fallback --- **Diagram Version**: 1.0 **Last Updated**: 2025-10-03 **Wave**: 73 Agent 4