Chaturmind
LearnDSASystem DesignDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Low-Level Design & Design Patterns

OOP Fundamentals & SOLID Principles

  • Object-Oriented Design Refresher
  • Single Responsibility & Open/Closed
  • Liskov Substitution & Interface Segregation
  • Dependency Inversion

Creational Design Patterns

  • Singleton Pattern
  • Factory & Abstract Factory
  • Builder Pattern
  • Prototype Pattern

Structural Design Patterns

  • Adapter & Facade
  • Decorator Pattern
  • Composite & Proxy
  • Bridge & Flyweight

Behavioral Design Patterns

  • Strategy Pattern
  • Observer Pattern
  • Command Pattern
  • Template Method
  • State Pattern
  • Chain of Responsibility
  • Iterator & Mediator
  • Visitor Pattern

Machine Coding Exercises

  • Parking Lot — Requirements & Class Design
  • Parking Lot — Implementation
  • Elevator System — Requirements & Class Design
  • Elevator System — Implementation & Scheduling
  • Library Management System
  • Tic-Tac-Toe / Board Game Design
  • Chess Engine Design
  • Vending Machine Design
  • ATM System Design
  • In-Memory Rate Limiter
  • Thread-Safe LRU Cache
  • Connection Pool Design
  • Producer-Consumer Class Design
  • Movie Ticket Booking System
  • Hotel Reservation System
  • Airline Booking / Seat Selection System
  • Splitwise / Expense Sharing System
  • Design a Logging Framework
  • Design a Notification/Observer-Based Pub-Sub
  • Design a Cache with Pluggable Eviction Policy
  • Design Snake and Ladder
  • Design a Card Game Framework
  • Design an Inventory Management System
  • Restaurant Management System
  • Food Delivery Order Matching
  • Design a Shopping Cart & Checkout Flow
  • Design a Recommendation Engine
  • Design a Job Scheduler
  • Design a Circuit Breaker
  • Design a Distributed ID Generator
  • Design a Pub-Sub Message Broker
  • Design Twitter/X
  • Multi-Pattern Problem: Design a Ride Booking Class Model
  • Multi-Pattern Problem: Design a Food Ordering Class Model
  • Design an Authentication/Authorization Module
  • Design a Form/Survey Builder
  • Design a Meeting Room / Calendar Booking System
  • Design a Search/Filter Engine for an E-Commerce Catalog
  • Design a Voting/Polling System
  • Design a Retry Mechanism with Backoff
  • Design a Health Check Aggregator
  • Design a Feature Flag System
  • Design a Config Management Client
  • Design a Leader Election Algorithm
  • Design a Distributed Counter
  • Design a Bloom Filter
  • Design a Consistent Hashing Ring
  • Design a Distributed Tracing Library
  • Design a Metrics Collection Library
  • Design an Audit Logging Framework
  • Design a Plugin/Extension System
  • Design a Workflow Engine
  • Multi-Pattern Capstone: Design a Food Delivery App
Chaturmind
← Low-Level Design & Design Patterns

OOP Fundamentals & SOLID Principles

  • Object-Oriented Design Refresher
  • Single Responsibility & Open/Closed
  • Liskov Substitution & Interface Segregation
  • Dependency Inversion

Creational Design Patterns

  • Singleton Pattern
  • Factory & Abstract Factory
  • Builder Pattern
  • Prototype Pattern

Structural Design Patterns

  • Adapter & Facade
  • Decorator Pattern
  • Composite & Proxy
  • Bridge & Flyweight

Behavioral Design Patterns

  • Strategy Pattern
  • Observer Pattern
  • Command Pattern
  • Template Method
  • State Pattern
  • Chain of Responsibility
  • Iterator & Mediator
  • Visitor Pattern

Machine Coding Exercises

  • Parking Lot — Requirements & Class Design
  • Parking Lot — Implementation
  • Elevator System — Requirements & Class Design
  • Elevator System — Implementation & Scheduling
  • Library Management System
  • Tic-Tac-Toe / Board Game Design
  • Chess Engine Design
  • Vending Machine Design
  • ATM System Design
  • In-Memory Rate Limiter
  • Thread-Safe LRU Cache
  • Connection Pool Design
  • Producer-Consumer Class Design
  • Movie Ticket Booking System
  • Hotel Reservation System
  • Airline Booking / Seat Selection System
  • Splitwise / Expense Sharing System
  • Design a Logging Framework
  • Design a Notification/Observer-Based Pub-Sub
  • Design a Cache with Pluggable Eviction Policy
  • Design Snake and Ladder
  • Design a Card Game Framework
  • Design an Inventory Management System
  • Restaurant Management System
  • Food Delivery Order Matching
  • Design a Shopping Cart & Checkout Flow
  • Design a Recommendation Engine
  • Design a Job Scheduler
  • Design a Circuit Breaker
  • Design a Distributed ID Generator
  • Design a Pub-Sub Message Broker
  • Design Twitter/X
  • Multi-Pattern Problem: Design a Ride Booking Class Model
  • Multi-Pattern Problem: Design a Food Ordering Class Model
  • Design an Authentication/Authorization Module
  • Design a Form/Survey Builder
  • Design a Meeting Room / Calendar Booking System
  • Design a Search/Filter Engine for an E-Commerce Catalog
  • Design a Voting/Polling System
  • Design a Retry Mechanism with Backoff
  • Design a Health Check Aggregator
  • Design a Feature Flag System
  • Design a Config Management Client
  • Design a Leader Election Algorithm
  • Design a Distributed Counter
  • Design a Bloom Filter
  • Design a Consistent Hashing Ring
  • Design a Distributed Tracing Library
  • Design a Metrics Collection Library
  • Design an Audit Logging Framework
  • Design a Plugin/Extension System
  • Design a Workflow Engine
  • Multi-Pattern Capstone: Design a Food Delivery App
HomeLearnSystem DesignLow-Level Design & Design PatternsMachine Coding Exercises
✓ FreeAdvanced· 5 min read

Design a Distributed ID Generator

The Snowflake ID generator class, focused specifically on the clock-drift edge case and sequence-counter thread safety that the URL Shortener case's version didn't dwell on.

Published September 23, 2026


Design a Distributed ID Generator

Design a URL Shortener's implementation section already built a full SnowflakeIdGenerator (timestamp + machine ID + sequence bits, synchronized for thread safety). This lesson focuses specifically on the one real edge case that implementation glossed over: clock drift.

The clock-drift problem

synchronized long nextId() {
    long timestamp = System.currentTimeMillis();
    if (timestamp < lastTimestamp) {
        // SYSTEM CLOCK MOVED BACKWARD — e.g. NTP correction, VM migration, manual clock adjustment
        throw new IllegalStateException(
            "Clock moved backwards. Refusing to generate id for " + (lastTimestamp - timestamp) + "ms");
    }
    // ... normal sequence logic from the URL Shortener version continues here
}

Snowflake's uniqueness guarantee depends on time moving forward — the timestamp bits are what make IDs from the same machine at different moments distinct. If the system clock jumps backward (an NTP time-sync correction, a VM live-migration pause, a manual clock change), a naive implementation could generate an ID with a timestamp smaller than one it already issued — a genuine collision risk, since two different real moments could map to the same (timestamp, machineId, sequence) triple.

Handling strategies, from strictest to most lenient

  • Refuse and throw (shown above): the strictest, safest option — the generator simply stops issuing IDs during the backward-clock window rather than risk a collision. Simple to reason about, but means a real (if usually brief) service disruption during a clock correction.
  • Wait it out: block until the clock catches back up past lastTimestamp, then resume — avoids an outright failure, at the cost of unpredictable latency spikes on affected calls during the drift window.
  • Borrow from a dedicated 'drift' bit range: some Snowflake variants reserve a small extra bit or use a logical (not wall-clock) counter specifically to tolerate small backward jumps without failing — more complex, reserved for systems where even a brief refusal-to-generate window is unacceptable.

Naming this tradeoff explicitly — "refuse-and-throw is simplest and usually correct for how rarely and briefly clocks actually drift backward in practice" — is a stronger answer than either ignoring the edge case entirely or over-engineering a solution to a genuinely rare failure mode.

Thread safety for the sequence counter

The synchronized keyword on nextId() (from the URL Shortener version) is the simplest correct fix, but worth stating the alternative explicitly for a follow-up: an AtomicLong-based CAS loop for the sequence counter specifically (see Visibility & Memory Model's Atomic classes) could reduce contention under very high-throughput ID generation, at the cost of more intricate logic to keep the sequence-reset-on-new-millisecond behavior correct without a full lock — a real engineering tradeoff between simplicity (synchronized, correct by construction) and throughput (lock-free, more subtle to get right), not a strictly-better upgrade in either direction.

Follow-up questions this topic invites — and their answers

Q: Why not just use a UUID instead of building a custom Snowflake generator? A: A UUID (particularly UUIDv4, random-based) has no inherent ordering — Snowflake IDs are roughly time-sortable (later-generated IDs are numerically larger), which matters for use cases needing natural chronological ordering (e.g. a database primary key benefiting from insert-order locality) that a random UUID can't provide.

Q: How severe is the clock-drift problem in practice — is this over-engineering? A: Genuinely rare in well-run infrastructure (NTP is usually configured to slew time gradually rather than jump it, specifically to avoid this class of problem) — but 'rare' isn't 'never,' and a production ID generator that silently produces a duplicate ID during a clock-drift event is a correctness bug with real downstream consequences (a broken unique-key constraint, a lost record), which is exactly why explicitly handling it (even with the simplest refuse-and-throw strategy) is worth the modest added code.

Q: What's the actual failure mode if TWO machines briefly generate IDs with the same machine ID (a misconfiguration)? A: A much more severe problem than clock drift — the machine ID is supposed to be the mechanism that guarantees uniqueness ACROSS machines; two generators sharing a machine ID could produce genuinely colliding IDs even with perfectly synchronized clocks, which is why machine ID assignment (via config, or a coordination service handing out unique IDs at startup) needs to be at least as carefully guarded as the clock-drift handling itself.

Q: Does the URL Shortener case's version need this clock-drift handling added retroactively? A: Yes, in a fully production-hardened version — this lesson's focus on the edge case specifically is what that earlier, simpler implementation deliberately deferred, exactly the kind of incremental depth-building this course uses rather than re-deriving the whole class from scratch a second time.

Previous

Design a Circuit Breaker

Next

Design a Pub-Sub Message Broker

AI Tutor

Lesson: Design a Distributed ID Generator

Quick actions

AI responses can be inaccurate. Verify critical information.