<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Wei Shan</title>
    <description>The latest articles on DEV Community by Wei Shan (@weiiishannn).</description>
    <link>https://dev.to/weiiishannn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F15781%2F15c4a4f6-a4c3-4335-88d7-6e759e27f7af.jpg</url>
      <title>DEV Community: Wei Shan</title>
      <link>https://dev.to/weiiishannn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/weiiishannn"/>
    <language>en</language>
    <item>
      <title>Top 3 Reasons to upgrade to MongoDB 3.4</title>
      <dc:creator>Wei Shan</dc:creator>
      <pubDate>Tue, 02 May 2017 16:29:40 +0000</pubDate>
      <link>https://dev.to/weiiishannn/top-3-reasons-to-upgrade-to-mongodb-34</link>
      <guid>https://dev.to/weiiishannn/top-3-reasons-to-upgrade-to-mongodb-34</guid>
      <description>&lt;p&gt;MongoDB 3.4 was released on Nov 29, 2016 withÂ &lt;a href="https://docs.mongodb.com/manual/release-notes/3.4/"&gt;tons of new features&lt;/a&gt;.Â In this post, I will be listing the top 3 reasons to upgrade to MongoDB 3.4.&lt;/p&gt;

&lt;h2&gt;&lt;a href="https://docs.mongodb.com/manual/release-notes/3.4/#faster-balancing"&gt;Faster Balancing&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;In the previous releases, it takes ages(4s per chunk, last I seen in 3.2.10) for the balancer to move or pre-split chunks. Imagine if you have 100,000 chunks to pre-split, it's going to take days. There are 2 reason for this:&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;In a sharded cluster, there can only be 1 chunk migration at any point of time. It's a single threaded operation&lt;/li&gt;
    &lt;li&gt;
&lt;a href="https://jira.mongodb.org/browse/SERVER-26778"&gt;This&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In MongoDB 3.4,Â for a sharded cluster with n shards, MongoDB can perform at most n/2 (rounded down) simultaneous chunk migrations. So, if you have a 4 shard MongoDB cluster, it will migrate 2 chunks at a time! If you have lesser than 4 shards, the behaviour will stay the same unfortunately. :(&lt;/p&gt;

&lt;h2&gt;&lt;a href="https://docs.mongodb.com/manual/release-notes/3.4/#linearizable-read-concern"&gt;Linearizable Read Concern&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;In general, MongoDB has various &lt;a href="https://docs.mongodb.com/manual/reference/read-concern/#read-concern-levels"&gt;readConcern option&lt;/a&gt;. These options allow the developers to choose different readConcern suitable for their application requirement. Do useÂ readConcern:local for unimportant queries that doesn't careÂ if it reads data that could be rolled back. Using readConcern:majority preventsÂ readingÂ data that can be rolled back. However, it does not prevent you from reading stale data in unique network partitions edge cases.&lt;/p&gt;

&lt;p&gt;In MongoDB 3.4, usingÂ readConcern:linearizable allows you to prevent stale reads too. This featureÂ is not free and has the highest performance overhead.Â readConcern:local only require 1 operation to return your results.Â readConcern:majority requires a majority acknowledgement from a majority of the nodes in your replica set which depends on your network latency.Â readConcern:linearizable requires an additionalÂ no-op majority write to the replica set to ensure the data being read is durable and not stale.&lt;/p&gt;

&lt;p&gt;In essence, what readConcern:linearizable does is the &lt;a href="https://jira.mongodb.org/browse/SERVER-24497"&gt;following&lt;/a&gt;:&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;Client sends query to primary node&lt;/li&gt;
    &lt;li&gt;Node gets the results without erroors&lt;/li&gt;
    &lt;li&gt;Node sends a noop majority write to the other nodes in the cluster to confirm that it is still the primary node.&lt;/li&gt;
    &lt;li&gt;Results gets returned to the client&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;&lt;a href="https://docs.mongodb.com/manual/reference/configuration-options/#net-compression-option"&gt;Network Compression&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;I'm not sure why this wasn't publicise widely as a new feature. I like it a lot because, now all the traffic between mongos/mongod are compressed so we save on bandwidth. This really saves you lots of money if you are running it on AWS or if you are constrained by the network bandwidth.&lt;/p&gt;

&lt;h2&gt;&lt;span&gt;Summary&lt;/span&gt;&lt;/h2&gt;

&lt;p&gt;In general, I do agree with &lt;a href="http://www.infoworld.com/article/3180620/application-development/review-mongodb-learns-cool-new-tricks.html"&gt;infoworld&lt;/a&gt; that MongoDB 3.4 is a no-brainer upgrade! We are currently waiting on it to be available on our O/SÂ of choice so we can start rolling it out!&lt;/p&gt;

&lt;p&gt;Hope this helps! :)&lt;/p&gt;

