> Markdown version of [/videos/100696-lessons-learned-after-6-7t-events-through-postgresql-queues](https://www.wearedevelopers.com/videos/100696-lessons-learned-after-6-7t-events-through-postgresql-queues). Every page supports `.md` or `Accept: text/markdown`. Links point to the HTML versions so they work for humans too. Agent guide: [/agents.md](https://www.wearedevelopers.com/agents.md). --- # Lessons learned after 6.7T events through PostgreSQL queues You might not need Apache Kafka for massive event streams. See how RudderStack processed 6.7 trillion events using a PostgreSQL queue by favoring table drops over deletes. - **Speakers:** [Mihir Gandhi](https://www.wearedevelopers.com/@mihir-gandhi) - **Event:** Conference India 2026 - **Published:** November 25, 2026 - **Duration:** 26:42 - **URL:** https://www.wearedevelopers.com/videos/100696-lessons-learned-after-6-7t-events-through-postgresql-queues - **Thumbnail URL:** https://customer-goifxnzhrq3d0d54.cloudflarestream.com/4b86947a6e4d58cffa713cd66daf3f85/thumbnails/thumbnail.jpg?time=30000ms ## Summary Organizations often default to Apache Kafka for high-throughput event processing, yet managing thousands of independent, ordered, and retryable streams in traditional brokers creates massive topic overhead or head-of-line blocking. Mihir Gandhi shares how RudderStack solved this by building a PostgreSQL-backed queuing system in Go that has processed 6.7 trillion events at 100,000 events per second. To work within database constraints, the architecture favors appends over updates and table drops over deletes. Events and append-only status logs are grouped into 100,000-job datasets. Once processed, entire dataset tables are dropped to avoid dead tuples and vacuuming overhead, while pending jobs from partially completed datasets are compacted into new tables with minimal locking. Scaling this model required continuous query and storage optimizations. Implementing composite indexes for index-only scans and simulating loose index scans via recursive CTEs slashed active-connection polling latency from 150ms to 1ms. The team cut 95% of empty-dataset queries using an application-level cache, used the `COPY` command for bulk inserts, and switched Go byte arrays to strings to prevent ORM hex-encoding network amplification. Finally, sizing disks for 3x write amplification and tuning PostgreSQL settings—lowering `random_page_cost` for SSDs, `autovacuum_naptime`, and `checkpoint_timeout`—ensured fast crash recovery and predictable performance. **Keywords:** postgresql event queues, high-throughput event processing, customer data platform architecture, head-of-line blocking, append-only log tables, postgresql dead tuples, table compaction strategies, index-only scans, loose index scan, recursive CTE optimization, postgresql COPY bulk insert, ORM hex encoding overhead, database write amplification, postgresql autovacuum tuning, random page cost SSD, ordered event delivery, consistent hashing ## Chapters 1. **Processing customer behavioral events in order at scale** (00:02) — Routing behavioral events across hundreds of destinations requires supporting thousands of parallel, independent, and retryable streams that guarantee per-user event ordering. 1. **Why PostgreSQL was chosen over Apache Kafka for queuing** (03:16) — Supporting thousands of isolated streams in Kafka introduces either massive topic overhead or head-of-line blocking, whereas PostgreSQL enables isolated streams and simple CRUD debugging. 1. **Database queue design using append-only tables and datasets** (04:40) — To avoid the performance overhead of dead tuples from PostgreSQL update and delete operations, the system uses append-only status tables and drops entire dataset tables after processing. 1. **System architecture and multi-node Kubernetes deployment strategy** (08:54) — Traffic is segregated into namespaces and distributed across self-contained nodes—each running dedicated gateway, processor, and PostgreSQL containers—using consistent hashing on user IDs. 1. **Optimizing query latency with composite indexes and recursive CTEs** (11:13) — Replacing sequential index scans on duplicate connection entries with composite indexes for index-only scans and recursive CTEs for loose index scans reduced query latency from 150ms to 1ms. 1. **Mitigating retry storms and minimizing lock times during compaction** (13:59) — Downstream outages cause table bloat from repeated failed status inserts, requiring targeted deletions of old statuses and lock-free background table compaction to prevent processing pauses. 1. **Reducing insertion, query, and network amplification overheads** (16:27) — Switching to bulk COPY insertions, adding application-level caching to skip empty datasets, and storing payloads as plain text instead of hex-encoded byte arrays drastically reduced system and network load. 1. **Managing storage amplification and tuning PostgreSQL configuration parameters** (19:07) — Accounting for 3x disk write amplification from WAL, TOAST, and indexes must be paired with tuning autovacuum naptime, random page cost for SSDs, and checkpoint timeouts for fast crash recovery. 1. **Core engineering takeaways for building high-throughput database queues** (22:14) — Scaling PostgreSQL to 100,000 events per second succeeded by questioning conventional broker wisdom, designing around database constraints, continuously measuring query bottlenecks, and iterating in production. 1. **Guaranteeing message ordering and evaluating loose index scan performance** (24:19) — Event ordering is preserved by processing datasets chronologically per user ID, while recursive CTE loose index scans outperform DISTINCT queries specifically because active connections per dataset remain low. ## Related Moments - [Introducing data management and the shift to streaming](https://www.wearedevelopers.com/videos/538-event-messaging-and-streaming-with-apache-pulsar) (from "Event Messaging and Streaming with Apache Pulsar") - **Thumbnail URL:** https://customer-goifxnzhrq3d0d54.cloudflarestream.com/aa4d72c55bc0fa3eda9e7e8295882d73/thumbnails/thumbnail.jpg?time=277700ms - [Recognizing architectural drivers pushing event streaming system adoption](https://www.wearedevelopers.com/videos/538-event-messaging-and-streaming-with-apache-pulsar) (from "Event Messaging and Streaming with Apache Pulsar") - **Thumbnail URL:** https://customer-goifxnzhrq3d0d54.cloudflarestream.com/aa4d72c55bc0fa3eda9e7e8295882d73/thumbnails/thumbnail.jpg?time=277700ms - [Demonstrating event-driven auto-scaling with native Quarkus and Kafka](https://www.wearedevelopers.com/videos/1008-serverless-java-in-action-cloud-agnostic-design-patterns-and-tips) (from "Serverless Java in Action: Cloud Agnostic Design Patterns and Tips") - **Thumbnail URL:** https://customer-goifxnzhrq3d0d54.cloudflarestream.com/d3d39d905de8fc0af0410c8b26e52342/thumbnails/thumbnail.jpg?time=122885ms - [Comparing PostgreSQL and DynamoDB for read-heavy storage](https://www.wearedevelopers.com/videos/746-swapping-low-latency-data-storage-under-high-load) (from "Swapping Low Latency Data Storage Under High Load") - **Thumbnail URL:** https://customer-goifxnzhrq3d0d54.cloudflarestream.com/14898b9f3d06f85b0fc31597d639ce5c/thumbnails/thumbnail.jpg?time=143620ms - [Scaling database infrastructure for massive retail events](https://www.wearedevelopers.com/videos/983-reliable-scalability-how-amazon-com-scales-on-aws) (from "Reliable scalability: How Amazon.com scales on AWS") - **Thumbnail URL:** https://customer-goifxnzhrq3d0d54.cloudflarestream.com/2977b382943ec1ced637655f0595a685/thumbnails/thumbnail.jpg?time=112625ms - [Moving from traditional databases to decoupled event streaming](https://www.wearedevelopers.com/videos/91-from-event-streaming-to-event-sourcing-101) (from "From event streaming to event sourcing 101") - **Thumbnail URL:** https://customer-goifxnzhrq3d0d54.cloudflarestream.com/cc9053b551852fd7f72d36b452e5f8b3/thumbnails/thumbnail.jpg?time=102805ms ## Related Articles - [A Tour of the PostgreSQL Analytics Ecosystem](https://www.wearedevelopers.com/magazine/792-a-tour-of-the-postgresql-analytics-ecosystem) - [Why Event-Driven Architecture Isn’t About Speed (and When You Actually Need It)](https://www.wearedevelopers.com/magazine/745-why-event-driven-architecture-isn-t-about-speed-and-when-you-actually-need-it) - [How We Built a Worry-Free System That Runs for 10+ Years – And What We’d Do Again](https://www.wearedevelopers.com/magazine/751-how-we-built-a-worry-free-system-that-runs-for-10-years-and-what-we-d-do-again) - [Scaling E-commerce Search: 3 Million Products, 1,500 RPS, Under 200ms](https://www.wearedevelopers.com/magazine/803-scaling-e-commerce-search-3-million-products-1-500-rps-under-200ms) ## Related Jobs - [Software Development Engineer 4 - Database Engineer](https://www.wearedevelopers.com/jobs/48591-software-development-engineer-4-database-engineer) at **WEX** - [Staff Systems Engineer, Database Infrastructure](https://www.wearedevelopers.com/jobs/ext/4580910-staff-systems-engineer-database-infrastructure) at **GitHub** - [Tech Lead Java/Spring](https://www.wearedevelopers.com/jobs/ext/4814922-tech-lead-java-spring) at **Neo4J** - [Director of Engineering - Data Platform SDM5](https://www.wearedevelopers.com/jobs/48598-director-of-engineering-data-platform-sdm5) at **WEX** - [Software Development Engineer 5](https://www.wearedevelopers.com/jobs/48599-software-development-engineer-5) at **WEX** - [Senior Data & AI Architect](https://www.wearedevelopers.com/jobs/48602-senior-data-ai-architect) at **reeeliance IM GmbH**