# Ordering of events within a transaction on flat transaction stream

**URL:** <https://forum.canton.network/t/ordering-of-events-within-a-transaction-on-flat-transaction-stream/970>\
**Category:** App Development\
**Tags:** ledger-api\
**Created:** [August 3, 2020, 9:16am UTC](https://forum.canton.network/t/ordering-of-events-within-a-transaction-on-flat-transaction-stream/970 "2020-08-03T09:16:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [August 3, 2020, 9:16am UTC](https://forum.canton.network/t/ordering-of-events-within-a-transaction-on-flat-transaction-stream/970/1 "2020-08-03T09:16:33Z")

</div>

Hi,  
I’m wondering if there are any guarantees on the ordering of events in the flat transaction stream at `com.daml.ledger.api.v1.TransactionService`.

Consider the following example:

1. I have a template with a contract key and one contract of that template.
2. In a transaction, I now archive that contract and create a new one with the same key. The ordering matters here. If I first create the new one and then archive the existing contract, the transaction fails.

Am I guaranteed to see the `archive` before the `create` in the list of events?

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [August 3, 2020, 9:42am UTC](https://forum.canton.network/t/ordering-of-events-within-a-transaction-on-flat-transaction-stream/970/2 "2020-08-03T09:42:58Z")

</div>

Excellent question. The answer depends on your relationship to the contract and key, the Ledger’s consensus policies, and the Ledger’s topology. So the answer in general is “no”, but it’s “yes” when it matters.

Let’s first of all see how this can fail:

Let’s say `Alice` is hosted on participant P and participant P is connected to two domains/partitions A and B.

1. A contract C of type T with key K exists in A.
2. A transaction TX1 is committed in A through which Alice witnesses the archival of C
3. A transaction is TX2 committed on B through which Alice witnesses the creation of C’ of type T with key K

If the consensus algorithm in A and B is such that P’s confirmation is not needed for TX1, and P’s connection to A is slow, it could happen that P receives TX2 before TX1. You now get the `create` before the `archive`.

So when is it guaranteed that the archive comes before the create?

- If the `archive` and `create` are causality ordered in `Alice`'s projection. Eg if `Alice` is a maintainer of K, or if `Alice` witnesses a consuming choice on `C` that creates `C'`.
- If the create and archive happen on the same domain/partition, and that domain/partition give a total order to transactions. This applies in particular to all non-partitioned DAML ledgers currently in existence.
- If the `archive` and `create` happen in the same transaction.

In other words, the scenario above illustrates the the circumstances required to not get this guarantee:

- A Ledger which guarantees only causality ordering (eg multi-domain partitioned topology)
- A Participant which is not involved in the consensus of the transactions in question

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [August 3, 2020, 9:46am UTC](https://forum.canton.network/t/ordering-of-events-within-a-transaction-on-flat-transaction-stream/970/3 "2020-08-03T09:46:08Z")

</div>

Great answer, thank you! However, I think you might have misunderstood my question. In my example, the create and archive both happen within a single transaction. I’m asking about the ordering of the `events` list in a transaction not the ordering of transactions.

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [August 3, 2020, 10:21am UTC](https://forum.canton.network/t/ordering-of-events-within-a-transaction-on-flat-transaction-stream/970/4 "2020-08-03T10:21:54Z")

</div>

Oh, in that case the answer is a simple “yes”. Every transaction is an ordered forest of actions. All parties that witness any part of the transaction get events according to that ordering.

---

<div class="post-metadata">

**Author:** ![Mr\_Mannoroth](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/mr_mannoroth/32/5824_2.png) [@Mr\_Mannoroth](https://forum.canton.network/u/Mr_Mannoroth)\
**Post date:** [April 25, 2023, 10:29am UTC](https://forum.canton.network/t/ordering-of-events-within-a-transaction-on-flat-transaction-stream/970/5 "2023-04-25T10:29:16Z")

</div>

Follow on question re this - if I submit a list of commands in a CommandsSubmission batch, and each of these commands is a CreateAndExercise command that creates a new contract, so say the batch is `[Cmd1, Cmd2, Cmd3]`, then in the single Transaction that I receive as a response on the flat Tx stream, am I guaranteed to get the following events in the exact order?

```haskell
[Create-1-Cmd1, Archive-1-Cmd1, Create-2-Cmd1, 
Create-1-Cmd2, Archive-1-Cmd2, Create-2-Cmd2,
Create-1-Cmd3, Archive-1-Cmd3, Create-2-Cmd3]

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [May 2, 2023, 8:11am UTC](https://forum.canton.network/t/ordering-of-events-within-a-transaction-on-flat-transaction-stream/970/6 "2023-05-02T08:11:51Z")

</div>

Yes. Commands in a single submission are executed atomically (ie all or nothing) in the given order.

---

<div class="post-metadata">

**Author:** ![Mr\_Mannoroth](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/mr_mannoroth/32/5824_2.png) [@Mr\_Mannoroth](https://forum.canton.network/u/Mr_Mannoroth)\
**Post date:** [May 2, 2023, 9:18am UTC](https://forum.canton.network/t/ordering-of-events-within-a-transaction-on-flat-transaction-stream/970/7 "2023-05-02T09:18:48Z")

</div>

Cheers, thanks @bernhard
