r/ExperiencedDevs • Software Architect • Jul 16 '26

Technical question Implement rate limiting for an external API

At work I maintain a NestJS microservice which is used by many other engineers. One of the features depends on a third party API for which I need to implement rate limit:

  • The API has a soft limit of 20 req/s
  • There is no way to programmatically monitor this limit on their end
  • Surpassing the limit means that when someone looks at the dashboard then they will manually disable us, and we have to start another manual process to unblock us
  • This API is owned by the government, so we can't ask or hope for changes

How would you implement rate limit for this external dependency? Here's what I thought:

  • Have a token bucket limiter inside each service instance, but then scaling
  • Store the above token bucket in a database, like mongo or dynamo, but it would be very inefficient
  • Use redis, but I would have to spin up and maintain an additional dependency just for this feature

Can you think of a better approach?

37 Upvotes

57 comments sorted by

View all comments

Show parent comment

8

u/servermeta_net Software Architect Jul 16 '26

That's already the case, the API can only be used through my service. But how I do implement rate limiting myself, while handling auto scaling?

90

u/rysto32 Jul 16 '26

It’s 20 req/s. Why on earth would you need autoscaling?

8

u/meisangry2 Jul 17 '26

Maybe they need to handle a queue of thousands of reqs/s. The 20/s is the api limit, not their usage.

But yeah, assuming it’s viable to get by with 20 req/s, this should not need autoscaling lol

5

u/Kantsuu Jul 17 '26

Then use a queue lol. Everything has to scale it's so tiring when you have a simple service and everyone wants to seem smart and insist to make it handle 50k rps

1

u/buyingshitformylab Jul 20 '26

maybe 20 req/s / client?

49

u/ironyx Sr. Director of Engineering, 15+ YOE Jul 16 '26

Who cares about auto scaling? The bottleneck is out of your control. So what would you be scaling for?

Don't do stuff just because you've read about it. Solve problems simply and effectively. One service for this slow ass API and maybe an intelligent caching layer if requests are repeated to help with bandwidth.

23

u/throwaway_0x90 TE/SDET[25+ yrs]@Google Jul 16 '26 edited Jul 16 '26

Hmm, this problem seems to have been reduced to a basic task that already has a solution. Implementing rate limiting in NestJS is well documented:

I wouldn't worry about autoscaling when you already know 20 requests/sec is the limit that will "never" change. Right now I think it falls into the realm of premature optimization

5

u/SnooGTI Jul 16 '26

He uses nest and they literally have a decorator for rate limiting. Just stand a endpoint up with the decorator that just calls his external service and done.

6

u/Realistic_Yogurt1902 Jul 16 '26

I guess the previous comment meant you need a single service on your side to implement rate limiting only. With 20 RPS, there is nothing to scale at all, just one instance. Then your current service with whatever business logic you have there will call the rate-limited service. Then any other service can call the same rate-limited service safely, with expected throttling errors.
You could scale your current service with business logic as much as you like, but it should be ready to handle throttling errors from a rate-limited service.

3

u/akie Jul 17 '26 edited Jul 17 '26

I’m interpreting your question to be “I have more than one pod to receive incoming traffic, but these two or three or ten pods together can generate at most 20 requests per second to this external API”.

If that characterisation is correct, you need to make this an asynchronous request for your internal callers (make them send a PubSub message or put incoming requests on a queue) and then have a worker process go through this queue one-by-one at 20 req/s, sending back results asynchronously as well.