← How-to Guides

Enable rate limiting for a consumer with Kong Gateway

TL;DR

Enable an authentication plugin and create a consumer with credentials, then enable the Rate Limiting plugin on the new consumer.

Prerequisites

Kong Konnect

This is a Konnect tutorial. If you don’t have a Konnect account, you can get started quickly with our onboarding wizard.

The following Konnect items are required to complete this tutorial:

  • Personal access token (PAT): Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
  • Control plane: You can use an existing control plane or create a new one to use for this tutorial.
  • Control plane ID: You can see your control plane ID by selecting a control plane from the Gateway Manager in Konnect.

Kong Gateway running

This tutorial requires Kong Gateway. If you don’t have it set up yet, you can use the quickstart script to get an instance of Kong Gateway running almost instantly:

curl -Ls https://get.konghq.com/quickstart | bash -s

Once Kong Gateway is ready, you will see the following message:

Kong Gateway Ready

decK

decK is a CLI tool for managing Kong Gateway declaratively with state files. To complete this tutorial you will first need to:

  1. Install decK
  2. Create a deck_files directory and a kong.yaml file in the directory:

     mkdir deck_files  && touch deck_files/kong.yaml
    

Pre-configured entities

For this tutorial, you’ll need Kong Gateway entities, like services and routes, pre-configured. These entities are essential for Kong Gateway to function but installing them isn’t the focus of this guide. Follow these steps to pre-configure them:

  1. Create a prereqs.yaml file within your deck_files directory, and add the following content to it:

    _format_version: '3.0'
    services:
      - name: example-service
        url: http://httpbin.org/anything
    routes:
      - name: example-route
        paths:
        - "/anything"
        service:
          name: example-service
    
  2. Sync your changes.

    deck gateway sync deck_files
    

    Make sure to substitute your Konnect Personal Access Token for konnect_token and the control plane name for KONNECT_CP_NAME in the command:

    deck gateway sync deck_files \
      --konnect-token $KONNECT_TOKEN \
      --konnect-control-plane-name $KONNECT_CP_NAME
    

To learn more about entities, you can read our entities documentation.

1. Create a consumer

Consumers let you identify the client that’s interacting with Kong Gateway. We’re going to use key authentication in this tutorial, so the consumer needs an API key to access any Kong Gateway services.

Add the following content to kong.yaml to create a consumer:

_format_version: '3.0'
consumers:
  - username: jsmith
    keyauth_credentials:
    - key: example-key

2. Enable authentication

Authentication lets you identify a consumer so that you can apply rate limiting. This example uses the Key Authentication plugin, but you can use any authentication plugin that you prefer.

Enable the plugin globally, which means it applies to all Kong Gateway services and routes:

plugins:
  - name: key-auth
    config:
      key_names:
      - apikey

3. Enable rate limiting

Enable the Rate Limiting plugin for the consumer. In this example, the limit is 5 requests per minute and 1000 requests per hour.

Add the following to your existing plugins section in kong.yaml:

  - name: rate-limiting
    consumer: jsmith
    config:
      minute: 5
      hour: 1000

4. Apply configuration

Synchronize your decK configuration files. Make sure you created the deck_files directory in the prerequisites.

First, compare the decK file or files to the state of the Kong Gateway:

deck gateway diff deck_files
deck gateway diff deck_files \
  --konnect-token $KONNECT_TOKEN \
  --konnect-control-plane-name $KONNECT_CP_NAME

The output shows you which entities will change if you sync the state files.

If everything looks right, synchronize them to update your Gateway configuration:

deck gateway sync deck_files
deck gateway sync deck_files \
  --konnect-token $KONNECT_TOKEN \
  --konnect-control-plane-name $KONNECT_CP_NAME

5. Validate

You can run the following command to test the rate limiting as the consumer:

for _ in {1..6}; do curl -i http://localhost:8000/anything -H 'apikey:example-key'; echo; done
for _ in {1..6}; do curl -i http://{host}/anything -H 'apikey:example-key'; echo; done

Replace {host} with the proxy URL for this data plane node.

This command sends six consecutive requests to the route. On the last one you should get a 429 error with the message API rate limit exceeded.

Cleanup

Clean up Konnect environment

If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.

Destroy the Kong Gateway container

curl -Ls https://get.konghq.com/quickstart | bash -s -- -d

Did this doc help?

Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!