Scaling PostgreSQL for High-Traffic: Read Replicas, Sharding, and Connection Pooling Strategies
How to Scale PostgreSQL for High-Traffic Applications: Read Replicas and Sharding
PostgreSQL scales vertically until hardware limits are reached. For high-traffic applications, horizontal scaling through read replicas and sharding distributes load across multiple servers. This guide covers implementation strategies for both approaches.
Read Replicas
Read replicas offload SELECT queries from the primary database using streaming replication. The primary ships Write-Ahead Log (WAL) records to replicas, maintaining near real-time synchronization.
Streaming Replication Setup
Configure the primary server in postgresql.conf:
wal_level = replica
max_wal_senders = 10
wal_keep_size = 1GB
max_replication_slots = 10
Create a replication user and replication slot:
CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'secure_password';
SELECT pg_create_physical_replication_slot('replica1_slot');
Add to pg_hba.conf on the primary:
host replication replicator 192.168.1.0/24 scram-sha-256
Initialize the replica using pg_basebackup:
pg_basebackup -h primary_host -D /var/lib/postgresql/data -U replicator -P -v -R -S replica1_slot
The replica's standby.signal file and primary_conninfo in postgresql.conf maintain the connection. Replication slots prevent WAL retention issues during extended replica disconnections.
Replication Lag Considerations
Replicas may lag behind the primary due to network latency or heavy write workloads. Monitor lag using:
SELECT
pg_wal_lsn_diff(pg_current_wal_lsn(), pg_last_wal_receive_lsn()) AS lag_bytes,
pg_wal_lsn_diff(pg_current_wal_lsn(), pg_last_wal_replay_lsn()) AS replay_lag_bytes;
For applications requiring strong consistency, route writes and critical reads to the primary.
Sharding
Sharding distributes data across multiple physical servers based on a shard key. Each shard handles a subset of data, enabling horizontal scaling beyond single-server limits.
Native Partitioning
PostgreSQL declarative partitioning splits a table into smaller physical pieces on a single node:
CREATE TABLE events (
id SERIAL,
event_time TIMESTAMP,
user_id INTEGER,
data JSONB
) PARTITION BY RANGE (event_time);
CREATE TABLE events_2025_q1 PARTITION OF events
FOR VALUES FROM ('2025-01-01') TO ('2025-04-01');
CREATE TABLE events_2025_q2 PARTITION OF events
FOR VALUES FROM ('2025-04-01') TO ('2025-07-01');
Partition pruning optimizes queries by scanning only relevant partitions. Native partitioning operates on a single node and does not provide horizontal scaling across multiple servers.
Distributed Sharding with Citus
Citus extends PostgreSQL with distributed query execution across multiple nodes:
-- Create distributed table
SELECT create_distributed_table('users', 'user_id');
-- Create reference table (replicated to all nodes)
SELECT create_reference_table('countries');
-- Query across shards transparently
SELECT u.name, c.name
FROM users u
JOIN countries c ON u.country_id = c.id
WHERE u.user_id = 12345;
Citus colocates related data on the same shard to minimize cross-node joins.
Shard Key Selection
Choose shard keys that:
- Distribute data evenly across nodes
- Minimize cross-shard queries
- Align with query patterns (e.g.,
user_idfor user-scoped data)
Avoid monotonically increasing keys like timestamps, which cause hotspots. Use UUIDs or hash-based distribution for even data spread.
Connection Pooling
High-traffic applications require connection pooling to prevent connection exhaustion. PostgreSQL's process-based architecture creates a new process per connection, consuming significant memory.
PgBouncer Configuration
Install and configure PgBouncer in pgbouncer.ini:
[databases]
postgres = host=primary_host port=5432
[pgbouncer]
pool_mode = transaction
max_client_conn = 10000
default_pool_size = 100
reserve_pool_size = 10
reserve_pool_timeout = 3
Transaction pooling mode returns connections to the pool after each transaction, maximizing concurrency. Transaction pooling breaks prepared statements, advisory locks, and LISTEN/NOTIFY. Use session pooling mode if your application relies on these features.
PgCat for Sharded Environments
PgCat provides automatic sharding and load balancing:
[pgcat]
pool_mode = transaction
max_pool_size = 100
[[databases]]
name = "sharded_db"
users = ["app_user"]
[[databases.shards]]
name = "shard_1"
hosts = ["shard1.example.com:5432"]
[[databases.shards]]
name = "shard_2"
hosts = ["shard2.example.com:5432"]
Load Balancing
Distribute read traffic across replicas using HAProxy:
frontend postgres_read
bind *:5433
mode tcp
default_backend postgres_read_backends
backend postgres_read_backends
mode tcp
balance roundrobin
option tcp-check
server replica1 192.168.1.11:5432 check
server replica2 192.168.1.12:5432 check
Route writes to the primary, reads to replicas. Use health checks to automatically remove failed nodes.
Backup Strategy
Replicated and sharded environments require coordinated backup strategies. Use tools like pgBackRest, Barman, or WAL-G for physical backups with point-in-time recovery. Back up each shard independently and ensure replication slots are included in backup procedures. Test restore procedures regularly.
Getting Started
- Baseline optimization: Set
shared_buffersto 25% of system RAM,work_memto 4-64MB per connection based on query complexity, andmaintenance_work_memto 512MB-2GB before scaling - Add read replicas: Start with 2-3 replicas for read-heavy workloads
- Implement connection pooling: Deploy PgBouncer or PgCat before traffic spikes
- Monitor replication lag: Set alerts for lag exceeding 1 second
- Plan sharding early: Design shard keys based on data access patterns
- Gradual migration: Move to sharding when single-node performance plateaus despite vertical scaling
MatterAI builds frontier AI infrastructure for engineering teams — from inference-optimized models to autonomous coding agents and agentic code reviews.
Explore what we're building:
- Orbital IDE — Autonomous AI coding agent with background agents and deep codebase memory
- AI Code Reviews — Agentic pre-commit reviews across GitHub, GitLab, and Bitbucket
- Axon Models — Frontier-grade reasoning models at 70% lower inference cost
Share this Guide:
More Guides
Local LLMs in Your IDE: Connecting Ollama to Coding Agents and Autocomplete
Wire local models into VS Code, JetBrains, Cline, Continue, and Aider via the OpenAI-compatible API. Covers model routing, context budgets, tool calling with small models, and when a local model is the right choice for the job.
15 min readBuilding a Self-Hosted AI Stack: Ollama, Open WebUI, and Local RAG
Stand up a fully self-hosted AI stack on a single machine: Ollama for inference, Open WebUI as the chat interface, local embeddings for RAG, and a reverse proxy for secure access. No cloud dependency, no data leaving your network.
17 min readTop 5 Open-Source Coding Models to Run on Your Mac (2026)
The best local coding models for Apple Silicon in 2026, ranked by quality per gigabyte of unified memory. Covers qwen3-coder, devstral, gpt-oss, and more with real pull tags, sizes, and context windows.
14 min readRunning LLMs Locally: GGUF, Quantization, and Memory Planning
Learn the GGUF format, the quantization ladder from Q2 to FP16, and the exact memory math for running models on Apple Silicon and NVIDIA GPUs. Includes Ollama and llama.cpp tuning for KV cache and context.
15 min readOllama vs vLLM vs llama.cpp: Choosing the Right Local LLM Runtime
Compare the three dominant local LLM runtimes on architecture, throughput, hardware, and deployment context. Includes benchmark data, a decision framework, and a migration path from Ollama to vLLM.
16 min readContinue Reading
Local LLMs in Your IDE: Connecting Ollama to Coding Agents and Autocomplete
Wire local models into VS Code, JetBrains, Cline, Continue, and Aider via the OpenAI-compatible API. Covers model routing, context budgets, tool calling with small models, and when a local model is the right choice for the job.
15 min readBuilding a Self-Hosted AI Stack: Ollama, Open WebUI, and Local RAG
Stand up a fully self-hosted AI stack on a single machine: Ollama for inference, Open WebUI as the chat interface, local embeddings for RAG, and a reverse proxy for secure access. No cloud dependency, no data leaving your network.
17 min readTop 5 Open-Source Coding Models to Run on Your Mac (2026)
The best local coding models for Apple Silicon in 2026, ranked by quality per gigabyte of unified memory. Covers qwen3-coder, devstral, gpt-oss, and more with real pull tags, sizes, and context windows.
14 min readShip Faster. Ship Safer.
Join thousands of engineering teams using MatterAI to autonomously build, review, and deploy code with enterprise-grade precision.
