{"id":1067,"date":"2019-04-11T06:56:56","date_gmt":"2019-04-11T13:56:56","guid":{"rendered":"https:\/\/www.kenwalger.com\/blog\/?p=1067"},"modified":"2019-04-11T08:23:58","modified_gmt":"2019-04-11T15:23:58","slug":"building-with-patterns-the-preallocation-pattern","status":"publish","type":"post","link":"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/building-with-patterns-the-preallocation-pattern\/","title":{"rendered":"Building with Patterns: The Preallocation Pattern"},"content":{"rendered":"\n<p>One of the great things about MongoDB is the <a href=\"https:\/\/docs.mongodb.com\/manual\/core\/document\/\">document<\/a> data model. It provides for a lot of flexibility not only in schema design but in the development cycle as well. Not knowing what fields will be required down the road is easily handled with MongoDB documents. However, there are times when the structure is known and being able to fill or grow the structure makes the design much simpler. This is where we can use the Preallocation Pattern.<\/p>\n\n\n\n<p>Memory allocation is often done in blocks to avoid performance issues. In the earlier days of MongoDB (prior to MongoDB version 3.2), when it used the <a href=\"https:\/\/docs.mongodb.com\/manual\/core\/mmapv1\/\">MMAPv1<\/a> storage engine, a common optimization was to allocate in advance the memory needed for the future size of a constantly growing document. Growing documents in MMAPv1 needed to be relocated at a fairly expensive cost by the server. With its lock-free and rewrite on update algorithms, <a href=\"https:\/\/docs.mongodb.com\/manual\/core\/wiredtiger\/\">WiredTiger<\/a> does not require this same treatment. <\/p>\n\n\n\n<p>With the deprecation of MMAPv1 in MongoDB 4.0, the Preallocation Pattern appeared to lose some of its luster and necessity. However, there are still use cases for the Preallocation Pattern with WiredTiger. As with the other patterns we&#8217;ve discussed in the <em>Building with Patterns<\/em> series, there are a few application considerations to think about.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The Preallocation Pattern<\/h4>\n\n\n\n<p>This pattern simply dictates to create an initial empty structure to be filled later. It may sound trivial, however, you will need to balance the desired outcome in simplification versus the additional resources that the solution may consume. Bigger documents will make for a larger working set resulting in more RAM to contain this working set.<\/p>\n\n\n\n<p>If the code of the application is much easier to write and maintain if it uses a structure that is not completed filled, it may easily outweigh the cost of the RAM. Let&#8217;s say there is a need to represent a theater room as a 2-dimensional array where each seat has a &#8220;row&#8221; and &#8220;number&#8221;, for example, the seat &#8220;C7&#8221;. Some rows may have fewer seats, however finding the seat &#8220;B3&#8221; is faster and cleaner in a 2-dimensional array, than having a complicated formula to find a seat in a one-dimensional array that has only cells for the existing seats. Being able to identify accessible seating is also easier as a separate array can be created for those seats.<\/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\/preallocation-1a-rqdo5jz3vk.png?w=840&#038;ssl=1\" alt=\"Seating Map\"\/><\/figure>\n\n\n\n<p><em>Two dimensional representation of venue, valid seats available in green. Accessible seating notated with a blue outline.<\/em><\/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\/preallocation-2a-f8obtyomt0.png?w=840&#038;ssl=1\" alt=\"One-dimensional seating map\"\/><\/figure>\n\n\n\n<p><em>One dimensional representation of venue, accessible seats shown in blue.<\/em><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Sample Use Case<\/h4>\n\n\n\n<p>As seen earlier, representing a 2 dimension structure, like a venue, is a good use case. Another example could be a reservation system where a resource is blocked or reserved, on a per day basis. Using one cell per available day would likely make computations and checking faster than keeping a list of ranges.<br><\/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\/preallocation-month1-6e7yq8s400.png?w=840&#038;ssl=1\" alt=\"Image of the month of April 2019 with an array\"\/><\/figure>\n\n\n\n<p><em>The month of April 2019 with an array of U.S. work days.<\/em><\/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\/preallocation-month2-v64w8svj6t.png?w=840&#038;ssl=1\" alt=\"Image of the month of April 2019 with a list of ranges\"\/><\/figure>\n\n\n\n<p><em>The month of April 2019 with an array of U.S. work days as a list of ranges.<\/em><br>\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion<\/h4>\n\n\n\n<p>This pattern may be one of the most used when using the MMAPv1 storage engine with MongoDB. However due to the depreciation of this storage engine, it has lost its generic use case, but it is still useful in some situations. And like other patterns, you have a trade-off between &#8220;simplicity&#8221; and &#8220;performance&#8221;.<\/p>\n\n\n\n<p>The next post in this series will look at the <em>Document Versioning Pattern<\/em>.<\/p>\n\n\n\n<p>If you have questions, please leave comments below. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Previous Parts of Building with Patterns:<\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-polymorphic-pattern\">Polymorphic<\/a> pattern<\/li><li>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-attribute-pattern\">Attribute<\/a> pattern<\/li><li>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-bucket-pattern\">Bucket<\/a> pattern<\/li><li>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-outlier-pattern\">Outlier<\/a> pattern<\/li><li>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-computed-pattern\">Computed<\/a> pattern<\/li><li>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-subset-pattern\">Subset<\/a> pattern<\/li><li>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-extended-reference-pattern\">Extended Reference<\/a> pattern<\/li><li>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-approximation-reference-pattern\">Approximation<\/a> pattern<\/li><li>The <a href=\"https:\/\/www.mongodb.com\/blog\/post\/building-with-patterns-the-preallocation-pattern\">Tree<\/a> pattern<\/li><\/ul>\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%2F1067&#038;t=Building%20with%20Patterns%3A%20The%20Preallocation%20Pattern&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1067&#038;p&#091;images&#093;&#091;0&#093;=https%3A%2F%2Fwebassets.mongodb.com%2F_com_assets%2Fcms%2Fpreallocation-1a-rqdo5jz3vk.png&#038;p&#091;title&#093;=Building%20with%20Patterns%3A%20The%20Preallocation%20Pattern\" 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%2F1067&#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%2F1067&#038;title=Building%20with%20Patterns%3A%20The%20Preallocation%20Pattern\" 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%2F1067&#038;title=Building%20with%20Patterns%3A%20The%20Preallocation%20Pattern\" 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%20The%20Preallocation%20Pattern&#038;body=Hey%20check%20this%20out:%20https%3A%2F%2Fwww.kenwalger.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1067\" 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>One of the great things about MongoDB is the document data model. It provides for a lot of flexibility not only in schema design but in the development cycle as well. Not knowing what fields will be required down the road is easily handled with MongoDB documents. However, there are times when the structure is &hellip; <a href=\"https:\/\/www.kenwalger.com\/blog\/nosql\/mongodb\/building-with-patterns-the-preallocation-pattern\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Building with Patterns: The Preallocation Pattern&#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":[1597,785],"yst_prominent_words":[1592,1587,1593,1595,1582,1590,1584,1510,1594,1583,1589,1591,1585,87,507,1586,1588,578,1596,1532],"class_list":["post-1067","post","type-post","status-publish","format-standard","hentry","category-mongodb","tag-preallocation-pattern","tag-schema-design","pmpro-has-access"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8lx70-hd","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/1067","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=1067"}],"version-history":[{"count":1,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/1067\/revisions"}],"predecessor-version":[{"id":1068,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/posts\/1067\/revisions\/1068"}],"wp:attachment":[{"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/media?parent=1067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/categories?post=1067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/tags?post=1067"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.kenwalger.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=1067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}