Skip to content

Send Batch Commands

With GLIDE 2.0, Batch API replaces the previous Transaction API to support both atomic and non-atomic execution of commands in batches. This guide will go over how to send commands in batches using the new Batch API.

To learn more on how GLIDE handles transactions and pipelining, see our article.

In GLIDE 2.0, the Batch API provides a unified interface for handling both Transactions and Pipelining, allowing you to choose between strict consistency or high-throughput performance depending on your needs.

A transaction is an all-or-nothing set of commands sent in a single request to Valkey. GLIDE handles this as atomic batches.

import glide.api.GlideClient;
import glide.api.models.configuration.GlideClientConfiguration;
import glide.api.models.Batch;
import glide.api.models.BatchOptions;
import glide.api.models.exceptions.RequestException;
// Create client configuration
GlideClientConfiguration config = GlideClientConfiguration.builder()
.address(NodeAddress.builder().host("localhost").port(6379).build())
.build();
// Initialize client
GlideClient client = GlideClient.createClient(config).get();
// Configure batch options
BatchOptions options = BatchOptions.builder()
.timeout(2000) // 2-second timeout
.build();
// Create atomic batch
Batch atomicBatch = new Batch(true)
.set("account:source", "100")
.set("account:dest", "0")
.incrBy("account:dest", 50)
.decrBy("account:source", 50)
.get("account:source");
// Execute with raiseOnError = true
try {
Object[] results = client.exec(atomicBatch, true, options).get();
System.out.println("Atomic Batch Results: " + Arrays.toString(results));
// Atomic Batch Results: ["OK", "OK", 50, 50, "50"]
} catch (RequestException e) {
System.err.println("Batch failed: " + e.getMessage());
}
import glide.api.models.ClusterBatch;
import glide.api.models.ClusterBatchOptions;
import glide.api.models.exceptions.RequestException;
import glide.api.models.configuration.GlideClusterClientConfiguration;
import glide.api.models.NodeAddress;
import glide.api.GlideClusterClient;
// Initialize cluster client
GlideClusterClientConfiguration config = GlideClusterClientConfiguration.builder()
.address(
NodeAddress.builder()
.host("127.0.0.1")
.port(6379)
.build()
)
.build();
// Initialize client
GlideClusterClient glideClusterClient = GlideClusterClient.createClient(config).get();
// Configure atomic batch options
ClusterBatchOptions options = ClusterBatchOptions.builder()
.timeout(3000) // 3-second timeout
.build();
// Create atomic cluster batch (all keys map to same slot)
ClusterBatch atomicClusterBatch = new ClusterBatch(true)
.set("user:100:visits", "1")
.incrBy("user:100:visits", 5)
.get("user:100:visits");
// Execute with raiseOnError = true
try {
Object[] clusterResults = glideClusterClient.exec(atomicClusterBatch, true, options).get();
System.out.println("Atomic Cluster Batch: " + Arrays.toString(clusterResults));
// Expected- Atomic Cluster Batch: ["OK", 6, "6"]
} catch (RequestException e) {
System.err.println("Atomic cluster batch failed: " + e.getMessage());
}

Pipelining is a group of commands sent in a single request that does not guarantee atomicity or isolation. GLIDE handles this as non-atomic batches.

import glide.api.GlideClient;
import glide.api.models.Batch;
import glide.api.models.BatchOptions;
// Create client configuration
GlideClientConfiguration config = GlideClientConfiguration.builder()
.address(NodeAddress.builder().host("localhost").port(6379).build())
.build();
// Initialize client
GlideClient client = GlideClient.createClient(config).get();
// Configure batch options
BatchOptions options = BatchOptions.builder()
.timeout(2000) // 2-second timeout
.build();
Batch pipeline = new Batch(false)
.set("temp:key1", "value1")
.set("temp:key2", "value2")
.get("temp:key1")
.get("temp:key2");
Object[] results = client.exec(pipeline, false, options).get();
System.out.println("Pipeline Results: " + Arrays.toString(results));
// Pipeline Results: ["OK", "OK", "value1", "value2"]
import glide.api.models.ClusterBatch;
import glide.api.models.ClusterBatchOptions;
import glide.api.models.commands.batch.ClusterBatchRetryStrategy;
import glide.api.models.configuration.GlideClusterClientConfiguration;
import glide.api.models.NodeAddress;
import glide.api.GlideClusterClient;
// Initialize cluster client
GlideClusterClientConfiguration config = GlideClusterClientConfiguration.builder()
.address(
NodeAddress.builder()
.host("localhost")
.port(6379)
.build()
)
.build();
GlideClusterClient glideClusterClient = GlideClusterClient.createClient(config).get();
// Configure retry strategy and pipeline options
ClusterBatchRetryStrategy retryStrategy = ClusterBatchRetryStrategy.builder()
.retryServerError(false)
.retryConnectionError(true)
.build();
ClusterBatchOptions pipelineOptions = ClusterBatchOptions.builder()
.timeout(5000) // 5-second timeout
.retryStrategy(retryStrategy)
.build();
// Create pipeline spanning multiple slots
ClusterBatch pipelineCluster = new ClusterBatch(false)
.set("page:home:views", "100")
.incrBy("page:home:views", 25)
.get("page:home:views")
.lpush("recent:logins", "user1")
.lpush("recent:logins", "user2")
.lrange("recent:logins", 0, 1);
// Execute with raiseOnError = false
Object[] pipelineResults = glideClusterClient.exec(pipelineCluster, false, pipelineOptions).get();
System.out.println("Pipeline Cluster Results: " + Arrays.toString(pipelineResults));
// Pipeline Cluster Results: ["OK", 125, "125", 1, 2, ["user2", "user1"]]

To lean more about how GLIDE handles batch commands, see our explanation.