KatInterview

Build a Log Collection REST Service for a Fleet of Cameras

System DesignModel answer
MediumModel answerSystem Designrest-apiflaskhttp-status-codes

Last updated

Design and code a small HTTP service where cameras push log entries and users fetch them by camera, time range, or severity, with validation, status codes, and thread safety.

Camera Log Gateway

A fleet of cameras emits log entries. Between the cameras and the people who want to inspect those logs sits a middle-tier HTTP service, and you are asked to build it.

  • Cameras (or tests pretending to be cameras) push log entries to the service.
  • Users query the service to read those entries back.
  • How logs are produced on the device is out of scope. Test code will generate fake logs and call your endpoints.

You write the server yourself in a language and lightweight HTTP framework of your choice (Flask or similar is fine). That covers routing, request parsing, validation, response bodies, and status codes, plus whatever in-memory storage is needed to answer queries. Persistence beyond the process lifetime is not required unless you are asked to add it.

What to produce

  1. An endpoint design: paths, methods, request and response formats.
  2. A working implementation of at least:
    • an endpoint for a camera to submit log entries,
    • an endpoint for a user to fetch logs for a camera, with optional time-range filtering,
    • an endpoint for a user to fetch the most recent entries or filter by severity (or a similar third call).
  3. Validation and error handling with correct HTTP status codes.
  4. A storage layout that supports the queries efficiently and returns results in a defined order.
  5. Thread-safe handling of concurrent requests.
  6. A few tests that push fake logs through the API and check both the happy paths and the error paths.

Points to settle up front

State your assumptions explicitly, for instance:

  • Fields of a log entry (timestamp, severity, message) and whether submissions are single or batched.
  • Which queries are needed: by camera, by time range, latest N, by severity.
  • How to treat unknown cameras, malformed entries, and duplicate submissions.
  • Maximum response size and whether results are paginated.
  • Whether authentication or per-user access control is in scope.

Hints

Hint 1

Start by writing down the resource model: a camera owns an ordered collection of log entries. Let the URLs reflect that, for example POST and GET on a path containing the camera id.

Hint 2

Decide on one validation function that returns either a clean entry or a list of errors, and map outcomes to codes: 201 for created, 400 for malformed input, 404 for an unknown camera on reads, 409 or idempotent 200 for duplicates.

Hint 3

Keep each camera's entries sorted by timestamp so a time-range query is two binary searches plus a slice. Guard the shared structure with a lock, and keep the critical section small.