Parallel request read and update within two documents using aggregation

Hello fellow mongodb community,

I have two collections named installmentPromoUsers and installmentPromos . installmentPromoUsers collection stored data about users and the eligibility for each promo, here is the sample data

{
  "_id": {
    "$oid": "66a87013018edd61a7355042"
  },
  "cif": "1AH3CU",
  "promotionIds": [
    "SEGMENT_A_TEST",
    "SEGMENT_B"
  ],
  "usedInstallmentIds": []
}

installmentPromos collection stored information related to promotion details, here is the sample data

{
  "_id": {
    "$oid": "66a1c0854784fc46ec8727a2"
  },
  "installmentId": "RMDHN_3M",
  "availableQuota": 3,
  "expiryDate": {
    "$date": "2025-08-07T08:19:21.000Z"
  },
  "interestRate": "1",
  "minAmount": 500000,
  "name": {
    "en": "3 months",
    "id": "3 bulan"
  },
  "normalRate": 21,
  "promoTitle": {
    "en": "Ramadhan Promo",
    "id": "Promo Ramadhan"
  },
  "promotionId": "SEGMENT_A",
  "quotaCustomer": 5,
  "quotaTotal": 40,
  "startDate": {
    "$date": "2024-07-22T17:00:00.000Z"
  },
  "summary": {
    "en": "- min. 500k\n- min 3 months",
    "id": "- min. 500k\n- min 3 bulan"
  },
  "tenure": 3,
  "termsAndConditions": {
    "en": "- min. 500k\n- min 3 months",
    "id": "- min. 500k\n- min 3 bulan"
  },
  "promotionName": "Ramadhan Promo"
}

In order to get the eligible promotions for particular user along with its detail , i need to join the two collection using aggregation lookup. Also, i need to perform aggregation with lookup and set to update availableQuota and usedInstallmentIds when user use the promotion. Let’s say, there are 5 parallel request to database, 3 of them are reading the date stored in collection installmentPromoUsers and installmentPromos, and 2 of them are updating the date stored in collection installmentPromoUsers and installmentPromos. The questions are :

  1. Does the mongodb block the read request of those 3 clients until the 2 clients finishes update data?
  2. Do i need certain locking mechanism to preserve data consistency for multiple read and write parallel requests?

Thank you

  1. Does the mongodb block the read request of those 3 clients until the 2 clients finishes update data?

No , it doesn’t

  1. Do i need certain locking mechanism to preserve data consistency for multiple read and write parallel requests?

You dont need a locking mechanism , coz it isnt going to help you block the reads
With mongo default read concern , your the read result returns the data written by latest writes.

Thank you for the answers. To clarify, let’s say there are 2 users. User A performs read (with aggregation) to know the availableQuota . User B performs write to decrement the availableQuota. If these requests happen at the same time, what would be the result?

Considering , read operation started and write happend on primary , not yet durable , read client would see the decremented value .

Do I need to use transactions or aggregation with $set is sufficient enough?

A simple update with $set is fine . Transaction would make sense if you have concurrent writes

Thank you for the response, you save the day

If 2 users perform operations at the same time does not imply that the server receives the operations at the same time. Network delays and other thing comes into play. A write by a client can be sent after the read of one but received first by the server.

Other factors come in. What is the write concern used by the writer? What is the read preference of the reader.

So basically there is no parallel request, right?
Currently, I’m using the default read preference as well as default write concern

The above is far from what I wrote.

By

I mean is that two simultaneous events on the clients do not necessarily reach the server at the same time because of network delays.

And with

I means that 2 non-simultaneous events may reach the server in a different order.

The corollary to this is that your code should not depends on this order. I often seen on a famous online retailer a non-zero inventory count but not being able to buy the item because now the inventory count is 0 as someone else just bought it.

This brightly written reply goes into some of the details.

Sorry for the misunderstanding, now I know what you meant earlier.
Thank you for the answers steeve

1 Like