Apigee Edge - 4MV4D - Quota Policy - Message Weight - S02E05

Hello Apigeeks,

In this 4MV4D, Find out how you can set message weights to API Requests to change how they consume Quota assigned to them hands-on. For example, POST requests might be expensive compared to GET requests. You might want to count all POST requests as double the weight compared to GET requests.

For example, if there are 4 API requests allowed to target server, You might want to count POST requests as heavy compared to GET requests. If message weight 2 is assigned to POST requests than each POST request will deduct 2 requests from API Count allowed. GET request will deduct 1 request from API count allowed from the Quota Limit.

Let’s take a sample scenario of quota 10 API request per minute & message weight 2 for all POST requests & 1 for other HTTP requests.

  • 1st API Call - POST Request - (4 - 2) - Available count - 2 Requests
  • 2nd API Call - GET Request - (2 - 1) - Available count - 1 Request
  • 3rd API Call - GET Request - (1 - 1) - Available count - 0 Requests
  • 4th API Call - Rejected by Apigee Edge - Quota Limit Reached

We have used Javascript policy before the Quota Policy to set a custom variable called messageWeight based on the request type. Please find code used in Javascript Policy below.

var verb = context.getVariable("request.verb");
context.setVariable("messageWeight", "1");
if (verb == 'POST') {
  context.setVariable("messageWeight", "2");    
}

Please find Quota Policy code used in 4MV4D below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Quota async="false" continueOnError="false" enabled="true" name="Quota-1">
    <DisplayName>Quota-1</DisplayName>
    <Properties/>
    <Allow count="4"/>
    <Interval>1</Interval>
    <Distributed>true</Distributed>
    <Synchronous>true</Synchronous>
    <TimeUnit>minute</TimeUnit>
    <MessageWeight ref="messageWeight"/>
</Quota>

4MV4D videos are completely focussed on API Developers & Help you learn many interesting concepts very quickly using hands-on approach. Don't forget to subscribe to channel here & share same with your friends / colleagues who are interested in Apigee.

Feel free to send your feedback/queries using comments below or Ask a question if you have any query. Stay tuned for more videos.

Version history
Last update:
‎03-08-2017 02:09 AM
Updated by: