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.

Bottom lineTrack and enforce per-user request limits using the database.

Breaking it down

Rate-limit checkrequest arrivesatomic upsertincrements countercount compared tolimitallowed, or rejectedwith a clear response
Rate-limit check

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.

LevelTime needed
Intermediate3-4 hours

What you need (tools and setup)

ToolWhat it isWhy you need itCost
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
GitVersion 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

Rate-limit counter table with an atomic increment checkSQL: edit and run

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

  1. Create a rate_limits table keyed by (user_id, window_start), with a request_count column.
  2. 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.
  3. Check the counter against the configured limit before allowing the request to proceed, returning a clear rate-limit response when exceeded.
  4. Add a scheduled cleanup for old window rows so the table does not grow unbounded.
  5. 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

CheckWhy it mattersHow to verify
Atomic incrementA read-then-write pattern can race under concurrent requestsConfirm an atomic upsert is used, not separate read/write
Clear limit responseUsers and clients need to know they were rate-limited, not just blocked silentlyTest the response when the limit is exceeded
Old-window cleanupPrevents unbounded table growth over timeConfirm a cleanup job or expiry mechanism exists
Tip:Commit the insecure version and the fixed version as two separate commits, so the diff itself becomes a teaching example.

Quick check

1. Why use an atomic upsert instead of a separate read-then-write?