Menu

Stream Rate Limiter

Stream Rate limiters control the bandwidth (bytes per second) of data transfer for streaming connections. They are essential for creating differentiated service tiers with varying speeds and for ensuring fair resource allocation in streaming scenarios like video, audio, real-time APIs, WebSockets, and Server-Sent Events (SSE).

Best Use Case: Stream Rate limiters are ideal for media streaming, WebSockets, SSE, and any scenario where you want to control the speed rather than just the total amount of data transferred.

Types of Stream Rate Limiters

ApiCharge provides two stream rate limiter types to control bandwidth in both directions:

Type Description Best For
StreamRateUpload Controls the upload bandwidth (client to server) Live streaming, data ingestion, WebSocket client uploads
StreamRateDownload Controls the download bandwidth (server to client) Video/audio streaming, data feeds, real-time dashboards

These can be used independently or together to create comprehensive bandwidth management policies.

How Stream Rate Limiters Work

Unlike other rate limiters that simply count and limit overall usage, Stream Rate limiters implement a sophisticated throttling mechanism:

  1. Define a maximum bandwidth in bytes per second (BytesPerSecond)
  2. Track data transfer in sliding time windows (WindowDurationInSeconds)
  3. Dynamically pace data transfer to maintain the specified bandwidth
  4. Use a throttling mechanism that introduces controlled delays when necessary

The key differentiator is that Stream Rate limiters don't just cut off access when limits are reached—they actively regulate the flow of data to match the specified bandwidth over time.

Configuration Parameters

Stream Rate limiters have two main configuration parameters:

Parameter Description Default
BytesPerSecond The maximum bandwidth in bytes per second to allow Required
WindowDurationInSeconds The time window over which bandwidth is calculated and averaged 60 seconds

Together, these parameters define how ApiCharge will manage the data transfer rate for streaming connections.

Bandwidth Control Experience

Stream Rate limiters provide a superior experience for streaming applications:

This approach is ideal for streaming applications where consistent, uninterrupted delivery is critical for user experience.

Configuration

To configure Stream Rate limiters in your quote definition, use the following syntax:

{
  "ApiChargeQuotes": {
    "Quotes": [
      {
        "RouteId": "video-stream",
        "Duration": 3600,
        "MicroUnitPrice": 500000,
        "RateLimiters": [
          {
            "$type": "StreamRateDownload",
            "BytesPerSecond": 262144,
            "WindowDurationInSeconds": 60
          }
        ]
      }
    ]
  }
}

In this example:

Bandwidth Notation: Common bandwidth values:
  • 128 KB/s = 131,072 bytes per second
  • 256 KB/s = 262,144 bytes per second
  • 1 MB/s = 1,048,576 bytes per second
  • 10 MB/s = 10,485,760 bytes per second

Tiered Streaming Example

Creating multiple quality tiers for a streaming service is a perfect use case for Stream Rate limiters:

{
  "ApiChargeQuotes": {
    "Quotes": [
      {
        "RouteId": "video-service",
        "Duration": 86400,
        "MicroUnitPrice": 500000,
        "RateLimiters": [
          {
            "$type": "StreamRateDownload",
            "BytesPerSecond": 524288,
            "WindowDurationInSeconds": 60
          },
          {
            "$type": "DataLimitDownload",
            "Bytes": 5368709120
          }
        ]
      },
      {
        "RouteId": "video-service",
        "Duration": 86400,
        "MicroUnitPrice": 1000000,
        "RateLimiters": [
          {
            "$type": "StreamRateDownload",
            "BytesPerSecond": 1048576,
            "WindowDurationInSeconds": 60
          },
          {
            "$type": "DataLimitDownload",
            "Bytes": 10737418240
          }
        ]
      },
      {
        "RouteId": "video-service",
        "Duration": 86400,
        "MicroUnitPrice": 2000000,
        "RateLimiters": [
          {
            "$type": "StreamRateDownload",
            "BytesPerSecond": 3145728,
            "WindowDurationInSeconds": 60
          },
          {
            "$type": "DataLimitDownload",
            "Bytes": 21474836480
          }
        ]
      }
    ]
  }
}

This configuration creates three video streaming quality tiers:

Note how each tier combines both a Stream Rate limiter (for bandwidth control) and a Data Limit (for total transfer caps), providing comprehensive streaming service management.

Client Experience During Rate Limiting

Stream Rate limiters provide a seamless client experience compared to other rate limiters:

This approach is ideal for streaming media, as it prevents interruptions while still enforcing the purchased quality of service.

Deployment Considerations

When using Stream Rate limiters in your deployment, consider these factors:

Important: For WebSocket and SSE connections with Stream Rate limiters, use load balancers that support session affinity (sticky sessions) to ensure consistent rate limiting behavior.

Configuring Window Duration

The WindowDurationInSeconds parameter allows you to adjust how the rate limiting behaves:

The recommended default is 60 seconds, which works well for most streaming applications.

Protocol Support

Stream Rate limiters work with all streaming protocols supported by ApiCharge:

The implementation automatically adapts to the protocol in use, providing consistent bandwidth control regardless of the underlying communication mechanism.

Use Cases

Video and Audio Streaming

The most common application is for media streaming services:

Real-Time Data Services

For applications that deliver continuous data streams:

API Rate Management

For high-volume or continuous API access:

Advanced Configuration Example

For bi-directional WebSocket applications, you can control bandwidth in both directions:

{
  "RouteId": "websocket-api",
  "Duration": 86400,
  "MicroUnitPrice": 1500000,
  "RateLimiters": [
    {
      "$type": "StreamRateUpload",
      "BytesPerSecond": 262144,
      "WindowDurationInSeconds": 60
    },
    {
      "$type": "StreamRateDownload",
      "BytesPerSecond": 1048576,
      "WindowDurationInSeconds": 60
    },
    {
      "$type": "DataLimitUpload",
      "Bytes": 1073741824
    },
    {
      "$type": "DataLimitDownload",
      "Bytes": 5368709120
    }
  ]
}

This configuration creates an asymmetric WebSocket connection with:

This is ideal for applications like collaborative editing tools where downloads (content syncing) require more bandwidth than uploads (user edits).

Next Steps

Return to other rate limiter types: