{"id":1079,"date":"2019-05-10T08:38:16","date_gmt":"2019-05-10T15:38:16","guid":{"rendered":"https:\/\/www.kenwalger.com\/blog\/?p=1079"},"modified":"2019-04-26T10:18:34","modified_gmt":"2019-04-26T17:18:34","slug":"building-with-patterns-a-summary","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/building-with-patterns-a-summary\/","title":{"rendered":"Building with Patterns: A Summary"},"content":{"rendered":"\n<p>As we wrap up the <em>Building with Patterns<\/em> series, it\u2019s a good opportunity to recap the problems the patterns that have been covered solve and highlight some of the benefits and trade-offs each pattern has. The most frequent question that is asked about schema design patterns, is \u201cI\u2019m designing an application to do X, how do I model the data?\u201d As we hope you have discovered over the course of this blog series, there are a lot of things to take into consideration to answer that. However, we\u2019ve included a <em>Sample Use Case<\/em> chart that we\u2019ve found helpful to at least provide some initial guidance on data modeling patterns for generic use cases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sample Use Cases<\/h2>\n\n\n\n<p>The chart below is a guideline for what we\u2019ve found after years of experience working with our customers of what schema design patterns are used in a variety of applications. This is not a \u201cset in stone\u201d set of rules about which design pattern can be used for a particular type of application. Ensure you look at the ones that are frequently used in your use case. However, don&#8217;t discard the other ones, they may still apply. How you design your application\u2019s data schema is very dependent on your data access patterns.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/webassets.mongodb.com\/_com_assets\/cms\/patternsmatrix-xv1kqjlrpb.png?w=840&#038;ssl=1\" alt=\"Use Cases vs Patterns Matrix\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Design Pattern Summaries<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Approximation<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-approximation-pattern\">Approximation Pattern<\/a> is useful when expensive calculations are frequently done and when the precision of those calculations is not the highest priority.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Fewer writes to the database.<\/li><li>Maintain statistically valid numbers.<\/li><\/ul>\n\n\n\n<h6 class=\"wp-block-heading\">Cons<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Exact numbers aren\u2019t being represented.<\/li><li>Implementation must be done in the application.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Attribute<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-attribute-pattern\">Attribute Pattern<\/a> is useful for problems that are based around having big documents with many similar fields but there is a subset of fields that share common characteristics and we want to sort or query on that subset of fields. When the fields we need to sort on are only found in a small subset of documents. Or when both of those conditions are met within the documents.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li> Fewer indexes are needed.<\/li><li>Queries become simpler to write and are generally faster.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Bucket<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-bucket-pattern\">Bucket Pattern<\/a> is a great solution for when needing to manage streaming data, such as time-series, real-time analytics, or Internet of Things (IoT) applications. \n<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Reduces the overall number of documents in a collection.<\/li><li>Improves index performance.<\/li><li>Can simplify data access by leveraging pre-aggregation.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Computed<\/h3>\n\n\n\n<p>When there are very read intensive data access patterns and that data needs to be repeatedly computed by the application, the <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-computed-pattern\">Computed Pattern<\/a> is a great option to explore.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Reduction in CPU workload for frequent computations.<\/li><li>Queries become simpler to write and are generally faster.<\/li><\/ul>\n\n\n\n<h6 class=\"wp-block-heading\">Cons<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>It may be difficult to identify the need for this pattern.<\/li><li>Applying or overusing the pattern should be avoided unless needed.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Document Versioning<\/h3>\n\n\n\n<p>When you are faced with the need to maintain previous versions of documents in MongoDB, the <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-document-versioning-pattern\">Document Versioning<\/a> pattern is a possible solution.\n<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Easy to implement, even on existing systems.<\/li><li>No performance impact on queries on the latest revision.<\/li><\/ul>\n\n\n\n<h6 class=\"wp-block-heading\">Cons<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Doubles the number of writes.<\/li><li>Queries need to target the correct collection.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Extended Reference<\/h3>\n\n\n\n<p>You will find the <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-extended-reference-pattern\">Extended Reference<\/a> pattern most useful when your application is experiencing lots of JOIN operations to bring together frequently accessed data.\n<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Improves performance when there are a lot of JOIN operations.<\/li><li>Faster reads and a reduction in the overall number of JOINs.<\/li><\/ul>\n\n\n\n<h6 class=\"wp-block-heading\">Cons<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Data duplication.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Outlier<\/h3>\n\n\n\n<p>Do you find that there are a few queries or documents that don\u2019t fit into the rest of your typical data patterns? Are these exceptions driving your application solution? If so, the <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-outlier-pattern\">Outlier Pattern<\/a> is a wonderful solution to this situation.\n<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Prevents a few documents or queries from determining an application\u2019s solution.<\/li><li>Queries are tailored for \u201ctypical\u201d use cases, but outliers are still addressed.<\/li><\/ul>\n\n\n\n<h6 class=\"wp-block-heading\">Cons<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Often tailored for specific queries, therefore ad hoc queries may not perform well.<\/li><li>Much of this pattern is done with application code.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Pre-allocation<\/h3>\n\n\n\n<p>When you know your document structure and your application simply needs to fill it with data, the <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-preallocation-pattern\">Pre-Allocation Pattern<\/a> is the right choice.    \n<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Design simplification when the document structure is known in advance.<\/li><\/ul>\n\n\n\n<h6 class=\"wp-block-heading\">Cons<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Simplicity versus performance.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Polymorphic<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-polymorphic-pattern\">Polymorphic Pattern<\/a> is the solution when there are a variety of documents that have more similarities than differences and the documents need to be kept in a single collection.\n<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Easy to implement.<\/li><li>Queries can run across a single collection.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Schema Versioning<\/h3>\n\n\n\n<p>Just about every application can benefit from the <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-schema-versioning-pattern\">Schema Versioning Pattern<\/a> as changes to the data schema frequently occur in an application\u2019s lifetime. This pattern allows for previous and current versions of documents to exist side by side in a collection.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>No downtime needed.<\/li><li>Control of schema migration.<\/li><li>Reduced future technical debt.<\/li><\/ul>\n\n\n\n<h6 class=\"wp-block-heading\">Cons<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Might need two indexes for the same field during migration.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Subset<\/h3>\n\n\n\n<p>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-subset-pattern\">\nSubset Pattern<\/a> solves the problem of having the working set exceed the capacity of RAM due to large documents that have much of the data in the document not being used by the application.    \n<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Reduction in the overall size of the <a href=\"https:\/\/docs.mongodb.com\/manual\/reference\/glossary\/#term-working-set\">working set<\/a>.<\/li><li>Shorter disk access time for the most frequently used data.<\/li><\/ul>\n\n\n\n<h6 class=\"wp-block-heading\">Cons<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>We must manage the subset.<\/li><li>Pulling in additional data requires additional trips to the database.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Tree<\/h3>\n\n\n\n<p>When data is of a hierarchical structure and is frequently queried, the <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-tree-pattern\">Tree Pattern<\/a> is the design pattern to implement.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Pros<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li>Increased performance by avoiding multiple JOIN operations.<\/li><\/ul>\n\n\n\n<h6 class=\"wp-block-heading\">Cons<\/h6>\n\n\n\n<ul class=\"wp-block-list\"><li> Updates to the graph need to be managed in the application.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>As we hope you have seen in this series, the MongoDB document model provides a lot of flexibility in how you model data. That flexibility is incredibly powerful but that power needs to be harnessed in terms of your application\u2019s data access patterns. Remember that schema design in MongoDB has a tremendous impact on the performance of your application. We\u2019ve found that performance issues can frequently be traced to poor schema design. <\/p>\n\n\n\n<p>Keep in mind that to further enhance the power of the document model, these schema design patterns can be used together, when and if it makes sense. For example, Schema Versioning can be used in conjunction with any of the other patterns as your application evolves. With the twelve schema design patterns that have been covered, you have the tools and knowledge needed to harness the power of the document model\u2019s flexibility.<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1079&#038;t=Building%20with%20Patterns%3A%20A%20Summary&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1079&#038;p&#091;images&#093;&#091;0&#093;=https%3A%2F%2Fwebassets.mongodb.com%2F_com_assets%2Fcms%2Fpatternsmatrix-xv1kqjlrpb.png&#038;p&#091;title&#093;=Building%20with%20Patterns%3A%20A%20Summary\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1079&#038;text=Hey%20check%20this%20out\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-reddit nolightbox\" data-provider=\"reddit\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Reddit\" href=\"https:\/\/www.reddit.com\/submit?url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1079&#038;title=Building%20with%20Patterns%3A%20A%20Summary\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"reddit\" title=\"Share on Reddit\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/reddit.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1079&#038;title=Building%20with%20Patterns%3A%20A%20Summary\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=Building%20with%20Patterns%3A%20A%20Summary&#038;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1079\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.kenwalger.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/mail.png?resize=48%2C48&#038;ssl=1\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>As we wrap up the Building with Patterns series, it\u2019s a good opportunity to recap the problems the patterns that have been covered solve and highlight some of the benefits and trade-offs each pattern has. The most frequent question that is asked about schema design patterns, is \u201cI\u2019m designing an application to do X, how &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/building-with-patterns-a-summary\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Building with Patterns: A Summary&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pmpro_default_level":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[785],"yst_prominent_words":[871,123,99,869,870,784,1503,1174,538,1548,507,836,1625,1626,782,783,1624,1614,1456,1627],"class_list":["post-1079","post","type-post","status-publish","format-standard","hentry","category-mongodb","tag-schema-design","pmpro-has-access"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-hp","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/1079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/comments?post=1079"}],"version-history":[{"count":2,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/1079\/revisions"}],"predecessor-version":[{"id":1081,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/1079\/revisions\/1081"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=1079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=1079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=1079"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=1079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}