This project provides an efficient and scalable unique ID generation system using Redis. Unlike traditional UUID-based approaches, this method optimizes database partitioning, improves query performance, and ensures structured ID generation.
UUIDs (UUID.randomUUID()) are common for unique ID generation, but they have significant drawbacks:
- 🚫 Poor Indexing – UUIDs are random, making database indexing inefficient.
- 📏 Larger Storage – UUIDs require 128 bits, whereas numeric IDs are more compact.
- ❌ No Sequential Order – UUIDs do not provide a time-based order, reducing performance in sequential-write databases.
This project offers a partition-friendly ID format that ensures uniqueness while optimizing database performance.
- ID Format:
[Prefix][Timestamp][Sequence](e.g.,ORDER_231025_000000123)- Prefix: Categorizes data (e.g.,
ORDER,USER). - Timestamp: Date in
yyMMddformat for time-based partitioning. - Sequence: Incremental number ensuring uniqueness.
- Prefix: Categorizes data (e.g.,
- Benefits:
- Efficient time-based partitioning in databases.
- Faster queries and optimized storage.
- Avoids fragmentation issues common with UUIDs.
- Local Service: Generates IDs from a pre-reserved batch, reducing Redis dependency.
- Redis Backend: Tracks the highest reserved ID for global uniqueness.
- Benefits:
- Minimizes latency by reducing Redis calls.
- Ensures consistency across distributed systems.
- Producer-Consumer Model:
- Producer: Reserves a batch of IDs from Redis and pre-generates IDs.
- Consumer: Retrieves IDs from a thread-safe queue.
- Benefits:
- Prevents race conditions in multi-threaded environments.
- Ensures smooth ID generation without conflicts.
-
ID Preallocation
- The local service requests a range of IDs from Redis (e.g.,
1000 - 1999). - Redis updates the highest reserved ID and returns the range.
- The local service requests a range of IDs from Redis (e.g.,
-
Local ID Generation
- The service generates IDs using the format
[Prefix][Timestamp][Sequence]. - A
LinkedBlockingQueuestores IDs for fast retrieval.
- The service generates IDs using the format
-
Concurrency Handling
- Multiple threads retrieve IDs safely from the queue.
- When the queue is almost empty, the producer requests a new batch from Redis.
-
Redis Ensures Global Uniqueness
- Redis maintains the highest allocated ID, preventing duplication.
IdGenerator– Generates IDs based on reserved ranges.RangeProducer– Requests new ID ranges from Redis.RedisRangeProducer– Implements Redis-based ID reservation via Lua script.RedisConfig– Configures Redis connection and script integration.RedisApplication– Main application for multi-threaded ID generation.
- Java 11+
- Redis Server
- Spring Boot 2.7.7
keyPrefix=ORDER # Prefix for IDs (e.g., ORDER, USER)
reserveCount=100 # Number of IDs reserved per batch
numberProducer=2 # Number of producer threads- Start Redis.
- Run the application:
./gradlew bootRun
| Feature | Redis-based ID | UUID |
|---|---|---|
| Partitioning | ✅ Optimized (time-based) | ❌ Poor (random) |
| Performance | ✅ High (batch reserve) | ❌ Low (random) |
| Storage Size | ✅ Compact (20-30 chars) | ❌ Large (36 chars) |
| Sequential Order | ✅ Yes (incremental) | ❌ No |
| Complexity | ⚖️ Moderate | ✅ Simple |
- Scalability – Each service instance generates IDs independently.
- Efficiency – Reduces Redis calls for better performance.
- Better Partitioning – Unlike UUIDs, structured IDs help database indexing.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature
- Make your changes and commit them.
- Push to the branch.
- Open a Pull Request.
This project is licensed under the MIT License. See LICENSE for details.