Skip to content

Sophisticated Spring Java Kafka SAGA Transactional Outbox with Prometheus and other pointless extras

Notifications You must be signed in to change notification settings

Komdosh/leaky-tokens

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

126 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Leaky Tokens

Java Spring Boot Gradle License

CI Coverage

Enterprise-Grade Token Management & Rate Limiting System

πŸ“– Documentation β€’ πŸš€ Quick Start β€’ πŸ“Š Architecture β€’ πŸ”§ API


WHY?

This is an auto-generated project by AI agents. I already have experience with all of these technologies, but nobody cares if you don't have a GitHub repo with a pet projectβ€”so here it is.

Why do I use AI agents to generate projects? Because it's fun and I'm bored. Do I really work with all of these technologies? Yep. Ten years in, that's the answer.

  • But you still have to write code yourself, because AI agents are not perfect and can't write everything for you. And this project doesn't show your skills.
  • Well, that's the new world. I worked hard before. I wrote code day and night, but times have changed.

✨ Features

🎯 Core Capabilities

  • πŸͺ£ Leaky Bucket Rate Limiting - Smooth traffic flow
  • πŸ’° Token Quotas - Per-user & org-level management
  • ⚑ SAGA Orchestration - Distributed transactions
  • πŸ”‘ Multi-Auth - JWT + API Key support

πŸ—οΈ Architecture

  • ☁️ Cloud-Native - 12-Factor App ready
  • πŸ” Service Discovery - Eureka registry
  • βš–οΈ Load Balancing - Gateway routing
  • πŸ›‘οΈ Circuit Breaker - Resilience4j

πŸ“Š Observability

  • πŸ“ˆ Prometheus Metrics - Real-time monitoring
  • πŸ“‰ Grafana Dashboards - Visual insights
  • πŸ”Ž Distributed Tracing - Jaeger integration
  • πŸ“ Centralized Logs - Full audit trail

πŸ—„οΈ Data Layer

  • 🐘 PostgreSQL - Primary persistence
  • ⚑ Redis - Caching & rate limits
  • πŸ“¨ Kafka - Event streaming
  • πŸ“¦ Cassandra - Time-series analytics

πŸ›οΈ Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        🌐 API Gateway                           β”‚
β”‚                    (Port 8080 - Entry Point)                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚         β”‚         β”‚             β”‚
    β–Ό         β–Ό         β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  πŸ”   β”‚ β”‚  πŸͺ™    β”‚ β”‚  πŸ“Š    β”‚  β”‚  πŸ“      β”‚
β”‚ Auth  β”‚ β”‚ Token β”‚ β”‚Analyticsβ”‚ β”‚ Config   β”‚
β”‚ 8081  β”‚ β”‚ 8082  β”‚ β”‚  8083  β”‚  β”‚  8888    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                              
πŸ—„οΈ PostgreSQL  ⚑ Redis  πŸ“¨ Kafka  πŸ“¦ Cassandra

🎯 Service Registry

Service Port Purpose Status
🌐 API Gateway 8080 Entry point, routing, rate limiting βœ… Ready
πŸ” Auth Server 8081 JWT & API Key authentication βœ… Ready
πŸͺ™ Token Service 8082 Core business logic βœ… Ready
πŸ“Š Analytics 8083 Usage tracking & reports βœ… Ready
πŸ“ Eureka 8761 Service discovery βœ… Ready
βš™οΈ Config 8888 Centralized configuration βœ… Ready

πŸš€ Quick Start

Prerequisites

  • β˜• Java 25
  • 🐳 Docker & Docker Compose
  • πŸ“¦ Gradle (wrapper included)

⚑ One-Command Start

# Clone repository
git clone <repository-url> && cd leaky-tokens

# Start infrastructure
docker-compose -f docker-compose.infra.yml up -d

# Run all services
./gradlew bootRun --parallel

πŸ§ͺ Test It Works

# Check service health
curl http://localhost:8082/api/v1/tokens/status | jq

# Register a user
curl -X POST http://localhost:8081/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{"username":"demo","email":"[email protected]","password":"password"}' | jq

# Test token consumption
curl -X POST http://localhost:8082/api/v1/tokens/consume \
  -H "Content-Type: application/json" \
  -d '{"userId":"YOUR_USER_ID","provider":"openai","tokens":50}' | jq

