API Plan
Introduction
API Plans provide a holistic approach to API governance by unifying the management of rate limits and quotas into a single, cohesive resource. While they utilize existing functionalities, API Plans reorganize these features to better embody the concept of comprehensive API governance. By defining API Plans, you can manage access levels, control usage, and ensure fair distribution of resources among your API consumers in a more structured and policy-driven manner.
This unified approach allows you to:
- Centralize Policy Management: Consolidate rate limiting and quota enforcement into one resource, making it easier to maintain and update policies.
- Enhance Clarity: Provide a clear representation of your API consumption policies, improving understanding for both API managers and consumers.
- Streamline Access Control: Simplify the association between user groups and APIs through well-defined plans, leading to more efficient access management.
- Ensure Consistency: Maintain consistent governance across all APIs by applying standardized plans, reducing the risk of misconfiguration.
Prerequisites
For the APIPlan
feature to work, you need to deploy and configure Redis in your cluster and Traefik deployment. You can do this by following these steps:
- Install the Redis via the Helm chart
- Helm
helm install redis oci://registry-1.docker.io/bitnamicharts/redis -n traefik --wait
- Export your Redis instance password
- Export Redis password
export REDIS_PASSWORD=$(kubectl get secret --namespace traefik redis -o jsonpath="{.data.redis-password}" | base64 -d)
- Finally, upgrade your Traefik deployment to include Redis
- Upgrade Deployment
helm upgrade traefik -n traefik --wait \
--reuse-values \
--set hub.redis.endpoints=redis-master.traefik.svc.cluster.local:6379 \
--set hub.redis.password=${REDIS_PASSWORD} \
traefik/traefik
API Plans behavior
An APIPlan
is a resource that specifies rate limits and quotas for API consumption. It is associated with an APIAccess
resource, which grants access to specific APIs for certain user groups.
API Plans provide a flexible way to manage API consumption policies across different user groups and APIs.
Key concepts
- API Plan: Defines the rate limits and quotas to be applied.
Managed Subscription
: Grants access to APIs for applications and associates an API Plan.APICatalogItem
: Defines which APIs are visible to users on the Developer Portal.- Groups: User groups defined in your Identity Provider (IDP).
Rate limit
API rate limiting defines consumption limits for API consumers. It also controls the rate at which requests are allowed over a short period of time. It limits how fast requests can be made by
delaying or rejecting excess requests to match the target rate. If requests exceed the allowed rate and cannot be delayed sufficiently, they are rejected with an HTTP status code 429 Too Many Requests
.
Overall, It serves three main purposes:
- Protecting infrastructure
- Managing quotas
- Enabling API monetization
To learn more about how API rate limiting works, check out the rate limiting section in the Distributed Rate Limit reference documentation
Quota
Quota defines a fixed number of requests allowed over a given period of time . Once the quota limit is reached, further requests are immediately denied until the quota resets for the next period.
If requests exceed the allowed rate and cannot be delayed sufficiently, they are rejected with an HTTP status code 429 Too Many Requests
.
In order to allow transition between two plans, the count of requests is stored on the API Access level. So, when a new API Plan is assigned to an API Access, the remaining requests are kept.
Creating an API plan
To create an API Plan, you can use the following Custom Resource (CR):
Configuration example
The following example defines an APIPlan
named my-plan
in the default namespace. It sets a rate limit of 1 request per second and a quota of 10,000 requests per month (expressed as 750 hours).
This plan can be associated with APIAccess
resources to enforce these limits on API consumers.
- APIPlan
apiVersion: hub.traefik.io/v1alpha1
kind: APIPlan
metadata:
name: my-plan
namespace: default
spec:
title: "Standard"
description: "**Standard Plan**"
rateLimit:
limit: 1
period: 1s
quota:
limit: 10000
period: 750h # 1 month
Configuration options
Field | Description | Required | Default |
---|---|---|---|
title | A name for the plan. This is displayed in the consumer's API portal | Yes | |
description | A description of the plan, it is displayed in the consumer's API portal and it only supports markdown | Yes | |
rateLimit | Specifies the rate limit settings. This defines how many requests are allowed within a specified time period using the Token Bucket algorithm. | Yes | N/A |
rateLimit.limit | The maximum number of requests allowed within the rateLimit.period . Determines the refill rate of tokens in the Token Bucket algorithm. | Yes (if rateLimit is set) | |
rateLimit.period | Time period for the rateLimit | No | Defaults to 1s if omitted |
quota | specifies number of requests allowed in a larger time frame (e.g month) | No | N/A |
quota.limit | Maximum number of requests in the specified time in quota.period | Yes (if quota is set) | |
quota.period | Time period for the quota. | No |
- The supported duration units for
periods
are s (seconds), m (minutes), and h (hours). Example: "3h10m" will be interpreted as 3 hours and 10 minutes. - The
description
field supports standard markdown and GitHub Flavored Markdown
Associating an API Plan with ManagedSubscription
& APICatalogItem
To enforce an API Plan, you need to associate it with both ManagedSubscription
and APICatalogItem
resources.
APICatalogItem
: Controls the visibility of APIs or API Bundles on the Developer Portal for specific user groups and associates the available API Plans.ManagedSubscription
: Grants consumption access to applications, specifying which applications can consume which APIs or API Bundles under specific API Plans.
When you associate an API Plan with these resources, the following occur:
-
Shared Quota Across APIs: The quota is shared across all APIs referenced by a single
ManagedSubscription
. For example, if anAPIPlan
sets a quota of 100 requests per month, and theManagedSubscription
is linked to two APIs, API A and API B, the combined requests to both APIs cannot exceed 100 requests per month. -
Independent Rate Limits per
ManagedSubscription
: Rate limits are enforced perManagedSubscription
. If an application is included in multipleManagedSubscription
resources with different API Plans on the same APIs, the system determines which plan to enforce based on the weight and the name of theManagedSubscription
.infoSee the Managing overlapping plans section for more information
Configuration example
The following example defines:
- An
APICatalogItem
namedmy-api-catalog
in thedefault
namespace, makingmy-api
visible to users in the groupmy-group
, and associating it with themy-plan
API Plan. - A
ManagedSubscription
namedmy-subscription
in thedefault
namespace, granting consumption access to the application withappId: "my-app-id"
, associating it withmy-api
and themy-plan
API Plan. The weight is set to100
, giving this subscription a higher priority when resolving overlaps with otherManagedSubscription
resources.
- APICatalogItem
- ManagedSubscription
apiVersion: hub.traefik.io/v1alpha1
kind: APICatalogItem
metadata:
name: my-api-catalog
namespace: default
spec:
groups:
- my-group
apis:
- name: my-api
apiPlan:
name: my-plan
apiVersion: hub.traefik.io/v1alpha1
kind: ManagedSubscription
metadata:
name: my-subscription
namespace: default
spec:
applications:
- appId: "my-app-id"
apis:
- name: my-api
apiPlan:
name: my-plan
weight: 100
- Your
APIPlan
,API
,APICatalogItem
, andManagedSubscription
resources must exist in the same namespace. - Application-Level Access: Unlike APIAccess, which used user groups,
ManagedSubscription
operates at the application level. You must explicitly list the applications (appIds) that are granted access. - One
ManagedSubscription
per Application Group: To avoid shared quotas, create separateManagedSubscription
resources for each application or group of applications that should share quotas. - When multiple plans are applied to one application, the plan with the highest weight/priority takes precedence.
Response Headers
The following headers will be present in your API responses to help you track your quotas and rate limits:
Rate limit headers
X-RateLimit-Limit
: The maximum number of requests allowed in the current rate limit window.
Quota headers
X-Quota-Limit
: The maximum number of requests allowed in the current quota period.X-Quota-Remaining
: The number of requests remaining in the current quota period.
Headers are only included if the quota or rate limit is set (not unlimited).
Managing overlapping plans
When an application is associated with multiple ManagedSubscription
resources pointing to the same API, the system determines which plan to enforce based on the weight and the name of the ManagedSubscription
.
- Weight: An optional integer value. Higher weight means higher priority.
- Name Sorting: If weights are equal or not set, the ManagedSubscription with the name that comes first alphabetically is selected.
Example
An application (appId: "forecast-app"
) is included in both the external
and vip
Managed Subscriptions. There are two ManagedSubscription
resources that grant access to the “forecast” API with different plans and weights.
External group
- Standard API Plan
- ManagedSubscription for External Application
apiVersion: hub.traefik.io/v1alpha1
kind: APIPlan
metadata:
name: std-plan
namespace: default
spec:
title: "Standard Plan"
rateLimit:
limit: 1
period: 1s
apiVersion: hub.traefik.io/v1alpha1
kind: ManagedSubscription
metadata:
name: external-subscription
namespace: default
spec:
applications:
- appId: "forecast-app"
apis:
- name: forecast
apiPlan:
name: std-plan
weight: 1 # Lower priority
VIP group
- VIP API Plan
- ManagedSubscription for VIP Application
apiVersion: hub.traefik.io/v1alpha1
kind: APIPlan
metadata:
name: vip-plan
namespace: default
spec:
title: "VIP Plan"
rateLimit:
limit: 20
period: 1s
quota:
limit: 1000
period: 24h
apiVersion: hub.traefik.io/v1alpha1
kind: ManagedSubscription
metadata:
name: vip-subscription
namespace: default
spec:
applications:
- appId: "forecast-app"
apis:
- name: forecast
apiPlan:
name: vip-plan
weight: 10 # Higher priority due to higher weight
Result: The system selects vip-subscription
because it has a higher weight, and the application receives the rate limits and quotas defined in the “VIP Plan”.
- If the weights were equal or not set for both
ManagedSubscription
resources, the system would select the one whose name comes first alphabetically. - Shared Quota Across APIs: If a
ManagedSubscription
resource grants access to multiple APIs, the quota is shared across those APIs. For example, with a quota of 1000 requests per 24 hours, requests to any of the associated APIs contribute to this total. - No Automatic Fallback: If the quota under extra-plan is exhausted, the system does not automatically fall back to std-plan. The application would receive an HTTP status code 429 Too Many Requests until the quota resets.
Related content
- Learn more about the
API
object in its dedicated section -
- Learn more about the
APICatalogItem
resource in its dedicated section
- Learn more about the
- Learn more about
API Plan
in its dedicated section - Learn more about the API Portal in its dedicated section
- Learn more about
ManagedSubscription
resource in its dedicated section - Learn more about User Management in its dedicated section