Skip to main content

Project Brief

In this module, you will implement a robust rate limiting mechanism to protect web applications from abuse and ensure fair usage of resources. You will learn how to choose and apply different rate limiting strategies, integrate them into the middleware layer, and configure them for different endpoints.

Overview

What is Rate Limiting?

Rate limiting is a critical security and reliability mechanism that controls how many requests a client can make to your application within a given time period. This guide covers the theory behind rate limiting, why it's essential for production applications, and best practices for implementation.

Why Rate Limiting Matters

Protecting Against Abuse

Without rate limiting, your application is vulnerable to:

ThreatDescriptionImpact
Denial of Service (DoS)Attackers flood your server with requestsService becomes unavailable for legitimate users
Brute Force AttacksAutomated attempts to guess passwords or OTPsAccount compromise, security breaches
Credential StuffingUsing leaked credentials to attempt loginsUnauthorized access to user accounts
API AbuseExcessive API calls to scrape data or exhaust resourcesIncreased infrastructure costs, degraded performance
Email SpamTriggering excessive OTP/notification emailsProvider blacklisting, user harassment, cost overruns

Real-World Consequences

Consider an email OTP authentication system without rate limiting:

  1. An attacker writes a script to request OTPs for victim@example.com repeatedly
  2. The victim's inbox is flooded with hundreds of OTP emails
  3. Your email provider flags your domain for suspicious activity
  4. Legitimate users can no longer receive emails from your application
  5. Your organization's reputation suffers

Rate limiting prevents this by capping how many OTP requests can be made per user, per IP, or globally within a time window.


Implementation requirements

We will be implementing rate limiting functionalities on the same application as the one in the previous lab on CRUD. The rate limiting will help protect the application from abuse and ensure fair usage of resources.

The rate limiting implementation should meet the following requirements:

  • Apply rate limiting at the middleware layer before business logic is executed
  • Use composite keys (user ID for authenticated users, IP address for unauthenticated users)
  • Implement dual-bucket rate limiting (sustained and burst limits)
  • Use Redis for distributed coordination with in-memory fallback
  • Return appropriate error responses (HTTP 429) with retry information
  • Support per-endpoint configuration via tRPC metadata