How Dub serves fast redirects and click analytics on a serverless stack
Dub is an open source platform for short links and link attribution, used by teams like Twilio, Vercel and Raycast. Its stack is built almost entirely from managed services: Next.js on Vercel for the app and redirects, Redis to cache links, MySQL on PlanetScale for core data and Tinybird for click analytics.
Copy this stackdub.co Open source link management and attribution · United States · founded 2022
Founder Steven Tey said one reason he built Dub was to show what Edge Functions can do: they run server-side logic like redirects very close to the user, and a link shortener is the perfect use case.
The code is public on GitHub under an open core model. Most of it is open source, and a smaller part in the /ee folder has a commercial license.
Redirects run in Next.js Middleware. To handle heavy traffic, Dub caches every link's metadata in Redis the moment the link is created, so a redirect never has to hit the MySQL database.
MySQL, hosted on PlanetScale, holds users and link data. Steven Tey credits Upstash Redis for redirect times under 200 ms.
Click data first lived in Redis Sorted Sets. It was hard to filter by location or referrer, and some queries took more than 5 seconds as customers' links passed hundreds of thousands of clicks.
The team liked ClickHouse's speed but not the work of running a cluster, so they moved to Tinybird, a managed ClickHouse. The app sends each click to Tinybird's Events API and reads SQL queries published as endpoints. Steven Tey says analytics got about 100x faster, and the same click data now feeds usage-based billing.