Preface
- Why Cloud-Native Distributed Systems?
- Who This Book Is For
- How to Read This Book
- About the Code
- What This Book Is Not
Introduction
- The Problem with Distributed Systems Books
- The Mental Model
- The Company SDK
- The System We Build
- The Code Style
- Before You Begin
Chapter 1: Why Distributed? The Cost of Coordination
- The Limits of One
- The Eight Things You Cannot Assume
- When You Have To
- The Hidden Tax
- The Struct-to-Service Progression
- Go’s Fit
- The Monolith-First Principle
- What You Will Build
- Q&A
Chapter 2: Concurrency as Distributed Systems in Miniature
- Goroutines
- Channels
- select
- sync.WaitGroup
- context.Context
- Putting It Together: A Worker Pool
- Graceful Shutdown
- The Concurrency Mental Model
- Q&A
- What Goes in the SDK
Chapter 3: Two Services, One Queue
- Why a Queue and Not an HTTP Call?
- NATS for a First Example
- The Interfaces
- The Producer
- The Consumer
- Running It
- Loose Coupling in Practice
- Error Handling and Reconnection
- The Complete Picture
- What Goes in the SDK
Chapter 4: HTTP Services and Middleware Chains
- net/http in Go 1.22
- The Handler Interface
- Building a Middleware Chain
- The Essential Middleware
- Structured Error Responses
- JSON Request Binding
- Health Check Patterns
- The Complete Service
- Testing HTTP Handlers
- What Frameworks Add
- Q&A
- What Goes in the SDK
Chapter 5: Authentication and JWT Across Services
- What a JWT Is
- HMAC vs RSA vs ECDSA
- Generating and Validating Tokens with ECDSA
- The JWKS Endpoint
- Auth Middleware
- Scope-Based Authorization
- The Auth Service
- Pluggable Authentication Agents
- Token Propagation Between Services
- Adding Auth to the Service Chain
- Testing Auth
- Q&A
- What Goes in the SDK
Chapter 6: Database Access and Migrations
- pgx: PostgreSQL Done Right
- The Repository Pattern
- Transactions
- sqlc: Type-Safe SQL Without an ORM
- Schema Migrations
- Zero-Downtime Migrations
- One Database Per Service
- Observability for Database Operations
- Q&A
- What Goes in the SDK
Chapter 7: Context Propagation — The Thread Connecting Everything
- How the Context Tree Fails
- The Context Discipline
- Deadline Propagation
- Narrowing Deadlines
- Context Values in Practice
- The Context Anti-Patterns
- Connecting the Layers
- Q&A
- What Goes in the SDK
Chapter 8: Message Queues in Depth — Kafka, Backpressure, and Consumer Groups
- How Kafka Works
- Writing a Kafka Producer
- Writing a Kafka Consumer
- Backpressure
- Consumer Groups in Action
- Dynamic Topic Subscriptions
- Message Schema Management
- Q&A
- What Goes in the SDK
Chapter 9: Service-to-Service Calls — gRPC, Protobuf, Retries, and Circuit Breakers
- Why gRPC for Internal Communication
- Defining a Service with Protocol Buffers
- Generating Go Code with buf
- Implementing the gRPC Server
- Running the gRPC Server
- The gRPC Client with Interceptors
- Service Token Authentication
- The Dual API Pattern: Management vs Consumption
- Timeouts and Retries
- The Circuit Breaker
- Choosing: HTTP vs gRPC
- Q&A
- What Goes in the SDK
Chapter 10: Orchestration — Parallel Flows and Fan-Out
- The Sequential Problem
- Fan-Out with errgroup
- Partial Failure Handling
- Pipeline Composition
- Fan-Out to Dynamic Targets
- Timeout Budget Across a Multi-Step Flow
- Measuring the Impact
- What Goes in the SDK
- Q&A
Chapter 11: Distributed Consistency in Practice
- The CAP Theorem, Applied
- Eventual Consistency
- Idempotency Keys
- Optimistic Locking
- The Saga Pattern
- Event-Driven Consistency
- Detecting Inconsistency
- The Outbox Pattern
- Reconciliation in Practice
- What Goes in the SDK
- Q&A
Chapter 12: Observability — Logging, Metrics, and Tracing
- Structured Logging with slog
- Metrics with Prometheus
- Distributed Tracing with OpenTelemetry
- Alerting: When Metrics Become Alarms
- Q&A
- What Goes in the SDK
Chapter 13: Testing Distributed Systems
- The Testing Pyramid for Distributed Systems
- Testing with Real Databases: testcontainers
- The -race Flag
- Testing with Fake Implementations
- Testing Idempotency
- Chaos Testing: Kill a Dependency Mid-Test
- goleak: Finding Goroutine Leaks
- Table-Driven Tests for Edge Cases
- Q&A
- What Goes in the SDK
Chapter 14: Configuration and Secrets
- The Twelve-Factor Principle
- Configuration With Struct Tags
- Viper for Complex Configuration
- Configuration Validation at Startup
- Secrets Management
- Conditional Secret Exposure
- Live Reload via Signal Handler
- Configuration in a Multi-Service System
- Configuration Testing
- Q&A
- What Goes in the SDK
Chapter 15: Capstone — One Complete System
- The Design
- The Data Flows
- The Repository Structure
- Implementing the Core: The Order Service
- The docker-compose
- The API Gateway
- Service Discovery
- Two-Phase Service Startup
- Deploying to Kubernetes
- Tracing a Request
- Running It
- The Integration Test
- What You Have Built
- Production Hardening Checklist
- Q&A
Appendix A: Go Concurrency Quick Reference
- Goroutines
- Channels
- select
- context.Context
- sync.WaitGroup
- sync.Mutex / sync.RWMutex
- sync/atomic
- errgroup
- signal.NotifyContext (graceful shutdown)
- Common Patterns
Appendix B: File and Object Storage Abstraction
- The Storage Interface
- The S3 Adapter
- The Local Filesystem Adapter
- Wiring It Up
- The Platform SDK Package
- When to Reach for Afero
Appendix C: CLI Tooling for Operations
- The CLI Structure
- The Migration Runner
- The Health Checker
- Admin Commands
- Testing CLI Commands
- What Goes in the SDK
- Beyond CLI: Operational TUIs
Glossary
Acknowledgements
Credits
- Further Reading