All Products
Search
Document Center

CloudFlow:Global variables

Last Updated:May 07, 2025

When data in a workflow passes through multiple states, each state receives the output of the previous state as input. Passing variables state by state complicates cross-state data passing. CloudFlow provides a global variable mechanism to simplify this process and improve efficiency.

Limits

The following table describes the limits on global variables. If the maximum value cannot meet your business requirements, submit a ticket to increase the value.

Item

Maximum value

Length of a variable key

128 Bytes

Length of a variable value

256 KiB

Notes

Important
  • $Output in GlobalConstructor refers to the raw output of the Task state instead of the construction result of OutputConstructor.

  • $Global in OutputConstructor refers to the raw value before the construction made by GlobalConstructor.

Limits on context size

When CloudFlow passes data by using $Global, the outputs of InputConstructor and OutputConstructor must meet the limit that the context size cannot be greater than 64 KiB.

Race condition

Global variables provide execution-level global key-value storage space, which is readable and writable in all states throughout the execution lifecycle. When parallel logic, such as map and parallel, exists in a flow, data may be overwritten between different states. Take note of the following suggestions:

  1. Use different keys to write global variables into different branches of Parallel states.

  2. Do not write global variables into Map states. All iteration definitions of Map are the same. Therefore, the variable names in GlobalConstructor are the same. The same variable names written from different branches overwrite each other.

  3. A child execution in distributed mode cannot access the global variables of the parent execution because the Execution levels of the child and parent executions are different.

Use global variables to pass data

Global variables provide execution-level key-value storage space. Any state in a workflow can write data to the global variable at the end of the state to pass the data to other states for consumption.

image

In the preceding figure, State A uses GlobalConstructor to store the data that corresponds to $Input.key_a, and comes from the upstream input to variable_a in the global variables. State A also uses GlobalConstructor to store the data corresponding to $Output.key_b in the output of State A to variable_b in the global variables. The workflow uses InputConstructor to extract values of variable_a and variable_b in the global variables and passes the values to State Z as inputs. This way, data is passed across states.

image

The inputs and outputs of CloudFlow workflows are used to explicitly pass data between tasks to ensure the independence and clarity of tasks. Global variables are used to maintain cross-task sharing states or global resource configurations. Inputs, outputs, and global variables are used together to achieve an efficient and flexible system design. The preceding figure compares InputConstructor, OutputConstructor, and GlobalConstructor. Description:

  • InputConstructor($Context,$Global), OutputConstructor($Context,$Global), and GlobalConstructor($Context,$Global) indicate that InputConstructor, OutputConstructor, and GlobalConstructor can reference $Context and $Global to extract information.

  • $Global=Merge($Global,GlobalConstructor($Context,$Global)) indicates that the variables constructed by the global constructor are merged with the existing variables in $Global, and then all variables are stored in $Global to update shared global variables.

  • $Output is the result of the Task call. If the call is initiated in asynchronous callback mode, the value of $Output is the result of the callback.

  • $Global indicates global variables, which are accessible throughout the workflow execution period.

Creation and update of global variables

Global variables are created and updated by using GlobalConstructor. When a state execution ends, GlobalConstructor is executed. The following state machine definition shows how to use GlobalConstructor.

Type: StateMachine
Name: MyWorkFlow
SpecVersion: v1
StartAt: InvokeFunction
States:
  - Type: Task
    Name: InvokeFunction
    Action: FC:InvokeFunction
    TaskMode: RequestComplete
    Parameters:
      resourceArn: >-
        acs:fc:cn-hangzhou:123456:functions/myFunction/LATEST
      invocationType: Sync
      body.$: $Input 
    GlobalConstructor:
      InvokeFunctionResultHeader.$: $Output.Header
      InvokeFunctionResultBody.$: $Output.Body
    End: true
  • The data structure of GlobalConstructor is Map[String]Any.

  • InvokeFunctionResultHeader.$: $Output. Header and InvokeFunctionResultBody.$: $Output. Body indicate that the header and body of the returned function value are separately stored in the InvokeFunctionResultHeader and InvokeFunctionResultBody keys in the global variables.

Reading of global variables

CloudFlow defines the $Global keyword for reading global variables. In all scenarios that support expression construction, the global storage space of the current execution can be accessed by using $Global. States can extract the values from the global storage space by using explicit keys. The following state machine definition shows how to use $Global.

Type: StateMachine
Name: MyWorkFlow
SpecVersion: v1
StartAt: InvokeFunction1
States:
  - Type: Task
    Name: InvokeFunction1
    Action: FC:InvokeFunction
    TaskMode: RequestComplete
    Parameters:
      invocationType: Sync
      resourceArn: acs:fc:cn-hangzhou:123456:functions/myFunction/LATEST
    GlobalConstructor:
      InvokeFunctionPayload.$: $Output.Body
    Next: InvokeFunction
  - Type: Task
    Name: InvokeFunction
    Action: FC:InvokeFunction
    TaskMode: RequestComplete
    Parameters:
      resourceArn: acs:fc:cn-hangzhou:123456:functions/myFunction/LATEST
      invocationType: Sync
      body.$: $Global.InvokeFunctionPayload
    OutputConstructor:
      demo.$: $Global
    End: true
  • When a state is executed, the InvokeFunction function executes GlobalConstructor to store the body of the return value of InvokeFunction1 in the InvokeFunctionPayload key in the global variables.

  • InvokeFunction uses the $Global.InvokeFunctionPayload expression in Parameters to extract the value of the InvokeFunctionPayload key from the global storage space and passes the value as the body field to the Task state for execution.

  • InvokeFunction can use OutputConstructor to view the values in $Global. image