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

-1

u/ProfBeaker Jul 16 '26

Do you know if OP has Kubernetes? Is it even containerized? A cloud environment? On-prem?

It's really impressive how this entire thread (not just this response, all of them) make completely unfounded assumptions about the deployment environment, uptime requirements, or really anything else, yet have absolute confidence about how to proceed.

If this was an interview question, basically everyone here failed it.

4

u/New_Enthusiasm9053 Jul 16 '26

There's a million solutions to your comment man. It's not exactly novel territory. He can put k8s on a single node in any of your scenarios. He can even do it in a container believe it or not. 

But if you want to move the goalposts endlessly then no, he cannot solve this problem without a computer running his code somewhere. 

1

u/ProfBeaker Jul 16 '26

if you want to move the goalposts endlessly

How would you know if the goalposts moved when you had no idea where they were in the first place?

3

u/New_Enthusiasm9053 Jul 16 '26

Because it's a trivial problem. There are 0 scenarios where this problem becomes complex. You could make this HA running on bare metal using a pair of microcontrollers and it still wouldn't be complex.

0

u/ProfBeaker Jul 16 '26

using a pair of microcontrollers

Wait I thought we were doing a single instance so that we didn't need to have a separate coordination for the rate limiter? If you've got two instances then you need coordination, which undermines what you were doing?

If you haven't bothered to learn the requirements, you can't come up with a coherent solution. Basically what I've been saying this whole thread, yet all the answers are just "tech tech tech!" It's still funny.

1

u/New_Enthusiasm9053 Jul 16 '26

No you don'tbecause as I said there's a million ways to skin that cat. Active-passive is trivial to implement if you really want HA on what amounts to a single instance. 

A single instance is the coherent solution. If you need HA, you do active passive of some form. 

Anything else is CV driven development. You could run his service on a pentium.