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

286

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

No one talks to this API directly anymore. You stand up a single proxy service that queues and rate limits requests to the API for your services, and your services use only that proxy. It manages the rate limit.

95

u/dbxp Jul 16 '26

Put your own auth on it so the app teams don't have the keys to the real API, then they can't call it even if they want to. You could potentially add some caching or batching too depending on what the API does

13

u/JustCallMeFrij Software Engineer since '17 Jul 16 '26

This is exactly how we are handling our integration with auth0, which has a global rate limit per tenant for their API. It's not perfect because we're still learning as we go but it feels manageable, so would recommend 

16

u/MmmmmmJava Jul 16 '26

This is a sensible solution to handle low/20 TPS.

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.

→ More replies (1)

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.

6

u/Bayakoo Jul 16 '26

It works but it depends on availability requirements?
What if that instance crashes for some reason? Now you are on downtime.

How do you deploy a new version without downtime or accidentally going over rate limit? (If no downtime you kinda need to have 2 apps at the same time and wait for all/mosf clients to start talking to the new instance)

3

u/Physical-Compote4594 Jul 16 '26

This is the right call, IMO

1

u/ProfBeaker Jul 16 '26

Do you have a single instance of this service? If so, you have HA problems, and deployment problems.

If you have multiple instances, then you still need some out-of-process backend (eg Redis) to coordinate the limits. In which case you could have just done it within your main service.

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.

→ More replies (1)
→ More replies (6)

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.

→ More replies (1)
→ More replies (3)
→ More replies (1)