How uhhfeef/cambai-challenge works
Project Type & Purpose
• This is a full-stack backend service built in Python using FastAPI.
• The primary purpose is secure API key management with multi-tenant support. It ensures high availability (via Redis master-replica and Sentinel), logs all operations to Loki (for audit/log management), and exposes REST API endpoints for authentication, data operations, and API key management.
Main Components
a. FastAPI Application:
• REST API endpoints (Authentication, Data Operations, API Key management).
• Mention core modules (e.g., app/api/routes for endpoints, app/core for config and security, app/models for data structures, and app/tasks for background processing via Huey).
b. Database Layer:
• Redis as the primary datastore with a strategy for master-replica connections.
• Redis Sentinel (for monitoring and automatic failover).
c. Background Task Processing:
• Huey, which manages background logging tasks.
d. Logging and Monitoring:
• Loki for audit log storage.
• Grafana for log visualization.
• Prometheus for monitoring and alerting.
e. Deployment Infrastructure:
• Kubernetes clusters with deployment objects for FastAPI app, Redis, Huey tasks, Loki, Grafana, and Prometheus.
• Configuration files in the k8s directory (organized by component) indicate a microservices-style deployment.
Relationships & Data Flow
• The FastAPI application processes incoming API requests and interacts with the Redis database.
• Redis connection management includes trying multiple Redis pods (with failure handling via Sentinel).
• Background tasks (scheduled via Huey) pick up log offloading and auditing operations.
• Audit logs generated during API operations are stored temporarily in Redis and later forwarded to Loki.
Architectural Patterns & Principles
• Modular application structure: Separation of concerns among API endpoints, core logic (security, configurations), database interactions, and asynchronous task processing.
• Resilience/failover pattern: Smart retry mechanisms and handling of Redis ReadOnlyError during master-replica failovers.
• Multi-tenancy: Ensuring data isolation in API operations and logs (using tenant IDs in both the application and Loki logs).
• Containerization and orchestration via Docker and Kubernetes for scalable deployments.
Detailed Component Separation
• Divide the application into sub-components:
• "API Layer": Represent the FastAPI endpoints, clearly separating authentication endpoints, data operations, and API key management.
• "Core Services": Include the modules for configuration, security, and data models.
• "Database Services": A main block for Redis with annotations about the master-replica architecture and Sentinel for failover.
• "Monitoring & Logging": Separate blocks for Loki (with a note on multi-tenancy and audit logging), Grafana, and Prometheus.