A team chat app shows a preview card whenever someone pastes a link: page title, description and a thumbnail. Design the service that fetches the linked page, extracts this metadata and returns the card. About 600 new URLs per second need a preview at peak, and many links (news articles, popular videos) are pasted thousands of times. External sites can be slow, broken or malicious, and an attacker might paste a link to an internal address such as http://169.254.169.254 to probe the company's network.
Functional requirements
Return a preview (title, description, image) for a pasted URL
Reuse previews for URLs that were already fetched
Fetch and resize the preview image so clients never load it from the original site
Refuse to fetch internal or private network addresses
Non-functional requirements
The send-message request never waits on an external site
Cached previews return in under 100 ms
A slow or hostile site cannot exhaust the fetchers
Constraints and scale
600 new URLs per second at peak
Fetches time out after 5 seconds and download at most 1 MB
Previews are refreshed at most once a day
You will practise
Move slow external calls off the request path with a queue and workers
Cache results keyed by a normalised URL
Defend outbound fetches against server-side request forgery (SSRF)
Show 2 hints
Should the message appear immediately, with the card filled in a moment later?
Checking the hostname is not enough. What if it resolves to 10.0.0.5?