Microservices Up & Running A Step-by-Step Guide to Building a Microservices Architecture Ronnie Mitra & Irakli Nadareishvili Microservices: Up and Running A Step-by-Step Guide to Building a Microservices Architecture Ronnie Mitra and Irakli Nadareishvili BBeeiijjiinngg BBoossttoonn FFaarrnnhhaamm SSeebbaassttooppooll TTookkyyoo Microservices: Up and Running by Ronnie Mitra and Irakli Nadareishvili Copyright © 2021 Mitra Pandey Consulting, Ltd. and Irakli Nadareishvili. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Acquisitions Editor: Melissa Duffield Indexer: nSight, Inc. Development Editor: Melissa Potter Interior Designer: David Futato Production Editor: Deborah Baker Cover Designer: Karen Montgomery Copyeditor: Charles Roumeliotis Illustrator: Kate Dullea Proofreader: Piper Editorial, LLC December 2020: First Edition Revision History for the First Edition 2020-11-25: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781492075455 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Microservices: Up and Running, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the authors, and do not represent the publisher’s views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-492-07545-5 [LSI] To every person who took the time to chronicle and share their experiences. And to Kairav, who didn’t help me write this dedication. —Ronnie Mitra To Lucas, who was born shortly after we started working on this book and whose smiles gave me the strength to complete this book in the middle of a global pandemic; to my wife Ana, for her support; and to my amazing students at Temple University, in Phila‐ delphia, who kindly “test drove” early versions of a lot of the content in this book. —Irakli Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi 1. Toward a Microservices Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Are Microservices? 2 Reducing Coordination Costs 4 The Coordination Cost Problem 4 The Hard Parts 6 Learning by Doing 7 The “Up and Running” Microservices Model 8 Decisions, Decisions… 10 Writing a Lightweight Architectural Decision Record 11 Summary 13 2. Designing a Microservices Operating Model. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Why Teams and People Matter 16 Team Size 17 Team Skills 18 Interteam Coordination 19 Introducing Team Topologies 21 Team Types 21 Interaction Modes 23 Designing a Microservices Team Topology 24 Establish a System Design Team 24 Building a Microservices Team Template 26 Platform Teams 29 Enabling and Complicated-Subsystem Teams 31 Consumer Teams 32 v Summary 34 3. Designing Microservices: The SEED(S) Process. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Introducing the Seven Essential Evolutions of Design for Services: The SEED(S) Method 36 Identifying Actors 37 Example Actors in Our Sample Project 39 Identifying Jobs That Actors Have to Do 39 Using Job Story Format to Capture JTBDs 41 Example JTBDs in Our Sample Project 42 Discovering Interaction Patterns with Sequence Diagrams 43 Deriving Actions and Queries from JTBDs 45 Example Queries and Actions for Our Sample Project 47 Describing Each Query and Action as a Specification with an Open Standard 48 Example OAS for an Action in Our Sample Project 49 Getting Feedback on the API Specification 53 Implementing Microservices 53 Microservices Versus APIs 54 Summary 56 4. Rightsizing Your Microservices: Finding Service Boundaries. . . . . . . . . . . . . . . . . . . . . . . 57 Why Boundaries Matter, When They Matter, and How to Find Them 57 Domain-Driven Design and Microservice Boundaries 59 Context Mapping 62 Synchronous Versus Asynchronous Integrations 65 A DDD Aggregate 66 Introduction to Event Storming 66 The Event-Storming Process 68 Introducing the Universal Sizing Formula 73 The Universal Sizing Formula 74 Summary 74 5. Dealing with the Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 Independent Deployability and Data Sharing 75 Microservices Embed Their Data 77 Embedding Data Should Not Lead to an Explosion in the Number of Database Clusters 78 Data Embedding and the Data Delegate Pattern 79 Using Data Duplication to Solve for Independence 81 Distributed Transactions and Surviving Failures 82 Event Sourcing and CQRS 85 vi | Table of Contents Event Sourcing 85 Improving Performance with Rolling Snapshots 91 Event Store 92 Command Query Responsibility Segregation 93 Event Sourcing and CQRS Beyond Microservices 94 Summary 95 6. Building an Infrastructure Pipeline. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 DevOps Principles and Practices 98 Immutable Infrastructure 99 Infrastructure as Code 100 Continuous Integration and Continuous Delivery 102 Setting Up the IaC Environment 104 Set Up GitHub 104 Install Terraform 105 Configuring Amazon Web Services 106 Setting Up an AWS Operations Account 106 Configure the AWS CLI 110 Setting Up AWS Permissions 112 Creating an S3 Backend for Terraform 115 Building an IaC Pipeline 116 Creating the Sandbox Repository 117 Understanding Terraform 119 Writing the Code for the Sandbox Environment 120 Building the Pipeline 123 Testing the Pipeline 133 Summary 135 7. Building a Microservices Infrastructure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Infrastructure Components 137 The Network 138 The Kubernetes Service 139 The GitOps Deployment Server 140 Implementing the Infrastructure 142 Installing kubectl 142 Setting Up the Module Repositories 142 The Network Module 145 The Kubernetes Module 160 Setting Up Argo CD 171 Testing the Environment 175 Cleaning Up the Infrastructure 177 Table of Contents | vii Summary 178 8. Developer Workspace. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Coding Standards and the Developer’s Setup 180 10 Workspace Guidelines for a Superior Developer Experience 181 Setting Up a Containerized Environment Locally 187 Installing Multipass 188 Entering the Container and Mapping Folders 189 Installing Docker 190 Testing Docker 191 Advanced Local Docker Usage: Installing Cassandra 192 Installing Kubernetes 193 Summary 195 9. Developing Microservices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Designing Microservice Endpoints 197 Flights Microservice 201 Reservations Microservice 201 Designing an OpenAPI Specification 202 Implementing the Data for a Microservice 208 Redis for the Reservations Data Model 209 MySQL Data Model for the Flights Microservice 211 Implementing Code for a Microservice 212 The Code Behind the Flights Microservice 213 Health Checks 218 Introducing a Second Microservice to the Project 220 Hooking Services Up with an Umbrella Project 226 Summary 229 10. Releasing Microservices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Setting Up the Staging Environment 232 The Ingress Module 233 The Database Module 234 Forking the Staging Infrastructure Project 234 Configuring the Staging Workflow 235 Editing the Staging Infrastructure Code 237 Shipping the Flight Information Container 241 Introducing Docker Hub 241 Configuring Docker Hub 242 Configuring the Pipeline 242 Deploying the Flights Service Container 246 viii | Table of Contents