A microservices-based API monetization platform that issues tokens, enforces per-client rate limits using Token Bucket and Sliding Window algorithms, tracks usage, and generates async invoices — modeled after real-world API gateways like Stripe and RapidAPI.
ApiMeter simulates how production API platforms manage third-party access: clients get a token tied to a subscription tier (Free / Pro / Enterprise), every request is rate-limited according to that tier, usage is tracked in real time, and clients can pull an invoice summarizing what they've consumed — delivered asynchronously via email.
- Generate Token — client registers and receives a token tied to a tier (Free, Pro, Enterprise), each with its own capacity and refill rate.
- Rate Limiting Logic — ApiMeter supports two robust algorithms backed by Redis:
- Token Bucket: Allows bursts of traffic while maintaining a steady average rate. Tokens are added to the bucket at a fixed rate, and each request consumes a token.
- Sliding Window: Provides precise rate limiting over a rolling time window, preventing sudden spikes at the edge of fixed windows.
- Process Request — client calls the rate-limited endpoint with the token as a bearer credential. The Main Service checks the rate limit state in Redis before processing the request and logging usage.
- Inter-Service Communication — ApiMeter uses OpenFeign (Feign Client) for seamless, declarative communication between internal microservices.
- Get Invoice — client requests an invoice summarizing requests used and remaining quota. Main Service publishes an event to Kafka, and the Email Service asynchronously sends the invoice without blocking the response.
- Quota Alerts — when a client crosses 80% or 100% of their quota, an alert event is published and an email is sent automatically.
Each microservice has its own dedicated .md file for independent repository management if needed:
| Service | Responsibility | Key Tech |
|---|---|---|
| API Gateway | Routing, circuit breaking | Spring Cloud Gateway, Resilience4j |
| Main Service | Rate limiting algorithms (Token Bucket & Sliding Window), Token/tier management, request processing, usage logging | Spring Boot, MySQL, Redis, Kafka Producer, Feign Client |
| Auth Service | Authentication & Security | Spring Boot |
| Email Service | Async invoice & quota alert delivery | Spring Boot, Kafka Consumer, SMTP |
| Usage Service | Tracks API usage | Spring Boot |
| Eureka Server | Service discovery for all services | Spring Cloud Netflix Eureka |
| Client | Client application | Frontend technologies |
- Language/Framework: Java, Spring Boot(3.5.16), Spring Cloud
- Rate Limiting: Token Bucket & Sliding Window algorithms backed by Redis
- Inter-Service Communication: Spring Cloud OpenFeign
- Messaging: Apache Kafka (async invoice + alert processing)
- Databases: MySQL (persistent data), Redis (rate-limit state)
- Resilience: Resilience4j (circuit breaker, retry)
- Service Discovery: Netflix Eureka
- Analytics Dashboard: Implement a robust analytics module and UI for administrators to track aggregate metrics and for clients to view granular usage insights.
- Configuration: Each microservice has its own configuration file located at
src/main/resources/application.yaml(or.properties). Before running a service locally, ensure you update the database credentials, Redis host, and Kafka brokers in these files to point to your local instances. - Start external dependencies: Ensure your local MySQL, Redis, and Kafka servers are running.
- Run Eureka Server First: Service discovery must be up before others start.
cd eureka-server mvn spring-boot:run - Run Other Services: Navigate into each specific directory and run them. For example:
Repeat this for
cd main-service mvn spring-boot:runapi-gateway,auth-service,usage-service, andemail-service.
MIT
