Sequencer

A sequencer is a system that generates unique, ordered identifiers for events or transactions in a distributed system. These unique IDs ensure consistency and order when multiple services interact or process events in parallel.

In a large distributed system, there can be millions of events happening every second, like liking a video on TikTok, leaving a comment on a blog, or uploading a photo to Snapchat. To keep track of these events, we need to give each one a unique ID.

Another common use of unique IDs is in databases, where they act as primary keys for each entry. Many databases use an auto-increment feature to generate these IDs, but in distributed systems with multiple nodes working independently, that doesn’t work. Instead, we need a way to create unique IDs across all the nodes, especially in systems like a sharded database.

These unique IDs are also helpful for tracking events in logs, making it easier to debug issues. For example, Uber’s Jaeger tracing system uses unique IDs to follow requests as they move through different services, helping engineers understand how events flow through the system.


Requirements for our unique ID system:

  1. Uniqueness: Each event must get its own unique ID so we can easily identify and track it.
  2. Scalability: The system should be able to create at least a billion unique IDs every day to handle a large number of events.
  3. Availability: Since events can happen even within nanoseconds, the system needs to be able to generate IDs for all these events without delay.
  4. Low Latency: The ID generation must be fast with minimal delay.
  5. 64-bit numeric ID: We’ll use 64-bit IDs because they provide more than enough space for many years to come.

Sequencer Architecture

a. Centralized Sequencer:
b. Distributed Sequencer (Preferred for Scalability):

Solution 1: UUID

A UUID (Universally Unique Identifier) is a 128-bit value used to uniquely identify information in computer systems. It ensures uniqueness across systems and time, making it ideal for distributed environments where multiple entities need to generate unique IDs without central coordination.

Types of UUIDs

Example of a UUID (UUIDv4):
550e8400-e29b-41d4-a716-446655440000
Downsides of UUIDs
Complete implementation of the UUID generation API using Spring Boot

Solution 2: Database-based Sequencer

Creating a database-based sequencer using Spring Boot involves using a relational database (e.g., MySQL, PostgreSQL, or H2) to generate sequential IDs. This can be useful when you want to generate unique, ordered IDs for records (e.g., order numbers, transaction IDs) that might need to be consistent across multiple instances of the application.

Complete implementation of the database sequencer using Spring Boot

Solution 3: Snowflake ID Generator

Snowflake is a popular solution for generating unique, ordered, 64-bit IDs.

dns-hierarchy
Complete implementation of the Snowflake ID generator using Spring Boot

Unique ID generation systems used by prominent companies