Skip to content

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

Neon 12 min read Deep dive mediumAdept

Dynamically estimating and scaling Postgres’ working set size

Neon autoscales Postgres compute to keep the workload's working set in memory, using a modified HyperLogLog whose register bits are replaced with timestamps so the number of distinct pages accessed can be estimated for any lookback window. A heuristic scans windows from a 5-minute offset up to one hour and treats the point where the estimate jumps sharply as the end of the current workload's plateau. Scaling then projects working set growth to avoid repeated cache evictions. In benchmarks this matched fixed-size compute performance at 30% lower cost, roughly tripled throughput on a bursty pgbench workload, and raised cache hit rate from 30-70% to 100% on a batch workload.

Assumes: Postgres buffer cache basicsHyperLogLog cardinality estimationautoscaling concepts

Related posts

  • Autoscaling Lakebase Postgres

    Neon hardMaster

    Neon's Lakebase Postgres resizes compute in place about once every 81 seconds by taking the maximum of three target sizes: CPU load (one-minute average kept under 90%), memory (polled every 100 ms by an in-VM monitor and kept under 75%), and the working set of the compute cache. The working set comes from a modified HyperLogLog that stores timestamps instead of bits, so it can estimate distinct pages for any window from 1 to 60 minutes. The algorithm then looks for a jump in that curve to find where the current workload ends, and projects growth forward to the next 20-second interval. Resizes are applied to running QEMU/KVM VMs via NeonVM, with a modified Kubernetes scheduler approving upscales, live migration when a node is full, and in-VM checks before any downscale.

  • 1 Year of Autoscaling Postgres: How it’s going, and what’s next

    Neon hardMaster

    Neon autoscales Postgres by running each instance as a VM in Kubernetes (NeonVM), with a per-node autoscaler-agent, a modified scheduler, and an in-VM vm-monitor that resizes the Local File Cache. Over the past year the team replaced cgroup memory.high event triggers with 100ms polling of memory usage for stability, and traced a 0-TPS stall to a kernel bug in ACPI memory hotplug that was fixed by upgrading the kernel. Next steps include sizing compute with a HyperLogLog estimate of working set from LFC page accesses, since a slightly larger cache can yield 10x gains. They are also adding swap, set up via mkswap/swapon on pre-created VMs, and disabling overcommit so up-front allocations like pgvector 0.6's parallel HNSW builds don't OOM.

  • Monitor These Key Neon Metrics Via Datadog

    Neon mediumAdept

    Neon now exports database metrics to Datadog via an API-key integration covering all computes in a project. The metrics include connection counts, replication lag, CPU and memory, Local File Cache hits and misses, an estimated working set size, and pg_stat_database aggregates. A low LFC hit ratio or high miss count usually means the working set exceeds cache memory and reads are falling through to pageserver getpage requests, so the fix is a larger compute or better queries and indexes. Watching working set size trends, deadlocks and row operations helps with autoscaling limits and spotting inefficient queries, and an importable JSON dashboard is provided.

  • Autoscaling in Action: Postgres Load Testing with pgbench

    Neon mediumAdept

    A pgbench load test against Neon serverless Postgres runs a CPU-heavy query, SELECT log(factorial(32000)) / log(factorial(20000)), to trigger autoscaling. On ¼ vCPU the query spends about 1.9 s in planning, because constant folding happens there, so a modest number of concurrent clients is enough to make the compute scale within a ¼–7 CU range. The walkthrough covers the pgbench flags (-f, -c, -T, -P), reading EXPLAIN ANALYZE output and interpreting the results: roughly 6 s average latency, about 5 TPS and a high latency standard deviation.

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