By Bohan Zhang, Member of the Technical Staff
Overview
OpenAI details how they scaled PostgreSQL to support 800 million ChatGPT users, achieving millions of queries per second through a single-primary architecture with nearly 50 read replicas across multiple global regions. The article covers the challenges of MVCC-related write amplification, connection pooling with PgBouncer, workload isolation, caching strategies, and rate limiting that enabled them to maintain low double-digit millisecond p99 latency and five-nines availability.
What You'll Learn
How to scale PostgreSQL for read-heavy workloads using a single-primary architecture with dozens of read replicas
How to use PgBouncer connection pooling to reduce connection setup latency from 50ms to 5ms
Why PostgreSQL's MVCC implementation creates write amplification and how to mitigate it by offloading write-heavy workloads
How to implement cache locking mechanisms to prevent cache-miss storms from overwhelming the database
When to use workload isolation and tiered routing to prevent noisy neighbor problems in shared database infrastructure
Prerequisites & Requirements
- Understanding of PostgreSQL architecture including replication, WAL (Write Ahead Log), and MVCC (multiversion concurrency control)
- Familiarity with database scaling concepts such as sharding, read replicas, and connection pooling
- Experience operating databases at scale in production environments
- Understanding of caching layers and cache invalidation patterns(optional)
Key Questions Answered
How does OpenAI scale PostgreSQL to handle millions of queries per second for ChatGPT?
Why doesn't OpenAI shard their PostgreSQL database?
What problems does PostgreSQL MVCC cause at scale?
How does PgBouncer improve PostgreSQL connection management at scale?
How do you prevent cache miss storms from overwhelming PostgreSQL?
What is cascading replication in PostgreSQL and why is it needed?
How does OpenAI handle the single point of failure risk with one PostgreSQL primary?
What schema management practices does OpenAI follow for PostgreSQL at scale?
Key Statistics & Figures
Technologies & Tools
Some links below are affiliate links. We may earn a commission if you make a purchase.
Key Actionable Insights
1Implement cache locking mechanisms to prevent thundering herd problems during cache miss storms. When multiple requests miss the same cache key, only one should fetch from the database while others wait for the cache to be populated. This prevents cascading database overload from sudden cache failures.OpenAI experienced SEVs caused by widespread cache misses from caching-layer failures that saturated PostgreSQL CPU and triggered retry storms.
2Deploy PgBouncer in statement or transaction pooling mode as a proxy layer between your application and PostgreSQL to dramatically reduce connection overhead and prevent connection exhaustion. Co-locate the proxy, clients, and replicas in the same region to minimize network overhead.OpenAI reduced average connection time from 50ms to 5ms using PgBouncer. Azure PostgreSQL has a maximum connection limit of 5,000, and they previously experienced incidents from connection storms.
3Isolate database workloads by priority tier onto dedicated instances to prevent the noisy neighbor problem. Split requests into low-priority and high-priority tiers routed to separate database instances so resource-intensive workloads don't degrade critical requests.New feature launches at OpenAI introduced inefficient queries that consumed disproportionate PostgreSQL CPU, slowing down other critical features running on the same instances.
4Carefully review SQL generated by ORM frameworks rather than trusting their output. Complex multi-table joins generated by ORMs can become extremely costly at scale—consider breaking them down and moving complex join logic to the application layer instead.OpenAI identified an extremely costly ORM-generated query joining 12 tables whose volume spikes were responsible for past high-severity SEVs.
5Implement multi-layer rate limiting across application, connection pooler, proxy, and query layers to prevent traffic spikes from cascading into database failures. Enhance the ORM layer to support rate limiting and the ability to fully block specific query digests for targeted load shedding.Sudden traffic spikes and retry storms with overly short retry intervals can exhaust CPU, I/O, and connections, causing widespread service degradation.
6Enforce strict constraints on schema changes in production—only allow lightweight operations that don't trigger full table rewrites, set aggressive timeouts (e.g., 5 seconds), and rate-limit backfills even if they take weeks. Avoid adding new tables to existing high-traffic PostgreSQL deployments.At OpenAI's scale, even a small schema change like altering a column type can trigger a full table rewrite, causing production instability.