A URL Shortener

Here is the URL of a shared Google document:

https://docs.google.com/document/d/1aB2cD3eF4gH5iJ6kL7mN8oP9qR0sT1uV2wX3yZ4aB5c/edit?usp=sharing

It is too long to read aloud, to type, or to fit in a short post. A URL shortener is a system that maps a short URL to a long one. It could replace the URL above with this one:

https://short.example/x7Kp2Qa

The part after the domain, x7Kp2Qa, is the short code. The short code identifies the long URL.

In this chapter, we design a URL shortener using what the course has covered so far.

Two operations

A URL shortener supports two operations:

  • Shorten: given a long URL, create a short code and return the short URL.
  • Follow: given a short URL, redirect the browser to the long URL.

Three variants

Suppose you are asked to design a URL shortener for each of these three:

  • X’s t.co. Every link in a post gets a short link. X creates it when the user publishes the post.
  • Google Drive. A shared document, sheet, or form gets a short link. The owner creates it by opting in.
  • O’Reilly. A URL printed in a book gets a short link. O’Reilly’s staff create it.

We design all three side by side. At each decision, we ask what each variant needs and why.

Here is what is real about these variants, as of October 2026. X wraps every link in a post in a t.co link, including links in direct messages, and users cannot turn this off. Twitter started wrapping all links in 2011. A wrapped link counts as 23 characters toward the post limit. X gives two reasons for the wrapping: saving characters, and screening links for spam and malicious sites.

O’Reilly shortens long URLs in its books during production, so that a reader of the printed book can type them. Since May 2019, it has hosted these links on its own oreil.ly domain. Before that, it used Bitly.

As far as we know, Google Drive does not offer short links. Microsoft Forms has offered a “Shorten URL” option since 2021. It gives links such as forms.office.com/r/ followed by 10 characters. So the Drive variant is a feature we imagine adding.

The load numbers in this chapter are my assumptions, and so are the designs. I do not claim to know how any of these companies built its shortener.

What we leave out

Dedicated URL shorteners such as Bitly offer more than shorten and follow:

  • A password on a link
  • Links that expire
  • Click analytics
  • Several short links for one long URL, for example for A/B testing