# Step 2 - Propose high-level design and get buy-in

## Where to put the rate limiter?&#x20;

Intuitively, you can implement a rate limiter on either the client or server side.&#x20;

* Client-side implementation. Generally speaking, a client is an unreliable place to enforce rate limiting because client requests can easily be forged by malicious actors. Moreover, we might not have control over the client implementation.
* Server-side implementation.

<figure><img src="https://1722711354-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwNXdoUkfmcozr29fRrfb%2Fuploads%2FKP3CJ18ChIRjhBG7XtjM%2FScreenshot%202023-11-01%20at%2023.37.29.png?alt=media&#x26;token=56218e8a-8cd4-467c-b8d6-9b1c60ee8d1c" alt=""><figcaption></figcaption></figure>

Besides the client and server-side implementations, there is an alternative way. Instead of putting a rate limiter at the API servers, we create a rate limiter middleware, which throttles requests to your APIs

<figure><img src="https://1722711354-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwNXdoUkfmcozr29fRrfb%2Fuploads%2FIjEycgeRnfRNtmSyzxwk%2FScreenshot%202023-11-01%20at%2023.38.48.png?alt=media&#x26;token=63b6f4c0-3f69-483d-80ac-1e4114f28538" alt=""><figcaption></figcaption></figure>

Assume our API allows 2 requests per second, and a client sends 3 requests to the server within a second. The first two requests are routed to API servers. However, the rate limiter middleware throttles the third request and returns an HTTP status code 429. The HTTP 429 response status code indicates a user has sent too many requests.

<figure><img src="https://1722711354-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwNXdoUkfmcozr29fRrfb%2Fuploads%2Fwy7JmwcfTBK3XBSrGb5H%2FScreenshot%202023-11-01%20at%2023.43.57.png?alt=media&#x26;token=7549d086-6cc6-4006-99a9-4c5196960c21" alt=""><figcaption></figcaption></figure>

Cloud microservices have become widely popular and rate limiting is usually implemented within a component called API gateway. API gateway is a fully managed service that supports rate limiting, SSL termination, authentication, IP whitelisting, servicing static content, etc. For now, we only need to know that the API gateway is a middleware that supports rate limiting.&#x20;

While designing a rate limiter, an important question to ask ourselves is: where should the rater limiter be implemented, on the server side or in a gateway? There is no absolute answer. It depends on your company’s current technology stack, engineering resources, priorities, goals, etc. Here are a few general guidelines:&#x20;

* Evaluate your current technology stack, such as programming language, cache service, etc. Make sure your current programming language is efficient to implement rate limiting on the server side.&#x20;
* Identify the rate-limiting algorithm that fits your business needs. When you implement everything on the server side, you have full control of the algorithm. However, your choice might be limited if you use a third-party gateway.&#x20;
* If you have already used microservice architecture and included an API gateway in the design to perform authentication, IP whitelisting, etc., you may add a rate limiter to the API gateway.&#x20;
* Building your own rate-limiting service takes time. If you do not have enough engineering resources to implement a rate limiter, a commercial API gateway is a better option.
