Appearance
Step 6: Architecture & System Design
High-Level System Architecture
The 21-BIS-Compliance-Engine follows a decoupled, two-tier architecture:
- Frontend Presentation Tier: A lightweight Single Page Application (SPA) built with React 19 and Vite 8 that manages local component state, renders audit cards, and handles client-side CSV blob downloads.
- Inference & Regulatory API Tier: A high-performance Python 3.11 ASGI microservice powered by FastAPI and Uvicorn. It houses the entire machine learning inference pipeline (FastEmbed ONNX, FAISS vector index, Scikit-Learn classifier) and statutory business logic (domain guardrail, weighted gap engine) inside a single process address space.
Request Lifecycles (End-to-End)
1. Statutory Compliance Audit Lifecycle (POST /chat)
2. Statutory Audit Pack CSV Export Lifecycle (POST /audit/export)
Data Schema & In-Memory Entity Relationships
Because the platform operates in-memory without an external relational or NoSQL database, entities are structured as pre-indexed JSON documents in [model2_corpus.json](file:///Users/avishjain/avishjain9-repos/21-bis-compliance-engine/model2_corpus.json) and mapped to Python dictionaries.
Caching Strategy & Error Propagation
In-Memory Pre-Loaded Cache
- Cold-Start Preloading: Upon process boot,
app.pyloadsTextEmbedding,joblib.load(),faiss.read_index(), andmodel2_corpus.jsoninto resident memory ([app.py:L24-L29](file:///Users/avishjain/avishjain9-repos/21-bis-compliance-engine/app.py#L24-L29)). - Subsequent Query Speed: Every request reads directly from pre-warmed RAM pointers with zero disk I/O.
- Invalidation: Updates to
model2_corpus.jsontrigger [rebuild_faiss_index.py](file:///Users/avishjain/avishjain9-repos/21-bis-compliance-engine/rebuild_faiss_index.py) offline; restarting Uvicorn hot-reloads the new index.
Error Propagation Across Layers
Scaling to 100x Traffic & Bottleneck Analysis
Currently, a single Uvicorn process handles ~60 to 75 requests per second (RPS) on a standard 4-core CPU, with an average latency of 16ms. At 100x traffic (~7,500 RPS), the following architectural evolutions occur:
The First Three Bottlenecks at Scale
- CPU Saturation during ONNX Embedding Generation:
- Bottleneck: Calculating 384-dimensional dense vectors via ONNX Runtime consumes ~12ms of CPU time per query. At 7,500 RPS, this requires 90 dedicated CPU cores.
- Mitigation: Introduce an in-memory Redis vector cache caching embeddings for the top 5,000 common queries. For cache misses, deploy GPU-accelerated ONNX embedding sidecars using TensorRT.
- Python GIL Contention in Single Process:
- Bottleneck: Python's Global Interpreter Lock prevents parallel execution of CPU loops across threads in a single process.
- Mitigation: Run Uvicorn in multi-worker mode (
--workers 4per container) orchestrated by Kubernetes with horizontal pod autoscaling based on CPU utilization ($\ge 70%$).
- Frontend Connection Limit to Port 8000:
- Bottleneck: The frontend hardcodes
API_URL = "http://127.0.0.1:8000". - Mitigation: Externalize API URL to environment configuration (
VITE_API_URL) and place behind an Nginx reverse proxy with connection pooling and HTTP/2 multiplexing.
- Bottleneck: The frontend hardcodes