High-Level Design
There are 2 mains flows, from Admin and User perspectives.
User:
- Wants to call one of the endpoints executed by the API servers, and call it.
- The API Gateway gets the request, and checks against the Counter using the cached rules.
- The Counter (Redis) updates the token bucket for that user/api by consuming tokens (one by request + all tokens according to the rate since the last request).
- If there are no tokens available, the Counter returns a reject with a Retry-After header informing about the seconds needed to retry again.
- If there are tokens available, the Gateway redirects to the endpoint that the user wanted to hit in the first place
- The API gateway keeps the cached rules up-to-date by hitting the rules db.
Admin:
- Add/update/view/delete the Rules DB by using the Admin API.
- The main parameters to update are the capacity of the token bucket but also the refill rate for the bucket. This applies to user and API.
- Configuration rollout: When an Admin modifies a rule in one of the ways above, the API gateway updates the rules within 5 minutes/the time we want. For urgent changes we can publish to Redis using Pub/Sub.