Use one atomic counter with an expiry per client and time window to reject excess requests.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Use one atomic counter with an expiry per client and time window to reject excess requests.
Prerequisites
Use a dedicated development environment and sample data. Where a snippet uses an injected connection, repository, request value or mailer, supply it from your own application setup.
Example 1: Basic Use
A focused working pattern for this topic. Names such as $pdo, $authContext and $repo represent dependencies provided by the application unless initialized in the snippet.
<?php
$window = intdiv(time(), 60);
$key = 'rate:' . hash('sha256', $clientId) . ':' . $window;
$redis->set($key, 0, ['nx', 'ex' => 65]);
$count = $redis->incr($key);
if ($count > 60) { http_response_code(429); header('Retry-After: 60'); exit; }
Example 2: A Second Scenario
A second use case that adds a different operation or data flow specific to this topic.
<?php
$key = 'rate:token:' . hash('sha256', $apiToken) . ':' . intdiv(time(), 60);
$redis->multi(); $redis->incr($key); $redis->expire($key, 65); $results = $redis->exec();
$allowed = (int) $results[0] <= 30;
Example 3: Practical Checks
A practical validation, failure or deployment concern specific to this topic.
<?php
$key = 'rate:ip:' . hash('sha256', $clientIp) . ':' . intdiv(time(), 300);
$current = $redis->incr($key);
if ($current === 1) $redis->expire($key, 310);
if ($current > 100) { header('Retry-After: 300'); http_response_code(429); exit; }
Important Behavior and Edge Cases
For exact sliding windows or globally coordinated quotas use Redis scripts or sorted-set algorithms; a fixed window permits boundary bursts.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| PHP REST API Rate Limiting with Redis | Use one atomic counter with an expiry per client and time window to reject excess requests. |
| Related API / pattern | Use this workflow as a focused starting point rather than a substitute for application-level tests. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | For exact sliding windows or globally coordinated quotas use Redis scripts or sorted-set algorithms; a fixed window permits boundary bursts. |
| Using a related API interchangeably | Use this workflow as a focused starting point rather than a substitute for application-level tests. |
Key Takeaways
- Use one atomic counter with an expiry per client and time window to reject excess requests.
- For exact sliding windows or globally coordinated quotas use Redis scripts or sorted-set algorithms; a fixed window permits boundary bursts.
- Use this workflow as a focused starting point rather than a substitute for application-level tests.
Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.