Batching (Pipeline and Transaction)
In Valkey GLIDE 2.0, the concept of Batch and ClusterBatch replaces the previous Transaction and ClusterTransaction APIs. Glide 2.0 introduces a robust Batch API with two primary modes:
- Atomic Batch: Guarantees that all commands in a batch execute as a single, atomic unit. No other commands can interleave (similar to MULTI/EXEC).
- Non-Atomic Batch (Pipeline): Sends multiple commands in one request without atomic guarantees. Commands can span multiple slots/nodes in a cluster and do not block other operations from being processed between them.
Both modes leverage the same classes— Batch for standalone mode and ClusterBatch for cluster mode — distinguished by an isAtomic flag. Extra configuration is provided via BatchOptions or ClusterBatchOptions, allowing control over timeouts, routings, and retry strategies.
Atomic Batch (Transaction)
Section titled “Atomic Batch (Transaction)”- Definition: A set of commands executed together as a single, indivisible operation.
- Guarantees: Sequential execution without interruption. Other clients cannot interleave commands between the batched operations.
- Slot Constraint (Cluster Mode): When running against a cluster, all keys in an atomic batch must map to the same hash slot. Mixing keys from different slots will cause the transaction to fail.
- Underlying Valkey: Equivalent to
MULTI/EXECValkey commands. - Use Case: When you need consistency and isolation.
- See: Valkey Transactions.
Example
Section titled “Example”from glide import Batch
transaction = Batch(is_atomic=True)transaction.set("key1", "value1")
client.exec(transaction)import glide.api.models.Batch;
boolean isAtomic = true;Batch batch = new Batch(isAtomic);batch.set("key1", "value1");
var response = client.exec(batch);import {Batch} from "@valkey/valkey-glide";
const isAtomic = true;const batch = new Batch(isAtomic);batch.set("key1", "value1");
const response = await client.exec(batch);import "github.com/valkey-io/valkey-glide/go/v2/pipeline";
isAtomic := true;transaction := pipeline.NewStandaloneBatch(true) .Set("key1", "value1");
res, err := client.Exec(context, transaction);Non-Atomic Batch (Pipeline)
Section titled “Non-Atomic Batch (Pipeline)”- Definition: A group of commands sent in a single request, but executed without atomicity or isolation.
- Behavior: Commands may be processed on different slots/nodes (in cluster mode), and other operations from different clients may interleave during execution.
- Underlying Valkey: Similar to pipelining, minimizing round-trip latencies by sending all commands at once.
- Use Case: Bulk reads or writes where each command is independent.
- See: Valkey Pipelines.
Example
Section titled “Example”from glide import Batch
transaction = Batch(is_atomic=False)transaction.set("key1", "value1")
client.exec(transaction)import glide.api.models.Batch;
boolean isAtomic = false;Batch batch = new Batch(isAtomic);batch.set("key1", "value1");
var response = client.exec(batch);import {Batch} from "@valkey/valkey-glide";
const isAtomic = false;const batch = new Batch(isAtomic);batch.set("key1", "value1");
const response = await client.exec(batch);import "github.com/valkey-io/valkey-glide/go/v2/pipeline";
isAtomic := true;transaction := pipeline.NewStandaloneBatch(false) .Set("key1", "value1");
res, err := client.Exec(context, transaction);What’s Next?
Section titled “What’s Next?”For detailed documentation, visit languages specific documentation: