PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Microsoft

Design a scalable URL shortener

Last updated: Jun 15, 2026

Quick Overview

Design a scalable URL shortener with create and redirect APIs, Base62 short-code generation, global uniqueness, KV storage, caching, CDN optimization, sharding, replication, abuse prevention, observability, expiration, analytics, and hash-collision handling.

  • easy
  • Microsoft
  • Coding & Algorithms
  • Data Engineer

Design a scalable URL shortener

Company: Microsoft

Role: Data Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

##### Question Design a **highly scalable URL shortening service** (like bit.ly / TinyURL) that converts long URLs into short links and supports redirection. Cover the architecture, data model, short-code generation, uniqueness, caching, scaling, and operational concerns below. 1. **Functional requirements** - Provide an API to **create** a short URL for a given long URL. - Provide an API to **redirect (resolve)** a short code back to the original long URL. - The mapping must be **globally unique** (no unintended collisions) and **reversible** (given a short code, you can always retrieve the original URL via lookup). 2. **Non-functional requirements & assumptions** - High availability and **high QPS**, with reads/redirects typically far outnumbering writes. - Support **horizontal scaling** across multiple instances/regions. - Short codes should be as short as possible (e.g., **6–10 characters**) and **URL-safe**. - Low redirect latency (e.g., **p95 < 50 ms**); the service must tolerate **partial failures**. - State any other assumptions you need. 3. **APIs** - Define example endpoints for creating short links and performing redirects (request/response shapes, status codes). 4. **Data model & storage** - What you store per short link (e.g., `short_code`, `long_url`, `created_at`, `expiry`, `owner_id`). - Choice of datastore(s): KV store vs relational DB vs both, and why. 5. **Short-code generation strategy** - **Option A:** hash the long URL. - **Option B:** generate globally unique IDs (e.g., Snowflake / auto-increment) and **Base62**-encode them. - How you ensure uniqueness and performance under concurrency. 6. **Guaranteeing global uniqueness** across multiple instances/regions. 7. **Caching & performance** - Use of in-memory cache (e.g., Redis) and/or CDN for hot links. - Cache key design, TTL, and cache-miss behavior. 8. **Scalability & reliability** - Partitioning/sharding strategy. - Replication, failover, and consistency trade-offs. 9. **Operational concerns** - Abuse prevention (rate limits), observability/metrics, link expiration, and analytics logging (optional). 10. **Follow-up:** If you use a **hash** of the long URL to generate the short code, **how do you detect and handle hash collisions?** Provide at least two approaches and their trade-offs.

Quick Answer: Design a scalable URL shortener with create and redirect APIs, Base62 short-code generation, global uniqueness, KV storage, caching, CDN optimization, sharding, replication, abuse prevention, observability, expiration, analytics, and hash-collision handling.

Solution

A scalable URL shortener has a write path for link creation and a much hotter read path for redirects. The read path should be simple: short code to long URL lookup, then return a redirect. Anything non-critical, such as analytics aggregation, should be asynchronous. Example APIs: ```http POST /api/v1/shorten Content-Type: application/json { "long_url": "https://example.com/some/long/path", "expiry": "2027-01-01T00:00:00Z", "custom_alias": "optional" } ``` Response: ```json { "short_url": "https://sho.rt/Ab3x9K", "short_code": "Ab3x9K", "long_url": "https://example.com/some/long/path" } ``` Redirect: ```http GET /Ab3x9K 302 Found Location: https://example.com/some/long/path ``` Use `302` if the service wants every redirect to hit the platform for analytics, expiration, or policy checks. Use `301` only when permanent browser and CDN caching is acceptable. The core data model is: ```text ShortLink( short_code primary key, long_url, owner_id, created_at, expires_at, status, created_by_ip ) ``` The access pattern is a point lookup by `short_code`, so a KV store or wide-column store such as DynamoDB or Cassandra is a good source of truth at large scale. A relational database can handle accounts, billing, and owner-facing list views. Shard the mapping table by a hash of `short_code` so traffic spreads evenly. Replicate across availability zones; reads can use replicas because mappings are mostly immutable after creation. For code generation, I would prefer globally unique ID plus Base62 encoding. Generate a 64-bit ID using a Snowflake-style layout with timestamp, region or machine ID, and sequence number, then Base62 encode it. This is collision-free by construction if machine IDs or ID ranges are unique. A 7-character Base62 code has about `62^7`, or roughly 3.5 trillion, possibilities. The database should still enforce uniqueness with a conditional insert or unique constraint as the final guard. Hashing the long URL is another option. It is deterministic and can dedupe identical URLs, but truncating the hash introduces collisions. If using hashes, handle collisions with conditional insert. If the generated code already exists for the same long URL, return the existing link. If it exists for a different URL, retry with a salt or nonce, or take more hash characters. The tradeoff is extra reads/writes and potentially longer codes. At high scale, unique ID plus Base62 is usually cleaner. The redirect path should be optimized with cache-aside: 1. Read `short_code` from Redis or another fast cache. 2. On hit, redirect immediately. 3. On miss, read from the mapping store, backfill cache, and redirect. 4. Cache negative lookups briefly to reduce scanning abuse. TTL should not exceed link expiry. For very hot links, use CDN or edge caching when analytics requirements allow it. To avoid cache stampedes, use request coalescing or single-flight on hot misses. For multi-region active-active writes, partition ID space by region or include a region ID in the Snowflake machine component. Avoid globally synchronized counters on the hot path. Replicate mappings asynchronously, accepting that a newly created link may take a short time to become readable everywhere unless the product requires stronger consistency. Operational concerns: - Rate limit creation by user, IP, and account. - Check links for malware, phishing, and policy violations. - Expire links through TTL or background sweepers. - Log redirect events asynchronously through a queue or stream. - Track QPS, p95/p99 redirect latency, cache hit rate, error rate, DB latency, link creation failures, abuse blocks, and redirect success. - If analytics is down, redirects should still work. - If cache is down, fall back to the database with rate protection. The strong design summary is: use globally unique IDs encoded as Base62, store `short_code -> long_url` in a sharded replicated mapping store, make redirects cache-heavy and low latency, keep analytics asynchronous, and use conditional writes, rate limits, observability, and abuse checks to keep the system reliable.

