How samyakshrma/proctoring-backend works
Purpose and Entry Points
This monorepo implements a distributed video-proctoring frame pipeline: authenticated clients upload frames to S3-compatible storage, workers infer results asynchronously, and PostgreSQL retains frame metadata/results. Runnable service entry points are:
• API: services/api/app/main.py
• ML worker: services/ml_worker/app/main.py
• Result worker: services/result_worker/app/main.py
• Cleanup worker: services/cleanup_worker/app/main.py
API Ingestion and Upload Authorization
FastAPI is the synchronous control-plane boundary. It validates JWT identity, applies Redis-backed per-user frame-rate limits, allocates the end-to-end event_id, and issues short-lived presigned blob-upload URLs. Clients upload bytes directly to storage; API completion verifies the blob then publishes the raw-frame event.
• Routes/auth: services/api/app/api/frames.py, services/api/app/api/auth.py
• Domain orchestration: services/api/app/services/frame_service.py, services/api/app/services/upload_service.py
• Rate limiting and Kafka publication: services/api/app/services/rate_limit_service.py, services/api/app/services/kafka_service.py
Shared Contracts and Infrastructure Adapters
shared/ is the consistency boundary across independently deployed services. Typed frame, event, and result schemas carry event_id through the pipeline. Shared clients isolate Kafka, blob storage, SQLAlchemy sessions, and persistence models from service-specific orchestration.
• Event/data contracts: shared/schemas/frame.py, shared/schemas/kafka_events.py, shared/schemas/results.py
• External adapters: shared/kafka/client.py, shared/storage/blob_client.py
• Database foundation: shared/database/models.py, shared/database/session.py
Asynchronous ML Processing
The ML worker consumes frames.raw as a Kafka consumer group, retrieves the referenced blob, executes the replaceable model abstraction, and emits frames.processed. Missing blobs or irrecoverable processing errors go to the configured DLQ, allowing offsets to advance rather than blocking a partition. Horizontal scaling is partition/lag driven.
• Consumer lifecycle: services/ml_worker/app/consumer.py, services/ml_worker/app/main.py
• Processing and model boundary: services/ml_worker/app/processor.py, services/ml_worker/app/model.py
• KEDA scaling: infrastructure/keda/ml-worker-scaler.yaml
Result Persistence and Idempotency
The result worker consumes processed events and writes/upserts frame records in PostgreSQL. event_id is the cross-service idempotency key: duplicate Kafka delivery or recovery after a crash overwrites the existing result instead of creating conflicting rows. Database schema evolution is managed with Alembic.
• Consumer and persistence orchestration: services/result_worker/app/consumer.py, services/result_worker/app/repository.py
• Initial frames schema: migrations/versions/20240520_0001_create_frames.py
• Migration runtime: migrations/env.py
Retention and Lifecycle Management
The cleanup worker periodically finds benign frames beyond retention, deletes associated blob objects, and nulls their blob reference while preserving metadata and inference records. Operations tolerate absent objects and are restart-safe.
• Cleanup loop and policy: services/cleanup_worker/app/cleanup.py, services/cleanup_worker/app/config.py
• Shared persistence/blob boundaries: shared/database/repositories.py, shared/storage/blob_client.py
Runtime, Deployment, and Observability
Services run on Python 3.12 in separate containers, with FastAPI, Kafka, PostgreSQL, Redis, and S3/MinIO as material dependencies. Docker Compose provides the local full stack; Kubernetes deploys API and each worker separately. The API exposes health/readiness and Prometheus metrics; shared JSON logging supports correlation by event/user.
• Local composition: infrastructure/docker/docker-compose.yml
• Kubernetes workloads: infrastructure/kubernetes/api/deployment.yaml, infrastructure/kubernetes/ml-worker/deployment.yaml, infrastructure/kubernetes/result-worker/deployment.yaml
• API health/metrics: services/api/app/api/health.py, services/api/app/metrics.py
Main entry points: processor.py ML consumer and processor · frames.py API routes and auth · frame_service.py Frame upload orchestration · blob_client.py Blob storage adapter
Components 21
API control plane
- API routes and auth
services/api/app/api/frames.py - Frame upload orchestration · Allocates event IDs, verifies uploads, and publishes raw-frame events.
services/api/app/services/frame_service.py - Per-user rate limiter
services/api/app/services/rate_limit_service.py - Health and metrics
services/api/app/api/health.py
Async pipeline
- ML consumer and processor · Fetches frame blobs, invokes the replaceable model, and emits processed events.
services/ml_worker/app/processor.py - Model abstraction
services/ml_worker/app/model.py - Result persistence worker · Upserts results by event_id for idempotent Kafka recovery.
services/result_worker/app/repository.py - Retention cleanup worker · Deletes expired benign blobs and clears their references restart-safely.
services/cleanup_worker/app/cleanup.py
Shared boundaries
- Frame event contracts · Carries event_id across service boundaries.
shared/schemas/kafka_events.py - Blob storage adapter
shared/storage/blob_client.py - Database models and sessions
shared/database/models.py - Alembic migrations
migrations/env.py
Infrastructure
- frames.raw
- frames.processed
- Processing DLQ · Irrecoverable ML failures are diverted without blocking partitions.
- S3 / MinIO blobs
- Redis
- PostgreSQL frames · Preserves frame metadata and inference records.
- KEDA ML scaler · Scales ML workers from Kafka partition lag.
infrastructure/keda/ml-worker-scaler.yaml - Container deployments · Compose supplies local stack; Kubernetes deploys API and workers separately.
infrastructure/docker/docker-compose.yml
Other components
- Authenticated client
Connections 25
- Authenticated client → API routes and auth: authenticated requests
- API routes and auth → Frame upload orchestration: frame commands
- API routes and auth → Per-user rate limiter: check quota
- Per-user rate limiter → Redis: counter state
- Frame upload orchestration → Frame event contracts: create event
- Frame upload orchestration → Blob storage adapter: presign and verify
- Authenticated client → S3 / MinIO blobs: direct presigned upload
- Blob storage adapter → S3 / MinIO blobs: blob operations
- Frame upload orchestration → frames.raw: publish after verification
- frames.raw → ML consumer and processor: consume
- ML consumer and processor → Blob storage adapter: retrieve frame
- ML consumer and processor → Model abstraction: infer
- ML consumer and processor → frames.processed: publish result
- ML consumer and processor → Processing DLQ: missing blob / fatal error
- frames.processed → Result persistence worker: consume
- Result persistence worker → Database models and sessions: idempotent upsert
- Database models and sessions → PostgreSQL frames: sessions and models
- Alembic migrations → PostgreSQL frames: evolve schema
- Retention cleanup worker → PostgreSQL frames: find retained frames
- Retention cleanup worker → Blob storage adapter: delete expired blobs
- KEDA ML scaler → ML consumer and processor: scale on lag
- Container deployments → API routes and auth: deploy
- Container deployments → ML consumer and processor: deploy
- Container deployments → Result persistence worker: deploy
- API routes and auth → Health and metrics: health and metrics