ebook img

Building Event-Driven Microservices - Leveraging Organizational Data at Scale (Java) PDF

324 Pages·2020·10.342 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Building Event-Driven Microservices - Leveraging Organizational Data at Scale (Java)

Building Event-Driven Microservices Leveraging Organizational Data at Scale Adam Bellemare Building Event-Driven Microservices Leveraging Organizational Data at Scale Adam Bellemare BBeeiijjiinngg BBoossttoonn FFaarrnnhhaamm SSeebbaassttooppooll TTookkyyoo Building Event-Driven Microservices by Adam Bellemare Copyright © 2020 Adam Bellemare. 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: Potomac Indexing, LLC Development Editor: Corbin Collins Interior Designer: David Futato Production Editor: Christopher Faucher Cover Designer: Karen Montgomery Copyeditor: Rachel Monaghan Illustrator: O’Reilly Media, Inc. Proofreader: Kim Wimpsett August 2020: First Edition Revision History for the First Edition 2020-07-02: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781492057895 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Building Event-Driven Microservices, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the author, and do not represent the publisher’s views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author 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-05789-5 [LSI] Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Why Event-Driven Microservices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Are Event-Driven Microservices? 2 Introduction to Domain-Driven Design and Bounded Contexts 3 Leveraging Domain Models and Bounded Contexts 4 Aligning Bounded Contexts with Business Requirements 5 Communication Structures 6 Business Communication Structures 7 Implementation Communication Structures 7 Data Communication Structures 8 Conway’s Law and Communication Structures 9 Communication Structures in Traditional Computing 10 Option 1: Make a New Service 10 Option 2: Add It to the Existing Service 11 Pros and Cons of Each Option 11 The Team Scenario, Continued 13 Conflicting Pressures 13 Event-Driven Communication Structures 13 Events Are the Basis of Communication 14 Event Streams Provide the Single Source of Truth 14 Consumers Perform Their Own Modeling and Querying 14 Data Communication Is Improved Across the Organization 15 Accessible Data Supports Business Communication Changes 15 Asynchronous Event-Driven Microservices 15 Example Team Using Event-Driven Microservices 16 Synchronous Microservices 17 iii Drawbacks of Synchronous Microservices 17 Benefits of Synchronous Microservices 19 Summary 20 2. Event-Driven Microservice Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Building Topologies 21 Microservice Topology 21 Business Topology 22 The Contents of an Event 23 The Structure of an Event 23 Unkeyed Event 24 Entity Event 24 Keyed Event 24 Materializing State from Entity Events 25 Event Data Definitions and Schemas 27 Microservice Single Writer Principle 28 Powering Microservices with the Event Broker 28 Event Storage and Serving 29 Additional Factors to Consider 30 Event Brokers Versus Message Brokers 31 Consuming from the Immutable Log 32 Providing a Single Source of Truth 34 Managing Microservices at Scale 34 Putting Microservices into Containers 35 Putting Microservices into Virtual Machines 35 Managing Containers and Virtual Machines 35 Paying the Microservice Tax 36 Summary 37 3. Communication and Data Contracts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Event-Driven Data Contracts 39 Using Explicit Schemas as Contracts 40 Schema Definition Comments 41 Full-Featured Schema Evolution 41 Code Generator Support 42 Breaking Schema Changes 43 Selecting an Event Format 45 Designing Events 46 Tell the Truth, the Whole Truth, and Nothing but the Truth 46 Use a Singular Event Definition per Stream 46 Use the Narrowest Data Types 47 iv | Table of Contents Keep Events Single-Purpose 47 Minimize the Size of Events 51 Involve Prospective Consumers in the Event Design 51 Avoid Events as Semaphores or Signals 51 Summary 52 4. Integrating Event-Driven Architectures with Existing Systems. . . . . . . . . . . . . . . . . . . . 53 What Is Data Liberation? 54 Compromises for Data Liberation 55 Converting Liberated Data to Events 57 Data Liberation Patterns 57 Data Liberation Frameworks 58 Liberating Data by Query 58 Bulk Loading 59 Incremental Timestamp Loading 59 Autoincrementing ID Loading 59 Custom Querying 59 Incremental Updating 59 Benefits of Query-Based Updating 60 Drawbacks of Query-Based Updating 61 Liberating Data Using Change-Data Capture Logs 61 Benefits of Using Data Store Logs 63 Drawbacks of Using Data Base Logs 63 Liberating Data Using Outbox Tables 64 Performance Considerations 65 Isolating Internal Data Models 65 Ensuring Schema Compatibility 67 Capturing Change-Data Using Triggers 70 Making Data Definition Changes to Data Sets Under Capture 74 Handling After-the-Fact Data Definition Changes for the Query and CDC Log Patterns 75 Handling Data Definition Changes for Change-Data Table Capture Patterns 75 Sinking Event Data to Data Stores 75 The Impacts of Sinking and Sourcing on a Business 76 Summary 78 5. Event-Driven Processing Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Composing Stateless Topologies 80 Transformations 80 Branching and Merging Streams 81 Repartitioning Event Streams 81 Table of Contents | v Example: Repartitioning an Event Stream 82 Copartitioning Event Streams 83 Example: Copartitioning an Event Stream 83 Assigning Partitions to a Consumer Instance 84 Assigning Partitions with the Partition Assignor 84 Assigning Copartitioned Partitions 85 Partition Assignment Strategies 85 Recovering from Stateless Processing Instance Failures 87 Summary 87 6. Deterministic Stream Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Determinism with Event-Driven Workflows 90 Timestamps 90 Synchronizing Distributed Timestamps 92 Processing with Timestamped Events 92 Event Scheduling and Deterministic Processing 93 Custom Event Schedulers 94 Processing Based on Event Time, Processing Time, and Ingestion Time 94 Timestamp Extraction by the Consumer 95 Request-Response Calls to External Systems 95 Watermarks 95 Watermarks in Parallel Processing 96 Stream Time 97 Stream Time in Parallel Processing 98 Out-of-Order and Late-Arriving Events 99 Late Events with Watermarks and Stream Time 101 Causes and Impacts of Out-of-Order Events 101 Time-Sensitive Functions and Windowing 103 Handling Late Events 105 Reprocessing Versus Processing in Near-Real Time 106 Intermittent Failures and Late Events 107 Producer/Event Broker Connectivity Issues 108 Summary and Further Reading 109 7. Stateful Streaming. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 State Stores and Materializing State from an Event Stream 111 Recording State to a Changelog Event Stream 112 Materializing State to an Internal State Store 113 Materializing Global State 114 Advantages of Using Internal State 114 Disadvantages of Using Internal State 116 vi | Table of Contents Scaling and Recovery of Internal State 116 Materializing State to an External State Store 120 Advantages of External State 120 Drawbacks of External State 121 Scaling and Recovery with External State Stores 122 Rebuilding Versus Migrating State Stores 124 Rebuilding 124 Migrating 124 Transactions and Effectively Once Processing 125 Example: Stock Accounting Service 126 Effectively Once Processing with Client-Broker Transactions 127 Effectively Once Processing Without Client-Broker Transactions 128 Summary 133 8. Building Workflows with Microservices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 The Choreography Pattern 136 A Simple Event-Driven Choreography Example 137 Creating and Modifying a Choreographed Workflow 138 Monitoring a Choreographed Workflow 139 The Orchestration Pattern 139 A Simple Event-Driven Orchestration Example 141 A Simple Direct-Call Orchestration Example 142 Comparing Direct-Call and Event-Driven Orchestration 142 Creating and Modifying an Orchestration Workflow 143 Monitoring the Orchestration Workflow 144 Distributed Transactions 144 Choreographed Transactions: The Saga Pattern 145 Orchestrated Transactions 146 Compensation Workflows 149 Summary 149 9. Microservices Using Function-as-a-Service. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Designing Function-Based Solutions as Microservices 151 Ensure Strict Membership to a Bounded Context 151 Commit Offsets Only After Processing Has Completed 152 Less Is More 153 Choosing a FaaS Provider 153 Building Microservices Out of Functions 153 Cold Start and Warm Starts 155 Starting Functions with Triggers 155 Triggering Based on New Events: The Event-Stream Listener 155 Table of Contents | vii Triggering Based on Consumer Group Lag 157 Triggering on a Schedule 158 Triggering Using Webhooks 159 Triggering on Resource Events 159 Performing Business Work with Functions 159 Maintaining State 160 Functions Calling Other Functions 160 Event-Driven Communication Pattern 161 Direct-Call Pattern 162 Termination and Shutdown 165 Tuning Your Functions 165 Allocating Sufficient Resources 165 Batch Event-Processing Parameters 166 Scaling Your FaaS Solutions 166 Summary 167 10. Basic Producer and Consumer Microservices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Where Do BPCs Work Well? 170 Integration with Existing and Legacy Systems 170 Stateful Business Logic That Isn’t Reliant Upon Event Order 171 When the Data Layer Does Much of the Work 172 Independent Scaling of the Processing and Data Layer 173 Hybrid BPC Applications with External Stream Processing 174 Example: Using an External Stream-Processing Framework to Join Event Streams 174 Summary 176 11. Heavyweight Framework Microservices. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 A Brief History of Heavyweight Frameworks 178 The Inner Workings of Heavyweight Frameworks 179 Benefits and Limitations 181 Cluster Setup Options and Execution Modes 183 Use a Hosted Service 183 Build Your Own Full Cluster 183 Create Clusters with CMS Integration 184 Application Submission Modes 186 Driver Mode 186 Cluster Mode 186 Handling State and Using Checkpoints 186 Scaling Applications and Handling Event Stream Partitions 188 Scaling an Application While It Is Running 189 viii | Table of Contents

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.