Cassandra

Posted on Posted in Technologies

Cassandra are non-relational/NoSQL is a highly scalable, high-performance distributed database designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure. Cassandra used by giants like NetFlix, eBay, Twitter and Reddit.

Snitches 

Determines how Cassandra can tell the topology of the infrastructure. In layman’s term how nodes would talk to each other.

Types of Snitches

  1. Dynamic
  2. Simple
  3. Rack Inferring
  4. Property File
  5. Gossiping Property File
  6. Ec2
  7. Ec2 MultiRegion

By Default, for nodes to work each nodes should have same snitch type, for example:

PropetyFileSnitch Example
Node1: 130.77.100.147=<DataCenter>:<Rack>
Node2: 130.77.100.148=<DataCenter>:<Rack>
Node3: 130.77.100.149=<DataCenter>:<Rack>

Con for propertyFileSnitch: very tedious as you need to update manually for each node

Key Concepts

Internal communication : Gossip – is how to node communicate with each other – runs every 1 second
External Communication : CQL / Thrift

Data distribution

  • data rows are distributed
  • partitioner is used to determine distribution
  • Murmur3 is default partitioner, it determine a hash to which node to distribute toEach node has a hash determined to handle which set of hash to handle
  • You can use murmur3 calculator to determine hash

Replication Factor – replication of data to different nodes for high availability

Virtual Nodes – each node can have small token ranges (256 by default)

main config file: cassandra.yaml
partitioner: partioner type

COMMANDS:

CREATE KEYSPACE sampleWITH replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1};
INSERT INTO sample.activity (id,description, datetime, code)VALUES ('1', 'test', '2017-10-10 01:01:01', 'CODE');
INSERT INTO sample.activity (id) VALUES ('2');
select * from sample.activity;

CREATE TABLE sample.activity ( id text PRIMARY KEY, description text, datetime timestamp, code text);
COPY FROM CSV command:
copy activity (id, code, description) from '/var/lib/cassandra/import.csv' WITH header = false AND delimiter = '|';

Docker side notes:

docker exec -it <container_name> cqlsh – to run cqlsh from docker
docker cp to copy files into container
OR
Just add in Dockerfile

Data Storage:

DATA is stored via partition key and can be viewed via cassandra-client:

Partition key 1:  events events events

Partition key 2:  events events events

Data is stored on disk by both disk and in memory using memcache, then flushed to disk as SSTable

NOTE WHERE CLAUSE GENERALLY NEED TO INCLUDE PARTITION KEY can’t query using other columns that are not partition key, you need secondary indexes

CONTRARY TO SQL – SECONDARY INDEXES DOES NOT MAKE THE QUERY FASTER, INSTEAD IT CREATES A HIDDEN TABLE UNDERNEATH ANOTHER OPTION IS TO CREATE ANOTHER TABLE FOR SPECIFICALLY THAT QUERY ONLY.

Secondary INDEXES

  • PRO: you dont need to manually maintain
  • CON: Slower, as it accesses all nodes

For example you have a activities table, with code column where code is not a partition if you want to query activities per code, you can create a table with activities_per_code table, where you use code in this table as primary

  • PRO: you dont need to access all nodes when querying another table
  • CON: harder to maintain
HOW TO CREATE INDEX:
CREATE INDEX <index_name> ON <table> (column_name);
  1. HARDWARE:
    minimum 8gb per node
    prod 32gb of ram per node
  2. minimum core 4 gb
    common 8gb core
  3. Disk: ( do NOT SHARE)
    SSD are prefered 500gb to 1tb
    commit log should be seperate

When updates are called, it just keeps adding to it then it determines which is the latest versions

Command:

SOURCE: executes a series of commands in a file

DELETE command – delete a value from a row, or delete a whole row

TRUNCATE/DELETE are as is

DESCRIBE KEYSPACES

When you delete a data, a tombstone is created, to allow nodes to replicate this gc_grace_seconds, 10 days by default

Compaction is when is when delete is actually done, Compaction is when SStables are merged,

can be triggered manually via nodetool command

nodetool sstable2json – convert tables to json
TTL for cleanup of data, you can specify ttl on insert data to automatically delete said datayou can update TTL value as well

execute nodetool commands in docker, check node status:
 docker exec -it mel_cass sh -c "nodetool status"

NETWORKING ports:

  • 7000 for gossiping
  • 9042: expose
  • 9160: thrift
  • 7199: JMX

Cassandra.yml

listen_address – ip for other nodes to find

rpc_address – ip for your app to find cassandra

seeds – for declaring all the seeds in your cassandra

bootstrap

  • adding more cluster
  • auto-bootstrap should be true in cassandra.yml
  • num_tokens – number of tokens to be handled by node
  • cluster name should be the same
  • you can just specify one or two seeds on one cluster and it will autodetect other nodes

When a node auto bootstraps, say node 1 has data ABC, and node 2 joins and should handle BCnode1 has still that data, you need a cleanup command to clean node 1

cleanup command:nodetool -h <ip> cleanup
Cassandra-stress – used for stress-testing cassandra

MONITORING:

JConsole – Java UI

Nodetool – cassandra console

Datastax Ops Center – you need to install opscenter agent
nodetool decommission

  • choose to decommission a node, tokens are assigned to other nodes, copies to other data
  • node has still data, better have a clean node
  • so clean the whole data first: rm -r commitlog data saved_caches
  • nodetool removenode – so that tokens are reassinged to other nodes
  • nodetool removenode <id>

repair node comes into play if you have more then a replication factor of 1, you need to repair in the following cases:
node has been down replication factor of keyspace increased token ranges has been changed