Home / Learn Secure SQL / Project: Secure API with Database-Backed Rate Limiting
Project: Secure API with Database-Backed Rate Limiting
Track and enforce per-user request limits using the database.
Breaking it down
The basic idea
An API rate limiter records a request count per user per time window in the database, and the request-handling code checks and increments this count with safe, concurrency-aware queries before allowing a request through.
Why this comes up
Real projects are where secure patterns become habits instead of theory.
The mechanics
Work through the steps in order; do not skip the checklist.
Where it is used
- Portfolio project
- Code review practice
- Interview preparation
What you will build
An API rate limiter records a request count per user per time window in the database, and the request-handling code checks and increments this count with safe, concurrency-aware queries before allowing a request through.
| Level | Time needed |
|---|---|
| Intermediate | 3-4 hours |
What you need (tools and setup)
| Tool | What it is | Why you need it | Cost |
|---|---|---|---|
| Code editor (VS Code or similar) | The program you write code in. | You need somewhere to write and run the example app. | free |
| A language runtime (Node.js, Python, PHP or similar) | Installs the interpreter so your code can run on your computer. | The project examples run as small scripts. | free |
| SQLite (bundled, no install needed) | A tiny file-based database engine. | Every project in this course can be tried with SQLite first, with zero setup. | free |
| Git | Version control: tracks every change you make. | Lets you compare the insecure version and the fixed version of the code. | free |
Step 1: see the problem and the fix in the sandbox
The ON CONFLICT upsert increments the counter atomically at the database level, avoiding a separate read-then-write race where two concurrent requests could both read the same stale count.
Step by step
- Create a rate_limits table keyed by (user_id, window_start), with a request_count column.
- Use an atomic upsert (ON CONFLICT DO UPDATE, or the equivalent for your database) to increment the counter, rather than a separate SELECT then UPDATE.
- Check the counter against the configured limit before allowing the request to proceed, returning a clear rate-limit response when exceeded.
- Add a scheduled cleanup for old window rows so the table does not grow unbounded.
- Write a test that fires many rapid requests for one user and confirms the limit is enforced correctly even under concurrency.
Security checklist before you ship it
| Check | Why it matters | How to verify |
|---|---|---|
| Atomic increment | A read-then-write pattern can race under concurrent requests | Confirm an atomic upsert is used, not separate read/write |
| Clear limit response | Users and clients need to know they were rate-limited, not just blocked silently | Test the response when the limit is exceeded |
| Old-window cleanup | Prevents unbounded table growth over time | Confirm a cleanup job or expiry mechanism exists |
Quick check
1. Why use an atomic upsert instead of a separate read-then-write?
Atomicity avoids races between concurrent increments.