PHPackages                             twinh/rate-limiter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. twinh/rate-limiter

ActiveLibrary[Caching](/categories/caching)

twinh/rate-limiter
==================

A flexible and scalable Rate Limiter, supporting various common strategies and storage methods.

v1.0.0(1y ago)04PHPPHP &gt;=8.0

Since Oct 23Pushed 1y ago1 watchersCompare

[ Source](https://github.com/twinh/rate-limiter)[ Packagist](https://packagist.org/packages/twinh/rate-limiter)[ RSS](/packages/twinh-rate-limiter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

PHP Rate Limiter
================

[](#php-rate-limiter)

[![GitHub Workflow Status](https://camo.githubusercontent.com/87e9c91865d935aa00a175ac3e9f4aeaef486b27aa02d0636cc1d3971d232a23/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7477696e682f726174652d6c696d697465722f6275696c642e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/twinh/rate-limiter/actions)[![Coverage Status](https://camo.githubusercontent.com/d94c136d8724a790ce88610dc36aaf0be647092ddf282fbd44b151988b2782d4/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f7477696e682f726174652d6c696d697465722e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/r/twinh/rate-limiter)[![License](https://camo.githubusercontent.com/30597ff9a350144f03bffdd9183e16468e0b3ca1193e1d08591d992622738d55/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](http://www.opensource.org/licenses/MIT)

A flexible and scalable Rate Limiter, supporting various common strategies and storage methods.

Getting started
---------------

[](#getting-started)

```
use RateLimiter\FixedWindow;
use RateLimiter\FixedWindow\MemoryStorage;

$limiter = new FixedWindow(new MemoryStorage(), windowSize: 10, limit: 1);
if (!$limiter->attempt('user_id')) {
    echo 'Too many requests';
}
```

Feature
-------

[](#feature)

- ✨ Supports multiple strategies: `FixedWindow`, `SlidingWindow`, `LeakyBucket`, and `TokenBucket` (not yet).
- 🖥️ Compatible with `Redis`, `Memory`, and `Database` (not yet) storage methods.
- 💪 Robust and scalable architecture with **atomic** operations for high traffic and large user bases.
- ⚙️ Flexible rate limit and window size configuration.
- 🚪 Clear API for attempting, getting remaining attempts, and clearing rate limits.
- 🌐 Supports global, and user-specific rate limits.

Install
-------

[](#install)

```
composer require twinh/rate-limiter
```

Document
--------

[](#document)

### Initialize the FixedWindow Rate Limiter

[](#initialize-the-fixedwindow-rate-limiter)

```
use RateLimiter\FixedWindow;
use RateLimiter\FixedWindow\MemoryStorage;

$limiter = new FixedWindow(new MemoryStorage(), windowSize: 60, limit: 1);
```

### Initialize the SlidingWindow Rate Limiter

[](#initialize-the-slidingwindow-rate-limiter)

```
use RateLimiter\SlidingWindow;
use RateLimiter\SlidingWindow\MemoryStorage;

$limiter = new SlidingWindow(new MemoryStorage(), windowSize: 60, limit: 1);
```

### Initialize the LeakyBucket Rate Limiter

[](#initialize-the-leakybucket-rate-limiter)

```
use RateLimiter\LeakyBucket;
use RateLimiter\LeakyBucket\MemoryStorage;

$limiter = new LeakyBucket(new LeakyMemoryStorage(), rate: 10, limit: 1);
```

### Set Redis Storage

[](#set-redis-storage)

```
use RateLimiter\FixedWindow;
use RateLimiter\FixedWindow\RedisStorage;

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$limiter = new FixedWindow(new RedisStorage($redis), windowSize: 10, limit: 1);
```

### Attempts to execute a rate limited operation

[](#attempts-to-execute-a-rate-limited-operation)

```
if (!$limiter->attempt('user_id')) {
    echo 'Too many requests';
}
```

### Attempts to execute a rate limited operation with global scope

[](#attempts-to-execute-a-rate-limited-operation-with-global-scope)

```
$limiter->attempt();
```

### Attempts to execute a rate limited operation with client IP

[](#attempts-to-execute-a-rate-limited-operation-with-client-ip)

```
$limiter->attempt($_SERVER['REMOTE_ADDR'] ?? 'unknown');
```

### Attempts to execute a rate limited operation with server IP

[](#attempts-to-execute-a-rate-limited-operation-with-server-ip)

```
$limiter->attempt($_SERVER['SERVER_ADDR'] ?? 'unknown');
```

### Get the remaining attempts

[](#get-the-remaining-attempts)

```
$remaining = $limiter->getRemaining('user_id');
```

### Clear the request limit

[](#clear-the-request-limit)

```
$limiter->clear('user_id');
```

Extend more storages and rate limiters
--------------------------------------

[](#extend-more-storages-and-rate-limiters)

### Add Storage

[](#add-storage)

1. Create a new class that implements the `RateLimiter\Xxx\StorageInterface` interface of the rate limiter.
2. Implement the methods of the storage class.
3. Create the rate limiter object with the new storage object.

### Add Rate Limiter

[](#add-rate-limiter)

1. Create a new class that implements the `RateLimiter\RateLimiterInterface`.
2. Implement the methods of the rate limiter class.
3. Create a `StorageInterface` for the rate limiter class.
4. Implement storage object.

Class Diagram
-------------

[](#class-diagram)

[![Class Diagram](docs/class-diagram.svg)](https://raw.githubusercontent.com/twinh/rate-limiter/refs/heads/main/docs/class-diagram.svg)

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

565d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8a09e00cb028f695bc3e5a4319c26b3955f546c4d7a0aa7cab553e4de1f3efab?d=identicon)[twin](/maintainers/twin)

---

Top Contributors

[![twinh](https://avatars.githubusercontent.com/u/594436?v=4)](https://github.com/twinh "twinh (14 commits)")

---

Tags

redislimiterrate-limiterrate limittoken bucketleaky bucketsliding-windowfixed-window

### Embed Badge

![Health badge](/badges/twinh-rate-limiter/health.svg)

```
[![Health](https://phpackages.com/badges/twinh-rate-limiter/health.svg)](https://phpackages.com/packages/twinh-rate-limiter)
```

###  Alternatives

[predis/predis

A flexible and feature-complete Redis/Valkey client for PHP.

7.8k305.7M2.4k](/packages/predis-predis)[snc/redis-bundle

A Redis bundle for Symfony

1.0k39.4M67](/packages/snc-redis-bundle)[nikolaposa/rate-limit

General purpose rate limiter implementation.

2711.5M3](/packages/nikolaposa-rate-limit)[maba/gentle-force-bundle

Symfony bundle that integrates gentle-force library for limiting both brute-force attempts and ordinary requests, using leaky/token bucket algorithm, based on Redis

53517.6k1](/packages/maba-gentle-force-bundle)[maba/gentle-force

Library for limiting both brute-force attempts and ordinary requests, using leaky/token bucket algorithm, based on Redis

45591.0k2](/packages/maba-gentle-force)[clue/redis-protocol

A streaming Redis protocol (RESP) parser and serializer written in pure PHP.

5311.0M13](/packages/clue-redis-protocol)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