πŸ§ͺ Postman

  1. Import the collection docs/postman/leaky-tokens.postman_collection.json.
  2. Import the environment docs/postman/leaky-tokens.postman_environment.json.
  3. Select the Leaky Tokens Local environment.
  4. Run Auth/Login to auto-populate accessToken and userId, then exercise the other requests.

πŸ“– Documentation

πŸ“š Guide πŸ“ Description
πŸ“˜ Overview Project purpose & architecture
πŸš€ Getting Started Installation & setup
πŸ‘€ User Guide How to use the API
πŸ—οΈ Architecture Technical deep-dive
🎯 Use Cases Business scenarios
βš™οΈ Configuration All config options
πŸ“Š Monitoring Metrics, logs, tracing
πŸ“‹ API Reference Complete API docs
πŸ”§ Troubleshooting Common issues
πŸ’» Development Contributing guide

πŸ“Š Key Capabilities

πŸͺ£ Rate Limiting Strategies

Leaky Bucket (Default)

Tokens leak at a constant rate, smoothing out traffic bursts:

token:
  bucket:
    capacity: 1000
    leakRatePerSecond: 10.0
    strategy: LEAKY_BUCKET
Token Bucket

Allows short bursts while maintaining average rate:

token:
  bucket:
    capacity: 1000
    leakRatePerSecond: 10.0
    strategy: TOKEN_BUCKET
Fixed Window

Simple counter-based limiting:

token:
  bucket:
    capacity: 1000
    windowSeconds: 60
    strategy: FIXED_WINDOW

πŸ’° Quota Management

# Check quota
curl "http://localhost:8082/api/v1/tokens/quota?userId=...&provider=openai"

# Consume tokens
curl -X POST http://localhost:8082/api/v1/tokens/consume \
  -d '{"userId":"...","provider":"openai","tokens":100}'

# Purchase more tokens
curl -X POST http://localhost:8082/api/v1/tokens/purchase \
  -H "Idempotency-Key: purchase-001" \
  -d '{"userId":"...","provider":"openai","tokens":1000}'

πŸ” Observability

πŸ“ˆ Monitoring URLs

Tool URL Description
πŸ“Š Grafana http://localhost:3000 Dashboards (admin/admin)
πŸ“ˆ Prometheus http://localhost:9090 Metrics collection
πŸ”Ž Jaeger http://localhost:16686 Distributed tracing
πŸ“ Eureka http://localhost:8761 Service registry

πŸ•΅οΈ Distributed Tracing

View end-to-end request flows across microservices:

# Start with full tracing stack (includes Jaeger)
docker-compose -f docker-compose.full.yml up -d

# Access Jaeger UI
open http://localhost:16686

# Make some requests and watch traces appear
curl -X POST http://localhost:8080/api/v1/tokens/consume \
  -H "Content-Type: application/json" \
  -d '{"userId":"...","provider":"openai","tokens":50}'

Trace Analysis:

  • See request paths through all services
  • Identify latency bottlenecks
  • Debug distributed issues
  • 100% sampling in development mode

πŸ“š Full Tracing Guide

πŸ“ Quick Log Check

# Service logs
./gradlew :token-service:bootRun 2>&1 | tee service.log

# Docker logs
docker-compose logs -f token-service

# Search for errors
grep "ERROR" service.log

🎯 API Reference

Authentication Methods

# JWT Token
curl -H "Authorization: Bearer $JWT_TOKEN" ...

# API Key
curl -H "X-Api-Key: leaky_userid_xxxx" ...

Core Endpoints

Method Endpoint Description Auth
POST /api/v1/auth/register Create user account Public
POST /api/v1/auth/login Authenticate Public
GET /api/v1/tokens/quota Check user quota JWT
POST /api/v1/tokens/consume Consume tokens JWT
POST /api/v1/tokens/purchase Buy tokens JWT
GET /api/v1/analytics/report Usage report JWT

πŸ“š Full API Documentation β†’


πŸ§ͺ Testing

# Run all tests
./gradlew test

# Run with coverage
./gradlew jacocoTestReport

# Performance tests
./gradlew :performance-tests:gatlingRun

🀝 Contributing

We welcome contributions! Please see our Development Guide for:

  • πŸ—οΈ Project structure
  • πŸ“ Code style guidelines
  • πŸ§ͺ Testing best practices
  • πŸ”§ Development setup

⭐ Star this repo if you find it helpful!

Made with ❀️ and πŸ€– AI assistance

About

Sophisticated Spring Java Kafka SAGA Transactional Outbox with Prometheus and other pointless extras

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors