I run a small Bulgarian website that sells study materials for school. It has about 1,200 visitors a week, a Symfony API on MySQL (RDS), a Next.js front end, and Cloudflare in front of everything. It's not a big site, and that's what this story is about: small sites have weak spots that normal traffic never touches.
One evening in November the home page started taking 5 to 12 seconds to load, or didn't load at all. Here's what happened, what we changed, and what I'd check on any small site with a search box.
Disclosure up front: I'm building InfraStudio Agents, a tool where AI agents read a site's analytics, error tracking and code, then suggest fixes as pull requests. This site is our own, and the agents were running on it during all of this. I'll say where they helped and where a person had to step in.
The warning came two days early
On 19 November, the agents reported that the site had been waiting two to three times longer for its database since 17 November, although nobody had changed the code. No errors, no complaints, nothing visible. The suggestion was for me: someone with server access should look.
By then our database (a burstable RDS instance) had also run out of CPU credits, so every slow query was getting slower. Nothing looked broken yet, so it looked harmless.
21 November: it breaks
Between 19:45 and 21:05 UTC, about 21,500 requests hit the search page, almost all from desktop browsers abroad. Our users are Bulgarian school students and teachers. This was not them.
About 7,600 of those requests got through to the server. The RDS slow query log for 20:00 UTC tells the rest:
- 3,596 slow queries and 6,850 seconds of database time in one hour
- The search page query ran ~1,877 times at ~1.98 s each
- The
COUNTquery for pagination ran ~1,730 times at 1.7–1.9 s each - Each query scanned ~9,800 rows
- RDS CPU went from under 11% to 91.3%
Search was slow, but the real damage was everywhere else. Every page waits on the same database, so GET / took 5 to 12 seconds, and Cloudflare logged 616 visitors who gave up (HTTP 499) on the home page alone.
Fix 1 (that night): stop pages hanging forever
The first question wasn't "why is search slow" but "why does a slow API make the whole site hang?" The answer was a chain of generous timeouts:
| Layer | How long it waited |
|---|---|
Node's fetch (Next.js server) | up to 300 s for headers |
| PHP | 600 s |
| nginx | 60 s |
| Our uptime monitor | less than all of the above |
The home page and the materials page fetched the API on every visit with no timeout at all. When the database stalled, they stalled with it.
The fix was a small helper with a real deadline that covers headers and body:
// frontend/src/app/lib/serverFetch.ts (simplified) export async function fetchJsonWithTimeout<T>(url: string, ms = 5000): Promise<T> { const controller = new AbortController(); const timer = setTimeout(() => controller.abort(), ms); try { const res = await fetch(url, { signal: controller.signal }); if (!res.ok) throw new Error(`HTTP ${res.status}`); return (await res.json()) as T; // the timer still runs while the body is read } finally { clearTimeout(timer); } } Normal API answers take 30–90 ms, so 5 seconds is generous. The home page now renders without its "latest materials" list if the API is stuck, instead of not rendering at all. The health check got a 2-second deadline, so a stuck API shows up as a quick 503, not a hung monitor.
This went live 48 minutes after I wrote "this is happening right now, please investigate". It didn't fix the cause, but it stopped one slow part from taking the whole site down.
Fix 2 (next day): make each search cheap and bounded
The next morning, with the slow query log in hand, the cause was clear. Two problems.
Problem 1: every word made the query more expensive, with no limit. Our search is order-independent and multi-word. Each word added three LIKE '%…%' conditions (subject, snippet, tags). A leading % means no index can help, so the database reads every row for every condition. A bot sending long queries made each search cost many times more than a human's would.
The fix caps it. At most the first 5 words, only words of 2+ characters, from the first 100 characters:
private const SEARCH_MAX_TERMS = 5; private const SEARCH_MIN_TERM_LENGTH = 2; private const SEARCH_MAX_QUERY_LENGTH = 100; // in addMultiTermSearch(): $normalized = mb_substr($normalized, 0, self::SEARCH_MAX_QUERY_LENGTH); $terms = preg_split('/\s+/u', mb_strtolower($normalized)); $terms = array_values(array_filter( $terms, fn($t) => mb_strlen($t) >= self::SEARCH_MIN_TERM_LENGTH )); $terms = array_slice($terms, 0, self::SEARCH_MAX_TERMS); One-letter words like "и" ("and" in Bulgarian) match almost every row anyway. They cost CPU and add nothing.
Problem 2: the pagination COUNT was sorting rows it only had to count. We built the count query by cloning the page query, which carried its ORDER BY along:
$countQb = clone $qb; $countQb->resetDQLPart('orderBy'); // the one line that was missing $total = $countQb->select('COUNT(a.id)')->getQuery()->getSingleScalarResult(); If you clone a Doctrine query builder for a count, check for this. It's easy to miss because it's correct, just slow.
Problem 3: how many searches could run at once. nginx already had a limit_conn on the search location, but it allowed 16 concurrent searches. That's enough to fill a small database. We lowered it to 4. In a new flood, some searches now get "try again", and the rest of the site keeps working.
The code change was live 18 minutes after I approved it.
Where the AI helped, and where it didn't
Both fixes were written by the agents as pull requests. A second AI reviewed each one, and I merged them. A few things I liked:
- It said what it couldn't prove. The first PR said plainly that it couldn't find what the servers were doing at the moment of the alert, because the repository had no record of it. It made the site fail gracefully instead of pretending to know the cause.
- It knew where it wasn't allowed to go. The agents can't edit our
docker/folder, where the nginx config lives. So the PR didn't touch it. It told me to lowerlimit_connfrom 16 to 4 by hand and wrote the exact change for me. I'd rather have that than an agent that edits production config. - It wrote down how to check the fix later: what to watch in CloudWatch, Sentry and Cloudflare at the next flood, with numbers ("RDS CPU stays under 50%, connections under 10").
What it didn't do: tell me who the bots were or block them. That's still a Cloudflare question.
What I'd check on any small site
- Does every server-side fetch have a deadline? Defaults are minutes, not seconds.
- Can one endpoint take down the database for everyone? Search, filters and exports are the usual suspects. Bound the input and cap concurrency.
- Do your count queries sort? Remove
ORDER BYfrom anything that only counts. - Is your database on burstable CPU? When the credits run out, everything that was "fine" gets slow at the same time.
- Do you act on warnings that don't hurt yet? Our database warning came two days before the outage.
The 20 days around it
This incident happened during a 20-day test where I let the agents work on our site (5–24 November). The overall numbers, in case they're useful:
- 23 suggestions I said yes to; 18 became pull requests
- 18 of 18 merged, 17 exactly as written, 0 reverted
- Median time from my "yes" to live: 37 minutes
Most of the work was not dramatic. 9 of the 23 suggestions were about measuring something before changing it. The full write-up is here.
If you've dealt with search floods on a small site, I'd like to know what worked for you: rate limiting at Cloudflare, a captcha, FULLTEXT indexes, something else?

