FastAPI — Advanced (50+ Coding Exercises)
- Implement lifespan-managed shared
httpx.AsyncClientand inject it via dependency. - Implement bounded concurrency for outbound calls (max 20) using semaphore.
- Implement timeout + retry wrapper (exponential backoff + jitter) for outbound calls.
- Implement a circuit breaker for a flaky upstream dependency.
- Implement bulkhead isolation per upstream dependency (separate semaphores).
- Implement optimistic concurrency control on updates using ETag +
If-Match. - Implement stock decrement endpoint that prevents lost updates (transaction + check).
- Implement idempotency keys for POST/PUT with stored responses and TTL.
- Implement distributed rate limiting design using Redis (token bucket) (code skeleton).
- Implement distributed lock helper (Redis SETNX) and use it to serialize a job per user.
- Implement background job processing with Celery/RQ skeleton and retries.
- Implement dead-letter handling strategy for jobs that exceed retries.
- Implement a real-time notifications WebSocket endpoint with per-user channels.
- Implement SSE endpoint streaming progress updates from a background job.
- Implement file upload service design with S3 multipart upload (init/part/complete endpoints skeleton).
- Implement request/response logging middleware with PII redaction rules.
- Implement structured logging JSON output with correlation IDs.
- Implement OpenTelemetry instrumentation hooks (middleware + outgoing http + db) skeleton.
- Implement
/metricsendpoint compatible with Prometheus format (basic counters). - Implement API gateway friendly headers:
X-Request-Id,X-Forwarded-Forparsing. - Implement per-route timeouts (enforced in middleware) with configurable defaults.
- Implement graceful shutdown handling: stop accepting new requests, drain in-flight.
- Implement contract tests that validate OpenAPI schema fields and route presence.
- Implement versioned API migration: keep
/v1and introduce/v2with compatibility tests. - Implement quota system per API key (daily limit) with persistence (DB/Redis).
- Implement tenant isolation using JWT claims + DB row-level filters in queries.
- Implement a multi-tenant middleware that selects tenant and injects into dependencies.
- Implement a high-throughput endpoint optimized for validation (benchmark before/after).
- Implement caching layer with ETag/304 for heavy GET endpoints.
- Implement response compression (gzip) middleware and skip for already-compressed types.
- Implement “export job” workflow: create job, run async, poll status, download file.
- Implement job status storage and cleanup after TTL.
- Implement CSV export streaming and ensure constant memory usage.
- Implement paginated export using cursor iteration to avoid loading all rows.
- Implement database connection pooling configuration and enforce session lifecycle.
- Implement transaction wrapper dependency for endpoints that need atomicity.
- Implement saga-like compensating actions for multi-step workflows (code skeleton).
- Implement secure headers middleware (HSTS, X-Content-Type-Options, etc.).
- Implement CSP header configuration for docs/static responses (if serving).
- Implement secrets loading and validation at startup (fail-fast missing env vars).
- Implement an admin-only endpoint protected by mTLS header or gateway claim (stub).
- Implement request size limits for JSON bodies and return 413 when exceeded.
- Implement concurrency-safe in-memory cache with TTL for hot keys (fallback when Redis down).
- Implement dependency health checks for DB/Redis/upstream and expose
/ready. - Implement
/liveendpoint for liveness probe (fast, no dependencies). - Implement load test script skeleton (locust/k6) and define p95 targets.
- Implement error envelope standardization for all exceptions (problem+json style).
- Implement retries respecting idempotency and do not retry non-idempotent operations without key.
- Implement an audit log for sensitive operations (auth, delete) with structured fields.
- Implement log rotation/retention strategy for audit logs (external or file-based stub).
- Implement tracing for background jobs with correlation IDs from requests.
- Implement a “safe deploy checklist” script that validates config and dependencies.