Explanation

Rubric: a strong answer nails the read-heavy nature of the workload (cache/CDN the redirect path), picks an appropriate datastore for point lookups, and — critically — chooses unique-ID + Base62 over URL-hashing to make codes collision-free by construction, while still being able to articulate hash-collision handling for the follow-up. Senior signals: disjoint ID ranges / Snowflake for cross-instance uniqueness without hot-path coordination, conditional inserts as the datastore-level uniqueness guard, 301-vs-302 trade-off, eventual-consistency tolerance on reads, and graceful degradation under partial failure.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
|Home/Coding & Algorithms/Microsoft

Design a scalable URL shortener

Microsoft logo
Microsoft
Jan 22, 2025, 12:00 AM
easyData EngineerTechnical ScreenCoding & Algorithms
12
0

Design a highly scalable URL shortening service, similar to Bitly or TinyURL. The service converts long URLs into short links and redirects short-code requests back to the original URL.

Constraints & Assumptions

  • Redirect reads are much higher volume than short-link creation writes.
  • Short codes should be URL-safe, globally unique, and preferably 6 to 10 characters.
  • Redirection should be low latency and highly available.
  • The mapping must be reversible by lookup: given a short code, the service can retrieve the original URL.
  • Include abuse prevention and operational concerns.

Clarifying Questions to Ask

  • What expected read QPS, write QPS, and total number of links should we design for?
  • Are custom aliases required?
  • Should redirects be permanent or temporary for browser caching and analytics?
  • Do links expire?
  • Is active-active multi-region support required on day one?

Part 1 - Define Requirements And APIs

What functional and non-functional requirements would you support, and what APIs would you expose?

What This Part Should Cover

  • Create short link and resolve or redirect short code.
  • Example request and response shapes, status codes, validation, and error cases.
  • Latency, availability, scalability, and partial-failure assumptions.

Part 2 - Design Storage And Data Model

What data would you store, and what datastore would you choose?

What This Part Should Cover

  • Mapping from short_code to long_url plus metadata such as owner, expiry, created time, status, and analytics flags.
  • KV or wide-column store for high-volume point lookups, with relational storage for accounts and admin metadata if needed.
  • Sharding, replication, and consistency tradeoffs.

Part 3 - Generate Globally Unique Short Codes

How would you generate short codes and guarantee uniqueness across instances and regions?

What This Part Should Cover

  • Hash-based option and its collision tradeoffs.
  • Unique ID plus Base62 option.
  • Conditional insert or unique constraint as the final arbiter.
  • Region-aware or shard-aware ID generation for active-active operation.

Part 4 - Optimize Redirect Performance

How would you handle caching, scaling, and reliability?

What This Part Should Cover

  • Redis or in-memory cache, CDN or edge caching, cache-aside behavior, TTLs, negative caching, and cache stampede prevention.
  • Partitioning by short code, read replicas, failover, and degraded behavior when analytics or cache fails.

Part 5 - Handle Operations And Hash Collisions

How would you handle abuse, observability, expiration, analytics, and hash collisions?

What This Part Should Cover

  • Rate limiting, malware/phishing checks, structured metrics, redirect logs, async analytics, TTL cleanup, and collision-resolution strategies.
  • At least two hash-collision handling approaches and their tradeoffs.

What a Strong Answer Covers

  • Separates write path from read-heavy redirect path.
  • Chooses a collision-resistant code-generation strategy and explains uniqueness under concurrency.
  • Optimizes the hot redirect path without putting analytics on the critical path.
  • Discusses operational risks, abuse, observability, and failure handling.

Follow-up Questions

  • Why might you choose 302 instead of 301?
  • How many codes are possible with 7 Base62 characters?
  • How would you handle a celebrity link that becomes extremely hot?
  • How would you support custom aliases safely?
  • What changes for active-active multi-region writes?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Data Engineer•Microsoft Data Engineer•Microsoft Coding & Algorithms•Data Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.