Welcome to the new Golem Cloud Docs! 👋
Atomic Blocks and Durability Controls (Rust)

Atomic Blocks and Durability Controls (Rust)

Overview

Golem provides automatic durable execution — all agents are durable by default. These APIs are advanced controls that most agents will never need. Only use them when you have specific requirements around persistence granularity, idempotency, or atomicity.

Atomic Operations

Group side effects so they are retried together on failure:

use golem_rust::atomically;
 
let (a, b) = atomically(|| {
    let a = side_effect_1();
    let b = side_effect_2(a);
    (a, b)
});

Async version

use golem_rust::atomically_async;
 
let (a, b) = atomically_async(|| async {
    let a = side_effect_1().await;
    let b = side_effect_2(a).await;
    (a, b)
}).await;

If the agent fails mid-block, the entire block is re-executed on recovery rather than resuming from the middle.

Persistence Level Control

Temporarily disable oplog recording for performance-sensitive sections:

use golem_rust::{with_persistence_level, PersistenceLevel};
 
with_persistence_level(PersistenceLevel::PersistNothing, || {
    // No oplog entries — side effects will be replayed on recovery
    // Use for idempotent operations where replay is safe
});

Async version

use golem_rust::{with_persistence_level_async, PersistenceLevel};
 
with_persistence_level_async(PersistenceLevel::PersistNothing, || async {
    // No oplog entries — side effects will be replayed on recovery
}).await;

Idempotence Mode

Control whether HTTP requests are retried when the result is uncertain:

use golem_rust::with_idempotence_mode;
 
with_idempotence_mode(false, || {
    // HTTP requests won't be automatically retried
    // Use for non-idempotent external API calls (e.g., payments)
});

Async version

use golem_rust::with_idempotence_mode_async;
 
with_idempotence_mode_async(false, || async {
    // HTTP requests won't be automatically retried
}).await;

Oplog Commit

Wait until the oplog is replicated to a specified number of replicas before continuing:

use golem_rust::oplog_commit;
 
// Ensure oplog is replicated to 3 replicas before proceeding
oplog_commit(3);

Idempotency Key Generation

Generate a durable idempotency key that persists across agent restarts — safe for payment APIs and other exactly-once operations:

use golem_rust::generate_idempotency_key;
 
let key = generate_idempotency_key();
// Use this key with external APIs to ensure exactly-once processing

Retry Policy

Override the default retry policy for a block of code:

use golem_rust::{with_retry_policy, RetryPolicy};
 
with_retry_policy(RetryPolicy { /* ... */ }, || {
    // Code with custom retry behavior
});

Async version

use golem_rust::{with_retry_policy_async, RetryPolicy};
 
with_retry_policy_async(RetryPolicy { /* ... */ }, || async {
    // Code with custom retry behavior
}).await;