Skip to main content
Rate limiting is a technique used to control the number of requests or operations that a service can handle within a specific time period. It helps prevent system overload and ensures fair resource usage.
If you want to cap how many invocations run concurrently (rather than per time window), Restate now offers built-in flow control with scope-based concurrency limits. Built-in rate limits are planned as a follow-up. The token-bucket recipe below remains the way to do rate limiting today.

How does Restate help?

Restate provides several features that make it well-suited for implementing rate limiting:
  • Durable state: Store and manage rate limit counters reliably.
  • Virtual objects: Isolated rate limiters per key (user, API endpoint, etc.).
  • Durable timers: Schedule token refills and cleanup operations.
Restate doesn’t have built-in rate limiting functionality, but its building blocks make it easy to build this.

Example

This implementation provides a token bucket rate limiter that can control the rate of operations for any service or resource. You can copy the following files to your project: The limiter client interface, which you can use in your services:
The limiter implementation, which manages the token bucket state and logic:
This implementation provides a RateLimiter Virtual Object which implements the Token Bucket Algorithm:
  • Tokens are added at a specified rate (limit)
  • Tokens are consumed when operations are performed
  • A burst capacity allows for short bursts of activity
Key Methods (via the client interface):
  • limit() / burst(): Get maximum event rate and burst size
  • tokens(): Get number of available tokens
  • wait(): Block until events are permitted to happen
  • setRate() / setLimit() / setBurst(): Configure rate limiting parameters

Usage Example

Here’s how to use the rate limiter in your services:

Running the example

1

Download the example

2

Start the Restate Server

3

Start the Service

4

Register the services

5

Set up rate limiting

Set up the limiter named myService-expensiveMethod with a rate limit of 1 per second:
Try sending multiple requests quickly to see the rate limiting in action.
6

Send requests to the rate limited handler

You can send requests that are subject to the limiter like this:
You should observe that only one request is processed per second. You can then try changing the limit or the burst and sending more requests.
7

Observe in the Restate UI

In the Restate UI, you can observe:
  • Invocations getting scheduled one per second in the Invocations tab
  • Rate limiter state and token counts in the State tab