# Workflows Variables & Activities

**URL:** <https://community.temporal.io/t/workflows-variables-activities/834>\
**Category:** Community Support\
**Tags:** general-impl\
**Created:** [October 26, 2020, 4:33am UTC](https://community.temporal.io/t/workflows-variables-activities/834 "2020-10-26T04:33:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul\_Raj](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/rahul_raj/32/165_2.png) [@Rahul\_Raj](https://community.temporal.io/u/Rahul_Raj)\
**Post date:** [October 26, 2020, 4:33am UTC](https://community.temporal.io/t/workflows-variables-activities/834/1 "2020-10-26T04:33:06Z")

</div>

Have some questions on Workflows & Activities  
I understand that in a workflow, all variables and threads are stored/persisted and activities encapsulate external unreliable services.

1. Should all workflow variables be Serializable? What kind of serialization is used here?
2. Does Temporal persist the input and output of an Activity as well?
3. Is thread persistence achieved through variable persistence and rebuilding the state using event sourcing?
4. In a decision step between two activities, if the decision is made on an external state, should that call also be encapsulated in an activity?

---

<div class="post-metadata">

**Author:** ![samar](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/samar/32/12_2.png) [@samar](https://community.temporal.io/u/samar)\
**Post date:** [October 26, 2020, 4:49am UTC](https://community.temporal.io/t/workflows-variables-activities/834/2 "2020-10-26T04:49:07Z")

</div>

> Should all workflow variables be Serializable? What kind of serialization is used here?

Workflow variables are not required to be serializable as workflow state is rebuilt in the case of failure by replaying events in the workflow execution history (event sourcing). But if you use a workflow variable as input to an activity or return it in response to query handler, then it needs to be serializable.

> Does Temporal persist the input and output of an Activity as well?

Yes. All activity input parameters are serialized and stored within `ActivityScheduledEvent` in execution history.

> Is thread persistence achieved through variable persistence and rebuilding the state using event sourcing?

I’m not sure I fully understand the question. The only restriction Temporal puts on workflow logic is, that it needs to be deterministic. Temporal SDK also has a scheduler which executes multi-threaded code in deterministic manner. In the case of failure we replay your workflow code using execution history and considering it is deterministic we end up exact same state along with all threads, local variables and call-stack.

> In a decision step between two activities, if the decision is made on an external state, should that call also be encapsulated in an activity?

Anything which pulls in state from external source needs to be called within `Activity` or `LocalActivity`.

---

<div class="post-metadata">

**Author:** ![Rahul\_Raj](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/rahul_raj/32/165_2.png) [@Rahul\_Raj](https://community.temporal.io/u/Rahul_Raj)\
**Post date:** [October 26, 2020, 5:06am UTC](https://community.temporal.io/t/workflows-variables-activities/834/3 "2020-10-26T05:06:56Z")

</div>

In one of the previous discussions, it was mentioned that when the system is loaded threads could be removed to make resources available for other executions. When a removed thread comes back into action, I guess the states are replayed to continue the execution.

Lets say I make a rest callA. Based on its return value, I would like to decide between callB and callC. I guess all calls needs to be wrapped inside an activity to make it deterministic.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [October 26, 2020, 6:33am UTC](https://community.temporal.io/t/workflows-variables-activities/834/4 "2020-10-26T06:33:55Z")

</div>

The calls to A, B and C have to be inside the activities. The logic that decides which activity to call based on data should reside inside the workflow.

---

<div class="post-metadata">

**Author:** ![nadilas](https://avatars.discourse-cdn.com/v4/letter/n/e56c9b/32.png) [@nadilas](https://community.temporal.io/u/nadilas)\
**Post date:** [November 1, 2020, 8:06am UTC](https://community.temporal.io/t/workflows-variables-activities/834/5 "2020-11-01T08:06:00Z")

</div>

> [@Rahul\_Raj](#):
>
> Lets say I make a rest callA. Based on its return value, I would like to decide between callB and callC. I guess all calls needs to be wrapped inside an activity to make it deterministic.

I am also not 100% sure how this works.

Let’s say during the first run the rest call returns status “a”, then somewhere on the next lines the worker dies and the workflow will be replayed. This time, the rest call would return status “b” if it was called, but my understanding is that return status “a” from  
the previous call is persisted and the rest call will not be made again. So the replay actually happens with an old (stale) state.

@maxim Is this assumption correct?

---

<div class="post-metadata">

**Author:** ![Anand\_Sarda](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/anand_sarda/32/306_2.png) [@Anand\_Sarda](https://community.temporal.io/u/Anand_Sarda)\
**Post date:** [November 12, 2020, 7:14pm UTC](https://community.temporal.io/t/workflows-variables-activities/834/6 "2020-11-12T19:14:08Z")

</div>

> [@samar](#):
>
> > What kind of serialization is used here?

Trying to understand how the input parameters or the activity result get serialized in golang? Do you look for specific interface method?

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 12, 2020, 8:19pm UTC](https://community.temporal.io/t/workflows-variables-activities/834/7 "2020-11-12T20:19:31Z")

</div>

@nadilas Yes, the assumption is correct. On replay the activities are not reexecuted and their results are taken from the history.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [November 12, 2020, 8:59pm UTC](https://community.temporal.io/t/workflows-variables-activities/834/8 "2020-11-12T20:59:27Z")

</div>

Serialization is fully pluggable. It is done by implementing a [PayloadConverter](https://github.com/temporalio/sdk-go/blob/master/converter/payload_converter.go) interface.

The [default serialization](https://github.com/temporalio/sdk-go/blob/master/converter/json_payload_converter.go) uses JSON format. See [encoding/json package documentation](https://blog.golang.org/json) for details.

---

<div class="post-metadata">

**Author:** ![venkyvb](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/venkyvb/32/1176_2.png) [@venkyvb](https://community.temporal.io/u/venkyvb)\
**Post date:** [August 23, 2021, 9:38pm UTC](https://community.temporal.io/t/workflows-variables-activities/834/9 "2021-08-23T21:38:29Z")

</div>

> [@samar](#):
>
> I’m not sure I fully understand the question. The only restriction Temporal puts on workflow logic is, that it needs to be deterministic. Temporal SDK also has a scheduler which executes multi-threaded code in deterministic manner. In the case of failure we replay your workflow code using execution history and considering it is deterministic we end up exact same state along with all threads, local variables and call-stack.

Came across this topic while looking for information around the thread persistence. Just to make sure I understand, when we refer to `thread persistence`, we are referring to the replay of the workflow code using the event sourcing to arrive at the same state of the execution thread ? Or is the thread state serialized and persisted ?  
Thanks !

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [August 23, 2021, 11:45pm UTC](https://community.temporal.io/t/workflows-variables-activities/834/10 "2021-08-23T23:45:37Z")

</div>

Workflow code uses event sourcing to arrive at the same state of the execution thread.

---

<div class="post-metadata">

**Author:** ![Pritam\_Kumar2](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/pritam_kumar2/32/1342_2.png) [@Pritam\_Kumar2](https://community.temporal.io/u/Pritam_Kumar2)\
**Post date:** [September 28, 2021, 11:19am UTC](https://community.temporal.io/t/workflows-variables-activities/834/12 "2021-09-28T11:19:04Z")

</div>

Hii @maxim, Just wanted a clarification on local variables. I have a local variable in my workflow which is used to decide whether workflow is cancelled or not. My service is exposing an api and that api takes workflowId as input and retrieves the workflow and then access that local variable to change it’s value to “cancelled”. As this is happening on multi-thread, I just want to know how the local variable of a workflow is stored at temporal. Also, if this approach is correct or not??(This is also what described in Temporal subscription samples).

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [September 28, 2021, 3:46pm UTC](https://community.temporal.io/t/workflows-variables-activities/834/13 "2021-09-28T15:46:37Z")

</div>

> My service is exposing an api and that api takes workflowId as input and retrieves the workflow and then access that local variable to change it’s value to “cancelled”.

Would you explain how exactly the

> change it’s value to “cancelled”

is implemented?

The supported way to change such a variable is to send a signal to the workflow and update the variable from the signal handler.

---

<div class="post-metadata">

**Author:** ![Pritam\_Kumar2](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/pritam_kumar2/32/1342_2.png) [@Pritam\_Kumar2](https://community.temporal.io/u/Pritam_Kumar2)\
**Post date:** [September 29, 2021, 4:26am UTC](https://community.temporal.io/t/workflows-variables-activities/834/14 "2021-09-29T04:26:35Z")

</div>

Yes @maxim I am sending signal only to update the variable value and the value is updated in the implementation of signal method of workflow interface.

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [September 29, 2021, 5:49am UTC](https://community.temporal.io/t/workflows-variables-activities/834/15 "2021-09-29T05:49:35Z")

</div>

Then your approach sounds fine.

---

<div class="post-metadata">

**Author:** ![Pritam\_Kumar2](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/pritam_kumar2/32/1342_2.png) [@Pritam\_Kumar2](https://community.temporal.io/u/Pritam_Kumar2)\
**Post date:** [September 29, 2021, 9:55am UTC](https://community.temporal.io/t/workflows-variables-activities/834/16 "2021-09-29T09:55:11Z")

</div>

Thank you very much @maxim .

---

<div class="post-metadata">

**Author:** ![Shai\_Zaban](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/shai_zaban/32/1999_2.png) [@Shai\_Zaban](https://community.temporal.io/u/Shai_Zaban)\
**Post date:** [May 18, 2022, 5:42am UTC](https://community.temporal.io/t/workflows-variables-activities/834/17 "2022-05-18T05:42:24Z")

</div>

hi, question regarding the sentence:

> But if you use a workflow variable

what do you mean by “workflow variable”? only one that is defined at the workflow level? or also local variables defined in a **workflow method**?

Thanks,  
Shai

---

<div class="post-metadata">

**Author:** ![tihomir](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/tihomir/32/6580_2.png) [@tihomir](https://community.temporal.io/u/tihomir)\
**Post date:** [May 18, 2022, 2:22pm UTC](https://community.temporal.io/t/workflows-variables-activities/834/18 "2022-05-18T14:22:39Z")

</div>

I believe what’s meant is if you use a variable defined in your workflow code as input to activity / child workflow invocations, or return it from a query handler method it has to be serializable, for example (java sdk):

```auto
public class MyWorkflowImpl implements MyWorkflowInterface {
  private MyObject myObject = ...; 

  // Workflow method 
  @Override
  public void myWorkflowMethod() {
      // ...
      activities.doSometing(myObject);
  }

  // Query Method
  @Override
  public MyObject queryMyObject() {
     return myObject;
  } 
  // ...
}

```

In this case MyObject has to be serializable by either the default data converters or a custom one that you specify.

---

<div class="post-metadata">

**Author:** ![Shai\_Zaban](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/shai_zaban/32/1999_2.png) [@Shai\_Zaban](https://community.temporal.io/u/Shai_Zaban)\
**Post date:** [May 18, 2022, 4:00pm UTC](https://community.temporal.io/t/workflows-variables-activities/834/19 "2022-05-18T16:00:26Z")

</div>

question about logs:  
how to correctly log from WorkFlow?  
how to correctly log from Activity?  
talking about Java\Kotlin

Thanks,  
shai

---

<div class="post-metadata">

**Author:** ![maxim](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/maxim/32/8_2.png) [@maxim](https://community.temporal.io/u/maxim)\
**Post date:** [May 18, 2022, 4:09pm UTC](https://community.temporal.io/t/workflows-variables-activities/834/20 "2022-05-18T16:09:23Z")

</div>

Please create a new discussion topic for a new question.

---

<div class="post-metadata">

**Author:** ![Shai\_Zaban](https://sea2.discourse-cdn.com/flex016/user_avatar/community.temporal.io/shai_zaban/32/1999_2.png) [@Shai\_Zaban](https://community.temporal.io/u/Shai_Zaban)\
**Post date:** [May 18, 2022, 8:34pm UTC](https://community.temporal.io/t/workflows-variables-activities/834/21 "2022-05-18T20:34:45Z")

</div>

thanks much!
