# Concurrency issue

**URL:** <https://community.parseplatform.org/t/concurrency-issue/3401>\
**Category:** Uncategorized\
**Created:** [April 16, 2023, 6:43pm UTC](https://community.parseplatform.org/t/concurrency-issue/3401 "2023-04-16T18:43:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgerar](https://community.parseplatform.org/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@mgerar](https://community.parseplatform.org/u/mgerar)\
**Post date:** [April 16, 2023, 6:43pm UTC](https://community.parseplatform.org/t/concurrency-issue/3401/1 "2023-04-16T18:43:33Z")

</div>

Hi everyone, I’ve a concurrency issue in the code below:

```auto
Parse.Cloud.define("subscribe", async (request) => {
  .....
  const listId = .....
  const subscriberId = ...
  const SubscribersList = Parse.Object.extend("SubscribersList");
  const query = new Parse.Query(SubscribersList);
  query.equalTo("listId", listId);

  let numberSubscriptions = await query.count({ useMasterKey: true });
  
 // maxSubscribers retrieved from another collection linked to the SubscribersList with the same listId
  const maxSubscribers = ..... get("maxSubscribers");

  if (numberSubscriptions < maxSubscribers) {
      const subscriber = new SubscribersList();
      subscriber.set("subscriberId", subscriberId);
      subscriber.set("subscriptionDate", new Date(Date.now()));
      subscriber.save(null, { useMasterKey: true });       
   }
}, {
  requireUser: true
});

```

When I add a new subscriber, if there are more concurrent requests, they could exceed the “maxSubscribers” that I have for a specific subscription list.

Any suggestion on how to manage the concurrency in this case?

Thanks

---

<div class="post-metadata">

**Author:** ![Manuel](https://community.parseplatform.org/user_avatar/community.parseplatform.org/manuel/32/186_2.png) [@Manuel](https://community.parseplatform.org/u/Manuel)\
**Post date:** [April 16, 2023, 10:47pm UTC](https://community.parseplatform.org/t/concurrency-issue/3401/2 "2023-04-16T22:47:45Z")

</div>

MongoDB offers the [$min](https://www.mongodb.com/docs/manual/reference/operator/update/min/) operator for this, but I’m not sure whether Parse Server or the Parse JS SDK support this yet.

---

<div class="post-metadata">

**Author:** ![mgerar](https://community.parseplatform.org/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@mgerar](https://community.parseplatform.org/u/mgerar)\
**Post date:** [April 17, 2023, 7:29pm UTC](https://community.parseplatform.org/t/concurrency-issue/3401/3 "2023-04-17T19:29:49Z")

</div>

Is there any kind of mutex / lock that can be implemented in a safe way to manage these behaviors, including the write operation on the same collection field?

Thank you

---

<div class="post-metadata">

**Author:** ![Manuel](https://community.parseplatform.org/user_avatar/community.parseplatform.org/manuel/32/186_2.png) [@Manuel](https://community.parseplatform.org/u/Manuel)\
**Post date:** [April 17, 2023, 8:07pm UTC](https://community.parseplatform.org/t/concurrency-issue/3401/4 "2023-04-17T20:07:53Z")

</div>

It may be possible to use an aggregation query to do that. Aggregation queries can also perform modifications. So you could in 1 request look for the object with a condition that `subscriberCount < x`, then in the next pipeline step update the object.

However, I think the proper solution maybe be to implement the `findAndUpdate` command in Parse Server.

---

<div class="post-metadata">

**Author:** ![rgunindi](https://community.parseplatform.org/user_avatar/community.parseplatform.org/rgunindi/32/1553_2.png) [@rgunindi](https://community.parseplatform.org/u/rgunindi)\
**Post date:** [April 21, 2023, 12:13am UTC](https://community.parseplatform.org/t/concurrency-issue/3401/5 "2023-04-21T00:13:05Z")

</div>

Maybe You can do it in JavaScript, “async-mutex” packages.

```auto
const { Mutex } = require('async-mutex');
const mutex = new Mutex();

Parse.Cloud.define("subscribe", async (request) => {
  ...
  
  // maxSubscribers retrieved from another collection linked to the SubscribersList with the same listId
  const maxSubscribers = ..... get("maxSubscribers");

  const release = await mutex.acquire();

  try {
    let numberSubscriptions = await query.count({ useMasterKey: true });

    if (numberSubscriptions < maxSubscribers) {
        const subscriber = new SubscribersList();
        subscriber.set("subscriberId", subscriberId);
        subscriber.set("subscriptionDate", new Date(Date.now()));
        await subscriber.save(null, { useMasterKey: true });       
    }
  } finally {
    release();
  }
}, {
  requireUser: true
});

```

`mutex.acquire()` and `release()` .
