Multiple Consumers Sharing the Same Quota
When multiple consumers (such as different environments or clients) share the same API quota, managing that shared quota can quickly become challenging. Each consumer might have varying levels of traffic, unpredictable usage patterns, and specific priorities. To avoid one consumer monopolizing the quota—resulting in API rate limits for other consumers—you need an efficient strategy to allocate and manage API quotas across these consumers.

The Challenges of Sharing API Quotas
In situations where several environments or customers share a single API quota, such as development, staging, and production environments using the same API key, balancing API usage becomes complex. Each environment might have different traffic needs, and without proper quota management, one environment may exhaust the shared API quota, leaving others without the resources they need to function properly. This can lead to service disruptions, slower response times, or even failed requests.
In such scenarios, it is essential to ensure that API consumption is prioritized appropriately based on the importance of the environments and that API quotas are dynamically allocated to prevent any one consumer from exhausting the entire quota.
How Lunar.dev Helps
Lunar.dev provides the tools needed to manage API consumption efficiently across multiple consumers sharing the same quota. Through its API Consumption Gateway and customizable flows, Lunar.dev allows you to implement dynamic quota allocation, traffic prioritization, and rate-limiting to ensure all consumers can access the API resources they need without exceeding shared quotas.
Lunar.dev Installation
Step 1: Sign Up for Lunar.dev
Start by creating a Lunar.dev account here. After signing up, you'll be directed to the Lunar.dev Control Plane where you can manage and monitor your API consumption. Follow the installation steps to set up the Lunar.dev API Consumption Gateway.
Step 2: Install the API Consumption Gateway
Install the Lunar.dev API Consumption Gateway through Docker or Kubernetes based on your environment. Detailed installation instructions can be found on the API Consumption Gateway Installation page.
Step 3: Route Your API Traffic
Route your API traffic by modifying HTTP requests to include specific headers to capture and route outgoing traffic automatically. More details can be found on the Route Your API Traffic page.
Manage & Control
Step 1: Configure API Quotas for Multiple Consumers
First, create a quota.yaml file to define how API quotas are shared between different consumers, such as environments or clients. This setup ensures that each consumer has a defined limit on API usage, preventing one from exhausting the shared quota.
Set a company-wide quotas:
- Define an overall API usage limit for the company using a custom header (x-lunar-api-company).
- Limit API requests to api.com/*to ensure fair usage across environments.
Set individual limits for environments (e.g., production and staging):
- Group API usage by environment using the x-lunar-consumer-tagheader.
- Assign specific quotas to production and staging environments to control their API consumption independently.
quotas:
  - id: LimiterMonth
    filter:
      url: api.com/*
    strategy:
      fixed_window:
        static:
          max: 10000000
          interval: 1
          interval_unit: month
internal_limits:
  - id: LimiterDay
    parent_id: LimiterMonth
    strategy:
      fixed_window:
        static:
          max: 100000
          interval: 1
          interval_unit: day
  - id: LimiterProd
    parent_id: LimiterDay
    filter:
      headers:
        - key: x-lunar-consumer-tag
          value: production
    strategy:
      fixed_window:
        static:
          max: 100000
          interval: 1
          interval_unit: day
  - id: LimiterStaging
    parent_id: LimiterDay
    filter:
      headers:
        - key: x-lunar-consumer-tag
          value: staging
    strategy:
      fixed_window:
        static:
          max: 20000
          interval: 1
          interval_unit: day
Step 2: Apply a Quota Management Flow
In this step, you will apply a quota management flow that differentiates between production and staging traffic. This configuration ensures that requests to api.com/* are managed under both monthly and daily quotas. The flow prioritizes production traffic over staging traffic and generates a 429 Too Many Requests response when the quota limits are exceeded.
- Apply Quota Management:
- The QueueLimiterprocessor manages incoming API requests by applying the defined quotas. The requests are filtered by thex-lunar-consumer-tagheader, which categorizes the traffic into production and staging.
- Each environment has its own quota limits:
- Production has a higher priority (1).
- Staging has a lower priority (2).
 
 
- The 
- Response Handling:
- If requests exceed the queue size or quota, the GenerateResponseTooManyRequestsprocessor will return a429 Too Many Requestsresponse, ensuring that no requests are processed beyond the defined limits.
 
- If requests exceed the queue size or quota, the 
Here’s how the flow is configured:
name: MonthlyAndDailyLimit-Prod
filter:
    url: api.com/*
    headers:
      - key: x-lunar-consumer-tag
        value: production
      - key: x-lunar-consumer-tag
        value: staging
processors:
  QueueLimiter:
    processor: Queue
    parameters:
      - key: quota_id
        value: LimiterSecond
      - key: ttl_seconds
        value: 12
      - key: queue_size
        value: 10
      - key: priority_group_by_header
        value: x-lunar-consumer-tag
      - key: priority_groups
        value: 
          production: 1
          staging: 2
  LimiterProd:
    processor: Limiter
    parameters:
      - key: quota_id
        value: LimiterProd
  LimiterStaging:
    processor: Limiter
    parameters:
      - key: quota_id
        value: LimiterStaging
  ProdFilter:
    processor: Filter
    parameters:
      - key: header
        value: x-lunar-consumer-tag=production
  GenerateResponseTooManyRequests:
    processor: GenerateResponse
    parameters:
      - key: status
        value: 429
      - key: body
        value: "Quota Exceeded. Please try again later."
      - key: Content-Type
        value: text/plain
flow:
  request:
    - from:
        stream:
          name: globalStream
          at: start
      to:
        processor:
          name: QueueLimiter
    - from:
        processor:
          name: QueueLimiter
          condition: blocked
      to:
        processor:
          name: GenerateResponseTooManyRequests
    - from:
        processor:
          name: QueueLimiter
          condition: allowed
      to:
        processor:
          name: ProdFilter
      
    - from:
        processor:
          name: ProdFilter
          condition: hit
      to:
        processor:
          name: LimiterProd
          
    - from:
        processor:
          name: LimiterProd
          condition: above_limit
      to:
        processor: 
          name: GenerateResponseTooManyRequests
    - from:
        processor:
          name: LimiterProd
          condition: below_limit
      to:
        stream:
          name: globalStream
          at: end
    - from:
        processor:
          name: ProdFilter
          condition: miss
      to:
        processor:
          name: LimiterStaging
    - from:
        processor:
          name: LimiterStaging
          condition: above_limit
      to:
        processor: 
          name: GenerateResponseTooManyRequests
    - from:
        processor:
          name: LimiterStaging
          condition: below_limit
      to:
        stream:
          name: globalStream
          at: end
  response:
    - from:
        processor:
          name: GenerateResponseTooManyRequests
      to:
        stream:
          name: globalStream
          at: end
  
This flow ensures that requests are prioritized based on the traffic type, with production receiving higher priority than staging. Any requests that exceed the defined quotas or queue size will result in a 429 Too Many Requests response.
Conclusion
Managing multiple consumers that share the same API quota can be challenging, but Lunar.dev provides a robust solution for this. By defining quotas, setting rate limits, and implementing queuing mechanisms, Lunar.dev ensures that API traffic is controlled, prioritized, and managed efficiently. With these configurations, you can maintain smooth operations across all consumers, prevent service disruptions, and avoid exceeding shared quotas.