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.
The client times out after POST and retries. Should the server always create a second link?
No. If the API supports an idempotency key, the retry should return the first result. If it does not, duplicate links may be acceptable under an explicit product policy, but the service should not silently assume that equal destinations mean equal owners or expiry.
Use this as a guide for self review; written answers are not graded.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.