Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To enforce one quota across multiple Node.js instances, store each client’s rate-limit state in Redis and make every check-and-update a single atomic operation. A reliable design also chooses its burst policy, uses one time source, defines what happens when Redis is unreachable, and specifies how existing buckets behave when configuration changes.

How does Redis share a rate limit across Node.js instances?

A counter held in a Node.js process applies only to requests that reach that process. With multiple instances, each can independently admit traffic against its own counter, so the fleet may allow more requests than the intended quota. Put the limiter’s state in a shared Redis keyspace instead: each instance checks and updates the same state for the relevant client or resource.

Choose the key scope to match the policy. It might represent a user, IP address, tenant, endpoint, or a global resource. Define the identity carefully: if the key uses an IP address, for example, the application must use the client address it is authorized to trust rather than blindly trusting a forwarded header.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make the decision and state update atomic

A request must not be allowed to read an available balance, pause, and then consume it after another request has already spent the same allowance. Redis Lua scripts execute atomically, so a short script can read the bucket, calculate its current balance, decide whether to admit the request, update the state, and return the result as one operation. Keep scripts bounded: while a script runs, Redis blocks other server activity.

#1 Best Overall
CanaKit Raspberry Pi 5 Starter Kit PRO - Turbine Black (128GB Edition) (8GB RAM)
  • Includes Raspberry Pi 5 with 2.4Ghz 64-bit quad-core CPU (8GB RAM)
  • Includes 128GB Micro SD Card pre-loaded with 64-bit Raspberry Pi OS, USB MicroSD Card Reader
  • CanaKit Turbine Black Case for the Raspberry Pi 5
  • CanaKit Low Noise Bearing System Fan
  • Mega Heat Sink - Black Anodized

A token-bucket script typically stores the available tokens and the last-refill time. On each request it calculates elapsed time, adds tokens at the configured refill rate up to the bucket capacity, and deducts the request’s cost only if enough tokens are available. It returns the allow-or-deny decision and the remaining balance. Treat this as an implementation pattern, and check it against the Redis version and replication mode used by the deployment.

Key design and script availability

Use a consistent key scheme that separates policies and identities without exposing sensitive information in key names. All application instances enforcing the same policy must agree on the key and its parameters. The Redis Node.js tutorial demonstrates loading a script and calling it by SHA with EVALSHA, then using EVAL if the script is absent from the server’s script cache. Restart or failover can require scripts to be loaded again, so handle cache loss rather than assuming a script SHA is permanently available.

Redis Functions are another server-side option beginning with Redis 7.0. Choose scripts or functions in accordance with the server version and deployment process; neither removes the need to test behavior after restart and failover.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
CanaKit Raspberry Pi 4 4GB Starter PRO Kit - 4GB RAM
  • Includes Raspberry Pi 4 4GB Model B with 1.5GHz 64-bit quad-core CPU (4GB RAM)
  • Includes Pre-Loaded 32GB EVO+ Micro SD Card (Class 10), USB MicroSD Card Reader
  • CanaKit Premium High-Gloss Raspberry Pi 4 Case with Integrated Fan Mount, CanaKit Low Noise Bearing System Fan
  • CanaKit 3.5A USB-C Raspberry Pi 4 Power Supply (US Plug) with Noise Filter, Set of Heat Sinks, Display Cable - 6 foot (Supports up to 4K60p)
  • CanaKit USB-C PiSwitch (On/Off Power Switch for Raspberry Pi 4)

Which rate-limiting algorithm fits the desired burst behavior?

There is no universally best algorithm. The choice depends on whether short bursts are acceptable, how closely the quota must be enforced at window boundaries, and how much state the expected number of active keys can consume. The following trade-offs are those described in Redis’s rate-limiting guide, not independent performance benchmarks.

Algorithm Accuracy and burst behavior State and typical fit
Token bucket Exact in the guide’s comparison; permits controlled bursts up to bucket capacity. One hash key; useful when a burst is acceptable but sustained traffic should be limited.
Fixed window Approximate; requests on either side of a window boundary can allow roughly twice the configured limit in a short interval. One string key; simple and low-state, but boundary behavior must be acceptable.
Sliding-window log Exact in the comparison; does not permit bursts. O(n) entries; consider when precision matters enough to justify higher state use, such as for a high-value or audit-sensitive action.
Sliding-window counter Near-exact; smooths the abrupt boundary of a fixed window. Two string keys; a compromise when smoother enforcement is wanted without a per-request log.
Leaky-bucket policing Exact in the comparison; does not permit bursts. One hash key; suited to strict no-burst enforcement.

For a bursty API, a token bucket makes the burst allowance explicit through its capacity. For a quota that must not be exceeded at a boundary, avoid assuming a fixed window behaves like a continuously sliding quota. Also consider how the algorithm can report a meaningful retry delay: calculate that value from the same server-side state and decision, rather than rebuilding a separate approximation in Node.js.

Should the limiter use Redis time or the Node.js process clock?

