⚡ Example: Rate Limiting in Azure API Management
Scenario:
You have a public API and want to limit each user to 5 calls per minute to prevent abuse or accidental overload.
π‘️ What is Rate Limiting?
Rate limiting controls how many requests a client can make to your API within a specific time window.
It helps:
-
Prevent API abuse
-
Protect backend systems
-
Fairly distribute API usage
π‘ Using the rate-limit-by-key
Policy
Azure APIM uses a policy called rate-limit-by-key
. It limits requests based on a unique identifier — usually the caller's subscription key or IP address.
π§ Example Policy (XML)
Here’s how you can add this to your inbound policy:
Explanation:
Attribute | Meaning |
---|---|
calls="5" | Maximum 5 calls allowed |
renewal-period="60" | Time window of 60 seconds (1 minute) |
counter-key | Uniquely tracks by subscription key (per user limit) |
π Where to Apply This
You can apply this policy at:
-
Product level (to all APIs in that product)
-
API level (to a specific API)
-
Operation level (for a specific endpoint)
⚠️ What Happens When the Limit is Hit?
When a caller exceeds the limit, APIM responds with:
-
HTTP 429 (Too Many Requests)
-
Optionally, include a custom error message or retry-after header
Comments
Post a Comment