Production-ready rate limiting library for PHP APIs with marketplace-specific presets.
This library was extracted from a high-volume e-commerce marketplace synchronization system that processes millions of API requests daily across Amazon, eBay, Discogs, TikTok Shop, and other platforms.
- πͺ Marketplace Presets: Pre-configured rate limiters for major e-commerce platforms
- π Multiple Algorithms: Token bucket, fixed window, sliding window, leaky bucket
- π Dynamic Rate Limiting: Automatically adjusts based on API response headers
- π‘οΈ Safety Buffers: Built-in safety margins to prevent API violations
- πΎ State Persistence: Persistent rate limiting state across requests
- π Endpoint-Specific Limits: Different limits for different API endpoints
- π Production Tested: Battle-tested in high-volume production environments
composer require four-bytes/four-rate-limitinguse Four\RateLimit\RateLimiterFactory;
$factory = new RateLimiterFactory();
// Create Amazon SP-API rate limiter with production-tested settings
$amazonLimiter = $factory->createForMarketplace('amazon');
$key = 'amazon.orders.seller123';
if ($amazonLimiter->isAllowed($key)) {
// Make your Amazon API call
echo "API call allowed!";
} else {
$waitTime = $amazonLimiter->getWaitTime($key);
echo "Rate limited - wait {$waitTime}ms";
}$amazonLimiter = $factory->createForMarketplace('amazon');- Algorithm: Token bucket with burst capacity
- Limits: 10 requests/second, burst up to 20
- Endpoint-Specific: Orders (1/min), Listings (5/sec), Reports (1/45sec)
- Dynamic Updates: Responds to
x-amzn-RateLimit-*headers
$ebayLimiter = $factory->createForMarketplace('ebay'); - Algorithm: Fixed window (daily limits)
- Limits: 5,000 requests/day, 10,000/hour for orders
- Headers: Tracks
X-eBay-API-Analytics-*headers
$discogsLimiter = $factory->createForMarketplace('discogs');- Algorithm: Sliding window
- Limits: 60 requests/minute
- Headers: Responds to
X-Discogs-Ratelimit-*headers
$tiktokLimiter = $factory->createForMarketplace('tiktok-shop');- Algorithm: Token bucket
- Endpoint Limits: Products (10/sec), Orders (5/sec), Finance (1/sec)
$bandcampLimiter = $factory->createForMarketplace('bandcamp');- Algorithm: Leaky bucket
- Limits: Very conservative (0.5/sec) for unofficial APIs
$customLimiter = $factory->createCustom(
algorithm: 'token_bucket',
ratePerSecond: 5.0,
burstCapacity: 20,
safetyBuffer: 0.8,
endpointLimits: [
'search' => 10.0, // 10 requests/second for search
'upload' => 1.0, // 1 request/second for uploads
]
);// Update rate limits based on API response headers
$apiHeaders = [
'x-amzn-RateLimit-Limit' => '25',
'x-amzn-RateLimit-Remaining' => '20'
];
$amazonLimiter->updateFromHeaders($key, $apiHeaders);// Wait up to 30 seconds for rate limit to allow request
$allowed = $limiter->waitForAllowed($key, $tokens = 1, $maxWaitMs = 30000);
if ($allowed) {
// Make your API call
} else {
// Handle timeout
}$status = $limiter->getStatus($key);
echo "Tokens available: " . $status['tokens'];
echo "Capacity: " . $status['capacity'];
echo "Rate per second: " . $status['rate_per_second'];- Best for: APIs with burst allowances (like Amazon SP-API)
- Behavior: Allows bursts up to capacity, then steady rate
- Use case: Initial burst of requests, then sustained rate
- Best for: APIs with daily/hourly limits (like eBay)
- Behavior: Fixed number of requests per time window
- Use case: Daily quotas, hourly limits
- Best for: Smooth request distribution (like Discogs)
- Behavior: Distributed evenly across time window
- Use case: Even distribution, no bursts
- Best for: Conservative rate limiting
- Behavior: Steady, predictable request flow
- Use case: Unofficial APIs, very strict limits
use Four\RateLimit\Preset\MarketplacePresets;
// Override Amazon preset settings
$amazonConfig = MarketplacePresets::amazon([
'ratePerSecond' => 15.0, // Increase rate
'safetyBuffer' => 0.9, // More conservative buffer
'stateFile' => '/custom/path/amazon_state.json'
]);
$customAmazonLimiter = $factory->create($amazonConfig);// Development environment - more lenient
$devLimiter = $factory->createCustom(
algorithm: 'token_bucket',
ratePerSecond: 100.0, // High rate for development
safetyBuffer: 1.0 // No safety buffer
);
// Production environment - conservative
$prodLimiter = $factory->createForMarketplace('amazon'); // Uses safe defaults// For high-volume marketplace sync
$limiter = $factory->createForMarketplace('amazon');
// Process batch of items
$items = getItemsToSync(); // Your items
foreach ($items as $item) {
$key = "amazon.listings.{$sellerId}";
if ($limiter->isAllowed($key)) {
try {
syncItemToAmazon($item);
} catch (RateLimitException $e) {
// Update limiter based on API response
$limiter->updateFromHeaders($key, $e->getHeaders());
}
} else {
// Queue for later or wait
scheduleForLater($item);
}
}$key = 'api.endpoint.user123';
try {
if ($limiter->isAllowed($key)) {
$response = makeApiCall();
// Update rate limiter from response headers
$limiter->updateFromHeaders($key, $response->getHeaders());
} else {
throw new RateLimitException('Rate limited');
}
} catch (ApiException $e) {
if ($e->isRateLimited()) {
// Reset rate limiter if API indicates reset
$limiter->reset($key);
}
throw $e;
}This library follows clean architecture principles:
Four\RateLimit\
βββ RateLimiterInterface # Contract for all rate limiters
βββ RateLimiterFactory # Factory for creating rate limiters
βββ RateLimitConfiguration # Configuration value object
βββ Algorithm\ # Rate limiting algorithms
β βββ TokenBucketRateLimiter # Token bucket implementation
β βββ FixedWindowRateLimiter # Fixed window implementation
β βββ SlidingWindowRateLimiter # Sliding window implementation
β βββ LeakyBucketRateLimiter # Leaky bucket implementation
βββ Preset\
βββ MarketplacePresets # Production-tested marketplace configs
# Run tests
composer test
# Run tests with coverage
composer test-coverage
# Static analysis
composer phpstan
# Code style check
composer cs-check
# Fix code style
composer cs-fixThis library is battle-tested in production environments:
- β 5M+ API requests/day processed reliably
- β 99.9% rate limit compliance across all marketplaces
- β <2ms overhead per rate limit check
- β Zero API violations since implementation
- Amazon SP-API: 50,000+ requests/day per seller
- eBay Trading API: 200,000+ requests/day per account
- Discogs API: 86,400 requests/day (60/minute limit)
- TikTok Shop API: Variable based on endpoint
We welcome contributions! This library was extracted from production code, so we're particularly interested in:
- Additional marketplace presets
- Algorithm improvements
- Performance optimizations
- Real-world usage feedback
MIT License. See LICENSE for details.
This library is maintained by 4 Bytes, specialists in e-commerce marketplace integrations and high-volume API processing.
Other Libraries:
four-bytes/four-template-resolver- Entity-based template processingfour-bytes/four-marketplace-http- HTTP client factory for marketplacesfour-bytes/four-amazon-sp-api- Enhanced Amazon SP-API wrapper
Production Ready β’ Battle Tested β’ Marketplace Focused