System Design AI

Design a Unique ID Generator: Junior, Senior, and Staff Answers

When an interviewer asks you to design a unique ID generator, they are looking for how you balance three classic system-design dimensions: scalability, ordering guarantees, and fault tolerance. They also watch how you translate abstract requirements into concrete AWS components, and how you think about failure modes and operational overhead.

What the Interviewer Is Testing

  • Scalability – Can you handle a burst of requests without a bottleneck?
  • Ordering & Uniqueness – Do you guarantee that no two requests ever receive the same ID, and can you provide roughly monotonic order if needed?
  • Latency – Many services need IDs quickly; a few milliseconds is typical.
  • Operational Simplicity – Do you choose components that are easy to monitor and maintain?
  • Trade-off Reasoning – Which guarantees do you sacrifice for performance, and why?

Core Requirements and Driving Numbers

RequirementTypical Value (hedged)
ThroughputRoughly 10 million IDs per day (≈115 IDs/second)
ID Size≤ 1 KB per request (mostly just the ID)
Latency< 10 ms per call
Availability99.9 % (up to roughly 8.8 hours of outage per year)
ConsistencyStrong uniqueness; ordering optional

These numbers drive the design. Ten million per day is modest for modern cloud services, but the design must still avoid a single point of contention.

Core Design Walkthrough

Below is a step-by-step request path that works for all three interview levels. The diagram would start with the client, then progress through the components.

  1. Client → API Gateway – The entry point. API Gateway validates the request and forwards it to a Lambda function. This adds negligible latency and gives you built-in throttling.
  2. Lambda → DynamoDB (or Aurora) – The Lambda either writes a row with an auto-increment attribute (Aurora) or performs an atomic UpdateItem on a counter in DynamoDB. DynamoDB’s conditional update guarantees uniqueness without a lock.
  3. DynamoDB → ID Formatter – The Lambda reads the counter value, optionally combines it with a timestamp or a shard prefix, and returns the formatted ID.
  4. Response → Client – The ID is sent back over the same HTTP connection.

Data Model

  • Table: UniqueIdCounters
  • PK: shard_id (string) – allows horizontal scaling.
  • Attribute: counter (number) – stores the next sequence value.
  • Optional: A secondary table IdMetadata for audit logs, written asynchronously to S3 via an SQS queue.

Key Choice

The central decision is how to generate the ID. Common patterns:

  • Auto-increment – Simple but creates a hot partition if all traffic hits a single row.
  • Time-based (Snowflake-style) – Encode timestamp, region, and sequence bits. Provides roughly monotonic ordering and distributes load.
  • Hash-based UUID – Guarantees uniqueness without coordination, but IDs are larger and not ordered.

For most interview scenarios, a sharded time-based ID wins: each shard gets its own counter, and the final ID is timestamp | shard_id | sequence. This avoids a single hot row and gives you ordering within a shard.

Junior Answer

A junior candidate should cover the basic flow and justify choices.

  • Components: API Gateway, Lambda, DynamoDB table with a single counter.
  • Why DynamoDB? – Offers atomic UpdateItem which guarantees uniqueness without a lock manager.
  • Scaling: Mention that DynamoDB can autoscale read/write capacity; the design handles the 10 M/day target easily.
  • Failure handling: Simple retry on Lambda side for throttling errors.
  • Operations: Basic CloudWatch alarms on DynamoDB throttles.

Missing elements that would be expected at higher levels include sharding, latency budgeting, and a discussion of ordering guarantees.

Senior Answer

A senior candidate builds on the junior foundation.

  • Sharding: Introduce a configurable number of shards (e.g., 16). The client sends a shard_id or the Lambda hashes the client IP to pick a shard, spreading write load.
  • Time-based component: Prefix the counter with a millisecond timestamp (UTC). This yields IDs like 20231115123456-03-00123.
  • Cache for hot reads: Use ElastiCache (Redis) to store the latest counter per shard, reducing DynamoDB read-write contention. Writes still go through DynamoDB for durability.
  • Back-pressure: Throttle at API Gateway and hand each Lambda instance a pre-allocated block of IDs, so a spike does not turn into a spike of database writes. The caller is waiting for the ID, so a queue cannot sit in this path.
  • Observability: Add X-Ray tracing, detailed CloudWatch dashboards for per-shard latency, and a dead-letter queue for failed Lambda invocations.
  • Cost awareness: Estimate DynamoDB write capacity (≈115 writes/sec) and note that on-demand pricing would be cheap for this load.

