Sarama
Kafka client library for Go providing producers, consumers, consumer groups and admin APIs.
Official Documentation ↗sarama v1.43+ / Kafka 0.11+ / Go 1.20+. Check the official docs for the latest API surface.Installation
go get github.com/IBM/saramaMethods
No methods match this filter.
Overview
Sarama is a Kafka client for Go covering producers, consumers, consumer groups and administrative operations. It gives fine control over batching, acknowledgements and offset management.
Core Methods
Build a config, create a producer or consumer group against broker addresses, then send messages or consume partitions. The consumer group handler manages session and claims.
Usage Notes
Choose sync producers for simplicity and async for throughput, enable idempotent producers where supported, and commit offsets after processing. Make handlers idempotent.
Common Pitfalls
Committing before processing risks data loss on crash. Ignoring rebalance callbacks causes duplicate work. Unbounded producer buffering can exhaust memory under broker outages.
Errors
The official API reference declares the following error types:
sarama.ErrOutOfBrokers(var error (errors.New)) — official text: “var ErrOutOfBrokers = errors.New(“kafka: client has run out of available brokers to talk to”)” (source: https://raw.githubusercontent.com/IBM/sarama/v1.61.1/errors.go, fetched 2026-11-08T16:02:45+08:00)sarama.ErrClosedClient(var error (errors.New)) — official text: “var ErrClosedClient = errors.New(“kafka: tried to use a client that was closed”)” (source: https://raw.githubusercontent.com/IBM/sarama/v1.61.1/errors.go, fetched 2026-11-08T16:02:45+08:00)sarama.ErrShuttingDown(var error (errors.New)) — official text: “var ErrShuttingDown = errors.New(“kafka: message received by producer in process of shutting down”)” (source: https://raw.githubusercontent.com/IBM/sarama/v1.61.1/errors.go, fetched 2026-11-08T16:02:45+08:00)sarama.ErrClosedConsumerGroup(var error (errors.New)) — official text: “var ErrClosedConsumerGroup = errors.New(“kafka: tried to use a consumer group that was closed”)” (source: https://raw.githubusercontent.com/IBM/sarama/v1.61.1/consumer_group.go, fetched 2026-11-08T16:02:45+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T16:02:45+08:00 via WebFetch pkg.go.dev + PowerShell Invoke-WebRequest raw.githubusercontent.com (IBM/sarama v1.61.1).
sarama.NewSyncProducer→ signature from https://raw.githubusercontent.com/IBM/sarama/v1.61.1/sync_producer.go; official text: “func NewSyncProducer(addrs []string, config *Config) (SyncProducer, error) {”producer.SendMessage→ signature from https://raw.githubusercontent.com/IBM/sarama/v1.61.1/sync_producer.go; official text: “// SendMessage produces a given message, and returns only when it either has // succeeded or failed to produce. It will return the partition and the offset // of the produced message, or an error if the message failed to”sarama.NewConsumerGroup→ signature from https://raw.githubusercontent.com/IBM/sarama/v1.61.1/consumer_group.go; official text: “func NewConsumerGroup(addrs []string, groupID string, config *Config) (ConsumerGroup, error) {”
FAQ
- Sync or async producer?
- A sync producer waits for acknowledgement, simplifying error handling at the cost of throughput. An async producer batches and returns results through channels, which is faster but requires handling delivery reports.
- How does offset commit work?
- Consumer groups commit offsets after processing. Commit after successful handling to get at-least-once delivery, and make handlers idempotent since redelivery is possible.
- How do I handle rebalances?
- Implement the group handler so it stops processing and commits offsets when partitions are revoked, then resumes on assignment, to avoid duplicate or lost work.
Report a problem with this API docOpens a pre-filled issue on GitHub.