---
schema_version: "1"
id: e7130768209fa18b1b287f48f66bbbcf4cac649dd38678c7e3d8fbce757f5974
title: Rate Limiting Requests
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/pdk/latest/policies-pdk-configure-features-rate-limiting.md
canonical_url: https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-rate-limiting
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 110175eebe213199f31a0639228095469115b720834a2c41e12097d4c0549ab4
description: MuleSoft Documentation Site
release: "1.10"
latest: true
---
---
product: Omni Gateway Policy Development Kit (PDK)
version: 1.10
is-latest-version: true
---

# Rate Limiting Requests

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Omni Gateway Policy Development Kit (PDK) provides rate limiting functionality to control request
rates.

Custom policies support both single Omni Replica and multi-replica deployments. For multi-replica
deployments, you must configure shared storage. To configure shared storage, see:

- [Configuring Redis Shared Storage for Omni Gateway in Connected Mode](https://docs.mulesoft.com/gateway/latest/conn-shared-storage-config)

- [Configuring Redis Shared Storage for Omni Gateway in Local Mode](https://docs.mulesoft.com/gateway/latest/local-shared-storage-config)

## Inject the `RateLimitBuilder`

To enable rate limiting functionality, you must inject the `RateLimitBuilder` in your custom
policy’s entrypoint and configure it for either local (single-replica) or clustered (multi-replica)
mode:

```Rust
// buckets example configuration
let buckets = vec![
    ("api", vec![Tier { requests: 100, period_in_millis: 60000 }]), // 100 requests per minute
    ("user", vec![Tier { requests: 10, period_in_millis: 30000 }]),  // 10 requests per 30 seconds
];

// timer example
let timer = clock.period(Duration::from_millis(100)); // 100ms intervals for rate limit sync

// for local mode (single-instance)
let builder = rate_limit_builder
    .new(builder_id);
let rate_limiter = builder.buckets(buckets).build()?;

// for clustered mode with shared storage (multi-instance)
let builder = rate_limit_builder
    .new(builder_id)
    .clustered(Rc::new(timer))
    .shared();
let rate_limiter = builder.buckets(buckets).build()?;
```

The `RateLimitBuilder` struct provides the following methods to configure the rate limits:

- `builder_id`: a string identifier for the rate limiter instance (required)

- `buckets`: rate limit tiers configuration with requests and time windows (optional - defaults to
  api instance configuration if unspecified)

- `timer`: a periodic timer for rate limit sync (required for clustered mode)

- `clustered`: Enables the rate limiter to use distributed storage backends. Without it, the rate
  limiter uses in-memory storage.

- `shared`: Enables the rate limiter to share state across different policy instances.

## Check Requests Against Rate Limits

After creating the rate limiter instance, use the `is_allowed` method to check if requests are
allowed with support for multiple independent rate limit configurations:

```Rust
// Check if request is allowed for a specific group and client key
match rate_limiter.is_allowed(group, &client_key, request_amount).await {
    Ok(RateLimitResult::Allowed(_)) => Flow::Continue(()),
    Ok(RateLimitResult::TooManyRequests(_)) => Flow::Break(Response::new(429)),
    Err(e) => Flow::Break(Response::new(503)),
}
```

## Rate Limiting Configuration Examples

PDK provides the
[Multi-Instance Rate Limiting Example policy](https://github.com/mulesoft/pdk-custom-policy-examples/tree/1.10.0/multi-instance-rate-limiting)
to demonstrate how to configure rate limiting in Rust code.

Within the example policy, see these functions for further configuration details:

- [`entrypoint` function](https://github.com/mulesoft/pdk-custom-policy-examples/tree/1.10.0/multi-instance-rate-limiting/src/lib.rs#L118)
  for how to configure the `RateLimitBuilder`.

- [`request_filter` function](https://github.com/mulesoft/pdk-custom-policy-examples/tree/1.10.0/multi-instance-rate-limiting/src/lib.rs#L54)
  for how the policy applies rate limits to incoming requests.

## Redis Shared Storage Configuration Example

For an example of how to configure Redis shared storage for testing, see the example
[`playground/config/shared-storage-redis.yaml`](https://github.com/mulesoft/pdk-custom-policy-examples/blob/1.10.0/multi-instance-rate-limiting/playground/config/shared-storage-redis.yaml)
and
[`playground/docker-compose.yaml`](https://github.com/mulesoft/pdk-custom-policy-examples/blob/1.10.0/multi-instance-rate-limiting/playground/docker-compose.yaml)
files.

## See Also

- [Configuring Delayed, Periodic, and Synchronous Functions](https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-timer)

- [Sharing Data Between Requests and Responses](https://docs.mulesoft.com/pdk/latest/policies-pdk-configure-features-share-data)
