Event-Driven Architecture in Golang

Book description

Begin building event-driven microservices, including patterns to handle data consistency and resiliency

Key Features

  • Explore the benefits and tradeoffs of event-driven architectures with practical examples and use cases
  • Understand synergy with event sourcing, CQRS, and domain-driven development in software architecture
  • Build an end-to-end robust application architecture by the end of the book

Book Description

Event-driven architecture in Golang is an approach used to develop applications that shares state changes asynchronously, internally, and externally using messages. EDA applications are better suited at handling situations that need to scale up quickly and the chances of individual component failures are less likely to bring your system crashing down. This is why EDA is a great thing to learn and this book is designed to get you started with the help of step-by-step explanations of essential concepts, practical examples, and more.

You'll begin building event-driven microservices, including patterns to handle data consistency and resiliency. Not only will you learn the patterns behind event-driven microservices but also how to communicate using asynchronous messaging with event streams. You'll then build an application made of several microservices that communicates using both choreographed and orchestrated messaging.

By the end of this book, you'll be able to build and deploy your own event-driven microservices using asynchronous communication.

What you will learn

  • Understand different event-driven patterns and best practices
  • Plan and design your software architecture with ease
  • Track changes and updates effectively using event sourcing
  • Test and deploy your sample software application with ease
  • Monitor and improve the performance of your software architecture

Who this book is for

This hands-on book is for intermediate-level software architects, or senior software engineers working with Golang and interested in building asynchronous microservices using event sourcing, CQRS, and DDD. Intermediate-level knowledge of the Go syntax and concurrency features is necessary.

