Redis rate limiting for Node.js, Express, and OTP endpoints
Authentication and SMS OTP routes are high-value targets for credential stuffing and billing abuse. Keeping rate-limit counters in Node.js process memory breaks as soon as traffic scales across multiple instances or a container restarts. Key Takeaway: Execute fixed-window or sliding-window increments inside an atomic Redis Lua script so key expiration never leaks on worker crash, enforce separate…
This brief discusses the importance of implementing rate limiting for authentication and SMS OTP routes in Node.js applications using Redis. The key takeaways include executing rate limit increments inside an atomic Redis Lua script to ensure key expiration does not leak on worker crashes, enforcing separate IP and phone/email identity keys on OTP endpoints, and returning standard HTTP 429 responses with Retry-After headers.
It is also emphasized to separate IP limits from recipient identity limits to avoid blocking legitimate users and to normalize email addresses and E.164 phone numbers before hashing or building Redis keys. The brief also provides a sample implementation of an atomic fixed-window counter using a Redis Lua script to guarantee atomicity and prevent race conditions.
Brief written by urgent.news; may contain errors — check the original.
Source: dev.to