Rate limiting is a vital concept in web development. It ensures server stability, efficient resource allocation, and protection against malicious attacks. So In this article, we’ll delve into the essence of rate limiting, its importance, various implementation methods, and practical examples to demonstrate its functionality. let’s dive right in ?
What is Rate Limiting?
Rate limiting is a strategy that is used to control the amount of incoming requests or traffic to a web service or to a server. it helps protect your applications from abuse, ensures fair resource distribution, and maintains service stability.
Why Use Rate Limiting?
Here are some of the reasons why you should use rate limiting ??
- Preventing Abuse: Stops bots or malicious users from overwhelming the server with requests.
- Resource Management: Ensures fair usage of resources across all users.
- Security: Helps prevent brute-force attacks by limiting attempts of some endpoints in your application.
- Cost Control: Helps prevent unexpected charges due to excessive API calls.
- Performance: Keeps your server responsive and reduces the risk of downtimes.
Types of Rate Limiting
- Fixed Window (or Simple) Rate Limiting: This method limits requests within a fixed time window. For example, "100 requests per minute.""
- Sliding Window Rate Limiting: A dynamic time frame that tracks and limits requests over a recent period, such as the last few minutes or seconds.
- Token Bucket Algorithm: This method uses a "bucket" filled with tokens to manage requests. Each incoming request consumes a token, and the bucket is refilled at set intervals. This approach allows for bursts of traffic while maintaining an overall rate limit.
- Leaky Bucket Algorithm: Similar to the token bucket, but with a twist. When the bucket is full, excess requests "leak" out or are discarded, maintaining a steady flow.
? I'm not even going to lie because I don't know much about the Token Bucket and Leaky Bucket algorithms, as I haven't needed them for my current projects. However, Fixed Window and Sliding Window are the most common types you'll encounter. For instance, OpenAI's GPT-4 uses Fixed Window rate limiting with tiered limits—their first tier allows 500 requests per minute This approach can lead to burst traffic, as users might hit their limit just before the window resets.
How Rate Limiting Works
The process typically involves:
- Tracking: Monitoring how many requests a user (mostly the userId) or IP has made within a specific timeframe.
- Threshold: Defining a limit (e.g., 100 requests per hour).
- Response: Sending a warning or blocking further requests when the limit is exceeded (usually with a 429 Too Many Requests HTTP status code).
Implementing Rate Limiting: Practical Examples
Now that you have a basic understanding of rate limiting and how it works, let's get our hands dirty by implementing it in a project we'll be creating.
We'll create two projects demonstrating rate limiting:
- A GET request example
- A POST request example
Tech Stack
- Frontend: React (using Vite)
- Backend: Express (Node.js framework)
GET request example
Create a folder with any name of your choice and open it on VS code or whatever code editor you use.
Inside that folder you've created, create two more folders called frontend and backend.
After that, cd into the backend folders and enter this command npm init -y to initialize a package.json file
After that install the follow npm packages inside the backend folder ??
npm install express cors express-rate-limit npm install -D nodemon
What these do:
- express: Creates your web server and handles API routes
- cors: Allows frontend to communicate with backend safely
- express-rate-limit: Protects your API from too many requests
- nodemon: Auto-restarts server during development (that's why we use D)
After that, create an index.js (you can this whatever you want) file because we’ll be using it to set up the rate limiter.
After you’ve done copy and paste this code that I am going to explain in a bit
const express = require("express"); const rateLimit = require("express-rate-limit"); const app = express(); // Set up rate limiter: 100 requests per 15 minutes const limiter = rateLimit({ windowMs: 15 * 60 * 1000, // 15 minutes max: 5, // Limit each IP to 5 requests per `window` (here, per 15 minutes) message: "Too many requests from this IP, please try again later.", }); // Apply the rate limiting middleware to all requests app.use(limiter); app.get("/api/data", (req, res) => { res.send("Welcome to the API!"); }); app.listen(5000, () => { console.log("Server running on http://localhost:5000"); });
Here's what each part does:
- First two lines import our needed packages
- app = express() creates our server
- The limiter is configured with:
- windowMs: Sets a 15-minute time window (15 × 60 × 1000 milliseconds)
- max: Allows 5 requests per IP address in that window
- message: The error message users see when they exceed the limit
Then:
- app.use(limiter) applies our rate limit to all routes
- We create a simple test route at '/api/data' that sends a welcome message
- Finally, we start the server on port 5000
When users hit your API more than 100 times in 15 minutes from the same IP, they'll get the error message instead of accessing the API.
Now that you know how it works, we want to enable auto-restart by adding to package.json ??
{ "scripts": { "dev": "nodemon index.js" } }
That’s all for the backend.
It’s time to set up the frontend.
- Open a new terminal and cd into the frontend folder and run ??
npm install express cors express-rate-limit npm install -D nodemon
- Go through the following instructions and I’ll advise you select JavaScript if you don’t know typescript
- You can do a little clean up by getting rid of some files you won’t need. here is how mine looks
- Once you are done, open the App.jsx and paste this code that I’ll explain ??
const express = require("express"); const rateLimit = require("express-rate-limit"); const app = express(); // Set up rate limiter: 100 requests per 15 minutes const limiter = rateLimit({ windowMs: 15 * 60 * 1000, // 15 minutes max: 5, // Limit each IP to 5 requests per `window` (here, per 15 minutes) message: "Too many requests from this IP, please try again later.", }); // Apply the rate limiting middleware to all requests app.use(limiter); app.get("/api/data", (req, res) => { res.send("Welcome to the API!"); }); app.listen(5000, () => { console.log("Server running on http://localhost:5000"); });
Here's what's happening:
- We import useState for managing data and axios for making API requests
- We create two state variables:
- response: Stores successful API responses
- error: Stores any error messages
- The fetchData function:
- Gets called when button is clicked
- Tries to fetch data from our API
- Updates either response or error state
- Uses try/catch to handle success and errors
- The UI shows:
- A title
- A button to trigger requests
- The API response (if successful)
- Error messages in red (if request fails) When you click the button too many times within 15 minutes, you'll see the rate limit error message because of our backend restrictions!
That’s all about the GET request example. Let’s move on to the next example
POST request example
For this example, you can decide to comment out the code of the first example and paste this code ??
{ "scripts": { "dev": "nodemon index.js" } }
You can see that most of the code are the same with the first example but here are just some key difference ??
- Added bodyParser to handle form data
- Creates a POST endpoint that processes form submissions
Also paste this code on the frontend as well
npm create vite@latest .
Here, we're simply making a request to the server through a form. Let's look at how this differs from the GET example:
- Uses a form instead of a single button
- Manages form state with formData
- Handles input changes with handleInputChange
- Uses POST request instead of GET
- Shows success message in green
The form allows 5 submissions in 15 minutes - after that, users see the rate limit error message.
Conclusion
Alright guys, congrats on getting to the end of this article ?. I hope you now have an idea on how rate limiting works and why you should use it on your projects especially if you are working on bigger projects that involves money. If you have any questions, feel free to ask in the comment. Happy coding ?
The above is the detailed content of The Basics of Rate Limiting: How It Works and How to Use It. For more information, please follow other related articles on the PHP Chinese website!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
