Skip to main content
短.be

Idempotency

The property that performing the same operation multiple times produces the same result as performing it once. Essential in API design for safely handling duplicate requests.

Dec 17, 2025 · About 1 min read

URL Shortening

Idempotency is the property that executing an operation once or multiple times yields the same outcome. In mathematics it is expressed as f(f(x)) = f(x). In web API design, idempotency ensures that retrying a request after a network failure or timeout does not cause unintended side effects such as duplicate records or double charges.

HTTP methods have defined idempotency characteristics. GET, PUT, and DELETE are idempotent (repeating them produces the same result). POST is non-idempotent (each call may create a new resource).

For a URL shortening API, idempotency is a core design question. If the same long URL is submitted twice, should two different short URLs be created, or should the same short URL be returned both times? An idempotent design returns the same short URL for the same input, preventing duplicates when a client retries after a network error.

A common implementation technique is the idempotency key. The client attaches a unique key (typically a UUID) to each request. The server caches the result for that key. If the same key appears again, the server returns the cached result without re-executing the operation. Stripe's payment API is a well-known example of this pattern.

In URL shortening services, idempotency is often combined with URL normalization. The input URL is normalized, a hash is computed, and the same hash always maps to the same short code. This also means that "https://example.com/page" and "https://example.com/page/" resolve to the same short URL. You can find related books on Amazon.

Share on XHatena

Was this article helpful?

Related Terms

Related Articles

FAQ

Why do APIs need idempotency?
Network failures and timeouts cause clients to retry requests. Without idempotency, retries can trigger duplicate processing - double charges, duplicate records, and so on. With idempotency, retries are safe because the result is always the same.
How is an idempotency key implemented?
The client sends a UUID in a request header (e.g., Idempotency-Key: 550e8400-...). The server caches the result in Redis or a similar store, keyed by that UUID. If the same key arrives again, the server returns the cached result without re-executing the operation.
Are GET requests always idempotent?
By the HTTP specification, GET must be idempotent. Implementing a GET endpoint that modifies data violates the spec and breaks caches and proxies. Use POST, PUT, or DELETE for state-changing operations.

Ready to create a short URL?

Shorten a URL for Free