Scenario 8 - Rate Limiting¶
This is a tutorial that is part of a series and can be used as a standalone tutorial on how to set up rate limiting for an API. For more details on the scenario and general prerequisites, please see the scenario overview page.
Time to Complete : 5 minutes
User story¶
While analyzing the traffic patterns and data, the GOGO DevOps team noticed that their backend is receiving a high number of requests and due to these high demand, their latency numbers also increased. The DevOps team did some performance tests on their user info backend and identified that their backend service can handle a maximum of 1000 TPS. So GOGO management decided to introduce rate limiting to manage their free users.
Step 1: Apply a rate limiting policy.¶
WSO2 API Manager provides various levels of rate limiting. For this case, let’s apply Subscription Rate Limiting Policy and Maximum Throughput for the backend for PassengerInfoAPI.
To create a Subscription Rate Limiting Policy, do the following.
- Log on to Admin Portal https://localhost:9443/admin using
admin
and passwordadmin
. -
Navigate to Rate Limiting Policies → Subscription Policies and create a new policy. Following is a sample policy.
-
Once policy is created, to apply this to the API, log in to the Publisher Portal using
apiprovider
with the passworduser123
and go to Develop → Portal Configuration → Subscriptions section. -
Apply the policy to the PassengerInfoAPI and save the API.
-
Now log in to the Developer Portal and create, application and try to subscribe to the API. You will notice that it has the newly created policy.
-
Subscribe to the API using this policy and invoke the API using the generated token.
-
When you request more than 10 times you will get a throttled-out message.
Step 2: Define maximum throughput¶
You can define a maximum throughput for the backend to protect the backend from high request load.
- Log in to the Publisher Portal and select the API and navigate to Develop → API Configurations → Runtime section.
-
Under the Backend section, you could define the maximum TPS for the backend. For this demo, to see the results quickly, lets set it to 1 TPS and save the API
-
For this configuration to take place, you need to create a new revision for this API. For that go to Deploy → Deployments and deploy a new revision.
- Invoke the API in quick succession (more than one request per second). You could use the previous token for this.
You should see requests getting throttled out with following message
{
"code": "900801",
"message": "API Limit Reached",
"description": "API not accepting requests"
}
This shows API Manager is limiting the requests to the backend.
What's next¶
Try out the next scenario in the series, Realtime Data with WebSocket API.
Top