For an elapsed-time algorithm, use one time source for all instances. If each Node.js process uses its own Date.now(), clock skew can make two servers calculate different refill amounts for the same bucket. The Redis Node.js example calls Redis TIME inside the script, so the decision uses the Redis server’s clock rather than each application host’s clock.

Rank #3
ELECROW CrowPi Case Kit for Raspberry Pi 5, 9-Inch Display
  • Not including the Raspberry Pi 5 (8GB), the Crowpi advanced version comes with the Raspberry Pi 5
  • ELECROW Black Case for the Raspberry Pi 5, CrowPi is equipped with a 9-inch HD touchscreen along with a camera; All the regular components used in DIY electronics are packed into the CrowPi development board, such as LCD, LED matrix, buzzer, light sensor, PIR sensor, ultrasonic sensor, IR sensor, etc
  • Raspberry Pi Sensors: The Crowpi raspberry pi 5 programming kit is jam-packed with lots of buttons such as 19 different sensors in a tidy easy to use package; You don't have to wait and wire things
  • Build Quality: Solid ABS shell and well made components in one place make it strong and convenient to travel
  • Programming Lessons: This raspberry pi 5 learning kit ships with step by step instructions and provides 21 lessons to take you through identifying components reading code and running it in the terminal

This centralizes time; it does not guarantee that the Redis host’s clock is correct or synchronized. Synchronize and monitor that host, and account for the effect of a clock jump on elapsed-time arithmetic. A backward jump can make elapsed time unexpectedly small or negative unless the script handles it; a forward jump can appear to refill a bucket too quickly. Decide how the script treats such cases and test that behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What should the API do if Redis is unavailable?

The limiter call can throw if its Redis connection is lost. Catch that failure and choose an explicit policy for each protected operation rather than letting an accidental exception determine whether requests proceed.

  • Fail open: allow the operation when the limiter cannot be reached. This preserves availability but means traffic may pass without quota enforcement during the incident.
  • Fail closed: reject the operation when the limiter cannot be reached. This protects the downstream resource but may block legitimate users while Redis is unavailable.

Base the choice on the cost of unauthorized traffic versus the cost of rejecting legitimate requests. Record and monitor limiter errors and the selected fallback outcome so an outage does not silently disable enforcement. For especially sensitive operations, consider whether a separate emergency control is needed; do not assume a local in-process counter is equivalent to the shared quota.

Rank #4
CanaKit Raspberry Pi 5 Desktop PC with SSD (Fully Assembled) (256 GB SSD)
  • Fully assembled for plug-and-play operation
  • Includes Raspberry Pi 5 with 8GB RAM
  • 256 GB PCIe Pi NVMe SSD (Pre-loaded with Pi 64-Bit OS)
  • M.2 HAT+
  • CanaKit Turbine Black Case for the Pi 5

Atomic execution is not a durability guarantee

A Lua script’s atomicity prevents concurrent interleaving of its check and update on the Redis server. It does not promise that every deployment preserves the exact observed limiter state through every restart, replica promotion, or outage. Redis Sentinel coordinates high availability and failover, but the Redis documentation notes that a write outage can last longer under heavy write traffic or when the selected replica is behind. Define the topology, replication and persistence assumptions, and the amount of limiter-state loss the application can tolerate.

Scripts also need to remain short. Redis can return -BUSY when a script exceeds its configured Lua script time limit. SCRIPT KILL can safely stop only a script that has not modified the dataset. A limiter script should therefore perform bounded work on a small amount of state, not scan or process an unbounded collection.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What happens to buckets when the limit changes?

Redis does not prescribe what a stored bucket should mean after capacity, refill rate, or interval changes. Pick and document the transition semantics; otherwise, instances using different configuration values may interpret the same state differently.

Best Value
RasTech Raspberry Pi 5 8GB Kit with Active Cooler and Pi5 Case
  • 【What you Get】You will get 1*Pi 5 8GB Single Board,1*RasTech Case,1*Active Cooler,1*Screwdriver,1*Installation instructions,12-month free warranty, lifetime service, 24-hour prompt and friendly response.
  • 【More Connectors】There are two USB 3.0 ports(5Gbps simultaneously) and two USB 2.0 ports, which triple total bandwidth ,support any combination of up to two cameras or displays. Peak SD card performance is doubled through support for the SDR104 high-speed mode. It provides a smooth desktop experience for you. Offer Gigabit Ethernet and a PCIe interface, along with dual-band Wi-Fi and Bluetooth 5.0/BLE wireless capability. The RasTech Pi 5 Kit use the new 27W 5.1V 5A USB-C power connector.
  • 【 Support Dual 4Kp60 Display 】Each of the two microHDMI sockets can control a 4K display at 60 Hertz, now support HDR, offering super HD video for media streaming projects. RPi 5 is the first RPi model that comes with a PCI Express port (PCIe 2.0 x1 with 500 MB/s) to attach SSDs (requires separate M.2 HAT).
  • 【 Excellent Chips And Applications】Pi 5 is a full-size Pi computer using silicon built in-house at Pi. The RP1 “southbridge” provides the bulk of the I/O capabilities for Pi 5. Pi 5 is more friendly and convenient in the development of Internet of Things, Web development, machine identification, automatic control and other electronic equipment applications and network.
  • 【 Faster CPU, Better GPU 】 Pi 5 features a Broadcom BCM2712 64-bit quad-core Arm Cortex-A76 processor running at 2.4GHz, it delivers a 2–3× increase in CPU performance relative to RaspberryPi 4. The 800MHz VideoCore VII GPU is compatible to OpenGL ES 3.1 and Vulkan 1.2, substantial uplift in graphics performance. Pi 5 Offers lightning-fast CPU speed, a PCI Express interface, a Real Time Clock (RTC) and a power button and runs significantly cooler than Pi 4.

