Was this page helpful?
Caution
You're viewing documentation for an unstable version of ScyllaDB Rust Driver. Switch to the latest stable version.
Lightweight transaction (LWT) statement¶
A lightweight transaction statement can be expressed just like any other statement, via Session, with the notable difference of having an additional consistency level parameter - the serial_consistency_level.
Format of the statement¶
A lightweight transaction statement is not a separate type - it can be expressed just like any other statements: via Statement, PreparedStatement, batches, and so on. The difference lays in the statement string itself - when it contains a condition (e.g. IF NOT EXISTS), it becomes a lightweight transaction. It’s important to remember that CQL specification requires a separate, additional consistency level to be defined for LWT statements - serial_consistency_level. The serial consistency level can only be set to two values: SerialConsistency::Serial or SerialConsistency::LocalSerial. The “local” variant makes the transaction consistent only within the same datacenter. For convenience, ScyllaDB Rust Driver sets the default consistency level to LocalSerial, as it’s more commonly used. For cross-datacenter consistency, please remember to always override the default with SerialConsistency::Serial.
use scylla::statement::unprepared::Statement;
use scylla::statement::{Consistency, SerialConsistency};
// Create a Statement manually to change the Consistency to ONE
let mut my_statement: Statement = Statement::new("INSERT INTO ks.tab (a) VALUES(?) IF NOT EXISTS".to_string());
my_statement.set_consistency(Consistency::One);
// Use cross-datacenter serial consistency
my_statement.set_serial_consistency(Some(SerialConsistency::Serial));
// Insert a value into the table
let to_insert: i32 = 12345;
session.query_unpaged(my_statement, (to_insert,)).await?;
The rest of the API remains identical for LWT and non-LWT statements.
SELECT as LWT¶
A SELECT statement can also be executed as a lightweight transaction by setting its consistency level to Serial or LocalSerial. Since SELECT statements never contain an IF clause, this is the only way to execute them as LWT. The driver automatically detects this case and applies LWT routing optimisation (deterministic replica ordering) for such requests.
use scylla::statement::unprepared::Statement;
use scylla::statement::Consistency;
let mut my_statement: Statement = Statement::new("SELECT * FROM ks.tab WHERE a = ?".to_string());
// Setting consistency to Serial makes the server execute this SELECT via Paxos.
my_statement.set_consistency(Consistency::Serial);
session.query_unpaged(my_statement, (12345_i32,)).await?;
See Statement API documentation for more options