Cloud Native Go, 2nd Edition

Book description

Learn how to use Go's strengths to develop services that are scalable and resilient even in an unpredictable environment. With this book's expanded second edition, Go developers will explore the composition and construction of cloud native applications, from lower-level Go features and mid-level patterns to high-level architectural considerations.

Each chapter in this new edition builds on the lessons of the previous chapter, taking intermediate to advanced developers through Go to construct a simple but fully featured distributed key-value store. You'll learn about Go generics, dependability and reliability, memory leaks, and message-oriented middleware. New chapters on security and distributed state delve into critical aspects of developing secure distributed cloud native applications.

With this book you will:

  • Learn the features that make Go an ideal language for building cloud native software
  • Understand how Go solves the challenges of designing scalable distributed services
  • Design and implement a reliable cloud native service by leveraging Go's lower-level features such as channels and goroutines
  • Apply patterns, abstractions, and tooling to effectively build and manage complex distributed systems
  • Overcome stumbling blocks when using Go to build and manage a cloud native service

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. What’s New in the Second Edition
    2. Who Should Read This Book
    3. Why I Wrote This Book
    4. Conventions Used in This Book
    5. Using Code Examples
    6. O’Reilly Online Learning
    7. How to Contact Us
    8. Acknowledgments
    9. Acknowledgments for the Second Edition
  2. I. Going Cloud Native
  3. 1. What Is a “Cloud Native” Application?
    1. The Story So Far
    2. What Is Cloud Native?
      1. Scalability
      2. Loose Coupling
      3. Resilience
      4. Manageability
      5. Observability
    3. Why Is Cloud Native a Thing?
    4. Summary
  4. 2. Why Go Rules the Cloud Native World
    1. The Motivation Behind Go
    2. Features for a Cloud Native World
      1. Composition and Structural Typing
      2. Comprehensibility
      3. CSP-Style Concurrency
      4. Fast Builds
      5. Linguistic Stability
      6. Memory Safety
      7. Performance
      8. Static Linking
      9. Static Typing
    3. Summary
  5. II. Cloud Native Go Constructs
  6. 3. Go Language Foundations
    1. Basic Data Types
      1. Booleans
      2. Simple Numbers
      3. Complex Numbers
      4. Strings
    2. Variables
      1. Short Variable Declarations
      2. Zero Values
      3. The Blank Identifier
      4. Constants
    3. Container Types: Arrays, Slices, and Maps
      1. Arrays
      2. Slices
      3. Maps
    4. Pointers
    5. Control Structures
      1. Fun with for
      2. The if Statement
      3. The switch Statement
    6. Error Handling
      1. Creating an Error
    7. Putting the Fun in Functions: Variadics and Closures
      1. Functions
      2. Variadic Functions
      3. Anonymous Functions and Closures
    8. Structs, Methods, and Interfaces
      1. Structs
      2. Methods
      3. Interfaces
      4. Composition with Type Embedding
    9. Generics
      1. The Before Times
      2. Generic Functions
      3. Generic Types
      4. Type Constraints
      5. Type Inference
    10. The Good Stuff: Concurrency
      1. Goroutines
      2. Channels
      3. Select
    11. Summary
  7. 4. Cloud Native Patterns
    1. The Context Package
      1. What Context Can Do for You
      2. Creating Context
      3. Defining Context Deadlines and Timeouts
      4. Defining Request-Scoped Values
      5. Using a Context
    2. Layout of This Chapter
    3. Stability Patterns
      1. Circuit Breaker
      2. Debounce
      3. Retry
      4. Throttle
      5. Timeout
    4. Concurrency Patterns
      1. Fan-In
      2. Fan-Out
      3. Future
      4. Sharding
      5. Worker Pool
      6. Chord
    5. Summary
  8. 5. Building a Cloud Native Service
    1. Let’s Build a Service!
      1. What’s a Key-Value Store?
    2. Requirements
      1. What Is Idempotence and Why Does It Matter?
      2. The Eventual Goal
    3. Generation 0: The Core Functionality
      1. Your Super Simple API
    4. Generation 1: The Monolith
      1. Building an HTTP Server with net/http
      2. Building an HTTP Server with gorilla/mux
      3. Building a RESTful Service
      4. Making Your Data Structure Concurrency-Safe
    5. Generation 2: Persisting Resource State
      1. What’s a Transaction Log?
      2. Storing State in a Transaction Log File
      3. Storing State in an External Database
    6. Generation 3: Implementing Transport Layer Security
      1. Transport Layer Security
      2. Private Key and Certificate Files
      3. Securing Your Web Service with HTTPS
      4. Transport Layer Summary
    7. Containerizing Your Key-Value Store
      1. Docker (Absolute) Basics
      2. Building Your Key-Value Store Container
      3. Externalizing Container Data
    8. Summary
  9. III. The Cloud Native Attributes
  10. 6. Cloud Native Design Principles
    1. What’s the Point of Cloud Native?
    2. It’s All About Dependability
    3. What Is Dependability and Why Is It So Important?
      1. Dependability: It’s Not Just for Ops Anymore
    4. Achieving Dependability
      1. Fault Prevention
      2. Fault Tolerance
      3. Fault Removal
      4. Fault Forecasting
    5. The Continuing Relevance of The Twelve-Factor App
      1. I. Codebase
      2. II. Dependencies
      3. III. Configuration
      4. IV. Backing Services
      5. V. Build, Release, Run
      6. VI. Processes
      7. VII. Data Isolation
      8. VIII. Scalability
      9. IX. Disposability
      10. X. Development/Production Parity
      11. XI. Logs
      12. XII. Administrative Processes
    6. Summary
  11. 7. Scalability
    1. What Is Scalability?
      1. Different Forms of Scaling
    2. The Four Common Bottlenecks
    3. State and Statelessness
      1. Application State Versus Resource State
      2. Advantages of Statelessness
    4. Scaling Postponed: Efficiency
      1. Efficient Caching Using an LRU Cache
      2. Efficient Synchronization
      3. Memory Leaks Can…​fatal error: runtime: out of memory
      4. On Efficiency
    5. Service Architectures
      1. The Monolith System Architecture
      2. The Microservices System Architecture
      3. Serverless Architectures
    6. Summary
  12. 8. Loose Coupling
    1. Coupling
      1. Coupling Takes Many Forms
    2. Service Discovery
    3. Messaging Protocols
    4. Messaging Patterns
      1. Request-Response Messaging
      2. Publish-Subscribe Messaging
    5. Loose Coupling Local Resources with Plug-ins
      1. In-Process Plug-ins with the plugin Package
      2. HashiCorp’s Go Plug-in System Over RPC
    6. Hexagonal Architecture
      1. The Architecture
      2. Implementing a Hexagonal Service
    7. Summary
  13. 9. Resilience
    1. Keep on Ticking: Why Resilience Matters
    2. What Does It Mean for a System to Fail?
      1. Building for Resilience
    3. Cascading Failures
      1. Preventing Overload
    4. Play It Again: Retrying Requests
      1. Backoff Algorithms
      2. Circuit Breaking
      3. Timeouts
      4. Idempotence
    5. Service Redundancy
      1. Designing for Redundancy
      2. Autoscaling
    6. Healthy Health Checks
      1. What Does It Mean for a Service to Be “Healthy”?
      2. Liveness and Readiness Probes
      3. The Three Types of Health Checks
      4. Failing Open
    7. Graceful Shutdowns
      1. Signals and Traps
      2. Stop Incoming Requests
      3. Clean Up Your Resources
      4. Putting It Into Action
    8. Summary
  14. 10. Manageability
    1. What Is Manageability and Why Should I Care?
    2. Configuring Your Application
      1. Configuration Good Practice
      2. Configuring with Environment Variables
      3. Configuring with Command-Line Arguments
      4. Configuring with Files
      5. Viper: The Swiss Army Knife of Configuration Packages
    3. Feature Management with Feature Flags
      1. The Evolution of a Feature Flag
      2. Generation 0: The Initial Implementation
      3. Generation 1: The Hardcoded Feature Flag
      4. Generation 2: The Configurable Flag
      5. Generation 3: Dynamic Feature Flags
    4. Summary
  15. 11. Observability
    1. What Is Observability?
      1. Why Do We Need Observability?
      2. How Is Observability Different from “Traditional” Monitoring?
    2. The “Three Pillars of Observability”
    3. OpenTelemetry
      1. The OpenTelemetry Components
    4. Distributed Tracing
      1. Distributed Tracing Concepts
      2. Distributed Tracing with OpenTelemetry
      3. Putting It All Together: Distributed Tracing
    5. Metrics
      1. Push Versus Pull Metric Collection
      2. Metrics with OpenTelemetry
      3. Putting It All Together: Metrics
    6. Logging
      1. Better Logging Practices
      2. Logging with Go’s Standard log Package
      3. Structured Logging with the log/slog Package
      4. OpenTelemetry Logging
    7. Summary
  16. 12. Security
    1. Go: Secure by Design
    2. Common Vulnerabilities
      1. Injection
      2. Broken Access Control
      3. Cryptographic Failures
    3. Handling Untrusted Input
      1. Input Validation
      2. Input Sanitization
    4. Output Encoding
    5. Authentication
      1. Password-Based Authentication
      2. Token-Based Authentication
    6. Communication Security
      1. Transport Layer Security
      2. HTTP Over TLS
      3. gRPC Over TLS
    7. Cryptographic Practices
      1. Hashing
      2. Hashing Algorithms
      3. Encryption
      4. Cryptographic Randomness
    8. Database Security
      1. Connections
      2. Parameterized Queries
    9. Regular Expressions
      1. How Go Regex Is Different
      2. Syntax
    10. Summary
  17. 13. Distributed State
    1. Distributed State Is Hard
    2. Theoretical Foundations
      1. The CAP Theorem
      2. Consistency Models
      3. Data Replication
    3. Common Distributed Algorithms
      1. Consensus Algorithms
      2. Status Dissemination Techniques
    4. Distributing Our Key-Value Store
      1. Adding Some Raft
      2. Defining the State Machine
      3. The Apply Method
      4. Setting Up the Raft Node
      5. Defining the API
      6. Putting It All Together
      7. Future Directions
    5. Summary
  18. Index
  19. About the Author

Product information

  • Title: Cloud Native Go, 2nd Edition
  • Author(s): Matthew A. Titmus
  • Release date: October 2024
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098156428