Go Event Bus
A modular, in-process and distributed event bus abstraction for Go, designed for high-throughput systems with strong observability, resilience, and extensibility.
Overview
Go Event Bus provides a clean abstraction for event-driven architectures in Go, with pluggable transports, an explicit execution pipeline, and production-grade resilience patterns.
Key Features
- LocalBus for in-process events with sync/async dispatch, an overflow policy and graceful shutdown
- NatsBus for fire-and-forget distributed messaging
- JetStreamBus for durable, distributed events with dead-letter replay
- Invoker Chain for composable cross-cutting concerns: tracing, rate limiting, idempotency, DLQ, retry and circuit breaking
- Failure model shared by every transport: each failed message is retried, dead-lettered or dropped by the same code, and reported to the Observer exactly once
- Observer per bus with backends for logs (
SlogObserver, the default), Prometheus metrics and OpenTelemetry traces and metrics - Redis stores for distributed idempotency and rate limiting
- Causal metadata: event id, correlation id and causation id travel in the envelope
Where to go next
- Getting started: install the modules and run the first bus
- Invoker chain: the recommended order and the reason for each position
- Observability: the Observer, logs, metrics and traces
- Examples: complete examples, compiled with the repository
- Guides: task-oriented guides, starting with JetStream log compaction
- Adapter contract: the rules a new transport adapter must follow
- Failure model: what happens to every error on every transport
Quick Example
Compiled from examples/01_basic_local_bus.go:
package main
import (
"context"
"fmt"
"log/slog"
"github.com/isaquesb/go-event-bus"
"github.com/isaquesb/go-event-bus/invoker"
)
// UserCreated is emitted when a new user registers
type UserCreated struct {
UserID string
Email string
UserName string
}
func (e UserCreated) Name() string { return "user.created" }
// SendWelcomeEmail handles user.created events
func SendWelcomeEmail(ctx context.Context, evt event.Event) error {
e := evt.(UserCreated)
slog.Info("sending welcome email", "user_id", e.UserID, "email", e.Email)
// ... send email logic
return nil
}
func main() {
bus := event.NewLocalBus(event.LocalBusOptions{
Invoker: invoker.NewChain(),
MaxConcurrent: 10,
// Observer receives every terminal failure once (Observer.Failure). The slog
// observer is also the default when Observer is nil.
Observer: event.NewSlogObserver(nil),
})
ctx := context.Background()
if err := bus.Subscribe(ctx, "user.created", SendWelcomeEmail, event.WithHandlerName("send-welcome-email")); err != nil {
slog.Error("subscribe failed", "error", err)
return
}
// Synchronous - waits for all handlers to complete
if _, err := bus.EmitSync(ctx, UserCreated{
UserID: "user-123",
Email: "john@example.com",
UserName: "John Doe",
}); err != nil {
slog.Error("emit sync failed", "error", err)
}
fmt.Println("Events emitted successfully")
}
Architecture at a Glance
The library is composed of three orthogonal layers:
- Event Definition & Serialization - Events, Registry, Envelopes
- Execution Pipeline (Invoker Chain) - Cross-cutting concerns as composable middleware
- Transport / Delivery - LocalBus, NATS Core, JetStream
These layers are intentionally decoupled, so you can mix and match based on your needs.
The failure model and the Observer cut across all three layers.