Top Related Projects
Sarama is a Go library for Apache Kafka.
Kafka library in Go
Confluent's Apache Kafka Golang client
franz-go contains a feature complete, pure Go library for interacting with Kafka from 0.8.0 through 3.7+. Producing, consuming, transacting, administrating, etc.
Goka is a compact yet powerful distributed stream processing library for Apache Kafka written in Go.
:alarm_clock: :fire: A TCP proxy to simulate network and system conditions for chaos and resiliency testing
Quick Overview
Sarama is a Go library for Apache Kafka 0.8 and later. It provides a pure Go client for producing and consuming messages using the Kafka protocol, offering high performance and full support for Kafka features.
Pros
- High performance and low overhead
- Full support for Kafka features, including consumer groups and offset management
- Extensive configuration options for fine-tuning
- Well-maintained with regular updates and active community support
Cons
- Steep learning curve for beginners due to its extensive API
- Documentation can be overwhelming for newcomers
- Some advanced features require careful configuration
- May require additional effort to integrate with certain Kafka ecosystem tools
Code Examples
- Producing a message:
producer, err := sarama.NewSyncProducer([]string{"localhost:9092"}, nil)
if err != nil {
log.Fatalln(err)
}
defer producer.Close()
msg := &sarama.ProducerMessage{
Topic: "test",
Value: sarama.StringEncoder("Hello, Kafka!"),
}
partition, offset, err := producer.SendMessage(msg)
if err != nil {
log.Printf("Failed to send message: %s\n", err)
} else {
log.Printf("Message sent to partition %d at offset %d\n", partition, offset)
}
- Consuming messages:
consumer, err := sarama.NewConsumer([]string{"localhost:9092"}, nil)
if err != nil {
log.Fatalln(err)
}
defer consumer.Close()
partitionConsumer, err := consumer.ConsumePartition("test", 0, sarama.OffsetNewest)
if err != nil {
log.Fatalln(err)
}
defer partitionConsumer.Close()
for message := range partitionConsumer.Messages() {
log.Printf("Received message: %s\n", string(message.Value))
}
- Using a consumer group:
config := sarama.NewConfig()
config.Consumer.Group.Rebalance.Strategy = sarama.BalanceStrategyRoundRobin
group, err := sarama.NewConsumerGroup([]string{"localhost:9092"}, "my-group", config)
if err != nil {
log.Fatalln(err)
}
defer group.Close()
ctx := context.Background()
for {
topics := []string{"test"}
handler := consumerGroupHandler{}
err := group.Consume(ctx, topics, handler)
if err != nil {
log.Println("Error from consumer: ", err)
}
}
Getting Started
-
Install Sarama:
go get github.com/Shopify/sarama
-
Import Sarama in your Go code:
import "github.com/Shopify/sarama"
-
Create a Kafka configuration:
config := sarama.NewConfig() config.Producer.Return.Successes = true
-
Connect to Kafka and start producing or consuming messages using the examples provided above.
Competitor Comparisons
Sarama is a Go library for Apache Kafka.
Pros of sarama
- Well-established and widely used Kafka client library for Go
- Comprehensive feature set covering most Kafka operations
- Active community and regular updates
Cons of sarama
- More complex API compared to some alternatives
- Steeper learning curve for beginners
- Can be challenging to configure for optimal performance
Code Comparison
This comparison is not applicable as IBM/sarama and IBM/sarama refer to the same repository. There are no distinct codebases to compare.
Additional Notes
sarama is a popular Kafka client library for Go, maintained by IBM. It provides a robust set of features for interacting with Apache Kafka, including producer and consumer implementations, support for various Kafka versions, and advanced functionality like transaction support.
The library is known for its performance and flexibility, but it may require more setup and configuration compared to simpler alternatives. It's particularly well-suited for complex Kafka deployments and advanced use cases.
Users should consider their specific requirements and familiarity with Kafka when choosing sarama as their Go client library. For simpler use cases or projects where ease of use is a priority, alternative libraries might be more appropriate.
Kafka library in Go
Pros of kafka-go
- Simpler API and easier to use for beginners
- Native support for Go contexts, making it more idiomatic
- Better performance in some scenarios, especially for newer Kafka versions
Cons of kafka-go
- Less mature and feature-rich compared to Sarama
- Smaller community and fewer third-party extensions
- May lack some advanced features present in Sarama
Code Comparison
kafka-go:
writer := kafka.NewWriter(kafka.WriterConfig{
Brokers: []string{"localhost:9092"},
Topic: "my-topic",
})
err := writer.WriteMessages(context.Background(),
kafka.Message{Value: []byte("Hello, World!")},
)
Sarama:
producer, err := sarama.NewSyncProducer([]string{"localhost:9092"}, nil)
msg := &sarama.ProducerMessage{
Topic: "my-topic",
Value: sarama.StringEncoder("Hello, World!"),
}
_, _, err = producer.SendMessage(msg)
Both libraries provide Go clients for Apache Kafka, but they differ in their approach and feature set. kafka-go offers a more straightforward API and better integration with Go's standard library, while Sarama provides a more comprehensive set of features and has been around longer. The choice between the two depends on specific project requirements and developer preferences.
Confluent's Apache Kafka Golang client
Pros of confluent-kafka-go
- Built on librdkafka, providing high performance and reliability
- Official Confluent library with direct support and updates
- Supports more advanced Kafka features like transactions and exactly-once semantics
Cons of confluent-kafka-go
- Requires CGo, which can complicate cross-compilation and deployment
- Less idiomatic Go code due to C library wrapper
- Steeper learning curve for Go developers unfamiliar with librdkafka
Code Comparison
sarama:
producer, err := sarama.NewSyncProducer([]string{"localhost:9092"}, nil)
message := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test message")}
partition, offset, err := producer.SendMessage(message)
confluent-kafka-go:
p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": "localhost:9092"})
topic := "test"
p.Produce(&kafka.Message{TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny}, Value: []byte("test message")}, nil)
Both libraries offer robust Kafka client implementations for Go, but they differ in their approach and feature set. sarama is a pure Go implementation, making it easier to work with in Go projects, while confluent-kafka-go leverages librdkafka for performance at the cost of some Go idioms. Choose based on your specific requirements and familiarity with Kafka ecosystems.
franz-go contains a feature complete, pure Go library for interacting with Kafka from 0.8.0 through 3.7+. Producing, consuming, transacting, administrating, etc.
Pros of franz-go
- More actively maintained with frequent updates
- Better support for newer Kafka features and protocols
- Improved performance, especially for producing messages
Cons of franz-go
- Less mature and battle-tested compared to Sarama
- Smaller community and ecosystem of third-party tools
Code Comparison
Producing a message with Sarama:
producer, _ := sarama.NewSyncProducer([]string{"localhost:9092"}, nil)
msg := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test message")}
partition, offset, _ := producer.SendMessage(msg)
Producing a message with franz-go:
client, _ := kgo.NewClient(kgo.SeedBrokers("localhost:9092"))
record := &kgo.Record{Topic: "test", Value: []byte("test message")}
result := client.ProduceSync(context.Background(), record)
partition, offset := result.Partition, result.Offset
Both libraries offer similar functionality, but franz-go's API is more modern and streamlined. franz-go also provides better context support and more flexible configuration options. However, Sarama's widespread adoption means it has more examples and community resources available.
Goka is a compact yet powerful distributed stream processing library for Apache Kafka written in Go.
Pros of goka
- Higher-level abstraction for building stream processing applications
- Built-in support for stateful processing and local storage
- Simplified API for defining processors and topics
Cons of goka
- Less flexible for low-level Kafka operations
- Smaller community and fewer contributors compared to Sarama
- Limited documentation and examples
Code Comparison
goka:
func (g *Processor) Define(topic string, cb ProcessCallback, codec Codec) {
g.graph.DefineGroup(g.group,
Group(topic, codec, cb),
)
}
Sarama:
consumer, err := sarama.NewConsumer([]string{"localhost:9092"}, nil)
partitionConsumer, err := consumer.ConsumePartition("topic", 0, sarama.OffsetNewest)
for msg := range partitionConsumer.Messages() {
// Process message
}
goka provides a higher-level API for defining processors and topics, while Sarama offers more fine-grained control over consumer and producer operations. goka simplifies the process of building stream processing applications, but Sarama provides greater flexibility for custom Kafka integrations.
goka is better suited for developers who want to quickly build stateful stream processing applications, while Sarama is ideal for those who need low-level control over Kafka operations or are working on more complex, custom implementations.
:alarm_clock: :fire: A TCP proxy to simulate network and system conditions for chaos and resiliency testing
Pros of toxiproxy
- Focused on network simulation and fault injection for testing
- Supports multiple protocols beyond just Kafka
- Easy to set up and use for various testing scenarios
Cons of toxiproxy
- Not specifically designed for Kafka, unlike sarama
- Limited to testing and simulation, not for production use
- May require additional setup for Kafka-specific testing
Code Comparison
toxiproxy:
proxy := toxiproxy.NewProxy("kafka", "localhost:9092", "localhost:8474")
proxy.AddToxic("latency", "latency", "downstream", 1.0, toxiproxy.Attributes{
"latency": 1000,
})
sarama:
config := sarama.NewConfig()
producer, err := sarama.NewSyncProducer([]string{"localhost:9092"}, config)
message := &sarama.ProducerMessage{Topic: "test", Value: sarama.StringEncoder("test message")}
partition, offset, err := producer.SendMessage(message)
Summary
toxiproxy is a versatile network simulation tool for testing various protocols, including Kafka. It excels in creating fault scenarios but is limited to testing environments. sarama, on the other hand, is a dedicated Kafka client library for Go, offering production-ready functionality for Kafka interactions. While toxiproxy is ideal for simulating network issues, sarama provides direct Kafka integration for both development and production use.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
sarama
Sarama is an MIT-licensed Go client library for Apache Kafka.
Getting started
- API documentation and examples are available via pkg.go.dev.
- Mocks for testing are available in the mocks subpackage.
- The examples directory contains more elaborate example applications.
- The tools directory contains command line tools that can be useful for testing, diagnostics, and instrumentation.
You might also want to look at the Frequently Asked Questions.
Compatibility and API stability
Sarama provides a "2 releases + 2 months" compatibility guarantee: we support the two latest stable releases of Kafka and Go, and we provide a two month grace period for older releases. However, older releases of Kafka are still likely to work.
Sarama follows semantic versioning and provides API stability via the standard Go module version numbering scheme.
A changelog is available here.
Contributing
- Get started by checking our contribution guidelines.
- Read the Sarama wiki for more technical and design details.
- The Kafka Protocol Specification contains a wealth of useful information.
- For more general issues, there is a google group for Kafka client developers.
- If you have any questions, just ask!
Top Related Projects
Sarama is a Go library for Apache Kafka.
Kafka library in Go
Confluent's Apache Kafka Golang client
franz-go contains a feature complete, pure Go library for interacting with Kafka from 0.8.0 through 3.7+. Producing, consuming, transacting, administrating, etc.
Goka is a compact yet powerful distributed stream processing library for Apache Kafka written in Go.
:alarm_clock: :fire: A TCP proxy to simulate network and system conditions for chaos and resiliency testing
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot