Skip to content

luharinos/token-management-service

Repository files navigation

πŸš€ Token Management Service

πŸ“– Overview

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.

🎯 Key Features

βœ… 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


πŸ› οΈ Tech Stack

Component Technology
Backend NestJS (TypeScript)
Database Redis (Atomic Operations, Pub/Sub)
Containerization Docker & Kubernetes
Monitoring Prometheus & Grafana
Load Balancer NGINX / Kubernetes Service

πŸ—οΈ System Architecture

        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β”‚          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) β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“– Key Design Decisions

βœ… 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.


πŸ’­ Logical Flow

Token Lifecycle


πŸš€ Setup & Installation

1️⃣ Clone the Repository

git clone https://github.com/luharinos/token-management-service.git
cd token-management-service

2️⃣ Install Dependencies

yarn install

3️⃣ Set Up Environment Variables

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

4️⃣ Start Redis Locally

Ensure Redis is running locally:

brew services start redis

5️⃣ Run the Application

yarn start:dev

πŸ”₯ API Endpoints

1️⃣ Generate Tokens

POST /tokens/generate

Request Body:

{
  "count": 10
}

Response:

{
  "tokens": ["abc123", "def456"]
}

2️⃣ Assign a Token

POST /tokens/assign

Response:

{
  "token": "abc123"
}

3️⃣ Unblock a Token (Available after 2 minutes)

POST /tokens/unblock

Request Body:

{
  "token": "abc123"
}

4️⃣ Delete a Token

DELETE /tokens/delete

Request Body:

{
  "token": "abc123"
}

5️⃣ Keep Token Alive (Prevent Expiry)

POST /tokens/keep-alive

Request Body:

{
  "token": "abc123"
}

🎯 Scaling Strategy

βœ… Redis Atomic Operations β†’ Prevents race conditions
βœ… Kubernetes Auto-Scaling β†’ Deploys multiple instances as needed


🎯 Future Enhancements

βœ… 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


🎯 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit changes
  4. Open a pull request

πŸ“„ License

This project is licensed under MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors