logo
Parslinks
Back to blog
Platform4 min read

Rate limits and bot defense: from the login form to your public API

A large share of any popular site's traffic isn't human. The question is which bots to let in.

Ehsan Yaghmori

Founder & CTO, Parslinks

A job board notices its servers are busier at 3 a.m. than at noon. The logs show a handful of IPs requesting dozens of listing pages per second: a competitor is copying every job post. At the same time, the login form sees thousands of failed attempts per night using leaked email-and-password pairs from other sites.

Neither is a sophisticated attack. A sensible rate-limiting layer stops most of both.

What rate limiting means

"Each key may make at most N requests per time window." The key is often the IP, but for signed-in users the user ID is better, and for a login form the username being tried.

Two common algorithms

Fixed window: a counter that resets every minute. Simple, but a client can send twice the limit by bursting at the end of one window and the start of the next.

Token bucket: each key has a bucket that refills at a steady rate; each request takes a token. It allows short bursts (a user quickly opening a few pages) while capping the average. A better default for most APIs.

Suggested limits per route

Set limits per route

One number for the whole site doesn't work. Listing pages can have generous limits. Login, signup, password reset and SMS code requests need much tighter ones; no real person submits a password twenty times a minute.

Answer with 429

When a limit is hit, return 429 Too Many Requests with a Retry-After header saying when to try again. Well-behaved clients respect it.

Where to enforce it

The earlier, the better. A limit at the edge or reverse proxy rejects traffic before it touches your app or database. A limit inside the app is more precise because it knows the user. Combining both is common: a coarse per-IP limit at the edge, and fine-grained per-user, per-route limits in the app.

With multiple instances, counters must be shared, usually in Redis. Otherwise each instance enforces its own limit and the real limit is multiplied.

Bots you want

Don't block search engine crawlers. Verify them with reverse DNS rather than the User-Agent, which anyone can fake. Write a sensible robots.txt, knowing that bad bots ignore it.

The outcome

The job board added a coarse per-IP limit at the edge, a per-account limit on the "view employer contact" endpoint and a cap of five login attempts per ten minutes per email. Overnight load fell by 70%, and credential stuffing stopped getting anywhere.

Frequently asked questions

Join the discussion

No comments yet. Be the first to share your thoughts.