Writing · 2026-11-26
From 126 Seconds to 4: The Unglamorous Fix Behind a 30x Speedup
A matching feature I worked on took about 126 seconds to answer a single request at the median. After the fix it took 4. Nothing about the fix was clever, and I think that is the useful part of the story.
The feature matched people against a table of about 1.24 million account records. It had to keep working with 100 requests in flight at once. When I started, the median response was a little over two minutes.
Where the time went
The code walked through records one after another and checked each one. With a few thousand rows that is fine. With 1.24 million rows and many users at once, every request paid for a full pass over the data, and the requests queued up behind each other.
There was no hidden bug. The design simply didn't match the size of the data any more.
What I changed
1. Let the database do the searching. I normalized the data the matching logic needed into proper tables and added indexes for the fields it filtered on. The application stopped scanning rows itself and asked PostgreSQL a precise question instead. This was the biggest single win.
2. Take heavy work off the request path. Work that didn't need to finish before the user got an answer moved to an event-driven path: API Gateway receives the request, a message goes onto SQS, and Lambda workers process it. Large payloads go to S3, and only a reference travels through the queue, so messages stay small.
3. Protect the database from the fix. Once work runs in parallel, the database becomes the bottleneck you created. I put PgBouncer in front of PostgreSQL for connection pooling and set concurrency limits on the workers, so a spike in traffic can't open hundreds of connections at once.
4. Make retries safe. Queues deliver messages more than once, and networks fail. Every step became idempotent, so processing the same message twice does no harm, and failed calls retry with exponential backoff instead of hammering the service that just failed.
The result
Median response time went from about 126 seconds to 4 seconds, across 1.24 million records at 100 concurrent requests. That is roughly 30 times faster.
What I took from it
- Measure first. The p50 number told me this was a design problem, not a tuning problem. Tweaking timeouts would have hidden it.
- Indexes beat cleverness. Most "slow AI feature" problems I've seen are ordinary data access problems wearing an AI label.
- Async is not free. Moving work to queues helps only if you also handle duplicates, retries and database load. Otherwise you trade a slow system for a flaky one.
- Keep the boring parts boring. PgBouncer, SQS and an index are not exciting. They are why the system still behaves at 100 concurrent requests.
If you are looking at a feature that got slow as your data grew, start with the query plan before you reach for a bigger instance.
Abuzar Zulfiqar is an AI/ML engineer who builds production AI agents, RAG assistants and the backends behind them. More case studies at abuzar18.github.io.