URL Shortener API Design and Data Model
Design the REST API and database schema for a URL shortener: endpoints, request/response shapes, indexes, SQL vs NoSQL choice and handling custom aliases.
Published November 27, 2026
With requirements and numbers agreed, the next step is the contract: what the API looks like and how links are stored. Both are small, but a few decisions here (how custom aliases are handled, what the primary key is, whether the same long URL gets the same code) ripple through the rest of the design.
The concept
The API needs two core operations. Creating a link is a POST that takes the long URL and optional alias and expiry, and returns the short URL. Resolving a link is a GET on the short path itself, which responds with an HTTP redirect rather than JSON. Management endpoints (list, delete, stats) sit behind authentication.
The data model is essentially a key-value mapping from short code to long URL, plus metadata. Because almost every query is a lookup by exact code, the code should be the primary key. Both a relational database and a key-value store like DynamoDB or Cassandra work well; the choice usually comes down to what the team operates comfortably and whether you need relational features for users and billing.
API surface
| Method | Path | Purpose | Response |
|---|---|---|---|
| POST | /api/v1/links | Create a short link | 201 {code, short_url, long_url, expires_at} |
| GET | /{code} | Redirect | 301/302 Location: long_url (404/410 if missing/expired) |
| GET | /api/v1/links/{code} | Link details (owner only) | 200 JSON |
| GET | /api/v1/links/{code}/stats | Click analytics | 200 JSON time series |
| DELETE | /api/v1/links/{code} | Disable a link | 204 |
Creating a link
Schema
A single table carries the core mapping. Users and analytics live in separate tables or separate systems so the hot lookup table stays narrow.
CREATE TABLE links (
code VARCHAR(16) PRIMARY KEY, -- Base62 or custom alias
long_url VARCHAR(2048) NOT NULL,
user_id BIGINT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NULL,
is_active BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE INDEX idx_links_user ON links (user_id, created_at);
CREATE INDEX idx_links_expiry ON links (expires_at) WHERE expires_at IS NOT NULL; -- PostgreSQL partial indexStorage options
| Option | Strengths | Weaknesses |
|---|---|---|
| PostgreSQL / MySQL | Familiar, transactions, unique constraints for aliases | Needs sharding past a few TB or very high write rates |
| DynamoDB / Cassandra | Horizontal scale built in, predictable latency | No joins; uniqueness via conditional writes |
| Redis alone | Extremely fast | Memory-bound; must be backed by durable storage |
Practical examples
Create request and response
Idempotency keys let clients retry safely without creating duplicates.
POST /api/v1/links
Authorization: Bearer <token>
Idempotency-Key: 6f1c2b9e-...
Content-Type: application/json
{"long_url": "https://example.com/blog/2026/11/designing-systems?utm_source=x", "alias": "design-blog", "expires_at": "2027-01-01T00:00:00Z"}
HTTP/1.1 201 Created
{"code": "design-blog", "short_url": "https://sho.rt/design-blog", "expires_at": "2027-01-01T00:00:00Z"}Atomic custom alias reservation
Let the primary key enforce uniqueness instead of check-then-insert races.
INSERT INTO links (code, long_url, user_id) VALUES ('design-blog', 'https://example.com/...', 42);
-- on duplicate key error → return 409 Conflict "alias already taken"DynamoDB equivalent
A conditional put gives the same guarantee.
table.put_item(
Item={"code": "design-blog", "long_url": url, "user_id": 42},
ConditionExpression="attribute_not_exists(code)",
)Common mistakes
- Checking whether an alias exists and then inserting in two steps, creating a race between concurrent users.
- Returning JSON from the redirect endpoint instead of a real HTTP redirect.
- Storing analytics counters in the links row, turning every redirect into a hot-row update.
- Not validating or normalising long URLs (scheme, length, javascript: URLs).
- Using an auto-increment ID as the public code, which exposes volumes and makes links enumerable.
Key takeaways
- Two core endpoints: POST to create, GET /{code} to redirect.
- The short code is the primary key; lookups are exact-match only.
- Let the database enforce alias uniqueness atomically.
- Keep analytics and user data out of the hot lookup table.
FAQ
Should the same long URL always return the same code?
Optional. Deduplicating saves space but needs an index on long_url (or its hash) and leaks whether someone else shortened it. Many services create a new code per request, scoped per user.
Should expired links return 404 or 410?
410 Gone communicates that the link existed and was intentionally removed, which is slightly more informative to clients and crawlers.
How long can a long URL be?
Browsers handle very long URLs, but cap at around 2,048-8,192 characters to protect storage and prevent abuse.