# Side Effects

> For the complete documentation index, see [llms.txt](https://docs.temporal.io/llms.txt).
> Any documentation page is available as raw Markdown by appending `.md` to its URL.
> For related questions, query https://docs.temporal.io/api/retrieve?q=<question>&jtb=<job-to-be-done>&src=md-page&agent=<your-name> (no auth; returns JSON `{content, source_url, page_url}`, with `source_url` pointing to Markdown for docs pages). Don't include personal data or credentials.
> If this page is wrong or missing something you needed, or Temporal behaves differently from what it says, report it with https://docs.temporal.io/api/feedback?title=<summary>&message=<what-happened>&category=documentation&page_url=/develop/java/workflows/side-effects&src=md-page&agent=<your-name> (use `category=product` when the product, not the page, is wrong). Report only a specific problem you hit, send it once, and don't include personal data or credentials.

> Side Effects in a Workflow execute non-deterministic code like generating a UUID. The result is saved in Workflow Event History for consistent replays without re-execution.

Side Effects are used to execute non-deterministic code, such as generating a UUID or a random number, without compromising determinism in the Workflow.
This is done by storing the non-deterministic results of the Side Effect into the Workflow [Event History](/workflow-execution/event#event-history).

A Side Effect does not re-execute during a Replay. Instead, it returns the recorded result from the Workflow Execution Event History.

Side Effects should not fail. An exception that is thrown from the Side Effect causes failure and retry of the current Workflow Task.

An Activity or a Local Activity may also be used instead of a Side effect, as its result is also persisted in Workflow Execution History.

> **📝 Note:**
>
> You shouldn't modify the Workflow state inside a Side Effect function, because it is not reexecuted during Replay. Side Effect function should be used to return a value.
>

To use a Side Effect in Java, set the [`sideEffect()`](https://www.javadoc.io/doc/io.temporal/temporal-sdk/latest/io/temporal/workflow/Workflow.html#sideEffect(java.lang.Class,io.temporal.workflow.Functions.Func)) function in your Workflow Execution and return the non-deterministic code.

<!--SNIPSTART java-side-effects-basic {"enable_code_dedenting": true}-->
[java-sandbox/workflows/side-effects/src/main/java/io/temporal/docs/workflows/sideeffects/SideEffectsExample.java](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/java-sandbox/workflows/side-effects/src/main/java/io/temporal/docs/workflows/sideeffects/SideEffectsExample.java)
```java
int randomValue = Workflow.sideEffect(Integer.class, () -> new Random().nextInt(100));
if (randomValue < 50) {
    // ...
} else {
    // ...
}
```
<!--SNIPEND-->

Here's another example that uses `sideEffect()`.

<!--SNIPSTART java-side-effects-secure-random {"enable_code_dedenting": true}-->
[java-sandbox/workflows/side-effects/src/main/java/io/temporal/docs/workflows/sideeffects/SideEffectsExample.java](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/java-sandbox/workflows/side-effects/src/main/java/io/temporal/docs/workflows/sideeffects/SideEffectsExample.java)
```java
// implementation of the @WorkflowMethod
public void execute() {
    int randomInt = Workflow.sideEffect(int.class, () -> {
        Random random = new SecureRandom();
        return random.nextInt();
    });

    String userHome = Workflow.sideEffect(String.class, () -> System.getenv("USER_HOME"));

    if (randomInt % 2 == 0) {
        // ...
    } else {
        // ...
    }
}
```
<!--SNIPEND-->

Java also provides a deterministic method to generate random numbers or random UUIDs.

To generate random numbers in a deterministic method, use [`newRandom()`](https://javadoc.io/doc/io.temporal/temporal-sdk/latest/io/temporal/workflow/Workflow.html#newRandom).

<!--SNIPSTART java-side-effects-new-random {"enable_code_dedenting": true}-->
[java-sandbox/workflows/side-effects/src/main/java/io/temporal/docs/workflows/sideeffects/SideEffectsExample.java](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/java-sandbox/workflows/side-effects/src/main/java/io/temporal/docs/workflows/sideeffects/SideEffectsExample.java)
```java
// implementation of the @WorkflowMethod
public void execute() {
    int randomInt = Workflow.newRandom().nextInt();
    // ...
}
```
<!--SNIPEND-->

To generate a random UUID in a deterministic method, use [`randomUUID()`](https://www.javadoc.io/doc/io.temporal/temporal-sdk/latest/io/temporal/workflow/Workflow.html#randomUUID()).

<!--SNIPSTART java-side-effects-random-uuid {"enable_code_dedenting": true}-->
[java-sandbox/workflows/side-effects/src/main/java/io/temporal/docs/workflows/sideeffects/SideEffectsExample.java](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/java-sandbox/workflows/side-effects/src/main/java/io/temporal/docs/workflows/sideeffects/SideEffectsExample.java)
```java
// implementation of the @WorkflowMethod
public void execute() {
    String randomUUID = Workflow.randomUUID().toString();
    // ...
}
```
<!--SNIPEND-->
