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.

from glide import (
GlideClientConfiguration,
NodeAddress,
GlideClient,
BatchOptions,
)
# Create client configuration
addresses = [
NodeAddress("server_primary.example.com", 6379),
NodeAddress("server_replica.example.com", 6379)
]
config = GlideClientConfiguration(addresses)
# Initialize client
client = await GlideClient.create(config)
# Configure batch options
options = BatchOptions(timeout=2000)
# Create atomic batch (true indicates atomic/transaction mode)
atomic_batch = Batch(True)
atomic_batch.set("account:source", "100")
atomic_batch.set("account:dest", "0")
atomic_batch.incrby("account:dest", 50)
atomic_batch.decrby("account:source", 50)
atomic_batch.get("account:source")
try:
# Execute with raiseOnError = true
results = await client.exec(atomic_batch, raise_on_error=True, options=options)
print("Atomic Batch Results:", results)
# Expected output: Atomic Batch Results: ['OK', 'OK', 50, 50, '50']
except RequestError as e:
print(f"Batch failed:", e)
from glide import (
GlideClusterClientConfiguration,
NodeAddress,
GlideClusterClient,
ClusterBatch,
ClusterBatchOptions
)
# Initialize cluster client configuration
addresses = [
NodeAddress("127.0.0.1", 6379)
]
config = GlideClusterClientConfiguration(addresses)
# Initialize client
glideClusterClient = await GlideClusterClient.create(config)
# Configure atomic batch options
options = ClusterBatchOptions(timeout=3000) # 3-second timeout
# Create atomic cluster batch (all keys map to same slot)
atomicClusterBatch = ClusterBatch(True)
atomicClusterBatch.set("user:100:visits", "1")
atomicClusterBatch.incrby("user:100:visits", 5)
atomicClusterBatch.get("user:100:visits")
try:
# Execute with raise_on_error = True
clusterResults = await glideClusterClient.exec(atomicClusterBatch, raise_on_error=True, options=options)
print("Atomic Cluster Batch:", clusterResults)
# Expected output: Atomic Cluster Batch: ['OK', 6, '6']
except RequestError as e:
print("Atomic cluster batch failed:", e)

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.

from glide import (
GlideClientConfiguration,
NodeAddress,
GlideClient,
Batch,
BatchOptions
)
# Create client configuration
addresses = [
NodeAddress("localhost", 6379)
]
config = GlideClientConfiguration(addresses)
# Initialize client
client = await GlideClient.create(config)
# Configure batch options
options = BatchOptions(timeout=2000) # 2-second timeout
# Create non-atomic batch (False indicates pipeline mode)
pipeline = Batch(False)
pipeline.set("temp:key1", "value1")
pipeline.set("temp:key2", "value2")
pipeline.get("temp:key1")
pipeline.get("temp:key2")
# Execute with raise_on_error = False
results = await client.exec(pipeline, raise_on_error=False, options=options)
print("Pipeline Results:", results)
# Expected output: Pipeline Results: ['OK', 'OK', 'value1', 'value2']
from glide import (
GlideClusterClientConfiguration,
NodeAddress,
GlideClusterClient,
ClusterBatch,
ClusterBatchOptions,
ClusterBatchRetryStrategy
)
# Initialize cluster client configuration
addresses = [
NodeAddress("localhost", 6379)
]
config = GlideClusterClientConfiguration(addresses)
# Initialize client
glideClusterClient = await GlideClusterClient.create(config)
# Configure retry strategy and pipeline options
retry_strategy = ClusterBatchRetryStrategy(
retry_server_error=False,
retry_connection_error=True
)
pipeline_options = ClusterBatchOptions(
timeout=5000, # 5-second timeout
retry_strategy=retry_strategy
)
# Create pipeline spanning multiple slots
pipeline_cluster = ClusterBatch(False) # False indicates non-atomic (pipeline)
pipeline_cluster.set("page:home:views", "100")
pipeline_cluster.incrby("page:home:views", 25)
pipeline_cluster.get("page:home:views")
pipeline_cluster.lpush("recent:logins", ["user1"])
pipeline_cluster.lpush("recent:logins", ["user2"])
pipeline_cluster.lrange("recent:logins", 0, 1)
# Execute with raise_on_error = False
pipeline_results = await glideClusterClient.exec(pipeline_cluster, raise_on_error=False, options=pipeline_options)
print("Pipeline Cluster Results:", pipeline_results)
# Expected output: Pipeline Cluster Results: ['OK', 125, '125', 1, 2, ['user2', 'user1']]

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