Prompt
How do I process millions of events per second without dropping messages?
Latest observation
To process millions of events per second reliably, you need to optimize the whole pipeline: ingest, buffer, process, and persist. The main goal is to avoid unbounded queues, blocking calls, and single-thread bottlenecks.
Core principles
1) Use a durable buffer in front of processing
Don’t send events directly to workers. Put them into a high-throughput log or queue:
- Kafka
- Pulsar
- Redpanda
- NATS JetStream
- RabbitMQ (usually lower throughput than log-based systems at extreme scale)
This gives you:
- backpressure handling
- replay after failures
- decoupling of producers and consumers
2) Partition for parallelism
Split events across many partitions/shards by a stable key:
- user ID
- device ID
- account ID
- session ID
Then scale consumers horizontally:
- one partition can be consumed by one consumer in a group
- more partitions = more parallelism
- avoid hot partitions by choosing a good key
3) Keep the hot path minimal
In the critical ingestion path:
- avoid heavy validation
- avoid synchronous remote calls
- avoid serialization overhead when possible
- avoid per-event allocations
- avoid logging each event on the hot path
Do only:
- receive event
- validate minimally
- enqueue/append to durable buffer
- acknowledge
4) Batch everything
Batching is essential:
- batch network writes
- batch disk writes
- batch acknowledgements
- batch downstream DB inserts
- batch metric/log emission
This reduces syscall and network overhead dramatically.
5) Use async/non-blocking I/O
Use event-driven or async I/O rather than one thread per connection/event. Examples:
- epoll/kqueue/io_uring
- async runtimes
- reactive streams
6) Make processing idempotent
Assume duplicates can happen. To avoid data loss while allowing retries:
- include unique event IDs
- deduplicate downstream if needed
- design consumers to be idempotent
This lets you safely retry on failure.
7) Separate ingestion from heavy processing
Split the system into stages:
- ingest
- normalize
- enrich
- aggregate
- store
Each stage can scale independently and use its own queue.
8) Control backpressure
When downstream slows down:
- slow producers
- shed low-priority load
- spill to disk
- scale consumers
- alert before buffers fill
Never let memory grow without limit.
9) Persist checkpoints/offsets correctly
For stream processors:
- commit offsets only after processing is durable
- use transactional or exactly-once features where available
- otherwise use at-least-once + idempotency
10) Monitor saturation points
Watch:
- queue lag
- consumer lag
- p95/p99 latency
- dropped events
- retry rate
- disk usage
- GC pause time
- CPU steal / network saturation
- partition skew
You usually fail first due to one saturated component, not the whole system.
A practical architecture
Producers → Kafka/Pulsar → partitioned consumers → batch processor → DB/data lake/cache
Example:
- Producers publish events to Kafka with key =
user_id - Kafka partitions the stream across brokers
- Consumer group scales horizontally
- Each consumer processes in batches of 1k–10k events
- Results are written in bulk to storage
- Failed batches retry from the queue
If you truly need “no drops”
You need:
- durable queueing
- producer acknowledgements
- retry logic
- idempotent consumers
- sufficient storage and retention
- load shedding only for explicitly acceptable low-priority traffic
Note: “no drops” usually means no silent drops. Under extreme overload, you may still need to reject requests rather than accept and lose them.
Common mistakes
- Using in-memory queues only
- Synchronous writes to databases per event
- Too few partitions
- One giant lock around shared state
- Excessive object creation and GC pressure
- Not handling producer retries
- Ignoring hot keys
- No backpressure policy
- Committing offsets before work is durable
Rule of thumb
If you want millions/sec reliably:
- use a distributed log
- batch aggressively
- partition well
- make consumers idempotent
- scale horizontally
- measure everything
If you want, I can also give you:
- a reference architecture diagram,
- a Kafka-based design, or
- a code-level implementation strategy for a specific language.