Skip to content
← Back to blog

November 9, 2026 · 3 min read

A 4× faster API by counting database round trips: from 750 to 190 ms with FastAPI and Supabase

The API of a 3D store I built for university took almost a second to return a short list. It wasn't the query: it was four round trips to Virginia. Plus an atomic sale in a single SQL statement.

In this post
  1. The database is far away
  2. One trip instead of four
  3. The atomic sale
  4. Least privilege
  5. What I learned

Aeternum 3D is a university project: a second-hand sneaker store you walk through in 3D, with a sales dashboard and an inventory API. The API is small: FastAPI, SQLAlchemy and PostgreSQL on Supabase, with three tables. Listing the categories should be instant, and yet every GET took about 750 ms on the server, over a second in the browser.

The query wasn’t the problem. The distance was.

The database is far away

The Supabase project ended up in us-east-1 (Virginia), and the API runs on my server in Bolivia. Every round trip between the two costs something, and what matters is how many trips each request makes.

Counting them, a simple read made four:

  1. the pool’s ping, which checks the connection is still alive (pool_pre_ping);
  2. BEGIN, because SQLAlchemy opens a transaction by default;
  3. the query;
  4. ROLLBACK, when the connection goes back to the pool.

Four trips to Virginia to read eight rows.

One trip instead of four

The changes:

  • AUTOCOMMIT for the API’s connection: a read doesn’t need BEGIN or ROLLBACK. Each statement commits on its own.
  • No ping on every checkout: instead, pool_recycle renews connections before the pooler drops them.
  • The dashboard summary in one query instead of several.
  • The sale in a single statement, described below.

Result: from ~750 to ~190 ms on the server, and from ~1.1 s to ~400 ms in the browser. Moving the Supabase project to São Paulo would cut it further; I left that as optional.

The atomic sale

Selling a pair has to do two things: decrement the stock and record the sale. Done separately, two people buying the last pair at the same time can leave the stock at -1.

The naive version would read the stock, check it in Python and then write. Besides the race condition, that’s more trips. I did it in a single statement with a CTE:

with p as (
    update productos set stock = stock - :c
    where id = :id and stock >= :c
    returning id, nombre, precio, stock
), v as (
    insert into ventas (producto_id, cantidad, precio_unitario, canal)
    select id, :c, precio, :canal from p
    returning *
)
select v.*, p.nombre as producto, p.stock as stock_restante
from v join p on p.id = v.producto_id

The UPDATE only touches the row if there’s enough stock, and PostgreSQL locks that row meanwhile. If there isn’t enough, it returns nothing, the INSERT inserts nothing and the API answers 409. It’s one trip, it’s atomic, and it returns the remaining stock along the way. Just in case, the table also has a check (stock >= 0).

I tested it with 10 simultaneous sales on a product with 3 pairs: 3 went through (201), the other 7 got 409 and the stock ended at 0. Each sale’s total is computed by PostgreSQL in a generated column (quantity × unit_price), so nobody can send a made-up total.

Least privilege

The API doesn’t connect as the postgres user. It has its own role, aeternum_api, which can read and write what it needs but can’t edit or delete sales. If someone finds a flaw in the API, the worst they can do is bounded by the database, not by my code.

A network detail: my server has no IPv6, and Supabase’s direct database address only answers over IPv6. The connection goes through the session pooler, which does have IPv4.

What I learned

  • With the database far away, latency is counted in round trips, not queries. Before optimizing SQL, count the trips.
  • An ORM’s defaults (implicit transaction, pool ping) are sensible locally and expensive when the database is on another continent.
  • Important rules belong in the database: stock that can’t go below zero, the computed total and the role’s permissions hold even if the code has a bug.

Rossmel Abasto — Frontend → Fullstack Developer. LinkedIn · GitHub

Leer este artículo en español →See the case studyAeternum 3D →A 3D store you walk through, a sales dashboard and an inventory API for a second-hand sneaker shop. Fictional data.

Keep reading

Write to me: I read everything and reply by email.

Questions or comments about this post?

send-mail.sh