Question: How can you scale writes in PostgreSQL?


Scaling writes in PostgreSQL requires a careful approach because write operations (INSERT, UPDATE, DELETE) inherently demand consistency and durability, making them more complex to scale compared to read operations. Here are several strategies to consider:

1. Partitioning

Partitioning is dividing a large table into smaller, more manageable pieces called partitions. It can significantly improve performance for write-heavy workloads by reducing index size, improving cache hit rates, and enabling more efficient vacuuming.


CREATE TABLE measurements ( city_id int not null, logdate date not null, peaktemp int, unitsales int ) PARTITION BY RANGE (logdate); CREATE TABLE measurements_y2023 PARTITION OF measurements FOR VALUES FROM ('2023-01-01') TO ('2024-01-01');

2. Sharding

Sharding involves distributing data across multiple PostgreSQL instances (shards), each holding a subset of your data. This allows you to parallelize writes across multiple machines, significantly increasing write throughput. However, PostgreSQL does not natively support automatic sharding, so this would require additional tooling or custom implementation.

3. Replication and Write-Ahead Log (WAL) Shipping

Using streaming replication to create read replicas can offload read queries from the primary server, indirectly improving write performance on the primary by reducing its load. For writes, WAL shipping to standby servers ensures data consistency and high availability but doesn't directly scale write operations.

4. Connection Pooling

Connection pooling can help manage database connections more efficiently, reducing overhead and increasing the throughput of write operations. By reusing existing connections, the application can execute more writes faster.

Example using PgBouncer:

Configure PgBouncer as a lightweight connection pooler in front of PostgreSQL to manage and reuse connections.

5. Use of Faster Storage

Improving the hardware, especially using SSDs or NVMe storage, can significantly enhance write performance due to lower latency and higher throughput compared to traditional HDDs.

6. Tuning PostgreSQL Configuration

Adjusting PostgreSQL's configuration settings can also help optimize write performance. Important parameters include shared_buffers, wal_buffers, checkpoint_completion_target, and max_wal_size.


# postgresql.conf shared_buffers = 4GB wal_buffers = 16MB checkpoint_completion_target = 0.9 max_wal_size = 2GB


Scaling writes in PostgreSQL is a multifaceted challenge that often requires combining several strategies based on the specific needs of your application. Effective partitioning, considering sharding, optimizing connection management, investing in faster hardware, and fine-tuning PostgreSQL configurations are all critical steps towards achieving better write scalability.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.