Backend Performance··4 min read

Reducing P95 API Latency from 2.5s to 180ms with Redis & PostgreSQL Tuning

Shafikul Islam

Shafikul Islam

Backend & Cloud Infrastructure Engineer · Infrastructure & Cloud Team Lead

Reducing P95 API Latency from 2.5s to 180ms with Redis & PostgreSQL Tuning

Executive Summary & Background

API response times exceeding 2,000ms directly cause user churn, abandoned checkout carts, and customer dissatisfaction.

During an emergency backend performance audit for a high-traffic SaaS client, P95 API response times were reaching 2,500ms during peak business hours. Database CPU utilization on the primary PostgreSQL instance spiked above 88%, resulting in database connection timeouts and failed HTTP requests.

This case study breaks down how we diagnosed full table scans on a 15-million row table, fixed N+1 ORM query loops, implemented composite B-Tree indexing, deployed a Redis Cache-Aside layer, and configured PgBouncer connection pooling — dropping P95 API latency by 92.8% to 180ms.


Performance Benchmark Comparison

Benchmark MetricInitial Unoptimized BackendPost-Optimization BackendPerformance Improvement
P95 API Latency2,500ms (2.5 seconds)180ms92.8% Latency Drop
Primary DB CPU Usage88% (Near Collapse)18% (Idle Capacity)70% CPU Load Reduction
Max Throughput (RPS)120 Requests / sec1,450 Requests / sec12x Capacity Increase
Database Connection Failures45 errors / hour0 errors / hour100% Uptime & Stability

Root Cause Diagnostics

Using APM distributed tracing (OpenTelemetry) and PostgreSQL slow query logging (pg_stat_statements), we identified three severe bottlenecks:

  1. Missing Composite Index on 15M Rows: The main dashboard query performed a full sequential scan across 15,000,000 records to filter by tenant_id and status while sorting by created_at. Each query took 1,250ms.
  2. N+1 ORM Query Loop: The user list endpoint fetched 50 items in Query 1, then executed 50 individual SQL queries inside a loop to fetch nested user permissions.
  3. Database Connection Overhead: Backend microservices spawned new TCP database connections on every incoming HTTP request, consuming 35% of PostgreSQL server RAM on idle process overhead.

Step-by-Step Engineering Remediation

Step 1: Composite B-Tree Indexing

We analyzed EXPLAIN ANALYZE execution plans and created targeted composite B-Tree indexes using non-blocking concurrent index creation:

-- 1. Identify slow queries in pg_stat_statements
SELECT query, calls, total_exec_time / calls AS avg_time_ms, rows
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 5;

-- 2. Create non-blocking composite index on tenant_id + status + created_at
CREATE INDEX CONCURRENTLY idx_orders_tenant_status_created
ON orders (tenant_id, status, created_at DESC);

-- 3. Verify index usage with EXPLAIN (ANALYZE, BUFFERS)
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, total_amount, created_at
FROM orders
WHERE tenant_id = 'tenant_9821' AND status = 'COMPLETED'
ORDER BY created_at DESC
LIMIT 50;
Limit  (cost=0.56..68.42 rows=50 width=24) (actual time=0.041..3.129 rows=50 loops=1)
  Buffers: shared hit=54
  ->  Index Scan using idx_orders_tenant_status_created on orders  (actual time=0.039..3.112 rows=50 loops=1)
        Index Cond: ((tenant_id = 'tenant_9821'::text) AND (status = 'COMPLETED'::text))
        Buffers: shared hit=54
Planning Time: 0.142 ms
Execution Time: 3.184 ms

Step 2: PgBouncer Transaction Pooling

[pgbouncer]
pool_mode = transaction
default_pool_size = 25
reserve_pool_size = 5
max_client_conn = 1000
server_idle_timeout = 60

Switching backend workers to PgBouncer transaction pooling cut active PostgreSQL backend processes from 400+ down to a fixed 25-connection server pool, reclaiming ~35% RAM previously lost to connection allocation.

Step 3: Redis Cache-Aside with Explicit Write Invalidation

const cacheKey = `orders:${tenantId}:completed:p1`;
const cached = await redis.get(cacheKey);
if (cached) return JSON.parse(cached);

const rows = await db.query(HOT_ORDERS_SQL, [tenantId]);
// 60s +/- 10s jitter prevents synchronized expiration spikes (cache stampede)
const ttlSeconds = 60 + Math.floor(Math.random() * 10);
await redis.set(cacheKey, JSON.stringify(rows), 'EX', ttlSeconds);
// On order state transition in write path: await redis.del(cacheKey);

Final Technical Outcomes

  • P95 Latency: Reduced from 2,500ms to 180ms (92.8% decrease).
  • Database CPU Load: Dropped from 88% to 18%, providing ample headroom for business growth.
  • Cost Savings: Avoided a $4,000/mo database hardware upgrade by optimizing existing open-source software layer configuration.

Infrastructure & PostgreSQL Advisory

Scaling Infrastructure or Diagnosing P95 Latency?

I help engineering teams diagnose PostgreSQL indexing bottlenecks, concurrency limits, and K3s/Docker deployments. Book a 20-minute call with me to review your setup.

Related Technical Articles