Skip to content

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

Neon 8 min read How-to mediumAdept

Using Python & Django with Neon’s Serverless Postgres

Step-by-step setup of a Django app backed by Neon's serverless Postgres: pyenv and venv for the Python environment, psycopg and python-dotenv for the connection with sslmode=require, then models, migrations, views and templates, with Gunicorn for production. An Apache Bench test with 9 Gunicorn workers reached only 50 requests per second at 340ms average latency. Setting Django's CONN_MAX_AGE to enable persistent database connections raised throughput 8x, to 407 requests per second at 41ms average and 57ms p99.

Assumes: Python basicsDjango basics

Related posts

  • 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.

  • Edge-compatible Serverless Driver for Postgres

    Neon mediumAdept

    Neon's @neondatabase/serverless driver lets V8-isolate platforms like Cloudflare Workers, which lack TCP, speak the real Postgres wire protocol by tunnelling it over a WebSocket to an open-source Go proxy that relays to TCP. The driver is node-postgres with shims for Node APIs, and it does end-to-end TLS 1.3 via WolfSSL compiled to WebAssembly with Asyncify, offloading crypto to SubtleCrypto. Because SCRAM-SHA-256's 4,096 hash rounds exceed Workers' 10–50ms CPU limits, it falls back to cleartext auth over mandatory TLS. The post also covers latency tradeoffs from the extra proxy hop, round-trip reductions, and how to self-host the proxy for any Postgres.

  • Simplify Serverless in AWS: How to Use Neon and Ephemeral Environments

    Neon mediumAdept

    Neon's serverless Postgres pairs well with ephemeral environments on AWS Lambda because each environment can get its own instant copy-on-write database branch without paying for extra RDS uptime. Compared with Aurora Serverless v2, Neon claims ~500ms cold starts versus 15s, built-in PgBouncer supporting 10,000 connections, and an HTTP/WebSocket data API. A TODO API built with the Serverless Framework shows how the serverless driver's sql template tag guards against SQL injection, but only when called directly on a string literal, and how sql.query with $n placeholders handles dynamic updates. Scripts and GitHub Actions create a branch per feature or CI run, run unit and end-to-end tests against it, then tear down both the stack and the branch.

  • Performance tips for Neon Postgres

    Neon mediumAdept

    Neon's serverless Postgres follows mostly standard Postgres tuning, plus a few Neon-specific checks. Size compute at roughly 100 connections per GB of RAM, use pooling and autoscaling, then find slow queries with pg_stat_statements and EXPLAIN (ANALYZE, BUFFERS). Fix them with indexes, REINDEX and autovacuum tuning against bloat, and by keeping the local file cache hit ratio (shown in neon_stat_file_cache) above 99%. For faster ingests, batch inserts, drop unused indexes, offload reads to ephemeral read replicas, and test changes safely on a database branch.

The quest for software engineering knowledge.

Look:
Layout: