API Reference

Sarama

Kafka client library for Go providing producers, consumers, consumer groups and admin APIs.

Official Documentation ↗
CompatibilityThis documentation covers version sarama v1.43+ / Kafka 0.11+ / Go 1.20+. Check the official docs for the latest API surface.

Installation

gogo get github.com/IBM/sarama

Methods

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:

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).

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.