Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 42 ยท Projects

Project: URL Shortener with Click Stats

Build a URL shortener on Redis: INCR ids, expiring links with SET NX EX, hashes for stats, HyperLogLog unique visitors and a sorted-set top list.

A URL shortener is a small product with a surprisingly complete set of storage needs. Every short code must map to one long URL, and creating a code must never overwrite somebody else's. Some links should vanish after a while. Every click should be counted, ideally split into total clicks and distinct visitors, and the owner wants a "most clicked" list. That is five different access patterns, and Redis has a structure that fits each of them.

In this project you combine what you learned in the earlier lessons: strings and counters, expiry, hashes, HyperLogLog and sorted sets. We build the data layer only, as plain functions, so you can see exactly which Redis command does each job.

Real Redis cannot run on this page. The code below runs against a plain JavaScript emulator (a small command table with a fake clock) that implements the real semantics of the commands we use. The redis.call("NAME", ...) style mirrors how a driver sends commands. The emulator's HyperLogLog is a faithful sketch of the idea (4,096 registers, real Redis uses 16,384), so its estimates are close to real Redis behaviour but not identical to it.

One feature, one structure

Link A string with NX and EX.
Stats A hash with a click counter.
Visitors A HyperLogLog per link.
Top links A sorted set of clicks.
1 / 2

The design

Let us decide on keys before writing any code, because the key layout is the real design in Redis.

  • url:next-id is a counter. INCR is atomic, so two requests creating links at the same moment always get different numbers. We turn the number into a short code with base 62 (digits, lowercase and uppercase letters). Adding 62 cubed first guarantees at least four characters.
  • url:CODE holds the long URL as a plain string. Creating it with SET ... NX means "only if the key does not exist", which is how we refuse a duplicate custom alias without a race. Adding EX seconds makes the link expire by itself, with no cleanup job.
  • stats:CODE is a hash with created and clicks. A hash keeps all numbers of one link under one key, and HINCRBY counts a click atomically.
  • uniq:CODE is a HyperLogLog. PFADD records a visitor id, PFCOUNT estimates how many different ids were added, and it needs only about 12 KB per link in real Redis no matter how many millions of visitors there are. A plain set would grow with every visitor.
  • top:clicks is a sorted set where the member is a code and the score is its click count. ZINCRBY bumps it and ZRANGE ... REV reads the best links.

The part that is easy to get wrong is what happens when a link expires. The url:CODE key disappears on its own, but the hash, the HyperLogLog and the sorted-set entry do not, because Redis expiry works per key. We handle that in three ways: stats:CODE gets its own EXPIRE set to the link's lifetime plus one day (so the owner can still read final numbers), the visitor sketch gets the same grace period, and the top list is cleaned lazily: visit removes the code from top:clicks when it finds the link missing, and top skips codes whose link is gone.

The first TryIt contains the whole data layer, but only exercises the first half of it: creating codes, custom aliases and links with a lifetime. Look at the order of the code in shorten. It first does SET ... NX, and only if that succeeded does it touch anything else, so a failed attempt to steal an alias changes nothing.

JS Creating links: ids, aliases and expiry

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Read the output from top to bottom. The first two codes are 1001 and 1002: the counter started at 1 and we added 62 cubed (238,328), which in base 62 is 1000. The alias docs was accepted once and refused the second time, because SET NX returned (nil). Link 1001 has TTL -1, meaning "no expiry", while 1002 has 3600 seconds. After the clock jumps, GET url:1002 is (nil) but its stats hash still has about a day left, exactly as designed, and visit answers 404 for it while docs still redirects.

Sequential ids make codes easy to guess: anybody can walk 1001, 1002, 1003. For private links, generate random codes (and retry on SET NX failure), or scramble the counter with a bijective function. We keep ids readable here so the output is easy to follow.

Counting clicks and visitors

A redirect is the hot path of a shortener, so it must be quick and must never fail because of analytics. In visit the order matters: we first read the link, and only if it exists do we count. Four writes follow, one per structure: HINCRBY for total clicks, PFADD for the visitor, ZINCRBY for the ranking, and an EXPIRE on the sketch so it dies with the link.

flowchart LR
  A["GET /1001"] --> B{"GET url:1001"}
  B -- "nil" --> C["404, ZREM top:clicks"]
  B -- "found" --> D["HINCRBY clicks<br/>PFADD uniq<br/>ZINCRBY top"]
  D --> E["302 to the long URL"]

The redirect path: one read decides everything, then four small writes record the click.

The status is 302, not 301, on purpose. Browsers cache a 301 ("moved permanently") and stop asking your server, so later clicks would never be counted. A temporary redirect keeps every click flowing through you.

Those four writes are separate round trips here. In production you would send them in one pipeline (see pipelining) so the redirect costs a single network exchange, or even fire them after the response has been sent. Because the counters are commutative (adding 1 twice is the same in any order), a pipeline without MULTI is safe.

The second TryIt simulates 600 visits from 150 possible visitors across three links, with a seeded generator so every run is identical. For each link it compares the sketch's answer with an exact count kept in JavaScript.

JS Click stats, unique visitors and the top list

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Compare the sketch and exact columns. HyperLogLog is allowed to be off by a small percentage (real Redis quotes a standard error of 0.81%), and for "about how many people clicked" that is a bargain. If you must bill per unique visitor, use a real set instead and pay the memory.

Taking it further

The project is deliberately small. Some natural next steps, each using something you already know:

  • Per-day charts. Add HINCRBY stats:CODE:2026-01-15 clicks 1 or a daily key with an expiry, so the owner can see a curve, and delete old days automatically.
  • Rate limiting link creation per user with a counter and EXPIRE, from sessions and rate limiting.
  • A reverse index longurl:HASH so the same URL always gets the same code. Use SET NX again to make the claim race-free.
  • Atomic creation. shorten uses several commands. If two of them must succeed together, wrap them in MULTI/EXEC or a Lua script.
  • Persistence. Links are data you cannot rebuild, so turn on the append-only file (see persistence). Pure caches can skip it, a shortener cannot.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does shorten use SET with NX when it stores the long URL?

    SET NX only writes if the key is missing and returns nil otherwise, in one atomic step. A separate EXISTS followed by SET could let two clients claim the same alias.
  2. 2. A link expires. Which keys disappear automatically?

    Expiry is per key. The sorted-set entry is not a key, so it needs lazy cleanup, and the hash and sketch only vanish because we gave them their own EXPIRE.
  3. 3. Why does a click-counting redirect use 302 instead of 301?

    A permanent redirect may be remembered by the browser, which then goes straight to the target. A temporary redirect sends every click through the shortener.
  4. 4. What is the trade-off of using a HyperLogLog for unique visitors?

    A HyperLogLog keeps a fixed-size sketch (about 12 KB in real Redis) and answers with a small error, instead of storing every visitor like a set does.

Summary

  • Design the keys first: a counter for ids, a string with NX and EX for links, a hash for stats, a HyperLogLog for unique visitors and a sorted set for the ranking.
  • SET NX is Redis's atomic "claim this name" operation, and EX gives links a lifetime without a cleanup job.
  • Expiry is per key: give related keys their own EXPIRE and clean up members of shared structures lazily.
  • Use 302 when you count clicks, and pipeline the writes of the hot path.
  • PFADD and PFCOUNT trade a small error for constant memory; use a set when the count must be exact.

Next project: Chat Rooms with Presence.