&lt;p&gt;Regards,&lt;br&gt;
Wei Shan&lt;/p&gt;

</description>
      <category>mongodb34</category>
    </item>
    <item>
      <title>Compound Index Vs Multiple Single Field Index in MongoDB 3.x</title>
      <dc:creator>Wei Shan</dc:creator>
      <pubDate>Sat, 08 Apr 2017 12:52:25 +0000</pubDate>
      <link>https://dev.to/weiiishannn/compound-index-vs-multiple-single-field-index-in-mongodb-3x</link>
      <guid>https://dev.to/weiiishannn/compound-index-vs-multiple-single-field-index-in-mongodb-3x</guid>
      <description>

&lt;h1&gt;Compound Index Vs Multiple Single Field Index in MongoDB 3.x&lt;/h1&gt;

&lt;p&gt;MongoDB supports both &lt;a href="https://docs.mongodb.com/manual/indexes/"&gt;compound index and single field index&lt;/a&gt;. It's a common knowledge that index decreaseÂ your INSERTS/UPDATESÂ performance but increases your SELECTÂ performance. However, if you needÂ to index multiple fields, is it better to use a compound index or just create multiple single field indexes? This post tries to investigate which is the better option in terms of performance.&lt;/p&gt;

&lt;h2&gt;&lt;strong&gt;&lt;span&gt;Insert Performance&lt;/span&gt;&lt;/strong&gt;&lt;/h2&gt;

&lt;h3&gt;Test Setup:&lt;/h3&gt;

&lt;ul&gt;
    &lt;li&gt;Single MongoDB 3.2 instance running on my Mac&lt;/li&gt;
    &lt;li&gt;
&lt;a href="https://github.com/johnlpage/POCDriver"&gt;POC Driver&lt;/a&gt;Â uses single thread toÂ perform inserts only&lt;/li&gt;
    &lt;li&gt;Test run for 5 mins&lt;/li&gt;
    &lt;li&gt;Empty collection after every run&lt;/li&gt;
    &lt;li&gt;Default readConcern&lt;/li&gt;
    &lt;li&gt;Default writeConcern&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;span&gt;&lt;strong&gt;Sample Document:&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;

&lt;pre&gt;&amp;gt; db.testCol.findOne()
{
 "_id" : {
 "w" : 2,
 "i" : 0
 },
 "fld0" : NumberLong(530752),
 "fld1" : ISODate("2015-06-02T08:14:18.687Z"),
 "fld2" : "tempor invidunt ut labore et",
 "fld3" : "eirmod tempor invidunt ut",
 "fld4" : "Lorem ipsum dolor sit amet,",
 "fld5" : ISODate("2016-10-12T01:58:24.580Z"),
 "fld6" : NumberLong(1712708),
 "fld7" : "sit amet. Lorem ipsum dolor",
 "fld8" : "sit amet, consetetur",
 "fld9" : NumberLong(652363),
 "bin" : BinData(0,"")
}&lt;/pre&gt;

&lt;p&gt;&lt;span&gt;&lt;strong&gt;Phase 1 (No indexes except on _id field)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;

&lt;pre&gt;After 295 seconds, 13340672 new records inserted - collection has 13340672 in total
46498 inserts per second since last report 99.97 % in under 50 milliseconds
0 keyqueries per second since last report 100.00 % in under 50 milliseconds
0 updates per second since last report 100.00 % in under 50 milliseconds
0 rangequeries per second since last report 100.00 % in under 50 milliseconds&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;~45,222 inserts/second&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;strong&gt;Phase 2 (Compound index onÂ fld1Â to fld9)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;

&lt;pre&gt;After 295 seconds, 7739904 new records inserted - collection has 7739904 in total
20830 inserts per second since last report 99.72 % in under 50 milliseconds
0 keyqueries per second since last report 100.00 % in under 50 milliseconds
0 updates per second since last report 100.00 % in under 50 milliseconds
0 rangequeries per second since last report 100.00 % in under 50 milliseconds&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;~26,236 inserts/second&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;strong&gt;Phase 3 (Single field indexes on eachÂ fld2, fld6 and fld8)&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;

