Microservices Design Patterns in .NET

Book description

Learn to be deliberate and intentional in your design, technology, and pattern choices when developing an application using a microservices architecture.

Key Features

  • Tackle common design problems when developing a microservices application using .NET Core
  • Explore applying S.O.L.I.D development principles in developing a stable microservice application
  • Use your knowledge to solve common microservice application design challenges

Book Description

Are you a developer who needs to fully understand the different patterns and benefits that they bring to designing microservices? If yes, then this book is for you. Microservices Design Patterns in .NET will help you appreciate the various microservice design concerns and strategies that can be used to navigate them.

Making a microservice-based app is no easy feat and there are many concerns that need to be addressed. As you progress through the chapters of this guide, you'll dive headfirst into the problems that come packed with this architectural approach, and then explore the design patterns that address these problems. You'll also learn how to be deliberate and intentional in your architectural design to overcome major considerations in building microservices.

By the end of this book, you'll be able to apply critical thinking and clean coding principles when creating a microservices application using .NET Core.

What you will learn

  • Use Domain-Driven Design principles in your microservice design
  • Leverage patterns like event sourcing, database-per-service, and asynchronous communication
  • Build resilient web services and mitigate failures and outages
  • Ensure data consistency in distributed systems
  • Leverage industry standard technology to design a robust distributed application
  • Find out how to secure a microservices-designed application
  • Use containers to handle lightweight microservice application deployment

Who this book is for

If you are a .NET developer, senior developer, software architect, or DevOps engineer who wants to explore the pros and cons, intricacies, and overall implementation of microservice architecture, then this book is for you. You'll also get plenty of useful insights if you're seeking to expand your knowledge of different design patterns and supporting technologies.

Basic experience with application and API development with .NET Core (2+) and C# will help you get the most out of this book.

Table of contents

  1. Microservices Design Patterns in .NET
  2. Contributors
  3. About the author
  4. About the reviewer
  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: Understanding Microservices and Design Patterns
  7. Chapter 1: Introduction to Microservices – the Big Picture
    1. A deep dive into microservices and its key elements
      1. Building a monolith
      2. Building microservices
    2. Assessing the business need for microservices
      1. Scalability
      2. Availability
      3. Development speed
      4. Improved data storage
      5. Monitoring
      6. Deployment
    3. Determining the feasibility of implementing microservices
      1. Microservices and .NET Core
    4. Summary
  8. Chapter 2: Working with the Aggregator Pattern
    1. Technical requirements
    2. Exploring DDD and its significance
      1. Exploring the pros and cons of DDD
      2. DDD and microservices
    3. The purpose and use of aggregate patterns
      1. Aggregates and aggregate roots
      2. Relationships in aggregates
      3. Handling relationships that span aggregates
    4. Aggregates versus entities
      1. Entities and why we need them
      2. Practical uses of entities in code
      3. A rich domain model versus an anemic domain model
    5. Understanding and using value objects
    6. Summary
  9. Chapter 3: Synchronous Communication between Microservices
    1. Technical requirements
    2. Use cases for synchronous communication
      1. Challenges of microservice communication
    3. Implementing synchronous communication
      1. Implementing HTTP synchronous communication
      2. Implementing gRPC synchronous communication
      3. HTTP versus gRPC communication
    4. Disadvantages of synchronous communication between microservices
    5. Summary
  10. Chapter 4: Asynchronous Communication between Microservices
    1. Technical requirements
    2. Functioning with asynchronous communication
      1. HTTP asynchronous communication
    3. Understanding Pub-Sub communication
      1. Understanding message queues
      2. Understanding message bus systems
      3. Understanding eventual consistency
    4. Configuring a message bus (RabbitMQ or Azure Service Bus)
      1. Implementing RabbitMQ in an ASP.NET Core web API
      2. Implementing Azure Service Bus in an ASP.NET Core API
    5. Disadvantages of asynchronous communication between microservices
    6. Summary
  11. Chapter 5: Working with the CQRS Pattern
    1. Technical requirements
    2. Why use CQRS for microservices development?
      1. Benefits of the CQRS pattern
      2. Disadvantages of the CQRS pattern
      3. Using the Mediator pattern with CQRS in .NET
    3. Implementing a command
      1. Creating a command model
      2. Creating a command handler
      3. Invoking a command
    4. Implementing a query
      1. Creating a query model
      2. Creating a query handler
      3. Invoking a query
    5. Summary
  12. Chapter 6: Applying Event Sourcing Patterns
    1. Technical requirements
    2. What are events?
      1. Key attributes of events
    3. What can event sourcing patterns do for me?
      1. Pros of event sourcing
      2. Cons of event sourcing
    4. What are domain events?
      1. Domain events and event sourcing
      2. Exploring domain events in our application
    5. Creating an event store
      1. How to store events
      2. Implementing event sourcing using a relational database
      3. Implementing event sourcing using a non-relational database
      4. Reading state with CQRS
    6. Summary
  13. Part 2: Database and Storage Design Patterns
  14. Chapter 7: Handling Data for Each Microservice with the Database per Service Pattern
    1. Technical requirements
    2. How to make use of the Database-Per-Service pattern
      1. A single database technology per service
      2. Using different database technologies per service
      3. Disadvantages of this pattern
    3. Developing a database
      1. Relational databases
      2. Non-relational databases
      3. Choosing a database technology
      4. Choosing an ORM
      5. Choosing a database development technique
    4. Implementing the repository pattern
    5. Summary
  15. Chapter 8: Implement Transactions across Microservices Using the Saga Pattern
    1. Exploring the Saga pattern
      1. Issues and considerations
    2. Understanding and implementing choreography
      1. Rolling back on failure
      2. Pros and cons
    3. Understanding and implementing orchestration
      1. Rolling back on failure
      2. Pros and cons
    4. Summary
  16. Part 3: Resiliency, Security, and Infrastructure Patterns
  17. Chapter 9: Building Resilient Microservices
    1. Technical requirements
    2. The importance of service resiliency
      1. Possible failure scenarios and how to handle them
    3. Implementing resiliency with caching and message brokers
      1. Using a message broker
      2. Using a caching layer
      3. Using Redis Cache
    4. Implementing retry and circuit breaker policies
      1. Retry policy with Polly
      2. Circuit breaker policy with Polly
    5. Summary
  18. Chapter 10: Performing Health Checks on Your Services
    1. Technical requirements
    2. Health checks and microservices
      1. The liveness health check
      2. Readiness health checks
    3. Implementing ASP.NET Core health checks
      1. Adding liveness health checks
      2. Adding readiness health checks
    4. Configuring health probes in orchestrators
    5. Summary
  19. Chapter 11: Implementing the API and BFF Gateway Patterns
    1. Technical requirements
    2. What is the API gateway pattern?
      1. Advantages of an API gateway
      2. Disadvantages of an API gateway
    3. Implementing the API gateway pattern
      1. Thick API gateways
      2. Implementing an API gateway using Azure API Management
      3. Implementing an API gateway using Ocelot
      4. Adding cache management
      5. Adding rate limiting
      6. Adding response aggregation
    4. Backend for Frontend pattern
      1. BFF pattern using Ocelot
    5. Summary
  20. Chapter 12: Securing Microservices with Bearer Tokens
    1. Technical requirements
    2. Bearer tokens for securing communications
      1. Understanding bearer tokens
    3. Implementing bearer token security
      1. Securing API with bearer tokens
      2. Generating and issuing bearer tokens
    4. Using IdentityServer4 to secure microservices
      1. Configuring IdentityServer
      2. Securing an API using IdentityServer
      3. Securing the Ocelot API gateway with IdentityServer
      4. Additional API security considerations
    5. Summary
  21. Chapter 13: Microservice Container Hosting
    1. Technical requirements
    2. Using containers in microservices development
      1. What can containers do for me?
      2. Understanding Docker
      3. Understanding container images
      4. Pros and cons of containers
    3. Authoring a Dockerfile
      1. Launching a containerized application
      2. Using native .NET container support
    4. Understanding docker-compose and images
      1. Adding docker-compose to a project
    5. Publishing containers to a container registry
      1. Public versus private container registries
      2. Creating and uploading custom images
    6. Summary
  22. Chapter 14: Implementing Centralized Logging for Microservices
    1. Technical requirements
    2. Logging and its importance
      1. Choosing what to log
      2. Using the .NET logging API
      3. Adding Serilog
    3. Log aggregation and its uses
      1. Integrating with Seq
    4. Distributed log tracing
      1. Enhanced logging for distributed tracing
    5. Summary
  23. Chapter 15: Wrapping It All Up
    1. Aggregator pattern
    2. Synchronous and asynchronous communication
    3. CQRS
    4. Event sourcing patterns
    5. Database per service pattern
      1. Relational databases
      2. Non-relational databases
      3. One database for all services
      4. One database per service
    6. Using the saga pattern across services
    7. Resilient microservices
    8. Importance of health checks
    9. API Gateways and backend for frontend
    10. Bearer token security
    11. Containers and microservices
    12. Centralized logging
    13. Summary
  24. Index
    1. Why subscribe?
  25. 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: Microservices Design Patterns in .NET
  • Author(s): Trevoir Williams
  • Release date: January 2023
  • Publisher(s): Packt Publishing
  • ISBN: 9781804610305