title: Reliable, fault tolerant and scalable solution for tracking updates on different mongo collection
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>//CouponCartRule - MongoDB
  {
    "id": 1,
  
    "applicability": {
      "validFrom": "12-MAR-2017T01:00:00Z",
      "validTill": "12-MAR-2019T01:00:00Z"
    },
  
    "maxUsage": 100,
    "currentUsage": 99,
    "maxBudget": 1000,
    "currrentBudget": 990
  }
  
  
  //UniqueCoupon collection  - MongoDB
  {
    "ruleId": 1,
    "couponCode": "CITIDEALMAR18",
    "currentCouponUsage": 90,
    "validFrom": "12-MAR-2018T01:00:00Z",
    "validTill": "12-APR-2018T01:00:00Z"
  }
  
  {
    "ruleId": 1,
    "couponCode": "CITIDEALAPR18",
    "currentCouponUsage": 9,
    "validFrom": "12-JAN-2018T01:00:00Z",
    "validTill": "12-FEB-2018T01:00:00Z"
  }
  
  
  //Order - MongoDB
  {
    "id": 112,
    "total": {
      "discountCode": "CITIDEALMAR18",
      "discount": 10,
      "total": 90,
      "grandTotal": 90
  
    },
    "items": []
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Problem Description</strong> </p>
  
  <p>A CouponCartRule will have conditions defined for a coupon code to be applied on order.</p>
  
  <p>Each shopping cart rule can have n coupons where specific conditions might be overriden.</p>
  
  <p>This is done to reuse CouponCartRule but create different coupons under them and also coupons under CouponCartRule can grow to max of 10K record.</p>
  
  <p>When order is succesffuly placed the order will have couponCode and discount applied. </p>
  
  <p>Order collection is managed by OM team and it is very big.
  We receive order add / cancel event when order is created / cancelled.</p>
  
  <p>I have requirement to check if maxUsage and maxBudget is not breached during checkout.</p>
  
  <p>I  am planning to listen to order add and cancel event, update usage on order add and order cancel event.</p>
  
  <p>Steps </p>
  
  <ul>
  <li>Update currentUsage of UniqueCoupon usage [inc operator of mongo]</li>
  <li>Update currentUsage &amp; budget stats of CouponCartRule using </li>
  </ul>
  
  <p>Any suggestion on making the code idempotent if there is downtime, If there is failure in step 2. The listener would send message to DLQ and the count would be update again which is not desired.</p>
  
  <p>One option that I thought of is to track and record stats at UniqueCoupon level and later aggregate at SCR level [The aggregation operation will eventually be consistent when subsequent coupon is used]. </p>
  
  <p>Since this code is invoked real time it should be efficient.</p>