Should a public short-link service return the existing code when the same long URL is shortened again, per creator, globally, or never?
answer
- a product contract, not an optimisation
- ownership, analytics, takedown per link
- what makes two URLs equal
- channels want separate codes
- idempotent creates by request key
basics
~20 sUsually never globally: shared codes merge ownership, click analytics, expiry and takedown across customers and leak which URLs were shortened. Per-creator reuse, with an option to mint a fresh code, plus idempotent creates is a common default.
solid answer
~40 sIt is a product contract, so I decide it on what a link means to the customer. Global dedupe merges ownership, analytics and takedown: one marketer's clicks land in another's counts, disabling one customer's link disables the other's, and anyone can test whether a URL was shortened. It also needs a second unique lookup on a normalised-URL digest, and normalisation is risky because path case and query parameters can change the page. The saving is small, and marketers want separate codes per channel. For a marketing platform I would offer per-creator reuse as an option with a way to mint a fresh code, plus idempotent creates by request key so retries do not duplicate, and revisit global dedupe only for anonymous, analytics-free links.
go deeper
Recall that shortening the same long URL twice can return one code or two, and that services choose deliberately rather than by accident.
Explain how hash-and-truncate makes reuse cheap or accidental, and why the URL must be normalised before two creates can be compared.
Lay out what global dedupe merges, ownership, analytics, expiry and takedown, and the conditional insert on a URL digest it adds to the write path.
Own the contract: what same link means to customers, which merge cannot be taken back, and the evidence that would justify revisiting it.
## The decision When a long URL that has already been shortened is shortened again, a short-link service can **return the existing code** or **mint a new one**. There are three policies, and the choice is a product contract more than an optimisation, because once callers rely on "same URL, same code" it is hard to take back. | policy | what happens on a repeat | who it suits | |---|---|---| | **Never dedupe** | every create gets a new code | most public and marketing services | | **Per creator** | the same creator and the same URL get the same code | services that want idempotent creates per account | | **Global** | anyone shortening the URL gets the same code | anonymous, analytics-free services that want fewer rows | ## What global dedupe costs Global dedupe looks like a storage saving, but it merges things the product treats as separate: - **Ownership.** Two unrelated users now share one link. Whose dashboard shows its clicks, and who may edit it? - **Analytics.** Clicks from one marketer's email campaign land in another marketer's counts. - **Expiry and takedown.** Disabling one customer's link, because their campaign ended or their account was suspended, disables the other customer's link too. - **Privacy.** Anyone can learn whether a URL has already been shortened, and if codes are derived from the URL alone, compute its code. For a URL that points to a private document, that is a leak. - **A second unique lookup.** Every create must look up a digest of the normalised URL and write it under a conditional insert, so two concurrent creates of the same URL do not both mint codes. The index mechanics are standard; what matters here is that dedupe adds a second uniqueness rule to the write path. ## Normalisation decides what "the same URL" means Dedupe is only as good as the rule that makes two URLs equal: 1. **Safe to normalise:** lower-casing the scheme and host, dropping a default port, removing an empty fragment. 2. **Unsafe to normalise:** the path's case, the order and presence of query parameters, a trailing slash. The target server may treat these as different pages, and merging them sends visitors to the wrong page. 3. **Deliberately different:** campaign tracking parameters. A marketer who adds them wants a different link for each channel; stripping them to dedupe would merge exactly the channels they meant to compare. Under-normalising makes dedupe miss; over-normalising silently redirects someone to a page they did not link. The second is the worse failure, so services that dedupe tend to normalise conservatively. ## Why many services do not dedupe at all - **The saving is small.** A link record is small, and in many products duplicates are a minority of creates. - **Marketers want separate codes.** The same landing page shortened for email, social and print should give three codes and three columns on the dashboard. - **Per-link features stay simple.** Expiry, takedown, ownership and analytics all attach to one row with one owner. - **Hash-and-truncate does not force the choice.** Deterministic codes dedupe by accident only when the URL alone is the hash input; including the creator, or a nonce, restores separate links. ## A defensible default for a marketing platform For the scenario here, a public service with click dashboards, branded aliases and campaign end dates: - **No global dedupe.** Ownership, analytics and takedown must stay per customer. - **Per-creator reuse as an option, not a rule.** Return an existing link when the same creator repeats the same create, which makes retried requests safe, but let the caller ask for a fresh code for a new channel. - **Idempotent creates by request key.** A client retrying after a timeout sends the same request key and gets the same link, which solves the accidental-duplicate problem without merging deliberate ones. - **Revisit only with evidence:** if duplicates turn out to be a large share of rows and the product has no per-link features, global dedupe for anonymous links becomes worth discussing. The judgement a lead owns is not the index design but the contract: what "same link" means to the customer, and which merge the product can never take back.
- How do idempotent creates differ from per-creator dedupe?An idempotency key identifies one request: a client that times out and retries sends the same key and gets the same link, even if it would want a new code for the same URL later. Per-creator dedupe identifies the content: any later create of the same URL by that creator returns the old link. The first fixes accidental duplicates without merging deliberate ones.
- Hash-and-truncate gives the same URL the same code; does that commit the service to global dedupe?Only if the URL alone is the hash input. Putting the creator in the input gives per-creator reuse, and adding a nonce gives a new code every time. With the URL alone, anyone can also compute a URL's code, which matters when targets are private.
- Always dedupe globally, because duplicate rows are wasted storage.
- Normalise query parameters away so that more URLs match before deduping.
- Hash-and-truncate means the service must give every creator of a URL the same code.
- Dedupe is an internal optimisation that can be switched on or off later without anyone noticing.