Version keys for a new policy

Include a policy or configuration generation in the key so a changed policy starts a distinct bucket. This makes the transition easy to reason about, but requests using the new key may receive a fresh allowance while old keys still exist. Decide whether that burst is acceptable and whether old and new policies should overlap during rollout.

Keep keys stable and migrate the stored state

Retain the key and have the atomic script interpret existing tokens and timestamps under the new parameters. Specify how to handle a reduced capacity—for example, clamp stored tokens to the new capacity—and how to calculate refill after a rate change. The state transition and request decision should happen in the same script, so a request cannot observe a partially migrated bucket.

Wait for current state to expire

For algorithms and products that permit delay, apply a new policy after the current windows or buckets have run their course. This avoids an abrupt reinterpretation but means the old policy remains in force during the waiting period.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Whichever approach is chosen, coordinate configuration across the fleet through a central configuration or version source, a synchronized deployment, or a policy version embedded in keys. Do not let instances apply different limits to one shared key unless the script explicitly defines how that mixed rollout is supposed to work.

How should the API report a rate-limit decision?

When a request is denied, return HTTP 429 and provide a Retry-After value where the policy can calculate one. Expose the applicable limit and remaining allowance in response headers if clients need them. For a token bucket, derive the retry delay from the number of tokens missing and the refill rate; for window-based algorithms, derive it from the relevant window state. Have Redis return the decision and the values needed for the response together, so the headers describe the state that actually produced the decision.

Do not treat a reset estimate as an independent promise if the algorithm has more complex refill or window behavior. Ensure the application handles denied requests and Redis errors as separate outcomes: a quota denial is a normal policy response, while an unavailable limiter invokes the explicitly chosen failure policy.

Quick Recap

Bestseller No. 1
CanaKit Raspberry Pi 5 Starter Kit PRO - Turbine Black (128GB Edition) (8GB RAM)
CanaKit Raspberry Pi 5 Starter Kit PRO - Turbine Black (128GB Edition) (8GB RAM)
Includes Raspberry Pi 5 with 2.4Ghz 64-bit quad-core CPU (8GB RAM); CanaKit Turbine Black Case for the Raspberry Pi 5
$259.95
Bestseller No. 2
CanaKit Raspberry Pi 4 4GB Starter PRO Kit - 4GB RAM
CanaKit Raspberry Pi 4 4GB Starter PRO Kit - 4GB RAM
Includes Raspberry Pi 4 4GB Model B with 1.5GHz 64-bit quad-core CPU (4GB RAM); Includes Pre-Loaded 32GB EVO+ Micro SD Card (Class 10), USB MicroSD Card Reader
$159.99
Bestseller No. 4
CanaKit Raspberry Pi 5 Desktop PC with SSD (Fully Assembled) (256 GB SSD)
CanaKit Raspberry Pi 5 Desktop PC with SSD (Fully Assembled) (256 GB SSD)
Fully assembled for plug-and-play operation; Includes Raspberry Pi 5 with 8GB RAM; 256 GB PCIe Pi NVMe SSD (Pre-loaded with Pi 64-Bit OS)
$339.97

What to verify before deployment

  • Confirm every Node.js instance uses the same key format, policy parameters, and Redis endpoint for a given quota.
  • Test simultaneous requests against a nearly empty bucket to verify that no more requests are admitted than the algorithm permits.
  • Test script-cache loss, Redis disconnection, restart, and the deployed failover path.
  • Check the deployed Redis version and scripting behavior. Redis documents effects-based script replication as the default starting in Redis 5.0 and removal of verbatim script replication in Redis 7.0; confirm the relevant behavior for the version and mode in use.
  • Verify the deployment’s configuration mechanism. Redis can start without a configuration file, but its configuration overview characterizes that as suitable for testing and development; production setup should follow the platform’s actual configuration process.
  • If considering HEXPIRE, verify that the server supports it; Redis identifies it as a Redis 8 feature.
  • Measure the limiter under the application’s own workload. Redis product-level statements about latency or throughput are not a workload-specific guarantee for this design.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.