Throttling allows you to limit the number of hits to an API during a given period of time, typically in cases such as the following:
...
This section covers the following topics:
Table of Contents | ||||
---|---|---|---|---|
|
Different levels of throttling
Throttling is enabled in the API Manager in different levels as API-level, application-level, resource-level and IP-level.
API-level throttling
API-level throttling tiers are defined when Creating and Managing APIs using the API Publisher. At subscription time, the consumers of the API can log in to the API Store and select which tier they are interested in as follows:
...
According to the tiers s/he selects, the subscriber is granted a maximum number of requests to the API.
Setting tier permissions
Users with Manage Tiers
permission can set role-based permissions to API-level access throttling tiers. This is done using the Tier Permissions menu of API Publisher as shown below. For each tier, you can specify a comma-separated list of roles and either Allow or Deny access to the list.
...
A subscriber logged into the API Store can consume APIs using a specific tier, only if s/he is assigned to a role that is allowed access. In the API Store, the subscriber sees a list of tiers that is filtered based on the subscriber's role. Only the ALLOWED roles appear here. By default, all tiers are allowed to everyone.
Application-level throttling
Application-level throttling tiers are defined at the time an application is created using the API Store. For information, see Applications and application-level throttling.
Resource-level throttling
...
IP-level throttling
In IP address based throttling, you can limit the number of requests sent by a client IP (e.g., 10 calls from single client). For example, the throttling policy shown below allows only 1 API call per minute for a client from 10.1.1.1 and 2 calls per minute for a client from any other IP address:
Code Block | ||
---|---|---|
| ||
<wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:throttle="http://www.wso2.org/products/wso2commons/throttle"> <throttle:MediatorThrottleAssertion> <wsp:Policy> <throttle:ID throttle:type="IP">10.1.1.1</throttle:ID> <wsp:Policy> <throttle:Control> <wsp:Policy> <throttle:MaximumCount>1</throttle:MaximumCount> <throttle:UnitTime>60000</throttle:UnitTime> </wsp:Policy> </throttle:Control> </wsp:Policy> </wsp:Policy> <wsp:Policy> <throttle:ID throttle:type="IP">other</throttle:ID> <wsp:Policy> <throttle:Control> <wsp:Policy> <throttle:MaximumCount>2</throttle:MaximumCount> <throttle:UnitTime>60000</throttle:UnitTime> </wsp:Policy> </throttle:Control> </wsp:Policy> </wsp:Policy> </throttle:MediatorThrottleAssertion></wsp:Policy> |
How throttling tiers work
- When an API is invoked, it first checks whether the request is allowed by API-level throttling limit. If the consumer has exceeded his/her maximum number of allowed API requests, the new request will be terminated.
- If API-level limit is not exceeded, it then checks whether the request is allowed by application-level throttling limit. If it has exceeded, the request will be terminated.
- If application-level limit is not exceeded, it finally checks whether the request is allowed by resource-level throttling limit. If the limit is not exceeded, then the request will be granted.
With capability to define throttling at three levels, the final request limit granted to a given user on a given API is ultimately defined by the consolidated output of all throttling tiers together. For example, lets say two users subscribed to an API using the Gold subscription, which allows 20 requests per minute. They both use the application App1 for this subscription, which again has a throttling tier set to 20 requests per minute. All resource level throttling tiers are unlimited. In this scenario, although both users are eligible for 20 requests per minute access to the API, each ideally has a limit of only 10 requests per minute. This is due to the application-level limitation of 20 requests per minute.
How to write a throttling policy and engage it to APIs
The steps below show how to write a throttling policy and engage it to an API pointing to a backend service.
The following throttling policy allows 1000 concurrent requests to a service.
Code Block language html/xml <wsp:Policy xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:throttle="http://www.wso2.org/products/wso2commons/throttle" wsu:Id="WSO2MediatorThrottlingPolicy"> <throttle:MediatorThrottleAssertion> <throttle:MaximumConcurrentAccess>1000</throttle:MaximumConcurrentAccess> <wsp:Policy> <throttle:ID throttle:type="IP">other</throttle:ID> </wsp:Policy> </throttle:MediatorThrottleAssertion> </wsp:Policy>
- Start the API Manager, log in to its management console (
https://localhost:9443/carbon
) and click the Resource > Browse menu to view the registry. - Click the
goverence/apimgt/applicationdata
path to go to its detailed view. - In the detail view, click the Resource link and upload the created policy file to the server as a registry resource.
In the management console, select the Service Bus > Source View menu.
The configurations of all APIs created in the API Manager instance opens. To engage the policy to a selected API, add it to your API definition. In this example, we add it to the login API.
Code Block language html/xml <?xml version="1.0" encoding="UTF-8"?><api xmlns="http://ws.apache.org/ns/synapse" name="_WSO2AMLoginAPI_" context="/login"> <resource methods="POST" url-mapping="/*"> <inSequence> <send> <endpoint> <address uri="https://localhost:9493/oauth2/token"/> </endpoint> </send> </inSequence> <outSequence> <send/> </outSequence> </resource> <handlers> <handler class="org.wso2.carbon.apimgt.gateway.handlers.throttling.APIThrottleHandler"> <property name="id" value="A"/> <property name="policyKey" value="gov:/apimgt/applicationdata/throttle.xml"/> </handler> <handler class="org.wso2.carbon.apimgt.gateway.handlers.ext.APIManagerExtensionHandler"/> </handlers> </api>
Note Be sure to specify the same path used in step 3 in the policy key of your API definition.
- You have successfully engaged a throttling policy to an API.