# Message passing via a create vs an exercise

**URL:** <https://forum.canton.network/t/message-passing-via-a-create-vs-an-exercise/6126>\
**Category:** App Development\
**Created:** [February 15, 2023, 6:06pm UTC](https://forum.canton.network/t/message-passing-via-a-create-vs-an-exercise/6126 "2023-02-15T18:06:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![asarpeshkar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/asarpeshkar/32/1601_2.png) [@asarpeshkar](https://forum.canton.network/u/asarpeshkar)\
**Post date:** [February 15, 2023, 6:06pm UTC](https://forum.canton.network/t/message-passing-via-a-create-vs-an-exercise/6126/1 "2023-02-15T18:06:55Z")

</div>

AFAICT, there are at least two ways to pass a message to a stream integration:

1. Create a contract with the message payload and immediately archive it, and the integration listens to creates for that template.
2. Create a stateless contract and exercise a non-consuming choice with the message payload in the arguments. Here the integration subscribes to transaction tree stream and listens for exercises.

Is one of these approaches more performant than the other? The first is a create + exercise and the second is a fetch + exercise. My intuition is that fetches are cheaper than creates, but I have no evidence one way or the other.

---

<div class="post-metadata">

**Author:** ![Levente\_Barczy](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/levente_barczy/32/544_2.png) [@Levente\_Barczy](https://forum.canton.network/u/Levente_Barczy)\
**Post date:** [February 15, 2023, 6:59pm UTC](https://forum.canton.network/t/message-passing-via-a-create-vs-an-exercise/6126/2 "2023-02-15T18:59:40Z")

</div>

I’d generally stay away from 2. Daml should be written in a way that’s agnostic to the choice of integration, and 2 does not work if the integration uses the JSON API for example.

---

<div class="post-metadata">

**Author:** ![stefanobaghino-da](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stefanobaghino-da/32/7057_2.png) [@stefanobaghino-da](https://forum.canton.network/u/stefanobaghino-da)\
**Post date:** [February 16, 2023, 10:24am UTC](https://forum.canton.network/t/message-passing-via-a-create-vs-an-exercise/6126/3 "2023-02-16T10:24:08Z")

</div>

> [@asarpeshkar](#):
>
> Is one of these approaches more performant than the other? The first is a create + exercise and the second is a fetch + exercise. My intuition is that fetches are cheaper than creates, but I have no evidence one way or the other.

Consider that the Ledger API exposes primitives to create _and_ exercise as a single operation. This means that the contract being “created” never needs to be persisted beyond the boundary of the transaction, meaning one less trip to the persistence layer. So, performance-wise, I would say using a `CreateAndExercise` command should be faster.

However, performance is an infinitely complex topic and what I really recommend is to measure, assess and improve if and where needed. Are you aware of this difference being a performance bottleneck in your application? If not, I would solely base your decision based on how well this interacts with the rest of your application and the composability of your approach with other parts of your Daml model.

Intuitively I’d probably use the first option but I’m not 100% sure of the specific problem you are trying to solve.

---

<div class="post-metadata">

**Author:** ![asarpeshkar](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/asarpeshkar/32/1601_2.png) [@asarpeshkar](https://forum.canton.network/u/asarpeshkar)\
**Post date:** [February 16, 2023, 1:08pm UTC](https://forum.canton.network/t/message-passing-via-a-create-vs-an-exercise/6126/4 "2023-02-16T13:08:02Z")

</div>

Yes, this is probably a case of premature optimization on my part. From the integration point-of-view it’s slightly easier to use the flat transaction stream, so I’ll go with option 1 as well. Thanks for the help.

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [February 16, 2023, 5:31pm UTC](https://forum.canton.network/t/message-passing-via-a-create-vs-an-exercise/6126/5 "2023-02-16T17:31:18Z")

</div>

> [@Levente\_Barczy](#):
>
> 2 does not work if the integration uses the JSON API for example.

Neither works for the JSON API. [From docs](https://docs.daml.com/2.5.3/json-api/#contracts-query-stream):

> [@](#):
>
> Each result array means “this is what would have changed if you just polled `/v1/query` iteratively.” In particular, just as polling search can “miss” contracts (as a create and archive can be paired between polls), such contracts may or may not appear in any result object.
