Use lightweight transactions (LWTs)

INSERT and UPDATE statements using the IF clause support lightweight transactions, also known as Compare and Set (CAS). A common use for lightweight transactions is an insertion operation that must be unique, such as a user’s identification.

Lightweight transactions should not be used casually, as the latency of operations increases fourfold due to the round-trips necessary between the CAS coordinators.

Non-equal conditions for lightweight transactions are supported; you can use <, , >, >=, != and IN operators in WHERE clauses to query lightweight tables.

It is important to note that using IF NOT EXISTS on an INSERT, the timestamp will be designated by the lightweight transaction, and USING TIMESTAMP is prohibited.

For example:

  1. Insert a row that you will later change with a CAS operation. In this example, the cyclist’s name is misspelled.

    INSERT INTO cycling.cyclist_name (
      id, lastname, firstname
    ) VALUES (
      4647f6d3-7bd2-4085-8d6c-1229351b5498, 'KNETEMANN', 'Roxxane'
    )
    IF NOT EXISTS;
  2. Perform a CAS operation against the row by adding the predicate for the operation (IF) to the end of the query. In this example, the firstname column is changed if the existing value is the misspelled value:

    UPDATE cycling.cyclist_name SET firstname = 'Roxane'
      WHERE id = 4647f6d3-7bd2-4085-8d6c-1229351b5498 IF firstname = 'Roxxane';

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax, an IBM Company | Privacy policy | Terms of use | Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com