How We Dropped an API’s P99 Latency from 1.2s to 85ms on Node and Postgres

Home 

TechTalks

11:00 am

How We Dropped an API’s P99 Latency from 1.2s to 85ms on Node and Postgres

A client recently approached us with an API endpoint that was choking their checkout flow.

Under light traffic, it responded in 250 milliseconds. Acceptable. But whenever concurrent visitors spiked during a campaign, P99 latency climbed past 1.2 seconds. Mobile users were abandoning checkout thinking the screen had frozen.

Their first instinct was to upgrade their cloud instances—doubling CPU cores and RAM. Throwing hardware at unoptimized queries is the most expensive way to handle backend performance. Instead, we profiled the execution plan. Here are the three fixes that brought P99 latency down to 85ms without adding a dollar to their cloud invoice.

1. Eliminating Sequential Scans with Composite Indexes

Running EXPLAIN ANALYZE on the main query revealed a sequential scan across 450,000 order records. The query filtered by organization_id and sorted by created_at DESC. Adding a composite B-tree index on (organization_id, created_at DESC) transformed the scan into an immediate index lookup, cutting execution time from 410ms down to 18ms.

2. Fixing Database Connection Pool Starvation

Under burst traffic, incoming requests waited up to 500ms simply to acquire an available database connection from the pool. We added pgBouncer in transaction pooling mode in front of the PostgreSQL instance, allowing 500+ client connections to share 25 server connections with zero queuing delay.

3. Cache Invalidation with Redis Hashes

For frequently queried, rarely updated account settings, we placed a Redis cache layer ahead of the database. By using Redis hashes scoped by organization ID, cache invalidation occurs immediately on update without risking stale reads.

Optimize Your Backend Performance with TechnologyBae

Our engineering team at TechnologyBae specializes in high-throughput API architecture, database profiling, and cloud cost reduction. Request an engineering consultation on our Contact Us page.

Drive Innovation with Our IT Services

Uncover industry insights and expert advice in our blog.Get smarter, faster with the latest trends

Chat with an IT Expert

We'd Love to Work With You!

Please tell us about your project. If you're looking for the general contact form, that's here.