Urgent.News

What's breaking now, across thousands of outlets.

Tech

Design a URL shortener: start with one server and let the numbers grow it

Most answers to "design a URL shortener" start with the final picture: load balancers, a cache cluster, Kafka, ten boxes. Interviewers see that every day. It shows you remember a diagram, not that you can design. A better answer starts small. Then you let the numbers decide each next step. Here's the short version. First, the numbers Say people create 100 million links a month, and each link gets…

Designing a URL shortener typically begins with a grand vision of load balancers, cache clusters, Kafka, and multiple servers. However, interviewers often appreciate a more straightforward approach that starts small and evolves based on the data requirements.

To begin, consider the scale of the project. Suppose individuals generate 100 million links monthly, with each link receiving approximately 100 clicks. This results in about 40 new links per second. When a link goes viral, click rates can surge tenfold. Consequently, the database must handle the load, while the need for a cache increases due to the high number of reads.

With these figures in mind, let's plan the design. Step one involves creating a simple server with a single table that maps short codes to their corresponding long URLs. Additionally, there should be one endpoint for creating links and another for redirecting users. Initially, it's acceptable to acknowledge that this initial design may not be sustainable in the long run.

Moving on to step two, we need to generate unique codes efficiently. If each new link request waits for the database to provide the next number, it would lead to server delays. Instead, each server should take a block of 10,000 numbers at once and allocate them from memory. To avoid clashes, two servers should never be assigned the same block. Furthermore, the numbers can be shuffled using a secret key to prevent patterns that could be exploited to access all links sequentially.

In step three, a cache for tracking clicks is introduced. Once a link's target remains constant, it's unnecessary to constantly query the database. Instead, a cached copy of the target can reside in the server's memory, complementing Redis if required. This cached copy can be temporarily out of sync without causing errors, as each click verifies the information in memory first, then Redis, and finally, the database if needed.

Step four addresses the timing of click counting. Redirect responses shouldn't wait for analytics to be processed. Instead, a small event indicating a click can be placed into a queue, and the redirect can respond immediately. Subsequently, a separate job can tally the clicks in the background, ensuring minimal impact on the user experience.

An important question arises when discussing the design: should the redirect response be a 301 or a 302? A 301 indicates to browsers that the link should be remembered permanently, resulting in reduced traffic but also preventing the ability to track and modify links post-deletion. Conversely, a 302 remains flexible, ensuring that every click is directed back to the system. The choice of response code depends on the interviewer's focus on analytics early in the discussion, as it dictates the desired behavior.

When a link becomes highly popular, such as going viral, the challenge arises from cache misses. Thousands of requests hitting the database simultaneously could cause significant performance issues. The solution involves keeping hot links in each server's memory for a brief period, allowing only one request to reload the link while the others wait.

For a comprehensive guide to designing a URL shortener, refer to the full version available on the author's website. It includes a visual diagram that can be incrementally expanded, a calculator to estimate the required resources based on your numbers, scenarios illustrating potential pitfalls, and code implementations in five different programming languages. The resource is freely accessible without any sign-up requirements.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Cursor for Godot: how to use Cursor on a Godot game, and let it run the game too

Cursor for Godot works today: open your Godot project folder in Cursor and its agent reads and edits your GDScript and scenes like any codebase. What it cannot do alone is run the game.

  • Cursor for Godot is an AI-powered code editor for Godot projects
  • Flockbay offers a free AI-powered MCP server to run the game
  • Install godot-tools extension, add project rule, and connect MCP server

레타트루타이드 3상 임상 결과와 분자 구조 탐색의 새로운 방법

레타트루타이드 3상 임상 결과와 분자 구조 탐색의 새로운 방법 2026년 11월 Medical Xpress 보도에 따르면, 노스웨스턴 대학 주도 두 건의 3상 임상시험(참가자 1,000명 이상)에서 실험적 비만 치료제 레타트루타이드(retatrutide)가 무릎 골관절염(OA) 환자의 체중을 평균 25% 감소시키고 통증을 유의미하게 개선했습니다.

More from Monday 9 November →