This is a scalable Token Management Service built with NestJS and Redis, designed to efficiently generate, assign, unblock, and manage unique tokens with auto-expiry mechanisms.
β
Assigns unique tokens dynamically with O(1) operations
β
Supports multi-instance deployments using Redis Atomic Operations
β
Ensures scalability and consistency across multiple application pods
β
Deployable in Docker & Kubernetes environments
| Component | Technology |
|---|---|
| Backend | NestJS (TypeScript) |
| Database | Redis (Atomic Operations, Pub/Sub) |
| Containerization | Docker & Kubernetes |
| Monitoring | Prometheus & Grafana |
| Load Balancer | NGINX / Kubernetes Service |
βββββββββββββββββββββββββββββββββββββββ
β API Gateway (NestJS) β
βββββββββββββββββββββββββββββββββββββββ€
β Token Service (NestJS) β
β βββββββββββββββββββββββββββ β
β β Redis Database β β
β β - Token Storage β β
β β - Auto-Expiry Handling β β
β βββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββ€
β **Design Patterns Used:** β
β - Factory Pattern (Token Service) β
β - Singleton (Logger, Config, Redis) β
β - Repository Pattern (Redis Access) β
βββββββββββββββββββββββββββββββββββββββ
β Redis Sorted Sets (ZSET) for Token Storage β Allows O(log N) insertions & retrievals. β Factory Pattern for Token Service β Standardized token creation & assignment. β Singleton Pattern for Logger, Config, and Redis Connection β Ensures a single instance across the application. β Repository Pattern for Redis Interactions β Abstracts direct Redis operations from business logic. β Auto-Cleanup Jobs β Keeps the token pool optimized without manual intervention. β Microservice Architecture β Supports horizontal scaling with multiple API instances.
git clone https://github.com/luharinos/token-management-service.git
cd token-management-serviceyarn installCreate a .env file in the root directory:
REDIS_URL=redis://localhost:6379
TOKEN_LIFETIME=60 # Token auto-expiry in seconds
KEEP_ALIVE_LIMIT=300 # Keep-alive timeout in seconds
MAX_TOKENS=10000 # Maximum tokens allowed in the pool
TOKEN_UNBLOCK_LIFETIME=120 # Token unblock lifetime in seconds
LOG_LEVEL=INFOEnsure Redis is running locally:
brew services start redisyarn start:devPOST /tokens/generateRequest Body:
{
"count": 10
}Response:
{
"tokens": ["abc123", "def456"]
}POST /tokens/assignResponse:
{
"token": "abc123"
}POST /tokens/unblockRequest Body:
{
"token": "abc123"
}DELETE /tokens/deleteRequest Body:
{
"token": "abc123"
}POST /tokens/keep-aliveRequest Body:
{
"token": "abc123"
}β
Redis Atomic Operations β Prevents race conditions
β
Kubernetes Auto-Scaling β Deploys multiple instances as needed
β
Implement Rate Limiting using API Gateway
β
Support JWT Authentication for secured access
β
Add Kafka or RabbitMQ for distributed messaging
β
Improve monitoring using Prometheus & Grafana
β
Configure Redis to run in cluster mode for better scalability and fault tolerance
- Fork the repository
- Create a feature branch
- Commit changes
- Open a pull request
This project is licensed under MIT License.
