Schema Design Patterns and Antipatterns / Apply Schema Design Patterns

4:39
As we work on our bookstore app, we come across a scenario where we need to perform some computations often, like calculating the average star rating for a book. Computing results every time they're requested would mean running the same operations repeatedly for every single read. Frequent computations can decrease performance. The answer to this problem is the computed pattern. This pattern allows you to run expensive operations when the data changes and store the results for quick access. There are different kinds of computations covered in this pattern. In this video, we'll focus on mathematical and roll-up operations. For mathematical operations, the goal is to precompute the result when the data is written instead of running the calculation every time we need it. Let's see how this works in the bookstore app. When a user writes a new review for a book, a new document is added to the reviews collection. This review document contains a product SKU, user ID, and a star rating, in addition to other fields. This star rating must be used to update the average rating for that book. If we have to calculate the book's reading every time a user visits the book's page, it would result in repeated computations even if the rating hasn't changed. Remember the goal of our application is to have a billion reviews in three years, so performing these computations could get costly as our app grows. We can eliminate repeated operations by calculating the average stars only when a new review comes in. That way when a user visits the product page, the star reading comes from one document rather than a computation across many documents in the collection. This is much more efficient. To easily calculate the average we must store a couple of new fields in the book document. We'll add a sub document labeled "rating" to our existing book document. The rating sub document contains a field called review count, which holds the total number of reviews. Next, we store the average rating for the book. Now when we get a new review, we calculate the new average by first multiplying the previous average with the previous review count. Now we add the new review rating to that value to get a new total number of review stars. Finally, we divide the new total number of review stars by the new total number of reviews. Now let's examine a second type of computation, roll-up operations, which involve merging data together. Roll-up operations allow us to view data as a whole. They often involve grouping categories from smaller units into larger ones, such as hourly, daily, monthly or yearly reports. Let's look at an example from the bookstore app. Our internal stakeholders have requested information for each product type. They want to know the total number of books by product type and the average number of authors within a given product type. Instead of computing this information every time it is requested, we can store this information in a new document and update it at specific intervals. The internal stakeholders have also requested that this information be updated daily. In this example, we record the result at a specific interval, daily, rather than with every write. This decision depends on the use case for your application and the business requirements. If you have a write heavy workload and can tolerate some stale data, this is a good option. Now let's implement the roll-up operation using an aggregation pipeline. We need to roll up a summary document for each product type that includes the number of books and average number of authors. First, we use the group stage to classify the documents by product type. Next, we use the $sum operator to count the books for each type in increments of 1. We then use the $size operator to get the number of authors and the authors arrays. And lastly, we use the $average operator to find the average number of authors for each product type. We get the following output, which tells us how many books we have and the average number of authors for audio books, ebooks and printed books. To summarize, the computed pattern precomputes data before the read operations. This lets you run operations when the data changes and store results in a document for quick access. In this video, we covered mathematical and roll-up operations.