Table of contents

  1. Event-Driven Architecture in Golang
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
    9. Download a free PDF copy of this book
  6. Part 1: Event-Driven Fundamentals
  7. Chapter 1: Introduction to Event-Driven Architectures
    1. Technical requirements
    2. An exchange of facts
      1. Event notifications
      2. Event-carried state transfer
      3. Event sourcing
      4. Core components
      5. Wrap-up
    3. The MallBots application
      1. The pitch
      2. Application services
      3. API gateway services
      4. Clients
      5. A quick note about hexagons
    4. Benefits of EDA
      1. Resiliency
      2. Agility
      3. User experience (UX)
      4. Analytics and auditing
    5. Challenges of EDA
      1. Eventual consistency
      2. Dual writes
      3. Distributed and asynchronous workflows
      4. Debuggability
      5. Getting it right
    6. Summary
  8. Chapter 2: Supporting Patterns in Brief
    1. Domain-driven design
      1. DDD misconceptions
      2. So, what is it all about then?
      3. How is it useful for EDA?
    2. Domain-centric architectures
      1. An evolving solution
      2. Hexagonal architecture applied
      3. Testing
      4. A rulebook, not a guidebook
      5. Should you use domain-centric architectures?
      6. How is it useful for EDA?
    3. Command and Query Responsibility Segregation
      1. The problem being solved
      2. Applying CQRS
      3. When to consider CQRS
      4. CQRS and event sourcing
      5. Task-based UI
    4. Application architectures
      1. Monolithic architecture
      2. Microservices
      3. Recommendation for green field projects
    5. Summary
    6. Further reading
  9. Chapter 3: Design and Planning
    1. Technical requirements
    2. What are we building?
    3. Finding answers with EventStorming
      1. What is EventStorming?
      2. Big Picture EventStorming
      3. Design-level EventStorming
    4. Understanding the business
    5. Recording architectural decisions
    6. Summary
    7. Further reading
  10. Part 2: Components of Event-Driven Architecture
  11. Chapter 4: Event Foundations
    1. Technical requirements
    2. A tour of MallBots
      1. The responsibilities of the monolith
      2. Module code organization
      3. User interface
      4. Running the monolith
      5. A focus on event-driven integration and communication patterns
    3. Taking a closer look at module integration
      1. Using external data
      2. Commanding external components
    4. Types of events
      1. Domain events
      2. Event sourcing events
      3. Integration events
    5. Refactoring side effects with domain events
      1. What about the modules not using DDD?
    6. Summary
  12. Chapter 5: Tracking Changes with Event Sourcing
    1. Technical requirements
    2. What is event sourcing?
      1. Understanding the difference between event streaming and event sourcing
    3. Adding event sourcing to the monolith
      1. Beyond basic events
      2. Adding the event sourcing package
    4. Using just enough CQRS
      1. A group of stores is called a mall
      2. A group of products is called a catalog
      3. Taking note of the little things
      4. Connecting the domain events with the read model
      5. Recapping the CQRS changes
    5. Aggregate event stream lifetimes
      1. Taking periodic snapshots of the event stream
      2. Using snapshots
    6. Summary
  13. Chapter 6: Asynchronous Connections
    1. Technical requirements
    2. Asynchronous integration with messages
      1. Integration with notification events
      2. Integration with event-carried state transfer
      3. Eventual consistency
      4. Message-delivery guarantees
      5. Idempotent message delivery
      6. Ordered message delivery
    3. Implementing messaging with NATS JetStream
      1. The am package
      2. The jetstream package
    4. Making the Store Management module asynchronous
      1. Modifying the monolith configuration
      2. Updating the monolith application
      3. Providing to the modules the JetStreamContext
      4. Publishing messages from the Store Management module
      5. Receiving messages in the Shopping Baskets module
      6. Verifying we have good communication
    5. Summary
  14. Chapter 7: Event-Carried State Transfer
    1. Technical requirements
    2. Refactoring to asynchronous communication
      1. Store Management state transfer
      2. Customer state transfer
      3. Order processing state transfer
      4. Payments state transfer
      5. Documenting the asynchronous API
    3. Adding a new order search module
    4. Building read models from multiple sources
      1. Creating a read model record
    5. Summary
  15. Chapter 8: Message Workflows
    1. Technical requirements
    2. What is a distributed transaction?
      1. Why do we need distributed transactions?
    3. Comparing various methods of distributed transactions
      1. The 2PC
      2. The Saga
    4. Implementing distributed transactions with Sagas
      1. Adding support for the Command and Reply messages
      2. Adding an SEC package
    5. Converting the order creation process to use a Saga
      1. Adding commands to the saga participants
      2. Implementing the create order saga execution coordinator
    6. Summary
  16. Chapter 9: Transactional Messaging
    1. Technical requirements
    2. Identifying problems faced by distributed applications
      1. Identifying problems in synchronous applications
      2. Identifying problems in asynchronous applications
      3. Examining potential ways to address the problem
      4. The singular write solution
    3. Exploring transactional boundaries
      1. How the implementation will work
      2. The di package
      3. Updating the Depot module with dependency containers
    4. Using an Inbox and Outbox for messages
      1. Implementing a messages inbox
      2. Implementing a messages outbox
    5. Summary
  17. Part 3: Production Ready
  18. Chapter 10: Testing
    1. Technical requirements
    2. Coming up with a testing strategy
      1. Unit tests
      2. Integration tests
      3. Contract tests
      4. End-to-end tests
    3. Testing the application and domain with unit tests
      1. Table-driven testing
      2. Creating and using test doubles in our tests
    4. Testing dependencies with integration testing
      1. Incorporating the dependencies into your tests
      2. Running tests with more complex setups
      3. Testing ProductCacheRepository
      4. Breaking tests into groups
    5. Testing component interactions with contract tests
      1. Consumer expectations
      2. Provider verifications
      3. Not building any silos
      4. Contract testing with Pact
      5. REST consumer and provider example
      6. Message consumer and provider example
    6. Testing the application with end-to-end tests
      1. Relationship with behavior-driven development
      2. E2E test organization
      3. Making executable specifications out of our features
      4. What to test or not test
    7. Summary
  19. Chapter 11: Deploying Applications to the Cloud
    1. Technical requirements
    2. Turning the modular monolith into microservices
      1. Refactoring the monolith construct
      2. Updating the composition root of each module
      3. Making each module run as a service
      4. Updates to the Dockerfile build processes
      5. Updates to the Docker Compose file
      6. Adding a reverse proxy to the compose environment
      7. Fixing the gRPC connections
    3. Installing the necessary DevOps tools
      1. Installing every tool into a Docker container
      2. Installing the tools into your local system
    4. Using Terraform to configure an AWS environment
      1. Preparing for the deployment
      2. A look at the AWS resources we are deploying
      3. Deploying the infrastructure
      4. Viewing the Kubernetes environment
    5. Deploying the application to AWS with Terraform
      1. Getting to know the application resources to be deployed
      2. Deploying the application
      3. Tearing down the application and infrastructure
    6. Summary
  20. Chapter 12: Monitoring and Observability
    1. Technical requirements
    2. What are monitoring and observability?
      1. The three pillars of observability
      2. How tracing works
    3. Instrumenting the application with OpenTelemetry and Prometheus
      1. Adding distributed tracing to the application
      2. Adding metrics to the application
    4. Viewing the monitoring data
    5. Summary
  21. Index
    1. Why subscribe?
  22. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts
    3. Download a free PDF copy of this book

Product information

  • Title: Event-Driven Architecture in Golang
  • Author(s): Michael Stack
  • Release date: November 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803238012