cassandraconsistency

What are SERIAL and LOCAL_SERIAL consistency levels in Cassandra?


According to this documentation, Cassandra has different consistency levels, two of which are called SERIAL and LOCAL_SERIAL. I understood all of the consistency levels except for these two levels. It says it has something to do with lightweight transactions but I don't get that either.

Would be great if someone could explain these levels to me.


Solution

  • Lightweight transactions (LWTs) are operations which need to be performed in sequence and cannot be interrupted that requires linearizable consistency -- in database terms, a record is locked while a transaction is in progress so no other process can operate on it.

    LWT is otherwise known as a compare-and-set (CAS) operation where the "compare" step (a conditional statement) must be satisfied before the "set" (write statement) is executed. In order to satisfy the condition, LWTs must perform a read (to compare) before it can write -- a read-before-write.

    A classic example usage for LWTs is for creating new usernames. Before a username is allocated to a user, we must first check (read) if the username is already in use (the "compare" step). If it does not already exist then we can create it, otherwise the user must pick another username.

    The conditional statement for CQL includes the use of IF EXISTS or IF NOT EXISTS clauses. For example:

    INSERT INTO users (username, name) VALUES (?, ?) IF NOT EXISTS
    

    The serial consistencies SERIAL and LOCAL_SERIAL are exclusively used for lightweight transactions during the read phase of the "read-before-write". You can choose to use the SERIAL consistency (requires a quorum of replicas in all DCs) or LOCAL_SERIAL (requires a quorum of replicas in the local DC).

    Serial consistencies cannot be used for normal reads or writes, only for LWTs.

    For more info, see Lightweight transactions in Cassandra. Cheers!