Download the PHP package colinmollenhour/rate-limiter without Composer

On this page you can find all versions of the php package colinmollenhour/rate-limiter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package rate-limiter

Cm\RateLimiter is a flexible PHP library implementing multiple rate limiting algorithms using Redis with enhanced API supporting separate burst capacity and sustained rates plus concurrency-aware rate limiting to prevent request pileup. Includes comprehensive performance testing and supports Redis alternatives like Dragonfly, KeyDB, and Valkey.

Note: This is a standalone fork of bvtterfly/sliding-window-rate-limiter, refactored to remove Laravel dependencies and support multiple algorithms.

Features

Installation

See Packagist for the latest version.

Quick Start

  1. Install the library and set up a Redis client (using Credis).
  2. Create a RateLimiterFactory with the Redis client.
  3. Choose a rate limiting algorithm with or without concurrency control and create a rate limiter instance.
  4. Use the attempt() or attemptWithConcurrency() method to check if a request is allowed based on your rate limits.

Example without Concurrency Control

Example with Concurrency Control

🚀 Concurrency-Aware Rate Limiting

Solves the "request pileup" problem: Even with rate limiting, slow operations can pile up and overwhelm backends.

Example Problem:

Composable API Design

Any rate limiting algorithm can be combined with concurrency control using the factory:

How It Works

  1. Concurrency Check First - Acquire a concurrency slot using Redis semaphore pattern
  2. Rate Limit Check Second - Only requests with concurrency slots count against rate limits
  3. Clear Failure Modes - Different response codes for concurrency (503) vs rate limiting (429)
  4. Composable Design - Any algorithm can be combined with concurrency control

Use Cases

Algorithms

Each algorithm uses atomic Redis operations via Lua scripts for consistency and performance.

Algorithm Accuracy Memory Burst Support Performance Best For
Sliding Window High Higher Configurable (smooth rate) Good APIs requiring smooth rate limiting
Fixed Window Medium Lower Full (window limit) Excellent High-traffic applications
Leaky Bucket High Medium Full (bucket capacity) Good Traffic spike handling with average rate control
GCRA High Lower Configurable (smooth rate) Excellent Memory-efficient smooth rate limiting
Token Bucket High Medium Perfect (burst + refill) Excellent Burst-tolerant APIs with gradual refill

Sliding Window

Fixed Window

Leaky Bucket

GCRA (Generic Cell Rate Algorithm)

Token Bucket

Choosing the Right Algorithm

Need True Burst + Sustained Rate Control?

→ Use Token Bucket - The only algorithm that properly implements both parameters.

Need Maximum Performance?

→ Use GCRA - Highest throughput (~2x faster) with lowest memory usage.

Need Smooth Rate Limiting?

→ Use Sliding Window or GCRA - Both provide smooth traffic distribution without burst spikes.

Need Simple High-Performance Solution?

→ Use Fixed Window - Lowest complexity, highest throughput after GCRA, acceptable burst behavior.

Need Burst with Average Rate Control?

→ Use Leaky Bucket - Good balance of burst tolerance and average rate enforcement.

Performance Comparison

Based on max-speed benchmarking (requests/second with no throttling):

Algorithm Throughput (RPS) Latency Avg (ms) Latency P99 (ms) Memory per Key Best Use Case
GCRA ~25,900 0.151 0.460 Single float High-performance applications
Fixed Window ~13,700 0.286 0.670 Single counter + TTL Simple high-traffic apps
Leaky Bucket ~13,300 0.298 0.710 Hash with 3 fields Traffic spike handling
Sliding Window ~12,900 0.307 0.740 Sorted set Precise rate limiting
Token Bucket ~12,800 0.308 0.800 Hash with 4 fields Burst-tolerant APIs

Key Insight: GCRA offers the lowest memory usage, highest performance, AND lowest latency (~3x faster than other algorithms), making it ideal for high-scale applications.

Advanced Usage

Enhanced API with Burst + Sustained Rate Support

This library provides a powerful API that separates burst capacity from sustained rate, giving you fine-grained control over rate limiting behavior.

Core Methods

API Parameters

Algorithm-Specific Behavior

Token Bucket (Perfect Burst + Sustained)

Fixed Window (Burst as Window Limit)

Sliding Window & GCRA (Smooth Rate)

Leaky Bucket (Burst as Capacity)

Factory Methods

Direct Instantiation

Result Objects

Standard Rate Limiting Result

Concurrency-Aware Result

Common Use Cases

Web API with Burst Tolerance

Concurrency-Aware API with Token Bucket

Pure Concurrency Control (No Rate Limiting)

Smooth Rate Limiting

High-Performance with Acceptable Bursts

Memory-Efficient Smooth Limiting

Unit Testing

This library includes a comprehensive PHPUnit test suite covering all algorithms and features.

Requires Redis running on localhost:6379.

Testing with Docker

🎮 Interactive Playground

The playground provides a web interface for testing rate limiting algorithms with real-time results:

Quick Examples

Key Parameters

Accessing the Playground

Docker (Recommended):

The Docker setup includes:

PHP Built-in Server:

Stress Testing

Comprehensive stress testing tools are included to benchmark and compare algorithms under load.

Test Files

Prerequisites

  1. PHP Extensions Required:

    • pcntl - For multi-process testing
    • redis or Credis library - For Redis connectivity
  2. Redis Server:
    • Must be running on localhost:6379
    • Will be cleared (FLUSHDB) during tests

Running Stress Tests

Basic functionality validation:

Full stress test with CLI options:

HTTP Load Testing with "hey" CLI

For real-world HTTP load testing against the playground, use the hey CLI tool.

Basic Load Testing:

Advanced Load Testing Scenarios:

Key "hey" Parameters:

Expected Results:

Monitoring During Tests:

Test Scenarios

  1. High Contention (--scenarios=high) - 5 keys, tests algorithm behavior under high contention
  2. Medium Contention (--scenarios=medium) - 50 keys, balanced load testing
  3. Low Contention (--scenarios=low) - 1000 keys, tests distributed load performance
  4. Single Key Burst (--scenarios=burst) - 1 key, extreme contention scenario
  5. Custom (--keys=N) - User-defined parameters

CLI Options

Metrics Collected

For each algorithm and scenario:

Latency Measurement

The stress test includes comprehensive latency measurement using high-precision microtime() to measure the added latency of each rate limit check:

Features:

Examples:

Testing Modes

The stress test supports two distinct testing modes:

  1. Rate Limiting Behavior Test (default): Tests how algorithms behave under controlled load with request throttling
  2. Max Speed Performance Test (--max-speed): Tests raw algorithm throughput with no throttling to reveal true performance differences

Expected Performance Characteristics

SlidingWindow Algorithm:

FixedWindow Algorithm:

LeakyBucket Algorithm:

GCRA Algorithm:

Token Bucket Algorithm:

Troubleshooting

PCNTL Extension Missing:

Redis Connection Issues:


All versions of rate-limiter with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
colinmollenhour/credis Version ^1.13
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package colinmollenhour/rate-limiter contains the following files

Loading the files please wait ...