┌─────────────────────────────────────────────────────────────────┐
│ FastAPI Application │
│ (main.py - Entry Point) │
└──────────────┬──────────────────────────────────────────────────┘
│
├─────────────────────────────────────────┐
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ CRUD │ │ CSV │
│ Routes │ │ Routes │
│ (crud.py) │ │(csv_routes) │
└──────┬───────┘ └──────┬───────┘
│ │
└──────────────┬──────────────────────────┘
│
┌─────────────┴─────────────┐
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Resiliency │ │ CSV │
│ Wrapper │ │ Service │
│(resiliency) │ │(csv_service) │
└──────┬───────┘ └──────┬───────┘
│ │
└──────────────┬────────────┘
│
┌─────────────┴────────────┐
│ │
▼ ▼
┌──────────────┐ ┌───────────────┐
│ Database │ │ Error │
│ Core │ │ Handler │
│(database.py) │ │(error_handler)│
└──────┬───────┘ └───────────────┘
│
▼
┌──────────────────────┐
│ MySQL Database │
│ (via aiomysql pool) │
└──────────────────────┘
Client
│
├─ POST /api/users
│ Body: { "input": { "name": "John", "email": "john@example.com" } }
│
▼
┌─────────────────────────────────┐
│ FastAPI Routing Layer │
│ (app.include_router) │
└────────────┬────────────────────┘
│
▼
┌─────────────────────────────────┐
│ CRUD Route Handler │
│ (routes/crud.py:create_record) │
│ - Validates request format │
│ - Parses "input" field │
└────────────┬────────────────────┘
│
▼
┌─────────────────────────────────┐
│ Resiliency Wrapper │
│ (core/resiliency.py) │
│ - Wraps database call │
│ - Implements timeout logic │
│ - Manages retry schedule │
└────────────┬────────────────────┘
│
┌──────┴───────┬──────────┬──────────┐
│ │ │ │
▼ ▼ ▼ ▼
Attempt 0 Attempt 1 Attempt 2 Attempt 3
(200ms) (400ms) (800ms) (1600ms)
(wait 400ms)(wait 800ms)(wait 1600ms)
│ │ │ │
└──────┬───────┴──────────┴──────────┘
│
▼
┌─────────────────────────────────┐
│ Database Core │
│ (core/database.py) │
│ - Validate table existence │
│ - Build parameterized query │
│ - Execute INSERT command │
└────────────┬────────────────────┘
│
▼
┌─────────────────────────────────┐
│ MySQL Connection Pool │
│ (aiomysql) │
│ - Get connection from pool │
│ - Execute query │
│ - Release connection │
└────────────┬────────────────────┘
│
▼
┌─────────────────────────────────┐
│ Database Engine │
│ (MySQL Server) │
│ - Process INSERT │
│ - Return affected rows │
└────────────┬────────────────────┘
│
▼
┌─────────────────────────────────┐
│ Error Handler │
│ (utils/error_handler.py) │
│ - Package response │
│ - Add timestamp │
│ - Add retry_count │
└────────────┬────────────────────┘
│
▼
┌─────────────────────────────────┐
│ FastAPI Response │
│ HTTP 200 OK │
│ { │
│ "timestamp": "...", │
│ "message": "...", │
│ "data": { ... }, │
│ "retry_count": 0 │
│ } │
└────────────┬────────────────────┘
│
▼
Client
- Responsibility: Retry logic with exponential backoff
- Key Functions:
execute_with_retries()- Wraps async functions with retry logicexecute_with_fallback()- Returns fallback value on failure
- Exceptions:
GatewayTimeoutException(504) - All retries exhaustedServiceUnavailableException(503) - Database unavailable
- Responsibility: Database operations with validation
- Key Functions:
table_exists()- Validates table via INFORMATION_SCHEMAexecute_insert()- Parameterized INSERTexecute_select()- Parameterized SELECT with WHEREexecute_update()- Parameterized UPDATE with safety checksexecute_delete()- Parameterized DELETE with safety checks
- Security: All queries use parameterized statements (
%splaceholders)
- Responsibility: HTTP endpoint handlers for CRUD operations
- Endpoints:
POST /api/{table_name}- CREATEGET /api/{table_name}/query- RETRIEVE allPOST /api/{table_name}/query- RETRIEVE with filterPUT /api/{table_name}- UPDATEDELETE /api/{table_name}- DELETE
- Features:
- Integrates resiliency wrapper
- Validates table existence
- Standardized error responses
- Responsibility: CSV parsing and database operations
- Key Functions:
parse_csv_file()- Extract filename as table name, parse rowsbatch_import_csv()- Iterate records, track successes/failuresexport_to_csv()- Stream data to prevent memory overflow
- Features:
- Per-record error tracking
- Streaming export for large datasets
- Responsibility: HTTP handlers for CSV operations
- Endpoints:
POST /api/batch/import- Upload and import CSVGET /api/batch/{table_name}/export- Download CSV
- Features:
- File validation
- Streaming response
- Responsibility: Standardized error/success responses
- Classes:
ErrorResponse- Pydantic model for errorsSuccessResponse- Pydantic model for success
- Functions:
create_error_response()- Build error JSONcreate_success_response()- Build success JSONlog_error()- Log with context
- Responsibility: Request validation schemas
- Classes:
CRUDRequest- { "input": { ...data } }UpdateRequest- { "input": { ...new_values }, "where": { ...conditions } }DeleteRequest- { "input": { ...WHERE_conditions } }
Database Call
│
▼ [Execute async function with timeout]
│
├─ Success? ──────────────────────► Return Result
│
├─ Timeout/Error ─────┐
│ │
│ Retry Attempt 1? │
│ (200ms elapsed) │
│ No ◄────────────┴─────────► Return GatewayTimeoutException
│ │
│ Yes
│ │
│ ├─ Wait 400ms
│ │
│ ├─ Execute with 400ms timeout
│ │
│ ├─ Success? ──────────────► Return Result
│ │
│ ├─ Timeout/Error ──┐
│ │ │
│ │ Retry Attempt 2?│
│ │ No ◄─────────┴────────► Return GatewayTimeoutException
│ │ │
│ │ Yes
│ │ │
│ │ ├─ Wait 800ms
│ │ │
│ │ ├─ Execute with 800ms timeout
│ │ │
│ │ ├─ Success? ───────► Return Result
│ │ │
│ │ ├─ Timeout/Error ─┐
│ │ │ │
│ │ │ Retry Attempt 3?│
│ │ │ No ◄─────────┴─────► Return GatewayTimeoutException
│ │ │ │
│ │ │ Yes
│ │ │ │
│ │ │ ├─ Wait 1600ms
│ │ │ │
│ │ │ ├─ Execute with 1600ms timeout
│ │ │ │
│ │ │ ├─ Success? ──► Return Result
│ │ │ │
│ │ │ └─ Failure ────► Return GatewayTimeoutException
│ │ │
│ └─────┘
│
└──────────────────────────────────► All Attempts Exhausted
The system uses async/await for high-performance concurrent request handling:
Request 1: POST /api/users
├─ Async handler starts
├─ Resiliency wrapper (non-blocking)
├─ Database query (awaitable)
└─ Response sent
Request 2: GET /api/products/query
├─ Async handler starts (concurrent!)
├─ Resiliency wrapper (non-blocking)
├─ Database query (awaitable)
└─ Response sent
Request 3: PUT /api/orders
├─ Async handler starts (concurrent!)
├─ ... (same pattern)
Benefits:
- ✅ Single-threaded, highly efficient
- ✅ Handles 1000s of concurrent requests
- ✅ Non-blocking I/O (doesn't wait for DB sequentially)
- ✅ Lower resource overhead than multi-threading
Database Operation
│
├─ Normal Error (e.g., duplicate key)
│ └─ DatabaseError
│ └─ HTTP 400 Bad Request
│
├─ Timeout (connection/query timeout)
│ └─ asyncio.TimeoutError
│ └─ Retry wrapper catches
│ └─ If retries exhausted: HTTP 504 Gateway Timeout
│ └─ If retries pending: Sleep and retry
│
├─ Database Unavailable
│ └─ ServiceUnavailableException
│ └─ HTTP 503 Service Unavailable
│
├─ Table Not Found
│ └─ TableNotFoundError
│ └─ HTTP 404 Not Found
│
└─ Unexpected Error
└─ Generic Exception
└─ HTTP 500 Internal Server Error
Incoming Request
│
├─ CORS Middleware Check
│ └─ Allowed origins: * (configurable)
│
├─ Route Handler
│ │
│ ├─ Input Validation
│ │ ├─ Pydantic schema validation
│ │ └─ Type checking
│ │
│ └─ Table Existence Check
│ └─ INFORMATION_SCHEMA query
│
├─ Database Core
│ │
│ ├─ Parameterized Query Building
│ │ ├─ Column names escaped with backticks
│ │ └─ Values always use %s placeholders
│ │
│ ├─ Safety Guards
│ │ ├─ DELETE requires WHERE clause
│ │ └─ UPDATE requires WHERE clause
│ │
│ └─ Execution with Connection Pool
│ └─ Connection isolation, auto-commit
│
└─ Response (Sanitized)
└─ No sensitive info in errors
- Database Connections: Limited by MySQL
max_connectionssetting - Memory: Streaming CSV prevents OOM on large exports
- CPU: Async I/O means low CPU usage even under high load
- Network: Typical HTTP bandwidth limits apply
- Horizontal: Deploy multiple instances behind load balancer
- Database Replication: Master-slave for read scaling
- Caching: Add Redis for frequently accessed tables
- Rate Limiting: Add SlowAPI for API protection
- Async Workers: Use Gunicorn with multiple uvicorn workers
Example Deployment:
Load Balancer (nginx)
├─ API Instance 1
├─ API Instance 2
├─ API Instance 3
└─ API Instance N
│
└─ MySQL Database (Primary)
└─ MySQL Read Replicas (Secondary)
-
Authentication & Authorization
- JWT token validation
- Role-based access control (RBAC)
-
Advanced Querying
- Support for OR conditions
- Range queries (>, <, >=, <=)
- LIKE pattern matching
- JOIN operations
-
Caching Layer
- Redis integration
- Cache invalidation strategies
-
Monitoring & Observability
- Prometheus metrics
- OpenTelemetry tracing
- Structured logging (JSON)
-
API Documentation
- Swagger UI (auto-generated)
- OpenAPI schema
- Interactive API explorer
-
Testing
- Unit tests for core modules
- Integration tests for endpoints
- Load testing with locust
- Contract testing
This architecture provides: ✅ Modularity: Clear separation of concerns ✅ Resilience: Automatic retry with exponential backoff ✅ Safety: SQL injection prevention, schema validation ✅ Performance: Async I/O, connection pooling, streaming ✅ Maintainability: Clean code, comprehensive logging ✅ Scalability: Ready for horizontal expansion