Which database constraint is most important for preventing two different long URLs from getting the same short code?

6 year of experience System Design URL Shortener Bitly URL Shortener Bitly Medium Single Correct Pyq Inspired

Question

Which database constraint is most important for preventing two different long URLs from getting the same short code?

A
A UNIQUE constraint or primary key on short_code
B
A NOT NULL constraint on long_url
C
A default timestamp on created_at
D
A nullable constraint on expiration_date

Explanation

A UNIQUE constraint or primary key on short_code enforces uniqueness at the database level.

Application logic can try to generate unique short codes, but the database should still enforce uniqueness. A UNIQUE constraint or primary key on the short_code column prevents accidental collisions. If a collision occurs during insertion, the service can retry with a new code or salt.

Related Questions

Where should a URL shortener cache frequently accessed short_code to long_url mappings for...

URL Shortener Bitly

View Question

Why should a URL shortener maintain an index or primary key on the short_code column?

URL Shortener Bitly

View Question

A short URL has expired. What should the redirect service ideally return when the short co...

URL Shortener Bitly

View Question

Why is Base62 commonly used for short URL codes instead of Base64?

URL Shortener Bitly

View Question

Which short-code generation strategy best guarantees uniqueness without collision checks f...

URL Shortener Bitly

View Question

Why is using the first few characters of the long URL as the short code a bad approach?

URL Shortener Bitly

View Question

Related Resources

Related learning resources will appear as content grows.