Skip to content

Batch operations

Learn how to perform multiple database operations efficiently in a single network request. Batch operations reduce latency and improve throughput when working with multiple records.

Why use batch operations?

Approach100 RecordsNetwork Roundtrips
Individual operations~100ms+100
Batch operation~5-10ms1

Batch operations send multiple requests in a single network call, as well as sending requests to multiple servers concurrently, dramatically reducing latency.

Batch read

Read multiple records in one request:

import com.aerospike.client.sdk.DataSet;
import com.aerospike.client.sdk.Record;
import com.aerospike.client.sdk.RecordResult;
import com.aerospike.client.sdk.RecordStream;
DataSet users = DataSet.of("test", "users");
RecordStream result = session.query(users.ids("user-1", "user-2", "user-3"))
.execute();
// Get all records
List<Record> records = new java.util.ArrayList<>();
result.forEach(rr -> {
if (rr.isOk()) {
records.add(rr.recordOrThrow());
}
});
for (Record record : records) {
System.out.println("Name: " + record.getString("name"));
}
// Or access by index
Record first = records.get(0); // user-1
Record second = records.get(1); // user-2

Batch write

Create or update multiple records:

session.insert(users)
.bins("name", "email")
.id("user-1").values("Alice", "alice@example.com")
.id("user-2").values("Bob", "bob@example.com")
.id("user-3").values("Carol", "carol@example.com")
.execute();

Batch upsert

Insert or update multiple records:

session
.upsert(users.ids("user-1","user-2"))
.bin("status").setTo("active")
.bin("balance").add(500)
.upsert(users.id("user-3"))
.bin("status").setTo("inactive")
.execute();

Batch delete

Delete multiple records:

RecordStream result = session.delete(users.ids("user-1", "user-2", "user-3"))
.execute();
// Check which deletes succeeded
int i = 0;
while (result.hasNext()) {
RecordResult rr = result.next();
System.out.println("Record " + i++ + " deleted: " + rr.asBoolean());
}
result.close();

Mixed batch operations

Combine different operation types in one call. Note that the operations on each key are performed asynchronously across the nodes in the cluster, and hence the returned results may not be in the same order as the commands. Each returned item exposes a key and an index — key() and index() in Java, the key and index attributes in Python (row.key, row.index). key is the unique key of the record and index is the zero-based index of the command in the original list.

RecordStream stream = session
.query(users.ids("user-1", "user-2"))
.upsert(users.id("user-3"))
.bin("status").setTo("active")
.delete(users.id("user-4"))
.execute();
stream.forEach(result -> {
switch (result.index()) {
case 0 -> handleUser1(result);
case 1 -> handleUser2(result);
case 2 -> System.out.println("Upsert: " + (result.isOk() ? "ok" : result.message()));
case 3 -> System.out.println("Delete: " + (result.isOk() ? "ok" : result.message()));
}
});

Batch with selected bins

Read only specific bins in batch:

RecordStream result = session.query(users.ids("user-1", "user-2", "user-3"))
.bins("name", "email")
.execute();

Handle partial failures

Some operations in a batch may fail while others succeed:

RecordStream result = session
.update(users.ids("user-1", "nonexistent", "user-3"))
.bin("lastSeen").setTo(System.currentTimeMillis())
.execute(ErrorStrategy.IN_STREAM); // Place errors in the RecordStream
result.forEach(rr -> {
String keyId = rr.key().userKey.toString();
if (!rr.isOk()) {
System.out.println(keyId + " failed: " + rr.message());
} else {
System.out.println(keyId + ": updated");
}
});

Dynamic batch building

Build batches programmatically:

import java.util.List;
import com.aerospike.client.sdk.IdValuesRowBuilder;
IdValuesRowBuilder rows = session.upsert(users)
.bins("name", "score")
.id("user-1").values("User 1", 10);
for (int id = 2; id <= 50; id++) {
rows.id("user-" + id).values("User " + id, id * 10);
}
rows.execute();

Complete example

import com.aerospike.client.sdk.Cluster;
import com.aerospike.client.sdk.ClusterDefinition;
import com.aerospike.client.sdk.DataSet;
import com.aerospike.client.sdk.Record;
import com.aerospike.client.sdk.RecordResult;
import com.aerospike.client.sdk.RecordStream;
import com.aerospike.client.sdk.Session;
import com.aerospike.client.sdk.policy.Behavior;
public class BatchOperationsExample {
public static void main(String[] args) {
try (Cluster cluster = new ClusterDefinition("localhost", 3000).connect()) {
Session session = cluster.createSession(Behavior.DEFAULT);
DataSet users = DataSet.of("test", "users");
String key1 = "batch-example-1";
String key2 = "batch-example-2";
String key3 = "batch-example-3";
// Cleanup so the example is repeatable.
session.delete(users.ids(key1, key2, key3)).execute().close();
// Batch insert
session.insert(users)
.bins("name", "age")
.id(key1).values("Alice", 28)
.id(key2).values("Bob", 35)
.id(key3).values("Carol", 22)
.execute();
System.out.println("Batch insert complete");
// Batch read
RecordStream readStream = session.query(users.ids(key1, key2, key3))
.execute()
.forEach(result -> {
Record record = result.recordOrThrow();
System.out.println(" - " + record.getString("name"));
});
// Batch update
session
.upsert(users.id(key1), users.id(key2))
.bin("status").setTo("active")
.upsert(users.id(key3))
.bin("status").setTo("inactive")
.execute().close();
System.out.println("\nBatch update complete");
// Batch delete
session.delete(users.ids(key1, key2, key3))
.execute().close();
System.out.println("Batch delete complete");
}
}
}

API reference summary

JavaPythonDescription
session.query(dataSet.ids(...))await session.query(data_set.ids(...)).execute()Batch-read multiple record IDs
session.insert(dataSet) + repeated .id().values()session.batch().insert(key).put({...}) (one per key)Batch insert with one request
session.upsert(dataSet) + repeated .id().values()session.batch().upsert(key).put({...}) (one per key)Batch upsert with one request
session.delete(dataSet.ids(...))await session.batch().delete(key)…execute() (one per key)Batch delete multiple IDs
.bins(...) (varargs).bins([...]) (list)Select projected bins for batch reads
RecordStream / forEachRecordStream / async forIterate per-record results

Next steps

Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?