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-service
yarn install
Create 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=INFO
Ensure Redis is running locally:
brew services start redis
yarn start:dev
POST /tokens/generate
Request Body:
{
"count": 10
}
Response:
{
"tokens": ["abc123", "def456"]
}
POST /tokens/assign
Response:
{
"token": "abc123"
}
POST /tokens/unblock
Request Body:
{
"token": "abc123"
}
DELETE /tokens/delete
Request Body:
{
"token": "abc123"
}
POST /tokens/keep-alive
Request 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.