# How to ensure atomicity when calling a canister function?

**URL:** <https://forum.dfinity.org/t/how-to-ensure-atomicity-when-calling-a-canister-function/7602>\
**Category:** Developers\
**Created:** [September 29, 2021, 8:38am UTC](https://forum.dfinity.org/t/how-to-ensure-atomicity-when-calling-a-canister-function/7602 "2021-09-29T08:38:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinli](https://avatars.discourse-cdn.com/v4/letter/k/8edcca/32.png) [@kevinli](https://forum.dfinity.org/u/kevinli)\
**Post date:** [September 29, 2021, 8:38am UTC](https://forum.dfinity.org/t/how-to-ensure-atomicity-when-calling-a-canister-function/7602/1 "2021-09-29T08:38:34Z")

</div>

Let’s say I have a simple (pointless) canister that keeps a simple hashmap \<User, Nat\>. I also have a global variable counter which starts at 0. I have a method called saveId which assigns a user an id based on the counter. This get’s stored in the hashmap. After each call to the method, the counter gets incremented. How do I make this method atomic, so that no user gets assigned the same id? I assume many users can call this function at once.

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [September 29, 2021, 8:50am UTC](https://forum.dfinity.org/t/how-to-ensure-atomicity-when-calling-a-canister-function/7602/2 "2021-09-29T08:50:13Z")

</div>

Execution is atomic by default, you don’t have to do anything. In Motoko, you merely have to be aware that executing `await` allows other methods to interleave. But other than during awaits, no other method call can interfere.

---

<div class="post-metadata">

**Author:** ![kevinli](https://avatars.discourse-cdn.com/v4/letter/k/8edcca/32.png) [@kevinli](https://forum.dfinity.org/u/kevinli)\
**Post date:** [September 29, 2021, 8:51am UTC](https://forum.dfinity.org/t/how-to-ensure-atomicity-when-calling-a-canister-function/7602/3 "2021-09-29T08:51:42Z")

</div>

Ah okay, so if I call an async function, but don’t specify `await`, then it will be atomic?

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [September 29, 2021, 8:52am UTC](https://forum.dfinity.org/t/how-to-ensure-atomicity-when-calling-a-canister-function/7602/4 "2021-09-29T08:52:48Z")

</div>

If the async function itself (the callee) does not use `await`, then it is atomic. If it uses `await`, it is only atomic between awaits.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [September 29, 2021, 10:57am UTC](https://forum.dfinity.org/t/how-to-ensure-atomicity-when-calling-a-canister-function/7602/5 "2021-09-29T10:57:41Z")

</div>

Practically, I think the counter canister just needs to make sure it updates its value before calling an await or handing the answer back and it will guarantee that no one gets the same number. You’d get in trouble if you did something like:

user canister:

```auto
 a_func(){
    let number = await other.get_id_from_other_canister()
}

```

counter canister:

```auto
get_id_from_other_canister(){
  let answer = lastID + 1;
  //update last id here to maintain atomicity lastID:=answer
  await some_third_canister():
  lastID := answer; //bad
}

```

---

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [September 29, 2021, 2:18pm UTC](https://forum.dfinity.org/t/how-to-ensure-atomicity-when-calling-a-canister-function/7602/6 "2021-09-29T14:18:26Z")

</div>

By the way, state modification before external calls, is the recommended approach for re-entrancy attack prevention.
