System Design AI

Design a URL Shortener: The Junior, Senior and Staff Answer

When you hear "design a URL shortener" the interviewer is looking for three things: how you translate a simple user story into a scalable service, how you choose data structures and AWS primitives, and how you think about reliability and cost at scale.

What the interviewer is testing

  • Requirement gathering – can you elicit functional and non-functional needs?
  • System decomposition – do you break the request path into clear hops?
  • Trade-off reasoning – are you aware of latency vs. consistency, cost vs. durability?
  • Operational mindset – especially at senior and staff levels, do you think about monitoring, alerts, and failure isolation?

Core requirements and driving numbers

RequirementTypical value (hedged)
Total URLs storedon the order of 100 million (roughly 10 GB if each entry is ~100 bytes)
Read trafficabout 1 k requests / second (≈86 M per day)
Write trafficabout 100 requests / second (≈8.6 M per day)
Availability99.9 % uptime, sub-100 ms latency for redirects

These numbers drive three major design decisions: the choice of a low-latency key-value store, a caching layer to absorb the read burst, and a sharding strategy that can grow as traffic increases.

Core design in prose – request path hop by hop

  1. Client request – POST /shorten with a long URL.
  2. API layer – Amazon API Gateway forwards to a Lambda function.
  3. Key generation – Lambda creates a short token (e.g., base-62 of a counter or a hash). The token is the primary key.
  4. Persistence – The token and original URL are written to a DynamoDB table. A Global Secondary Index (GSI) on creation time enables analytics.
  5. Cache warm-up – The Lambda also writes the mapping to an ElastiCache Redis cluster (write-through). This ensures the next read hits the cache.
  6. Redirect – GET /{token} hits CloudFront edge. CloudFront serves the redirect from its own edge cache when it has one; on a miss the request goes to a Lambda that checks Redis first, falls back to DynamoDB, returns a 301, and updates the cache.
  7. Analytics – Each redirect fires an event to an SQS queue, later processed by a consumer Lambda that writes aggregates to Aurora for reporting.

Data model

  • Table: UrlMapping
  • PK (string) – the short token, e.g., b3K9X.
  • LongUrl (string) – the original URL.
  • CreatedAt (timestamp) – for TTL and analytics.
  • Optional fields: OwnerId, Expiration, Clicks (incremented via DynamoDB atomic counter).

The short token is chosen over a sequential numeric ID because it avoids exposing traffic volume and reduces predictability. A random or hash-based token also needs no shared counter, so there is no single counter row that every write must update. DynamoDB hashes the partition key, so either kind of token spreads evenly across partitions.

What a complete junior answer covers

  • Functional flow – describe the POST and GET paths.
  • Single DynamoDB table – explain why a NoSQL key-value store fits the low-latency requirement.
  • Basic caching – mention CloudFront for static assets and a simple in-memory cache (e.g., Redis) for redirects.
  • Scalability estimate – use the numbers above to show DynamoDB can handle 100 req/s writes and 1 k req/s reads with provisioned throughput or on-demand mode.
  • Missing pieces – a junior often omits async analytics, TTL handling, and operational concerns.

What a senior answer adds

  • Read-through cache pattern – detail how Redis is populated on write and refreshed on cache miss.
  • Sharding strategy – split the token space by prefix (e.g., first two characters) to distribute load across multiple DynamoDB tables or partitions.
  • Rate limiting – API Gateway usage plans or a Lambda authorizer to protect against abuse.
  • Partial consistency – explain eventual consistency of DynamoDB reads and why it is acceptable for redirects.
  • Monitoring – CloudWatch metrics for latency, error rates, and cache hit ratio; alarms on 5 xx spikes.
  • Cost awareness – estimate DynamoDB read/write units and Redis node count, showing how on-demand pricing scales with traffic.

What a staff answer adds

  • Failure modes – discuss DynamoDB regional outage, Redis node failure, and CDN edge cache miss storms. Show how a fallback to S3 static redirects can keep the service alive.
  • Operational playbook – describe a runbook for cache invalidation, table scaling, and automated backups (DynamoDB point-in-time recovery, S3 versioning for static assets).
  • Cost optimization – use DynamoDB TTL to purge expired URLs, reserve capacity for predictable traffic, and right-size the Redis cluster, with reserved nodes for the steady baseline.
  • Trade-off defense – argue why a hash token is chosen over a sequential ID despite the extra compute, citing partition hot-spot avoidance and security.
  • Advanced analytics – batch processing with Athena on S3-stored click logs, enabling A/B testing of token length.

Common mistakes that sink this question

MistakeWhy it hurts
Using a relational database for the primary mappingToo slow for sub-100 ms redirects is the wrong objection, since an indexed lookup takes milliseconds; what hurts is having no plan for growing writes and storage past one primary.
Generating a token by incrementing a global counterCreates a hot partition in DynamoDB and leaks traffic volume.
Ignoring cache miss storms after a cold startLeads to latency spikes and higher DynamoDB read cost.
Forgetting TTL or expiration handlingTable size grows unchecked, raising storage cost and scan time.
Over-engineering analytics in the core pathIncreases latency; analytics should be async.

Comparison of answer depth by level

AspectJuniorSeniorStaff
Core flowDescribes POST/GET, single DynamoDB tableAdds Redis cache, sharding, rate limitingIncludes fallback paths, runbooks, cost trade-offs
Data modelSimple PK-valuePK + GSI, TTL, atomic countersSame model + audit logs, versioning
ScalingOn-demand DynamoDB capacityPartition prefix sharding, read-through cacheMulti-region replication, right-sized Redis, async pipelines
ReliabilityBasic error handlingCache hit-ratio monitoring, alarmsFailure injection testing, disaster recovery plan
CostRough estimateDetailed unit calculations, reserved capacityCost-benefit analysis, lifecycle policies

How to practice this

  1. Study the worked design in the System Design AI library. It walks through the same token generation, DynamoDB schema, and caching steps.
  2. Write your own answer on the canvas. Start with the request path, then add layers (cache, analytics, ops) as you move from junior to senior to staff.
  3. Run a mock interview with a peer or the AI coach. Ask it to critique each layer and to point out missing failure scenarios.

Questions people ask

What is the simplest way to generate a short token?

Use a base-62 encoding of a randomly generated 64-bit integer. It yields about 1.8 × 10¹⁹ possible values, making collisions unlikely for the scale we target.

Do I need a relational database for analytics?

No. Capture click events in an SQS queue, process them with Lambda, and store aggregated counts in Aurora or Athena-queried files on S3 for reporting.

How do I handle a hot token prefix?

Split the token space by its first two characters and route each prefix to a separate DynamoDB partition or table. This spreads writes and avoids hot-spoting.

What monitoring metrics are critical?

Watch API latency, DynamoDB read/write throttling, Redis cache hit ratio, and CloudFront 4xx/5xx rates. Set alarms on any metric that drifts beyond acceptable thresholds.