LESSON 02 · API and data model

Design creation and lookup together

The creation response must leave a durable mapping for every later redirect.

SYSTEM VIEW

The system

Diagram view
POSTs—
Links stored—

Watch a link get stored before it is returned, then follow a retry whose first answer was lost.

  1. The Creator POSTs a long URL. The Create service validates it, assigns key p4Nz8Lc and commits the record to the Mapping database, where a unique index allows one row per key. Only then does it answer 201, so a returned link resolves.

    Order
    commit, then 201
    Unique index
    the short key

    What changed

    • POST /links
    • Record p4Nz8Lc, committed
    • 201 after the commit
  2. A Visitor opens the link. The Redirect service reads the record for p4Nz8Lc by its key: the destination, plus creation time and optional expiry. It answers 302 with a Location header, which tells the browser where to go next.

    Record
    key, destination
    Also
    created_at, expires_at

    What changed

    • Visitor and Redirect service
    • GET /p4Nz8Lc
    • 302 with Location
  3. A second owner POSTs the same long URL. Reusing p4Nz8Lc would save little: at 120 bytes a URL, a million repeats cost about 120 MB. Owners often need their own expiry and analytics, so this policy stores a distinct record, q9Wd2Xe.

    Policy
    a record per owner
    Per URL
    120 bytes

    What changed

    • Same long URL, second owner
    • Record q9Wd2Xe
  4. The Creator generates an idempotency key, token a81f, once for this logical request and sends it with the POST. The Create service commits key x7Kp2Qa and the token in one transaction, and a unique index now covers the token too.

    Token
    a81f
    Stripe’s header
    Idempotency-Key

    What changed

    • Token a81f
    • Record x7Kp2Qa with a81f
    • Unique index on the token
  5. The 201 carrying x7Kp2Qa is lost on the way back. The Mapping database holds the committed record, but the Creator times out without a link and will try again.

    What changed

    • 201 · …/x7Kp2Qa, lost
    • Creator times out
  6. The Creator retries with the same token, a81f. The unique index on the token finds the committed row, so the Create service returns x7Kp2Qa again instead of inserting. Four POSTs leave three links stored.

    What changed

    • Retry with a81f
    • Same key, x7Kp2Qa
    • No new record
  7. Kept shows what tokens cost: one more indexed column and a retention rule for old tokens, worth it for an API that bills per link. None shows a free anonymous form, where the retry stores duplicate Lc7Ny3p, wasting a few hundred bytes.

    Token costs
    an index + cleanup
    Duplicate costs
    a few hundred bytes
    Retry token

Space plays · ← → step · Home resets

THE IDEA

How it works

A client can POST a destination URL to create a link. The service validates it, assigns a unique key, persists a record, then returns the complete short URL. A request to GET /{key} looks up that record and responds with an HTTP Location header, which tells the browser where to go next. Store the key, destination, creation time, and optional expiry, and enforce a unique index on the key: a database rule that rejects a second row with the same value.

Choose an explicit duplicate policy. Reusing a key for the same destination can reduce storage, but distinct owners, expiry dates, and analytics often require distinct records. Deduplication also saves little: at 120 bytes per URL, a million repeats cost about 120 MB.

Retries need their own answer. An idempotency key is a token the caller generates once per logical request, so sending it again returns the first result instead of repeating the work. Stripe’s API uses this pattern through an Idempotency-Key header. Follow one failure: the client POSTs with token a81f; the server commits key x7Kp2Qa with that token in one transaction; the response is lost on the way back. The client retries with a81f, a unique index on the token finds the row, and the server returns x7Kp2Qa again.

The cost is one more indexed column and a retention rule for old tokens. A free anonymous form may skip this, since an occasional duplicate wastes only a few hundred bytes. An API that bills per link should not.

DECISION POINT

The client times out after POST and retries. Should the server always create a second link?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Persist before acknowledging creation so a returned link resolves.
  • A unique key index and an explicit duplicate policy protect the mapping contract.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

To cut latency, a teammate proposes returning the short link first and writing the mapping in a background job. What does this risk?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.