&lt;pre&gt;After 295 seconds, 2998272 new records inserted - collection has 2998272 in total
7012 inserts per second since last report 63.61 % in under 50 milliseconds
0 keyqueries per second since last report 100.00 % in under 50 milliseconds
0 updates per second since last report 100.00 % in under 50 milliseconds
0 rangequeries per second since last report 100.00 % in under 50 milliseconds&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;~10,163 inserts/second&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;&lt;strong&gt;&lt;span&gt;StorageÂ Utilisation&lt;/span&gt;&lt;/strong&gt;&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;span&gt;On the same collectionÂ withÂ 2,817,024 documents:&lt;/span&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;span&gt;Compound Index&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;db.testCol.stats().indexSizes
{
 "_id_" : 75079680,
 "fld0_1_fld1_1_fld2_1_fld3_1_fld4_1_fld5_1_fld6_1_fl7_1_fld8_1_fld9_1" : 2617061376
}&lt;/pre&gt;

&lt;p&gt;&lt;span&gt;&lt;strong&gt;~2.44GB&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;span&gt;Single-Field Index&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;pre&gt;&amp;gt; db.testCol.stats().indexSizes
{
 "_id_" : 75079680,
 "fld0_1" : 65343488,
 "fld1_1" : 79781888,
 "fld2_1" : 35471360,
 "fld3_1" : 35475456,
 "fld4_1" : 35475456,
 "fld5_1" : 79781888,
 "fld6_1" : 65339392,
 "fld7_1" : 35475456,
 "fld8_1" : 35475456,
 "fld9_1" : 65347584,
}&lt;/pre&gt;

&lt;p&gt;&lt;span&gt;&lt;strong&gt;~0.53GB&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;

&lt;h2&gt;&lt;span&gt;Index Selectivity Usage&lt;/span&gt;&lt;/h2&gt;

&lt;p&gt;For single-field index, there's no limitation at all. MongDB optimiser will pick and choose whichever index it deems fit. For compound index, it's slightly different.&lt;/p&gt;

&lt;p&gt;&lt;span&gt;Query like these will not use the compound index:&lt;/span&gt;&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;db.testCol.find({fld9:1,fld8:1,fld7:1})&lt;/li&gt;
    &lt;li&gt;db.testCol.find({fld9:1,fld3:1,fld6:1})&lt;/li&gt;
    &lt;li&gt;db.testCol.find({fld9:1,fld8:1,fld7:1})&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Query like these will use the compound index:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;db.testCol.find({fld0:1,fld1:1})&lt;/li&gt;
    &lt;li&gt;db.testCol.find({fld5:1,fld8:1,fld0:1}) (not very efficient..)&lt;/li&gt;
    &lt;li&gt;db.testCol.find({fld0:1,fld3:1,fld5:1})&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The general idea is that MongoDB will use it the compound index if it matchesÂ the &lt;a href="https://docs.mongodb.com/manual/core/index-compound/#prefixes"&gt;index prefix&lt;/a&gt;. It it doesn't match it correctly, it might still use the index if it is still more efficient that performing a full collection scan. The only way to find out is &lt;a href="https://www.mongodb.com/collateral/mongodb-performance-best-practices"&gt;educate&lt;/a&gt; and test the queries yourself!&lt;/p&gt;

&lt;h2&gt;&lt;span&gt;Summary&lt;/span&gt;&lt;/h2&gt;

&lt;p&gt;&lt;span&gt;A very short summary in key points below&lt;/span&gt;:&lt;/p&gt;

&lt;ol&gt;
    &lt;li&gt;No index has the best insert performance (duh!!)&lt;/li&gt;
    &lt;li&gt;Compound index is faster thanÂ Single-field index in insert performance&lt;/li&gt;
    &lt;li&gt;Compound index uses more storage than Single-field index&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Depending on the situation, choose your poison carefully :)&lt;/p&gt;

&lt;p&gt;&lt;span&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;/span&gt;:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;https://github.com/johnlpage/POCDriver&lt;/li&gt;
    &lt;li&gt;https://docs.mongodb.com/v3.2/core/index-compound/&lt;/li&gt;
    &lt;li&gt;https://docs.mongodb.com/v3.2/core/index-single/&lt;/li&gt;
    &lt;li&gt;https://docs.mongodb.com/manual/indexes/&lt;/li&gt;
    &lt;li&gt;https://www.mongodb.com/collateral/mongodb-performance-best-practices&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;P.S: The above test does not attempt to quantify the absolute performance numbers but rather provide a good performance degradation ratio for indexes&lt;/p&gt;

&lt;p&gt;Regards,&lt;br&gt;
Wei Shan&lt;/p&gt;


</description>
      <category>mongodb32</category>
      <category>mongodb</category>
      <category>mongodb34</category>
    </item>
  </channel>
</rss>
