Course Name :- Reactive Architecture: CQRS & Event Sourcing
What is CQRS/ES ?
Question 1 : CQRS stands for:
- Control Query Responsibility Segregation
- Command Query Read Separation
- Control Query Read Segregation
- Command Query Responsibility Segregation
Question 2: ES stands for:
- Entity Segregation
- Event Sourcing
- Entity Sourcing
- Event Separation
Question 3: True or False: You must always use CQRS and Event Sourcing together. They can’t be used independently.
- True
- False
Question 4 : You should consider using CQRS/ES if:
- It will give you a competitive advantage.
- You need auditability.
- You can replace that portion of the system with an off the shelf product.
- Your system experiences relatively low traffic.
- You need additional resilience or scalability.
Chapter Name : – Event Sourcing
Question 1 : State Based Persistence means:
· The events that led up to the current state are persisted, rather than the state itself.
· Each update adds a new state to the previous one, retaining both.
· Each update replaces the previous state with a new state.
· Data is geographically ordered according to what state the data was created in.
Question 2: True or False: Domain requirements are fluid, rather than fixed. They can change over time depending on many factors.
· True
· False
Question 2: When using State Based Persistence, if a change needs to be made to the domain we can usually apply that change:
· To all entries in the system, both new and old.
· Only to old entries in the system.
· To any new entries into the system.
· Nowhere. State Based Persistence presents us from changing the domain.
Question 3 : If the persisted state in our system is incorrect due to an error in our code, we can usually:
· Fix the code, and the incorrect state.
· Fix the incorrect state, but not the code.
· Fix the code, but not the incorrect state.
· Fix nothing.
Question 4: True or False: State Based Persistence captures the journey, rather than the destination.
· True
· False
Event Sourcing Review
Question 1: When using State Based Persistence, in order to recover from errors in state, or evolve our domain we can:
· Use shadow copies of the database to build a full history of the state.
· Leverage backup copies of our data, and restore them as necessary.
· Run scripts to retroactively modify existing records in the database.
· Persist an audit log alongside our state so we understand where the state came from.
Question 2: When an audit log is stored alongside the state, we must be wary of which of the following:
· If the audit log and the state disagree, we have two potential sources of truth.
· The audit log and the state must be updated in a transactional way.
· A bug in the code may cause the state and the audit log to become out of sync.
· All of the above
· None of the above
Question 3: Which of the following is true for Event Sourcing:
· Persists both state and intent.
· The Event Log can be reconstructed from the persisted state.
· Captures intent.
· Eliminates the persistence of state.
· Uses only the audit log.
Question 4 : In an Event Sourced system, state is recovered by:
· Keeping the state in a separate table(s) in the database.
· Using the state defined in the newest event.
· Using the state defined in the oldest event.
· Replaying events and performing the same state updates as when the event originally occured.
Question 5: True or False: If you replay events in an Event Sourced system, you must also replay the side effects.
· True
· False
Question 6 : A snapshot should be created:
· When we need to delete old events in order to free up space.
· After every event.
· When the time to restore from the log is becoming excessive.
· All of the above.
· None of the above.
Evolving the Model Review
Question 1 : Which of the following statements is FALSE:
· The integrity of the log is critical to the success of the system.
· The log in an Event Sourced system should be append only.
· Events represent future changes to the system.
· All of the above.
· None of the above.
Question 2 :If you need to evolve your data model, you should do so by:
· You can not evolve your data model.
· Running a script to edit the existing events.
· Creating a snapshot and deleting the old events.
· Creating a new version of the event.
Question 3: True or False: Java Serialization is an excellent choice for serializing your events.
· True
· False
Question 4 : Versioning of Events can be handled in the Lightbend ecosystem using:
· Lagom Persistent Entities
· Akka Serialization
· Akka Persistence Query
· Akka Event Adapters
Command Sourcing Review
Question1: When using Command Sourcing, the order we do things is:
· Persist the Event, Execute the Command
· Execute the Command, Persist the Command
· Execute the Command, Persist the Event
· Persist the Command, Execute the Command
Question 2: Which of the following are potential challenges with Command Sourcing:
· Commands must be idempotent.
· Commands may get lost.
· If a Command is invalid, we are unable to inform the user.
· Invalid Commands can block the queue.
· Invalid Commands may corrupt the state of the system.
Question 3 : True or False: Akka Persistence can be used to implement Command Sourcing.
· True
· False
Case Study: Introduction
Question 1: Using this existing table structure, can we determine how often an order is cancelled either before, or after it has been placed?
· Yes
· No
Question 2: Which of the following events would the Reactive BBQ Order system need to track in order to determine how often an order is cancelled either before, or after it has been placed?
· OrderCreated(locationId, serverId, tableId)
· ItemAddedToOrder(orderId, itemId, quantity)
· OrderPlaced(orderId)
· OrderClosed(orderId)
· OrderCancelled(orderId)
Note: Make sure you select all of the correct options—there may be more than one!
Question 3 : Which of the following problems might they encounter while trying to determine if people are ordering drinks after tasting their Reactive Wings or ReactiveRibs?
· There is no way to tell when an item was served.
· There is no way to tell when an order was placed.
· There is no way to determine the order that items were added to the menu.
· There is no way to tell what kind of item was added (Was it a glass of milk? Or was it an order of fries?)
· There is no way to tell how many guests are at the table.
Question 4 : If we simply added timestamps to the Order Items Table, would that solve our problem?
· Yes
· No
Question 5 : What events would we need to track in order to determine if a drink is being added after our wings or ribs are served?
· ItemAddedToOrder(orderId, itemId, quantity)
· OrderPlaced(orderId)
· ItemPrepared(orderId, itemId)
· ItemServed(orderId, itemId)
· ItemTasted(orderId, itemId)
Note: Make sure you select all of the correct options—there may be more than one!
Chapter Name :- CQRS
Read Models Vs Write Models Review
Question 1: Which of the following is a potential problem in an Event Sourced system:
· Commands that span multiple aggregate roots become difficult.
· Queries that access a single aggregate root become difficult.
· Commands that are executed by a single aggregate root become difficult.
· Queries that span multiple aggregate roots become difficult.
Question 2: True or False: The problem of conflicting models in our system is unique to Event Sourced systems.
· True
· False
Question 3: Fundamentally, the problem that we have encountered with these conflicting models is:
· Our writes (Commands) and reads (Queries) shouldn’t live in the same microservice.
· In a system of sufficient size, the idea of using a domain model rapidly breaks down.
· Models that are focused around aggregate roots ignore the possibility of transactions that cross aggregate root boundaries.
· A model that is optimized for writes (Commands) may not be optimized for reads (Queries).
CQRS Review
Question 1: True or False: The requirements for read and write in our system should be more or less the same.
· True
· False
Question 2: Most applications tend to have:
· Balanced reads and writes.
· More writes than reads.
· More reads than writes.
· Read, or read not. There is no write.
Question 3: In a CQRS based system, the Commands are handled by the:
· Read Model
· Write Model
· unanswered
· Queries are handled by the:
· Read Model
· Write Model
Question 4 : True or False: Event Sourcing is critical for CQRS systems. You can’t use CQRS without Event Sourcing.
· True
· False
Question 5 : When building a system with both CQRS and Event Sourcing, the components we normally include are:
· Read Model
· Write Model
· Event Store
· Event Bus/Message Bus (eg. Kafka, RabbitMQetc)
· Denormalized Store
Question 6 : True or False: An ideal read model is often setup so the data can be read directly from a single table in the database, as is, with minimal additional processing/formatting.
· True
· False
Question 7 : Polyglot Persistence means:
· Creating transactions that span multiple databases.
· Storing data in large blobs, rather than in a columnar format.
· Separating your reads and writes in the database.
· Using different databases within your system, depending on the use case.
Question 8 : Which of the following is a disadvantage of using CQRS:
· Optimization of the read model is restricted by the needs of the write model.
· Optimization of the write model is restricted by the needs of the read model.
· The initial choice of database will impact all areas of the system (both reads and writes).
· All of the above.
· None of the above.
Question 9 : A key benefit to combining CQRS and Event Sourcing is:
· The presence of the event log allows all new projections to be retroactive.
· The read and write models are decoupled, allowing them to evolve independently.
· The read and write models can be optimized individually, depending on their needs.
· All of the above.
· None of the above.
Question 10 : In the Lightbend Ecosystem, CQRS can be implemented using:
· Akka Cluster Sharding
· Akka Distributed Data
· Lagom Read Side Processors
· Akka Persistence Query
· Lagom Message Brokers
Case Study: Reactive BBQ Read Models
Question 1: Assuming we had access to the above events, what table structure could we use to answer the question “how often are orders cancelled after they are placed”?
- Table: Orders – Columns: orderId, locationId, serverId, tableId, status (one row per order)
- Table: OrdersStatuses – Columns: orderId, status, timestamp (one row per change in status)
- Table: OrderStatusTransitions – Columns: orderId, oldStatus, newStatus, timestamp (one row per change in status)
- Table: CancelledOrders – Columns: orderId, timestamp (one row per orderId)
- Table: OrdersCancelledAfterPlaced – Columns: orderId, timestamp (one row per orderId)
Question 2: Which of these table structures would provide the most efficient query?
- Table: Orders – Columns: orderId, locationId, serverId, tableId, status (one row per order)
- Table: OrdersStatuses – Columns: orderId, status, timestamp (one row per change in status)
- Table: OrderStatusTransitions – Columns: orderId, oldStatus, newStatus, timestamp (one row per change in status)
- Table: CancelledOrders – Columns: orderId, timestamp (one row per orderId)
Question 3 : It was determined that in order for them to track this, they would need access to the following events:
- ItemAddedToOrder(orderId, itemId, quantity)
- ItemServed(orderId, itemId)
Question 4: Assuming we had access to the above events, what table structure could we use to answer the question “how often do customers order milk after being served Reactive Wings or Ribs”?
- Table: OrderItems – Columns: orderId, itemId, quantity (one row per orderId)
- Table: OrdersWithMilk – Columns: orderId, timestamp (one row per orderId)
- Table: MilkOrderedAfterRibsServed – Columns: orderId, timestamp (one row per orderId)
- Table: MilkOrderedAfterWingsOrRibsServed – Columns: orderId, timestamp (one row per orderId)
- Table: DrinkOrderedAfterWingsOrRibsServed – Columns: orderId, drinkId, timestamp (one row per drink per order)
Question 5: Which of these table structures would provide the most efficient query?
- Table: OrderItems – Columns: orderId, itemId, quantity (one row per orderId)
- Table: OrdersWithMilk – Columns: orderId, timestamp (one row per orderId)
- Table: MilkOrderedAfterRibsServed – Columns: orderId, timestamp (one row per orderId)
- Table: MilkOrderedAfterWingsOrRibsServed – Columns: orderId, timestamp (one row per orderId)
- Table: DrinkOrderedAfterWingsOrRibsServed – Columns: orderId, drinkId, timestamp (one row per drink per order)
Chapter Name :- Consistency , Availability and Scalability with CQRS
Fine Grained Microservices Review
Question 1: True or False: In a CQRS/ES based system, the read and write models should always live in the same microservice.
· True
· False
Question 2: True or False: Microservices are cheap and easy. We shouldn’t have to worry about adding new ones.
· True
· False
Consistency, Availability & Scalability with CQRS Review
Question 1: Simple CQRS (without event sourcing) promises what kind of consistency?
· Weak Consistency
· Eventual Consistency
· Strong Consistency
· Whatever consistency is provided by your underlying database
Question 2: CQRS with Event Sourcing promises what kind of consistency?
· Whatever consistency is provided by your underlying database
· Strong Consistency
· Eventual Consistency
· Different consistency options for both your read, and write models.
Question 3: Strong Consistency is most important for:
· Read Models
· Write Models
· Both
· Neither
Question 4 : Pure reads are:
· Sometimes eventually consistent.
· Always strongly consistent.
· Never eventually consistent.
· Always eventually consistent.
Question 5: Which of the following options could be used to scale a read model?
· Caching
· Separate microservices for individual read models
· Duplicating read models in the same, or different databases
· All of the above
· None of the above
Question 6 : True or False: The write side of CQRS systems require Strong Consistency, and therefore are inherently not scalable.
· True
· False
Question 7 : True or False: Strongly Consistent write models require sacrifices in availability.
· True
· False
Question 8 : Options to provide availability in a CQRS system include:
· Disabling writes, but continuing to allow reads if the write model fails.
· Disabling reads, but continuing to allow writes if the read model fails.
· Using cached data for the read model when current data is unavailable.
· Disabling reads and writes and providing an error message instead.
· Sharding the writes so that only a subset of users are affected by an outage.
Note: Make sure you select all of the correct options—there may be more than one!
Cost of CQRS Review
Question 1: True or False: CQRS takes concepts that would commonly be isolated in a single large class or model, and spreads them across multiple smaller classes or models.
- True
- False
Question 2: CQRS is often considered to be more complex because:
- The large complex domain models make it cumbersome.
- It requires additional infrastructure in the form of a message bus.
- It is eventually consistent by design.
- Programming is hard.
Question 3: True or False: The Eventual Consistency introduced by CQRS based systems was actually always there. CQRS just makes it more explicit.
- True
- False
Question 4 : Some concrete costs associated with using CQRS and Event Sourcing include:
- More classes and objects.
- Multiple versions of events must be kept.
- Eventually Consistent write models.
- Limited ability to draw insight from the data.
- More storage space required.
Question 5 : When data between the read and write model is out of sync, we can:
- Write a script that extracts the state from the read model and updates the events in the write model.
- Delete the write model, and rebuild it from the read model.
- Restore from a backup.
- Delete the read model, and rebuild it from the write model.
Case Study: Reactive BBQ Scenarios
Question 1: Some possible reasons this problem exists include:
- The database (tables, indexes etc) was optimized for day to day operation, not for the reports, so the reports are overly expensive.
- The reports create contention in the database in the form of read locks.
- The reports create contention on the database hardware in the form of memory/cpu/disk usage.
- All of the above
- None of the above
Question 2: In the new system, they could avoid this problem by:
- Getting a better database.
- Allocating more hardware to the database.
- Leveraging the event log to build a read model(s) for the reports.
- All of the above
- None of the above
Question 3 : True or False: The data on the screen must be strongly consistent to avoid potential errors in the order.
- True
- False
Question 4 : In order to ensure that the information on the screen is always available we could:
- Create a read model which updates the screen, and a separate model which prints out a ticket. If the screen fails we can fallback to the ticket.
- Replicate the data to multiple locations by consuming the order event log multiple times.
- Isolate failures by separating screen data into it’s own independent microservice.
- All of the above
- None of the above
Question 5 : Which parts of the existing system would have already experienced Eventual Consistency?
- The host looks at a paper printout of upcoming reservations.
- The host creates a new reservation after a customer phones in.
- The customer creates a new reservation on the website.
- The customer makes a change to an existing reservation on the website.
- The host looks at an on screen display of current reservations.
Question 6: Which parts of the new system will need strong consistency (given that they have expressed a desire for consistency)?
- The host looks at a paper printout of upcoming reservations.
- The host creates a new reservation after a customer phones in.
- The customer creates a new reservation on the website.
- The customer makes a change to an existing reservation on the website.
- The host looks at an on screen display of current reservations.
Reactive Architecture: CQRS & Event Sourcing cognitive class final Exam Answers:-
Question 1: The game server is leveraging a(n) _______ sourced model.
- CQRS Auto is a automobile trading company formed in the 1980s by four partners (Christa, Quentin, Rhonda, and Samuel). They have since grown to be one of the largest auto traders in the nation.
- The company allows users to post an advertisement for a vehicle that they want to sell. They can create the ad, attach pictures, add descriptions, set the price etc.
- Once their ad has been created, it will be asynchronously added to the CQRS Auto website. On the website, other users can filter the vehicles, compare them, and look at the details.
- When creating an ad, users would be interfacing with the _______ model, or the _______ side of the CQRS system.
- When viewing ads, users would be interfacing with the _______ model, or the _______ side of the CQRS system.
Question 2 : Saga Electronics is an online retailer focusing on Electronics.
- A critical part of their business is the payment service. They use a third party payment processor to handle all transactions, however, it is still critical that they keep internal records which are then reconciled with the records from the payment processor.
- In order to do this, each time a payment is made, they enter the details of that payment into a log. This log can then be reconciled against any logs that come back from the payment processor. If a discrepancy is found then they will have to perform an audit to determine why there is a difference.
- The integrity of this log is critical. For that reason, they require that the log be append only. If a correction needs to be made, then they will make that correction by adding an additional log entry, rather than editing an existing one.
Question 3: Saga Electronics could leverage _______ to maintain their log.
- Spectrum Messages has created an online messaging platform.
- One of the features of their platform is the ability to broadcast messages to a large number of users. A user can create a message that will be shared with a group, rather than an individual.
- When a group message is sent, there is no guarantee that every receiver will be online at the time. They may have their phone turned off, the app may be disabled, or they may not have internet access.
- The sender can create their messages using _______ Consistency.
Question 4 : The receivers will receive the messages using _______ Consistency.
- Consistent Fitness has built a wearable fitness tracker. Users push data from their fitness tracker up to the company servers. This data is then stored in a raw form.
- One of the goals of the company is to create a series of rich insights about their users. These insights are presented to the users through a dashboard. They allow users to learn things about their own fitness habits such as how often they exercise in a week, how their heart rate changes when they exercise etc.
- They have a series of asynchronous processes which will consume the raw data, and aggregate it in various ways. This allows them to take the raw user data, and create new insights. And because they always have access to the raw data, these insights can be retroactive.