Docsright arrowEdge Stackright arrow1.12right arrowBasic Rate Limiting

4 min • read

Basic Rate Limiting

IMPORTANT: This guide applies to Ambassador API Gateway, use of this guide on the Ambassador Edge Stack is not fully supported. Use the existing RateLimitService instead.

Ambassador can validate incoming requests before routing them to a backing service. In this tutorial, we'll configure the Ambassador API Gateway to use a simple third party rate limit service. If you don't want to implement your own rate limiting service, the Ambassador Edge Stack integrates a powerful, flexible rate limiting service.

Before You Get Started

This tutorial assumes you have already followed the Ambassador API Gateway Installation and Quickstart Tutorial guides. If you haven't done that already, you should do so now.

Once completed, you'll have a Kubernetes cluster running Ambassador and the Quote of the Moment service. Let's walk through adding rate limiting to this setup.

1. Deploy the Rate Limit Service

Ambassador delegates the actual rate limit logic to a third party service. We've written a simple rate limit service that:

  • listens for requests on port 5000;
  • handles gRPC shouldRateLimit requests;
  • allows requests with the x-ambassador-test-allow: "true" header; and
  • marks all other requests as OVER_LIMIT;

Here's the YAML we'll start with:

This configuration tells Ambassador about the rate limit service, notably that it is serving requests at example-rate-limit:5000.

Ambassador will see the RateLimitService and reconfigure itself within a few seconds. Note that the v2 API is available for the Ambassador.

2. Configure Ambassador Mappings

Ambassador only validates requests on Mappings which set rate limiting descriptors. If Ambassador cannot contact the rate limit service, it will allow the request to be processed as if there were no rate limit service configuration.

v0 API

Ambassador 0.50.0 and later requires the v2 API Version for rate limiting. The v2 API uses the labels attribute to attach rate limiting descriptors. Review the Rate Limits configuration documentation for more information.

Replace the label that is applied to the service-backend with:

so the Mapping definition will now look like this:

v2 API

Ambassador versions 0.40.2 and earlier use the v2 API version which uses the rate_limits attribute to set rate limiting descriptors.

This configuration tells Ambassador about the rate limit rules to apply, notably that it needs the x-ambassador-test-allow header, and that it should set "A test case" as the generic_key descriptor when performing the gRPC request.

Note that both descriptor and headers are optional. However, if headers are defined, they must be part of the request in order to be rate limited.

Ambassador would also perform multiple requests to example-rate-limit:5000 if we had defined multiple rate_limits rules on the mapping.

3. Test Rate Limiting

If we curl to a rate-limited URL:

We get a 429, since we are limited.

If we set the correct header value to the service request, we will get a quote successfully:

More

For more details about configuring the external rate limit service, read the rate limit documentation.