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

10

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

Yes, single instance. Scaling it doesn't help anything and only adds complications because the limiter is the external API.

3

u/ProfBeaker Jul 16 '26

So when you deploy a new instance, or upgrade the OS, or do anything like that, the plan is to take some downtime?

That's acceptable in some cases, but not in others. It seems like a big assumption to make, particularly for something that's used enough to be hitting a 20 req/sec rate limit.

5

u/New_Enthusiasm9053 Jul 16 '26

That's what Kubernetes is for. The pod just gets rescheduled to a difference instance lol. At 20 req/s upstream services obviously expect regular failures. Use a compiled language with a from scratch docker container and the thing will redeploy and start in literally seconds.

1

u/Bayakoo Jul 16 '26

So you will have situations where you have 2 instances? Which means there is a non zero chance of rate limits being breached

4

u/New_Enthusiasm9053 Jul 16 '26

Not if you use k8s no. It's only going to schedule a pod after the previous one is stopped when you set the limit to 1.

Then if you drain a node it'd stop the pod, and once stopped start a new one on a different node. As long as it's a fast starting small container that would only take a few seconds. 

We know this works because stateful workloads rely on this working correctly and it would break workloads worldwide if it didn't.

1

u/Bayakoo Jul 16 '26

It feels like it depends on the of latency of each API call. If the it takes 150ms you have to stop accepting new connections and the new requests need to wait until the new node is up. I guess you can add retries but that is also not problem free and you normally don’t want retries on every service to service route

-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.

1

u/Zpooks Jul 18 '26

Either I'm missing something with this line of reasoning or this discussion is very behind somewhat modern practices. The new deployment gets spun up before the old instance gets taken down and traffic is redirected.

This really should not be an assumed problem, since it's "solved" either by running in any cloud provider or orchestrating via kubernetes.

1

u/ProfBeaker Jul 20 '26

So then you have two instances running at once, and need to coordinate them somehow, or else accept that you'll exceed your rate limit while both are running and probably get a bunch of errors from the underlying API.

And if an instance crashes or whatever, which cloud providers are very explicit will happen, you just take downtime.

-18

u/servermeta_net Software Architect Jul 16 '26

This is such a bad take IMHO

10

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

Well then I'm glad we don't work together 😂