Exploring CQRS and Event Sourcing with Windows Azure

376 333 0
Exploring CQRS and Event Sourcing with Windows Azure

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

The Command Query Responsibility Segregation (CQRS) pattern and event sourcing (ES) are cur rently generating a great deal of interest from developers and architects who are designing and build ing largescale, distributed systems. There are conference sessions, blogs, articles, and frameworks all dedicated to the CQRS pattern and to event sourcing, and all explaining how they can help you to improve the maintainability, testability, scalability, and flexibility of your systems. However, like anything new, it takes some time before a pattern, approach, or methodology is fully understood and consistently defined by the community and has useful, practical guidance to help you to apply or implement it. This guidance is designed to help you get started with the CQRS pattern and event sourcing. It is not intended to be the guide to the CQRS pattern and event sourcing, but a guide that describes the experiences of a development team in implementing the CQRS pattern and event sourcing in a real world application. The development team did not work in isolation; they actively sought input from industry experts and from a wider group of advisors to ensure that the guidance is both detailed and practical. The CQRS pattern and event sourcing are not mere simplistic solutions to the problems associ ated with largescale, distributed systems. By providing you with both a working application and written guidance, we expect you’ll be well prepared to embark on your own CQRS journey.

Exploring CQRS and Event Sourcing A journey into high scalability, availability, and maintainability with Windows Azure Exploring CQRS and Event Sourcing A journey into high scalability, availability, and maintainability with Windows Azure Dominic Betts Julián Domínguez Grigori Melnik Fernando Simonazzi Mani Subramanian 978-1-62114-016-0 This document is provided “as-is”. Information and views expressed in this document, including URL and other Internet Web site references, may change without notice. Some examples depicted herein are provided for illustration only and are fictitious. No real association or connection is intended or should be inferred. This document does not provide you with any legal rights to any intellectual property in any Microsoft product. You may copy and use this document for your internal, reference purposes. You may modify this document for your internal, reference purposes © 2012 Microsoft. All rights reserved. Microsoft, MSDN, SQL Azure, SQL Server, Visual Studio, Windows, and Windows Azure are trademarks of the Microsoft group of companies. All other trademarks are property of their respective owners.  What other readers are saying about this guide xvii Foreword by Greg Young xxi Preface xxiii Why we created this guidance now xxiii How is this guidance structured? xxiii A CQRS journey xxiv CQRS reference xxv Tales from the trenches xxv A CQRS journey xxv CQRS reference xxvi Tales from the trenches xxvi Selecting the domain for the RI xxvi Arrow legend xxvii Where to go for more information xxviii The Crew xxix Journey 1: Our Domain: Conference Management System 1 The Contoso Corporation 1 Who is coming with us on the journey? 2 The Contoso Conference Management System 3 Overview of the system 3 Selling seats for a conference 4 Creating a conference 4 Nonfunctional requirements 4 Scalability 4 Flexibility 5 Beginning the journey 5 More information 5 Contents vi Journey 2: Decomposing the Domain 7 Definitions used in this chapter 7 Bounded contexts in the conference management system 8 Bounded contexts not included 9 The context map for the Contoso Conference Management System 10 Why did we choose these bounded contexts? 11 More information 11 Journey 3: Orders and Registrations Bounded Context 13 A description of the bounded context 13 Working definitions for this chapter 14 Domain definitions (ubiquitous language) 15 Requirements for creating orders 17 Architecture 18 Patterns and concepts 18 Validation 23 Transaction boundaries 24 Concurrency 25 Aggregates and aggregate roots 25 Implementation details 25 High-level architecture 26 1. Querying the read model 27 2. Issuing commands 28 3. Handling commands 28 4. Initiating business logic in the domain 29 5. Persisting the changes 29 6. Polling the read model 29 Inside the write model 31 Aggregates 31 Aggregates and process managers 34 Infrastructure 40 Using the Windows Azure Service Bus 42 Delivering a command to a single recipient 44 Why have separate CommandBus and EventBus classes? 48 How scalable is this approach? 48 How robust is this approach? 48 What is the granularity of a topic and a subscription? 48 How are commands and events serialized? 49 Impact on testing 49 Summary 52 More information 52 vii Journey 4: Extending and Enhancing the Orders and Registrations Bounded Context 53 Changes to the bounded context 53 Working definitions for this chapter 53 User stories 54 Implement a login using a record locator 54 Tell the registrant how much time remains to complete an order 55 Enable a registrant to create an order that includes multiple seat types 55 Architecture 55 Patterns and concepts 56 Record locators 56 Querying the read side 56 Storing denormalized views in a database 57 Making information about partially fulfilled orders available to the read side 60 CQRS command validation 61 The countdown timer and the read model 62 Implementation details 62 The order access code record locator 63 The countdown timer 64 Using ASP.NET MVC validation for commands 66 Pushing changes to the read side 69 Querying the read side 72 Refactoring the SeatsAvailability aggregate 73 The AddSeats method 74 Impact on testing 74 Acceptance tests and the domain expert 74 Defining acceptance tests using SpecFlow features 74 Making the tests executable 76 Using tests to help developers understand message flows 81 A journey into code comprehension: A tale of pain, relief, and learning 83 Testing is important 83 Domain tests 84 The other side of the coin 86 Summary 90 More information 90 Journey 5: Preparing for the V1 Release 91 The Contoso Conference Management System V1 release 91 Working definitions for this chapter 91 User stories 92 Ubiquitous language definitions 92 Conference Management bounded context user stories 92 Ordering and Registration bounded context user stories 92 viii Architecture 93 Conference Management bounded context 97 Patterns and concepts 97 Event sourcing 97 Identifying aggregates 98 Task-based UI 99 CRUD 101 Integration between bounded contexts 101 Pushing changes from the Conference Management bounded context 102 Pushing changes to the Conference Management bounded context 104 Choosing when to update the read-side data 105 Distributed transactions and event sourcing 105 Autonomy versus authority 105 Favoring autonomy 106 Favoring authority 106 Choosing between autonomy and authority 106 Approaches to implementing the read side 107 Eventual consistency 107 Implementation details 108 The Conference Management bounded context 108 Integration with the Orders and Registration bounded context 108 The Payments bounded context 109 Integration with online payment services, eventual consistency, and command validation 111 Event sourcing 113 Raising events when the state of an aggregate changes 113 Persisting events to the event store 117 Replaying events to rebuild state 118 Issues with the simple event store implementation 120 Windows Azure table storage-based event store 120 Calculating totals 122 Impact on testing 123 Timing issues 123 Involving the domain expert 123 Summary 124 More information 124 Journey 6: Versioning Our System 125 Working definitions for this chapter 125 User stories 126 No down time upgrade 126 Display remaining seat quantities 126 Handle zero-cost seats 126 Architecture 126 ix Patterns and concepts 127 Handling changes to events definitions 128 Mapping/filtering event messages in the infrastructure 128 Handling multiple message versions in the aggregates 128 Honoring message idempotency 128 Avoid processing events multiple times 129 Persisting integration events 131 Message ordering 133 Implementation details 133 Adding support for zero-cost orders 134 Changes to the RegistrationProcessManager class 134 Changes to the UI 134 Data migration 136 Displaying remaining seats in the UI 138 Adding information about remaining seat quantities to the read model 138 Modifying the UI to display remaining seat quantities 140 Data migration 140 De-duplicating command messages 141 Guaranteeing message ordering 142 Persisting events from the Conference Management bounded context 146 Adding additional metadata to the messages 146 Capturing and persisting messages to the message log 146 Data migration 148 Migrating from V1 to V2 150 Generating past log messages for the Conference Management bounded context 151 Migrating the event sourcing events 151 Rebuilding the read models 151 Impact on testing 151 SpecFlow revisited 152 Discovering a bug during the migration 155 Summary 155 More information 155 Journey 7: Adding Resilience and Optimizing Performance 157 Working definitions for this chapter 157 Architecture 158 Adding resilience 159 Making the system resilient when an event is reprocessed 161 Ensuring that commands are always sent 161 Optimizing performance 162 UI flow before optimization 162 Optimizing the UI 163 UI optimization 1 164 UI optimization 2 165 x Optimizing the infrastructure 165 Sending and receiving commands and events asynchronously 165 Optimizing command processing 166 Using snapshots with event sourcing 166 Publishing events in parallel 167 Filtering messages in subscriptions 167 Creating a dedicated receiver for the SeatsAvailability aggregate 167 Caching conference information 167 Partitioning the Service Bus 168 Other optimizations 168 Further changes that would improve performance 169 Further changes that would enhance scalability 171 No down-time migration 172 Rebuilding the read models 173 Implementation details 174 Hardening the RegistrationProcessManager class 174 Detecting out-of-order SeatsReserved events 175 Detecting duplicate OrderPlaced events 178 Creating a pseudo transaction when the RegistrationProcessManager class saves its state and sends a command 178 Optimizing the UI flow 181 Receiving, completing, and sending messages asynchronously 186 Receiving messages asynchronously 186 Completing messages asynchronously 186 Sending messages asynchronously 186 Handling commands synchronously and in-process 186 Implementing snapshots with the memento pattern 189 Publishing events in parallel 191 Filtering messages in subscriptions 192 Creating a dedicated SessionSubscriptionReceiver instance for the SeatsAvailability aggregate 193 Caching read-model data 194 Using multiple topics to partition the service bus 195 Other optimizing and hardening changes 196 Sequential GUIDs 196 Asynchronous ASP.NET MVC controllers. 198 Using prefetch with Windows Azure Service Bus 198 Accepting multiple sessions in parallel 199 Adding an optimistic concurrency check 199 Adding a time-to-live value to the MakeSeatReservation command 199 Reducing the number of round-trips to the database 199 [...]... Commands 253 Example code 253 Command handlers 254 Commands and optimistic concurrency 256 Events and event handlers 256 Events and intent 256 How to model intent 258 Events 259 Sample Code 259 Event handlers 260 Sample code 260 Embracing eventual consistency 261 Eventual consistency and CQRS 263 Optimizing the read-side 266 Optimizing the write side 267 Concurrency and aggregates 267 Messaging and CQRS. .. CQRS and ES Deep Dive Introduction Read models and write models Commands and data transfer objects Domain-driven design (DDD) and aggregates Data and normalization Events and event sourcing Eventual consistency Defining aggregates in the domain model Aggregates and object-relational mapping layers Aggregates and event sourcing 247 247 247 247 248 248 248 248 249 249 250 xiii Commands and command handlers... Moving to the cloud 234 When should I avoid CQRS? 234 Summary 234 More information 234 Reference 3: Introducing Event Sourcing What is event sourcing? Comparing using an ORM layer with event sourcing Why should I use event sourcing? Event sourcing concerns CQRS/ ES Standalone event sourcing Event stores Basic requirements Underlying storage Performance, scalability, and consistency More information 235 236... 269 Event versioning 269 Redundant events 270 New event types 270 Changing existing event definitions 270 Task-based UIs 271 Taking advantage of Windows Azure 272 Scaling out using multiple role instances 273 Implementing an event store using Windows Azure table storage 273 Persisting events 274 Retrieving events 275 Publishing events 276 Implementing a messaging infrastructure using the Windows Azure. .. 328 Scenario 2 Local Web Server, Windows Azure Service Bus, Table Storage Event Store 329 Scenario 3 Compute Emulator, SQL Event Bus, SQL Event Store 329 Scenario 4 Compute Emulator, Windows Azure Service Bus, Table Storage Event Store 329 Scenario 5 Windows Azure, Windows Azure Service Bus, Table Storage Event Store 329 Running the Tests 329 Running the Unit and Integration Tests 329 Running the Acceptance... CQRS pattern and event sourcing, but a guide that describes the experiences of a development team in implementing the CQRS pattern and event sourcing in a realworld application The development team did not work in isolation; they actively sought input from industry experts and from a wider group of advisors to ensure that the guidance is both detailed and practical The CQRS pattern and event sourcing are... now The Command Query Responsibility Segregation (CQRS) pattern and event sourcing (ES) are currently generating a great deal of interest from developers and architects who are designing and building large-scale, distributed systems There are conference sessions, blogs, articles, and frameworks all dedicated to the CQRS pattern and to event sourcing, and all explaining how they can help you to improve... to organize and manage their own conferences and events This chapter describes, at a high-level, some of the functional and non-functional requirements of the new system, and why Contoso wants to implement parts of it using the Command Query Responsibility Segregation (CQRS) pattern and event sourcing (ES) As with any company considering this process, there are many issues to consider and challenges... journey xxvi CQRS reference • Chapter 1, CQRS in Context,” provides some context for CQRS, especially in relation to the domain-driven design approach • Chapter 2, “Introducing the Command Query Responsibility Segregation Pattern,” provides a conceptual overview of the CQRS pattern • Chapter 3, “Introducing Event Sourcing, ” provides a conceptual overview of event sourcing • Chapter 4, “A CQRS and ES Deep... fashion and helping developers to wade through the ambiguities of CQRS The real world experiences captured within the journey project will be invaluable to folks looking at applying CQRS within their application development” —Glenn Block, Senior Program Manager, Microsoft, Windows Azure SDK for Node.js, Organizer at ALT.NET Seattle Chapter The p&p team’s dedication and hard work go hand-in-hand with the . with CQRS, I would definitely point them to this guide. —Matias Woloski, CTO, Auth10 LLC The CQRS Journey guide is an excellent resource for developers who want to begin developing a CQRS. are saying about this guide xvii Foreword by Greg Young xxi Preface xxiii Why we created this guidance now xxiii How is this guidance structured? xxiii A CQRS journey xxiv CQRS reference xxv Tales. trenches xxv A CQRS journey xxv CQRS reference xxvi Tales from the trenches xxvi Selecting the domain for the RI xxvi Arrow legend xxvii Where to go for more information xxviii The Crew xxix Journey

Ngày đăng: 22/07/2014, 10:11

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan