Check Your Indexes Before Adding Redis

On one e-commerce API, a slow order-history query was cached in Redis when a composite index on the PostgreSQL table was enough. How I found it with EXPLAIN.

· 2 min read · Erik Kubica

The Redis Reflex

When an API endpoint takes 400ms to respond, a common first move is to add Redis.

The thought process makes sense: "PostgreSQL is too slow for real-time reads. Let's put the JSON into Redis with a 60-second TTL."

But now the architecture has two sources of truth. You have to handle cache invalidation, cache stampedes, serializing complex objects to strings, and the occasional bug where a user updates their billing email but still sees their old profile for two minutes.

I once looked at an e-commerce API running on a dedicated Linux instance. The team had a separate managed Redis instance costing 35 € a month, and in their code they were caching the results of an order history query.

In that case, the cache was covering for a missing index, and it was worth checking the query plan first.

What Was Happening

I ran EXPLAIN (ANALYZE, BUFFERS) on the query they were caching:

EXPLAIN (ANALYZE, BUFFERS)
SELECT id, total_cents, status, created_at
FROM orders
WHERE tenant_id = 'tenant_984'
ORDER BY created_at DESC
LIMIT 20;

Here was the query planner output:

Seq Scan on orders (cost=0.00..18450.20 rows=20 width=48) (actual time=382.410..385.120 rows=20 loops=1)
  Filter: (tenant_id = 'tenant_984'::text)
  Rows Removed by Filter: 489210
  Buffers: shared hit=4210 read=8240
Planning Time: 0.180 ms
Execution Time: 385.240 ms

The table had 500,000 orders. Because there was no index on (tenant_id, created_at DESC), Postgres had to scan the rows sequentially, discard 489,210 of them, sort the remaining records, and return the top 20.

It took 385ms and caused a lot of disk reads. It looked like Postgres was slow, but it was doing what the query and the schema told it to do.

The Fix: One Composite Index

Instead of keeping Redis and the cache-invalidation code in PHP or Node, I created a single composite B-tree index:

CREATE INDEX CONCURRENTLY idx_orders_tenant_created 
ON orders (tenant_id, created_at DESC);

Then I re-ran the same query:

Limit (cost=0.42..1.85 rows=20 width=48) (actual time=0.082..0.118 rows=20 loops=1)
  -> Index Scan using idx_orders_tenant_created on orders (actual time=0.080..0.112 rows=20 loops=1)
       Index Cond: (tenant_id = 'tenant_984'::text)
  Buffers: shared hit=8
Planning Time: 0.120 ms
Execution Time: 0.135 ms

The execution time dropped from 385ms to about 0.13ms on that table.

Notice Buffers: shared hit=8. Postgres did not need to read from disk, because the index pages were already in shared_buffers in RAM. Postgres has its own in-memory cache built in, so an indexed query like this one is served from memory.

What We Did Next

We removed Redis from that service and deleted around 350 lines of cache serialization and invalidation code. The code got simpler, the stale-data bugs went away, and the client saved 35 € a month.

Redis is still a good fit for other jobs, such as rate-limit counters or Pub/Sub. And some queries are slow even with a good index and do benefit from caching. But if a query is slow, I'd now look at EXPLAIN before reaching for a cache.

Tell me what you need built

Usually a reply within a day, by email or WhatsApp.