# How We Designed the Quotas Microservice to Prevent Resource Abuse

[Grab](https://yomu.fyi/company/grab) · Jim Zhan · Aug 10, 2018

**Type:** Problem & solution

## Summary

As Grab migrated from a monolith to hundreds of microservices, managing global rate limiting became essential to prevent cascading failures and resource exhaustion. To avoid putting a rate limiting service on the critical path of every API call, Grab built Quotas, an asynchronous rate limiting system. Client services use a lightweight SDK and middleware to read rate limiting decisions from local in-memory caches and stream usage metrics asynchronously via Apache Kafka. The Quotas service aggregates usage data locally, flushes stats to Redis periodically, and publishes updated rate limiting decisions back over Kafka topics. In production, Quotas successfully handles 200k peak transactions per second with decision enforcement delays capped at 200 milliseconds.

## Context

Migrating from a monolith to hundreds of microservices made local rate limiting insufficient, as auto-scaling instances could collectively overload databases and downstream services, while synchronous global rate limiting would introduce severe latency on the critical path.

## Approach / What changed

Grab built Quotas, an asynchronous rate limiting architecture where client instances evaluate requests against local in-memory caches, report request events via Kafka, and consume updated decisions calculated by a centralized service that aggregates stats in Redis.

## Takeaways

- Asynchronous event processing via Kafka decoupled rate limiting decisions from the synchronous request-response path, keeping client latency impacts negligible.
- Aggregating API usage stats locally in-memory and updating Redis periodically every 50 ms prevented the Redis cluster from becoming a calculation bottleneck.
- Quotas achieved global rate limiting with an enforcement decision delay of up to 200 milliseconds while supporting 200k peak production TPS using sliding windows.

**Tags:** [Architecture](https://yomu.fyi/topic/architecture), [Kafka](https://yomu.fyi/topic/kafka), [Microservices](https://yomu.fyi/topic/microservices), [Redis](https://yomu.fyi/topic/redis), [Scalability](https://yomu.fyi/topic/scalability)

[Read original post](https://engineering.grab.com/quotas-service)
