Friday, September 8, 2023

Consistency level for LWT in Cassandra

 How to set consistency level  and serial consistency.

Cassandra’s lightweight transactions are limited to a single partition. Internally, Cassandra stores a Paxos state for each partition. This ensures that transactions on different partitions cannot interfere with each other.

cqlsh:aug_space> consistency ONE;
Consistency level set to ONE.
cqlsh:aug_space> SERIAL CONSISTENCY;
Current serial consistency level is SERIAL.
cqlsh:aug_space> Insert into bank_emp_record(Name, bank_name, Id) values('Dinesh', 'Axis Bank', 201) IF NOT EXISTS; 

 [applied] | name   | id  | bank_name
-----------+--------+-----+-----------
     False | Dinesh | 201 | Axis Bank

cqlsh:aug_space> INSERT INTO users (id, addresses, emails, ip_numbers) VALUES (UUID(), {'home':'192, 10th cross, wilson'} ,['shweta001@yahoo.com'],{'10.10.11.1', '10.10.10.1', '10.10.12.2'}) IF NOT EXISTS;

 [applied]
-----------
      True

cqlsh:aug_space> select * from users;

 id                                   | addresses                             | emails                  | ip_numbers
--------------------------------------+---------------------------------------+-------------------------+--------------------------------------------
 44b37d64-ba2b-435f-99a7-c5a4bc81a10c | {'home': ('192, 10th cross, wilson')} | ['shweta001@yahoo.com'] | {'10.10.10.1', '10.10.11.1', '10.10.12.2'}
 dd463353-a538-430d-810f-7df19a3cc148 | {'home': ('192, 10th cross, wilson')} | ['shweta001@yahoo.com'] | {'10.10.10.1', '10.10.11.1', '10.10.12.2'}
 1c914387-7e4f-4e69-9aad-72d9b854d31d | {'home': ('192, 10th cross, wilson')} | ['shweta001@yahoo.com'] | {'10.10.10.1', '10.10.11.1', '10.10.12.2'}
 6ab09bec-e68e-48d9-a5f8-97e6fb4c9b47 |                                  null |                    null |               {'10.10.14.1', '10.10.24.1'}
 732d1cb7-53cf-4b0a-892e-3edfcc2c1d51 | {'home': ('192, 10th cross, wilson')} | ['shweta001@yahoo.com'] | {'10.10.10.1', '10.10.11.1', '10.10.12.2'}

(5 rows)
cqlsh:aug_space> INSERT INTO users (id, addresses, emails, ip_numbers) VALUES (1234567, {'home':'192, 10th cross, wilson'} ,['shweta001@yahoo.com'],{'10.10.11.1', '10.10.10.1', '10.10.12.2'}) IF NOT EXISTS;
InvalidRequest: Error from server: code=2200 [Invalid query] message="Invalid INTEGER constant (1234567) for "id" of type uuid"
cqlsh:aug_space> INSERT INTO users (id, addresses, emails, ip_numbers) VALUES (732d1cb7-53cf-4b0a-892e-3edfcc2c1d51, {'home':'192, 10th cross, wilson'} ,['shweta001@yahoo.com'],{'10.10.11.1', '10.10.10.1', '10.10.12.2'}) IF NOT EXISTS;

 [applied] | id                                   | addresses                             | emails                  | ip_numbers
-----------+--------------------------------------+---------------------------------------+-------------------------+--------------------------------------------
     False | 732d1cb7-53cf-4b0a-892e-3edfcc2c1d51 | {'home': ('192, 10th cross, wilson')} | ['shweta001@yahoo.com'] | {'10.10.10.1', '10.10.11.1', '10.10.12.2'}

cqlsh:aug_space> 


cqlsh:aug_space>  select * from bank_emp_record;

 name   | id  | bank_name
--------+-----+--------------------------
  David | 410 |               ICICI Bank
 Dinesh | 201 |                Axis Bank
 Dinesh | 202 |                Axis Bank
 Dinesh | 203 |                Axis Bank
 Dinesh | 204 |                Axis Bank
 Ashish | 101 | Employee bank change new
 Ashish | 102 | Employee bank change new
 Ashish | 103 | Employee bank change new
 Ashish | 104 | Employee bank change new
 Ashish | 105 | Employee bank change new

(10 rows)
cqlsh:aug_space>  update bank_emp_record set bank_name ='HDFC Bank'  where name='David'  IF EXISTS;

 [applied]
-----------
      True

cqlsh:aug_space>  update bank_emp_record set bank_name ='ICICI Bank'  where name='David' ;
cqlsh:aug_space>  select * from bank_emp_record;

 name   | id  | bank_name
--------+-----+--------------------------
  David | 410 |               ICICI Bank
 Dinesh | 201 |                Axis Bank
 Dinesh | 202 |                Axis Bank
 Dinesh | 203 |                Axis Bank
 Dinesh | 204 |                Axis Bank
 Ashish | 101 | Employee bank change new
 Ashish | 102 | Employee bank change new
 Ashish | 103 | Employee bank change new
 Ashish | 104 | Employee bank change new
 Ashish | 105 | Employee bank change new

(10 rows)
cqlsh:aug_space>  update bank_emp_record set bank_name ='ICICI Bank'  where name='David'  IF bank_name ='HDFC Bank' ;

 [applied] | bank_name
-----------+------------
     False | ICICI Bank


No comments: