O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Microservice Patterns and Best Practices

Book Description

Explore the concepts and tools you need to discover the world of microservices with various design patterns

About This Book

  • Get to grips with the microservice architecture and build enterprise-ready microservice applications
  • Learn design patterns and the best practices while building a microservice application
  • Obtain hands-on techniques and tools to create high-performing microservices resilient to possible fails

Who This Book Is For

This book is for architects and senior developers who would like implement microservice design patterns in their enterprise application development. The book assumes some prior programming knowledge.

What You Will Learn

  • How to break monolithic application into microservices
  • Implement caching strategies, CQRS and event sourcing, and circuit breaker patterns
  • Incorporate different microservice design patterns, such as shared data, aggregator, proxy, and chained
  • Utilize consolidate testing patterns such as integration, signature, and monkey tests
  • Secure microservices with JWT, API gateway, and single sign on
  • Deploy microservices with continuous integration or delivery, Blue-Green deployment

In Detail

Microservices are a hot trend in the development world right now. Many enterprises have adopted this approach to achieve agility and the continuous delivery of applications to gain a competitive advantage. This book will take you through different design patterns at different stages of the microservice application development along with their best practices.

Microservice Patterns and Best Practices starts with the learning of microservices key concepts and showing how to make the right choices while designing microservices. You will then move onto internal microservices application patterns, such as caching strategy, asynchronism, CQRS and event sourcing, circuit breaker, and bulkheads. As you progress, you'll learn the design patterns of microservices.

The book will guide you on where to use the perfect design pattern at the application development stage and how to break monolithic application into microservices. You will also be taken through the best practices and patterns involved while testing, securing, and deploying your microservice application. At the end of the book, you will easily be able to create interoperable microservices, which are testable and prepared for optimum performance.

Style and approach

Comprehensive guide that uses architectural patterns with the best choices involved in application development

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Get in touch
      1. Reviews
  2. Understanding the Microservices Concepts
    1. Knowing the application
      1. Domain-driven design
      2. Single responsibility principle
      3. Explicitly published interface
    2. Independently deploy, upgrade, scale, and replace
      1. Independent deployment
      2. Upgrade
      3. Scale
        1. The x-axis
        2. The y-axis
        3. The z-axis
        4. Replace
    3. Light weight communication
      1. Synchronous
      2. Asynchronous
    4. Potentially heterogeneous/polyglot
    5. Documentation of communication
    6. Endpoints for web applications
    7. Endpoints for mobile applications
    8. Caching at the client level
    9. Throttling for your client
    10. Identifying anemic domains
    11. Identifying the fat domains
    12. Identifying microservice domains for the business
    13. From domain to entity
    14. Summary
  3. The Microservice Tools
    1. Programming languages
      1. Proficiency
      2. Performance
      3. Development of practicality
      4. Ecosystem
      5. Scalability cost
      6. Making choices for our application
        1. Java
        2. C#
        3. Python
        4. JavaScript
        5. Go
    2. Microservice frameworks
      1. Python
      2. Go
        1. Logs
        2. Handlers
        3. Middleware
        4. Tests
        5. Package manager
        6. Golang ORMs
    3. Binary communication – direct communication between services
      1. Understanding the aspect
        1. Tools for synchronous communication
        2. MessagePack
        3. gRPC
        4. Apache Avro
        5. Apache Thrift
      2. Direct communication alerts
    4. Message broker – Async communication between services
      1. ActiveMQ
      2. RabbitMQ
      3. Kafka
    5. Caching tools
      1. Memcached
      2. Redis
    6. Fail alert tools
      1. Performance
      2. Build
      3. Components
      4. Implementation gaps
    7. The databases
    8. Locale proof performance
      1. Apache Benchmark
      2. WRK
      3. Locust
    9. Summary
  4. Internal Patterns
    1. Developing the structure
      1. Database
      2. Programming language and tools
      3. Project structure
        1. The models.go file
        2. The app.go file
        3. The main.go file
    2. Caching strategies
      1. Applying cache
      2. Caching first
      3. Enqueuing tasks
      4. Asynchronism and workers
    3. CQRS – query strategy
      1. What is CQRS?
      2. Understanding CQRS
      3. Advantages and disvantages of implementing CQRS
    4. Event sourcing – data integrity
      1. State mutation
        1. Understanding event sourcing
    5. Summary
  5. Microservice Ecosystem
    1. Separating containers
      1. Layered services architecture
      2. Separating UsersService
        1. Creating Dockerfile
        2. Using the containers
    2. Storage distribution
      1. Depreciating data
      2. Regionalizing data
    3. Bulkheads – using the ecosystem against failures
      1. Designing for redundancy
      2. Partitioning by criticality
      3. Designing with isolation
      4. Fail fast
    4. Circuit breaker
    5. Summary
  6. Shared Data Microservice Design Pattern
    1. Understanding the pattern
    2. Breaking a monolithic application into microservices
      1. Defining priorities
      2. Setting deadlines
      3. Defining the domain
      4. Making experiments
      5. Defining standards
      6. Creating a prototype
      7. Sending to production
      8. Developing new microservices
        1. Writing the microservice configuration file
        2. Creating our model
        3. Exposing the microservice data
        4. Preparing the app to run
        5. Creating the Dockerfile
        6. Dependencies with requirements.txt
    3. Data orchestration
    4. Consolidating responses
    5. Microservice communication
    6. Storage sharing anti-pattern
    7. Best practices
    8. Testing
    9. Pros and cons of the shared data pattern
    10. Summary
  7. Aggregator Microservice Design Pattern
    1. Understanding the pattern
    2. Applying CQRS and event sourcing
      1. Separating the database
        1. Writing the CommandStack container
        2. Creating the news databases
        3. Writing the QueryStack container
      2. Refactoring the microservices
        1. Selecting our requirements
        2. Configuring the framework
        3. Configuring the container
        4. Writing the models
        5. Creating the service
        6. Preparing the database containers to work together
    3. Microservice communication
      1. Building the orchestrator
        1. Preparing the microservice container
        2. Writing the dependencies
        3. Writing the configuration file
        4. Writing the server access
        5. Creating the orchestration controller
      2. Applying the message broker
        1. Making the containers work together
        2. Updating the proxy/load balancer
    4. Pattern scalability
    5. Bottleneck anti-pattern
    6. Best practices
    7. Applying tests
      1. Functional test
        1. Writing the functional test
      2. Integration test
        1. Writing the integration tests
    8. Pros and cons of aggregator design pattern
      1. Pros of aggregator design pattern
      2. Cons of aggregator design pattern
    9. Summary
  8. Proxy Microservice Design Pattern
    1. The proxy approach
      1. Dumb proxy
      2. Smart proxy
      3. Understanding our proxy
    2. Proxy strategy to orchestrator
    3. Microservice communication
    4. Pattern scalability
    5. Best practices
      1. Purest pattern
      2. Looking at the bottleneck
      3. Caching in the proxy
      4. Simple response
    6. Pros and cons of proxy design pattern
    7. Summary
  9. Chained Microservice Design Pattern
    1. Understanding the pattern
    2. Data orchestration and response consolidation
    3. Microservice communication
    4. Pattern scalability
    5. Big Ball of Mud anti-pattern
    6. Best practices
      1. Purest microservices
      2. Requesting consistent data
      3. Understanding chain in depth
      4. Paying attention to the communication layer
    7. Understanding the pros and cons of chained design pattern
    8. Summary
  10. Branch Microservice Design Pattern
    1. Understanding the pattern
    2. Data orchestration and response consolidation
    3. Microservice communication
    4. Pattern scalability
    5. Best practices
      1. Domain definition
      2. Respect the rules
      3. Attention to physical components
      4. Keep it simple
    6. Pros and cons of the branch design pattern
    7. Summary
  11. Asynchronous Messaging Microservice
    1. Understanding the pattern
    2. Domain definition – RecommendationService
    3. Data definition – RecommendationService
    4. Coding the microservice
    5. Microservice communication
      1. Applying the message broker and queues
      2. Preparing the pub/sub structure
    6. Pattern scalability
    7. Process sequence anti-pattern
    8. Best practices
      1. Application definition
      2. Don’t try to create responses
      3. Keep it simple
    9. Pros and cons of the asynchronous messaging design pattern
    10. Summary
  12. Microservices Working Together
    1. Understanding the current application status
      1. The public facing layer
      2. The internal layer
      3. Understanding general tools
    2. Communication layer and accreditation between services
      1. Understanding the data contract between services
      2. Applying binary communication 
    3. Pattern distribution
    4. Fail strategies
    5. API integration
    6. Summary
  13. Testing Microservices
    1. Unit tests
    2. Preparing the containers for the integration test
    3. Integration tests
    4. End-to-end tests
    5. Release pipelines
    6. Signature tests
    7. Monkey tests
    8. Chaos Monkey
    9. Summary
  14. Monitoring Security and Deployment
    1. Monitoring microservices
      1. Monitoring a single service
      2. Monitoring multiple services
      3. Looking at the logs
      4. Learning from the errors in the application
      5. The metrics – Understanding the numbers
    2. Security
      1. Understanding JWT
      2. Single Sign-On
      3. Security of data
      4. Defense for malicious requests – Identifying attacks
      5. Explaining the interceptor
      6. Web container
      7. The API gateway
    3. Deployment
      1. Continuous integration/continuous delivery/continuous deploy
      2. The blue/green deployment pattern and Canary releases
      3. Multiple service instances per host
      4. Service instance per host
        1. Service instance per VM
        2. Service instance per container
    4. Summary
  15. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think