This project demonstrates a unique ID generation system similar to Instagram's snowflake ID generation, implemented in MySQL with a Java client application. The system ensures distributed unique IDs for database entries using a combination of timestamps, shard IDs, and sequence numbers.
Instagram generates unique IDs using a technique similar to Twitter's Snowflake. This method combines:
- Timestamp (41 bits): Represents the number of milliseconds since a custom epoch (e.g., 1314220021721). Ensures IDs are roughly sortable by creation time.
- Shard ID (13 bits): Helps in partitioning the data across multiple database instances, supporting up to 8192 shards.
- Sequence Number (10 bits): Prevents collision within the same shard and timestamp, allowing up to 1024 unique IDs per shard per millisecond.
- Instagram's approach can generate up to 1024 IDs per millisecond per shard, using a 10-bit sequence number.
- With 8192 shards (13 bits for shard ID), this system can generate over 8 million IDs per millisecond.
-
script_1.sql
:- Drops and recreates the database
virtual_db1
.
- Drops and recreates the database
-
script_2.sql
:- Creates the
seq_tracker
table to maintain sequence counters. - Inserts an initial shard entry for shard ID
1
.
- Creates the
-
script_3.sql
:- Creates the
seq_counter
table to store generated IDs and associated user data.
- Creates the
-
script_4.sql
:- Defines the stored procedure
generate_id_virtual_db1
to generate unique IDs based on timestamp, shard ID, and sequence number.
- Defines the stored procedure
-
script_5.sql
:- Creates a trigger
before_insert_virtual_db1
to generate a unique ID for each newseq_counter
entry.
- Creates a trigger
This Java program provides functionality to:
- Insert a user into the database and generate a unique ID.
- Fetch and display the contents of the
seq_counter
table. - Clear the
seq_counter
table.
This Java program demonstrates concurrent user insertion using a thread pool.
- Database-Locked ID Generation: Ensures atomic updates to the sequence number using MySQL locks.
- Epoch-Based Unique ID: Combines a custom epoch timestamp, shard ID, and sequence number to ensure uniqueness.
- Concurrency Handling: Supports concurrent insertions with guaranteed unique ID generation.
- MySQL 8.0 or higher.
- Java 8 or higher.
- JDBC Driver for MySQL.
-
Database Setup:
- Run the SQL scripts in the following order:
-
Java Compilation and Execution:
- Compile the Java files:
- Run the Java programs:
-
Observe Output:
- Monitor the generated unique IDs and database contents via the program outputs.
Column | Type | Description |
---|---|---|
shard_id |
INT | Unique ID of the shard. |
seq_id |
BIGINT | Sequence number for the shard. |
Column | Type | Description |
---|---|---|
shard_id |
INT | Shard ID of the entry. |
seq_id |
BIGINT | Unique ID generated for the entry. |
userInsertion |
VARCHAR(255) | User ID associated with the generated ID. |
- Timestamp: Encodes the current time relative to a custom epoch.
- Shard ID: Differentiates entries across shards.
- Sequence Number: Ensures uniqueness within the same timestamp and shard.
- Triggers: Automates ID generation before row insertion.
- Stored Procedures: Encapsulates the logic for ID generation.
- Locks: Ensures atomic updates to sequence counters.
- Concurrency: Demonstrates concurrent database operations using a thread pool.
- JDBC: Handles database connections and SQL execution.
- Add support for dynamic shard IDs.
- Implement distributed systems integration for ID generation.
- Add logging and exception handling improvements.
This project is licensed under the MIT License.