HeadlinesBriefing favicon HeadlinesBriefing.com

Postgres LISTEN/NOTIFY Scales with Optimization

Hacker News •
×

Postgres LISTEN/NOTIFY, often criticized for not scaling, can be optimized for high-throughput, low-latency notifications. The perceived bottleneck stems from a global lock Postgres uses during NOTIFY to ensure transaction commit order.

This global lock serializes commits of transactions involving NOTIFY, preventing optimizations like group commit and limiting throughput to around 2.9K writes per second. To overcome this, notifications can be buffered in memory and flushed in batches. This approach reduces contention on the global lock, allowing individual writes to proceed rapidly and leverage database optimizations.

A fallback polling mechanism is implemented to ensure notifications are not lost in case of a buffer flush failure. Benchmarking this optimized solution demonstrates a significant improvement, achieving up to 60K writes per second with millisecond-scale latency, effectively utilizing the database's resources.