Go is an excellent language for real-time streaming applications. Goroutines and channels make it cheap to run thousands of concurrent pipelines, the runtime scheduler keeps latency predictable, and the standard library covers networking, JSON and timers. For production systems you pair Go with a broker such as Apache Kafka or NATS and keep the processing stages small, bounded and cancellable.
This article explains the building blocks, shows a runnable windowed aggregation pipeline, and covers backpressure, brokers, WebSockets and the pitfalls to avoid.
Streaming workloads are many small, long-lived, I/O-heavy tasks, which is what the Go runtime is designed for.
context for lifecycle. A single context.Context cancels every stage on shutdown or timeout.Go is not a stream-processing framework like Apache Flink. It does not provide distributed state, exactly-once semantics or event-time windowing out of the box. You build those pieces or delegate them to the broker.
A common design is producer, transform, aggregate. Each stage reads from one channel and writes to the next, and each stops when its context is cancelled or its input closes.
The following program emits an event every 10 ms, counts events per key, and prints the counts every second (a tumbling window). It stops after 2.5 seconds.
Typical output (counts vary by a few events because of timer jitter):
The producer owns the output channel and closes it when it stops, which tells the aggregator to flush and exit. The aggregation map is touched by one goroutine only, so no mutex is needed. That "one owner per piece of state" rule is the most reliable way to avoid data races in streaming code.
If a producer is faster than a consumer, something must give. Decide the policy explicitly:
| Policy | How in Go | Use when |
|---|---|---|
| Block the producer | Unbuffered or small buffered channel | Data must not be lost |
| Drop newest | select with a default case on send | Metrics, telemetry, previews |
| Drop oldest | Ring buffer in front of the consumer | Latest value matters most |
| Spill to disk or broker | Write to Kafka or a queue | Bursts exceed memory |
Never use an unbounded slice as a queue. Memory growth under load is the most common cause of streaming services being killed in production. Also use select on ctx.Done() for every send and receive, so a stuck stage cannot leak goroutines.
For parallel processing, use a fixed worker pool reading from one channel. If order matters per key, hash the key to pick a worker so events with the same key always go to the same goroutine.
In production the events come from a broker or go to clients over a network protocol.
segmentio/kafka-go, twmb/franz-go and Confluent's confluent-kafka-go (which wraps the C library through cgo). Use a consumer group so partitions are balanced across instances.nats.go client and JetStream give lightweight publish/subscribe with persistence.http.Flusher.A minimal Kafka consumer with kafka-go looks like this:
ReadMessage commits offsets automatically when a group ID is set. If you need at-least-once processing, use FetchMessage and CommitMessages after the work succeeds, and make the handler idempotent.
SIGTERM, stop reading, drain in-flight work, then commit offsets and exit.pprof and go test -race to find contention and data races before release.Go uses less memory per connection and starts faster, which suits many small services. Java has richer stream-processing frameworks such as Kafka Streams and Flink. Choose Go for custom, lightweight pipelines and Java when you need a full stateful processing framework.
A single well-written Go service can process very high rates, but the limit is usually the broker, the network and downstream storage. Scale out with partitions and multiple consumer instances.
Pass a context.Context to every stage, select on ctx.Done() for every blocking send and receive, and close channels from the sending side only.
Not by itself. You get it by combining broker features, such as Kafka transactions, with idempotent writes in your handler.
Go gives you the primitives for real-time streaming: goroutines, channels, contexts and a fast network stack. Build stages with a single owner per piece of state, choose a backpressure policy deliberately, use a broker for durability, and add graceful shutdown and metrics before going to production.