KatInterview

Build a Token-Auth Fetch-and-Upsert CLI Pipeline

System DesignModel answer
MediumModel answerSystem Designhttp-clientidempotencyupsert

Last updated

Design a layered CLI tool that calls a bearer-token API, validates JSON into records, and idempotently upserts them into a relational table.

Context

A downstream reporting job needs data that lives behind an HTTP API. The API requires a bearer token, returns a JSON payload containing a list of items (each with an id, a timestamp, and a free-text message), and the job must land those items in a relational table so that later queries can run against them. The tool runs on a schedule, so it will be invoked repeatedly against overlapping data.

What you must produce

A command-line program that:

  1. Calls the API. Issue an HTTP(S) request to a URL supplied at runtime, sending the token in an Authorization: Bearer <token> header.
  2. Parses the payload. Decode the JSON body and turn each element into a validated in-memory record. The payload may be a bare list or an object wrapping the list under an items key. Timestamps may arrive as ISO-8601 strings or as epoch numbers.
  3. Persists the records. Write them to a relational table (id, ts, message) using a connection string from configuration. Re-running the tool must not create duplicate rows, and an older timestamp must never clobber a newer one already stored.
  4. Fails loudly but predictably. Distinguish network/timeout failures, auth rejections, other non-2xx responses, malformed bodies, and database failures — each with a clear log message and a distinct exit code so schedulers can tell them apart.
  5. Takes its inputs from the environment. URL, token, and DB credentials are configurable; the token must not be passed as a visible command-line argument.

Constraints and edge cases to address

  • Empty item list is a success, not an error.
  • A 200 response whose body is HTML or otherwise not JSON.
  • Items missing id, with a null/blank id, with an unparseable timestamp, or that are not objects at all.
  • The same id appearing twice inside one response.
  • Retries must only apply to requests that are safe to repeat.
  • Non-ASCII message text must round-trip intact.
  • Entire-batch atomicity: a mid-batch DB failure leaves no partial writes.

Deliverable

Explain your layering, configuration strategy, retry and idempotency decisions, and testing approach, and give working code for the pipeline.

Hints

Hint 1

Split into three collaborators — an HTTP client, a pure parsing function, and a repository — and let `main` only do wiring plus exception-to-exit-code mapping. Each piece then gets tested without the other two.

Hint 2

Make the write an upsert keyed on the primary key, and add a guard so an incoming row only overwrites the stored row when its timestamp is at least as new. Also dedupe within the batch first, since some databases reject touching the same key twice in one statement.

Hint 3

The token belongs in an environment variable (sourced from a secret store), never in argv — arguments end up in shell history and process listings. Never log the token or the full connection string either.