Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Neon 9 min read How-to mediumAdept

How to Minimise the Impact of Database Latency

Database round-trip latency compounds with every sequential query, so co-locating the backend with the database and cutting round trips matters more than raw query speed. Neon's open-source Regional Latency Dashboard gives P50/P99 latencies between hosting providers and Neon regions, which can be multiplied by query count to estimate N+1 costs. The fixes are JOINs or bounded IN queries instead of N+1 loops, Django's select_related/prefetch_related, batching queries in one transaction (45% faster in a simple benchmark), Postgres RETURNING and ON CONFLICT clauses, connection reuse (8-9x lower response times in a Django sample), and application-level connection pools (10x+ throughput).

Assumes: SQL joinsPostgres basicsORM usage

Related posts

  • Sub-10ms Postgres queries for Vercel Edge Functions

    Neon mediumAdept

    Neon's serverless Postgres driver now supports SQL-over-HTTP via fetch, and Neon's proxy adds opt-in experimental connection caching (`fetchConnectionCache`). Together they cut query latency from Vercel Serverless and Edge Functions by about 40%, bringing same-region one-shot queries to single-digit milliseconds. Use the `neon` HTTP function for a single query per invocation and `Pool` when a function runs multiple queries. Drizzle-ORM also supports the driver for type-safe queries.

  • How to Handle Database Connection Pooling

    Railway mediumAdept

    Connection pooling keeps a set of reusable database connections open, so requests check one out and return it instead of opening and closing a connection per query. A walkthrough builds a Node.js Express app with node-postgres, comparing per-query Client connections against a Pool (max 20, connection and idle timeouts) over 100 concurrent queries. Query latency is measured with prom-client histograms. In this local setup the pooled queries finished roughly 3x faster in total, and more individual queries completed within the low-millisecond buckets.

  • HTTP vs. WebSockets: Which protocol for your Postgres queries at the Edge

    Neon mediumAdept

    Neon's serverless Postgres driver now supports SQL-over-HTTP alongside WebSockets, and benchmarks from Vercel Edge and Serverless functions (iad1 to us-east-1) show each wins in different cases. WebSockets, cut from nine to four round-trips, give ~5ms latency for queries after the first but ~37ms for a single-shot query including connection setup. HTTP is faster for one-shot queries but lacks sessions, interactive transactions, NOTIFY and COPY. An experimental proxy-side connection cache (fetchConnectionCache) saves almost 10ms by finding the compute node faster. Choose HTTP for single queries and WebSockets for multiple queries per invocation, and note the runtime matters as much as the protocol.

  • Using Neon’s Scale to Zero with Long-Running Applications

    Neon mediumAdept

    Neon's scale to zero suspends a Postgres compute after an idle window (only when no WAL senders are active and autovacuum isn't running), which severs connections held by long-running servers. A naive node-postgres Client crashes on the resulting unhandled error event. A client-side pg.Pool with an error handler tolerates the dropped connections and reconnects on demand, waking the database. In Apache Bench tests the pool beat per-request connections by more than 10x (about 46ms vs 678ms average latency, 370 vs 26 req/s), with the same pattern shown in a Fastify, Drizzle and HTMX example app.

  • Quicker serverless Postgres connections

    Neon mediumAdept

    Neon cut the network round-trips needed to get a first query result from its WebSocket-based serverless Postgres driver from nine to four. It moved to TLS 1.3, ran Postgres unencrypted inside a secure wss: WebSocket (or pipelined SSLRequest with the TLS Client Hello), and replaced CPU-heavy SCRAM-SHA-256 with plain password auth over TLS, since Neon only issues long random passwords. It also pipelined the startup, password and query messages into a single send. Pipelining at first added up to 50ms of delay over short distances because of Nagle's algorithm, fixed by setting TCP_NODELAY in the proxy. QUIC-based WebSockets or WebTransport might cut the count further.

The quest for software engineering knowledge.

Look:
Layout: