ScyllaDB University Live | Free Virtual Training Event
Learn more
ScyllaDB Documentation Logo Documentation
  • Server
  • Cloud
  • Tools
    • ScyllaDB Manager
    • ScyllaDB Monitoring Stack
    • ScyllaDB Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
  • Resources
    • ScyllaDB University
    • Community Forum
    • Tutorials
Download
ScyllaDB Docs Scylla C/C++ Driver Features Basics Batches

BatchesΒΆ

Note: Cassandra 2.0+ is required.

Batches can be used to group multiple mutations (UPDATE, INSERT, DELETE) together into a single statement; simple or prepared. There are three different types of batches supported by Cassandra 2.0 or later.

  • CASS_BATCH_TYPE_LOGGED is used to make sure that multiple mutations across multiple partitions happen atomically, that is, all the included mutations will eventually succeed. However, there is a performance penalty imposed by atomicity guarantee.

  • CASS_BATCH_TYPE_UNLOGGED is generally used to group mutations for a single partition and do not suffer from the performance penalty imposed by logged batches, but there is no atomicity guarantee for multi-partition updates.

  • CASS_BATCH_TYPE_COUNTER is used to group counters updates.

Important: Be careful when using batches as a performance optimization.

void execute_batch(CassSession* session) {
  /* This logged batch will make sure that all the mutations eventually succeed */
  CassBatch* batch = cass_batch_new(CASS_BATCH_TYPE_LOGGED);

  /* Statements can be immediately freed after being added to the batch */

  {
    CassStatement* statement
      = cass_statement_new("INSERT INTO example1(key, value) VALUES ('a', '1')", 0);
    cass_batch_add_statement(batch, statement);
    cass_statement_free(statement);
  }

  {
    CassStatement* statement
      = cass_statement_new("UPDATE example2 set value = '2' WHERE key = 'b'", 0);
    cass_batch_add_statement(batch, statement);
    cass_statement_free(statement);
  }

  {
    CassStatement* statement
      = cass_statement_new("DELETE FROM example3 WHERE key = 'c'", 0);
    cass_batch_add_statement(batch, statement);
    cass_statement_free(statement);
  }

  CassFuture* batch_future = cass_session_execute_batch(session, batch);

  /* Batch objects can be freed immediately after being executed */
  cass_batch_free(batch);

  /* This will block until the query has finished */
  CassError rc = cass_future_error_code(batch_future);

  printf("Batch result: %s\n", cass_error_desc(rc));

  cass_future_free(batch_future);
}

Was this page helpful?

PREVIOUS
Basics
NEXT
Binding Parameters
  • Create an issue
  • Edit this page
Scylla C/C++ Driver
  • master
    • master
  • C/C++ Driver for ScyllaDB
  • API Documentation
    • CassAggregateMeta
    • CassAuthenticator
    • CassAuthenticatorCallbacks
    • CassBatch
    • CassCluster
    • CassCollection
    • CassColumnMeta
    • CassCustomPayload
    • CassDataType
    • CassErrorResult
    • CassExecProfile
    • CassFunctionMeta
    • CassFuture
    • CassIndexMeta
    • CassInet
    • CassIterator
    • CassKeyspaceMeta
    • CassLogMessage
    • CassMaterializedViewMeta
    • CassMetrics
    • CassNode
    • CassPrepared
    • CassResult
    • CassRetryPolicy
    • CassRow
    • CassSchemaMeta
    • CassSession
    • CassSpeculativeExecutionMetrics
    • CassSsl
    • CassStatement
    • CassTableMeta
    • CassTimestampGen
    • CassTuple
    • CassUserType
    • CassUuid
    • CassUuidGen
    • CassValue
    • CassVersion
    • DseDateRange
    • DseDateRangeBound
    • DseLineString
    • DseLineStringIterator
    • DsePolygon
    • DsePolygonIterator
  • Features
    • Basics
      • Batches
      • Binding Parameters
      • Client-side timestamps
      • Consistency
      • Data Types
      • The date and time Types
      • Futures
      • Handling Results
      • Keyspaces
      • Prepared Statements
      • Schema Metadata
      • Tuples
      • User-Defined Types (UDTs)
      • UUIDs
    • Building
    • Client Configuration
    • Cloud
    • Configuration
      • Retry policies
    • Execution Profiles
    • FAQ
    • Installation
    • Logging
    • Metrics
    • Scylla Specific Features
    • Security
      • SSL
    • Testing
      • Cassandra Cluster Manager (CCM)
    • Tracing
  • DSE Features
    • Authentication
    • Geospatial types
Docs Tutorials University Contact Us About Us
© 2025, ScyllaDB. All rights reserved. | Terms of Service | Privacy Policy | ScyllaDB, and ScyllaDB Cloud, are registered trademarks of ScyllaDB, Inc.
Last updated on 28 April 2025.
Powered by Sphinx 7.4.7 & ScyllaDB Theme 1.8.6