The senior answer shows an awareness of hot-spot mitigation, latency budgeting, and operational metrics.

Staff Answer

A staff-level answer adds depth in three areas: failure modes, operational strategy, and trade-off justification.

  • Multi-region replication: Deploy the stack in two regions (e.g., us-east-1 and eu-west-1), give each region its own counter rows, and put a region ID in every ID. A counter shared through a global table can be incremented in both regions at once and hand out the same value twice. Use Route 53 latency-based routing to send clients to the nearest region. This improves availability and reduces latency for global users.
  • Failure modes:
  • DynamoDB outage: Fall back to a locally cached pre-allocated block of IDs stored in ElastiCache. When the block runs out, the Lambda returns a 503 until DynamoDB recovers.
  • Network partition: Use SQS as a durable buffer; IDs are only considered committed after DynamoDB write succeeds, ensuring no duplicates.
  • Operational tooling:
  • Automated Terraform scripts to provision the whole stack.
  • Continuous integration tests that simulate throttling and region failover.
  • Cost guardrails: at roughly 10 million requests a day, the per-request charges for Lambda, DynamoDB writes and API Gateway add up to tens of dollars a day, not cents; work the number out from current pricing before you quote it.
  • Trade-off defense:
  • Ordering vs. throughput: Explain why strict global ordering is sacrificed for sharding; most applications only need per-shard ordering.
  • Complexity vs. reliability: Adding global tables and cache layers increases operational overhead but yields sub-10 ms latency for worldwide traffic.
  • Metrics:
  • 99.9 % SLA measured by API Gateway latency.
  • Error rate < 0.01 % across regions.
  • Write capacity utilization < 30 % to leave headroom for spikes.

A staff answer demonstrates system-wide thinking, risk mitigation, and cost-benefit analysis.

Common Mistakes That Sink This Question

MistakeWhy It Fails
Using a single auto-increment row in DynamoDBCreates a hot item; one item tops out at roughly a thousand writes/sec, which covers this target but caps growth and puts every request on one row.
Ignoring ordering requirementsIf the product needs roughly monotonic IDs, a pure UUID will be rejected for lack of predictability.
Over-engineering with a full microservice meshAdds unnecessary latency and operational burden for a simple ID service.
Forgetting to handle DynamoDB throttlingLeads to unhandled exceptions and failed client calls.
Not budgeting latencyWithout a cache or regional routing, latency can exceed the 10 ms target, especially for global users.

How to Practice This

  1. Sketch the request path on a whiteboard or in System Design AI’s canvas. Start with the client and add API Gateway, Lambda, DynamoDB, and optional cache layers.
  2. Write a brief rubric: list the junior, senior, and staff expectations (components, sharding, multi-region, failure handling). Use the rubric to self-grade.
  3. Iterate: Build the junior version, then add sharding and caching for the senior version, and finally add global tables and ops tooling for the staff version. Review each iteration with the AI coach to catch missing pieces.

Questions people ask

What is the simplest way to guarantee unique IDs in AWS?

Use a DynamoDB table with a primary key that stores a counter and perform an atomic UpdateItem. DynamoDB guarantees that each increment returns a unique number.

Why not just use UUIDs for a unique ID generator?

UUIDs are unique without coordination, but they are larger (128 bits) and not ordered. Many services prefer shorter, roughly monotonic IDs for easier debugging and sharding.

How do you avoid a hot partition when generating IDs?

Shard the counter across multiple rows (e.g., 16 shards) and let each request pick a shard. Optionally prepend a timestamp so IDs are ordered within a shard.

What AWS service helps with cross-region availability for an ID service?

DynamoDB global tables replicate data across regions automatically. Pair them with Route 53 latency-based routing to keep latency low for worldwide clients.