The Real Fullstack

The Million-Row Problem

The page was instant at launch. A year and a million orders later it takes 150 ms per call. Why?

Engenheiro9 minLaboratório de performanceDesafio de código
postgresqlsqlredisspring-data-jpa

Este desafio ainda não foi traduzido para Português (Brasil). Você está lendo a versão em inglês.

Missão:Read the (simulated) execution plan and choose indexes so that even an uncached request meets a 20 ms budget, without slowing down writes with indexes nobody uses.Contexto · Objetivos de aprendizagem ▾

Fernway Outfitters, a fictional online shop, shows each customer their 20 latest shipped orders. The query has not changed since launch, but the orders table has grown to a million rows and the p95 latency of the endpoint has followed it. A teammate suggests putting Redis in front of it.

Missão: Read the (simulated) execution plan and choose indexes so that even an uncached request meets a 20 ms budget, without slowing down writes with indexes nobody uses.

Objetivos de aprendizagem

  • Read a query plan: sequential scan, index scan, sort, rows examined.
  • Order composite index columns: equality columns first, then the sort column.
  • Distinguish a cache (mitigation for repeated reads) from fixing the access path.
SimulaçãoSimulado
Account pageOrders APIRedisPostgreSQL
Executar simulação
SELECT id, total, status, created_at  FROM orders WHERE customer_id = $1 AND status = 'SHIPPED' ORDER BY created_at DESC LIMIT 20;
CREATE INDEX idx_status   ON orders (status);CREATE INDEX idx_created  ON orders (created_at);CREATE INDEX idx_customer ON orders (customer_id);-- equality columns first, then the ORDER BY column:CREATE INDEX idx_customer_status_created ON orders (customer_id, status, created_at);

Simplificação

A tiny cost model with uniform data replaces PostgreSQL's real statistics. Real planners consider correlation, caching of pages, parallel workers and many more paths. Modelo educacional — os números ilustram comportamento, não são benchmarks.

Conceitos

Desafios relacionados

Referências