Cassandra Architecture and how it performs read and write operations
Tags: software, database, architecture, devjournal
Published: 2022-04-17 05:45:29

Cassandra is a distributed database that is used to perform read and write operations at a very high scale. To understand the how to read and to write operations we need to understand a few concepts:

1. Gossip protocol and Failure detection
2. Snitches
3. Rings and Token
4. Partitioner
**
Gossip and Failure detection**
To support decentralized and partition tolerance, Cassandra uses a gossip protocol that allows each node to keep track of the state information of the other nodes in the cluster. The gossip runs every second on a timer.
Gossip protocols generally assume a faulty network are commonly employed in large decentralized network systems for an automatic mechanism for replication in a distributed database. When a server node is started, it registers itself with the gossiper to receive the endpoint state information. Cassandra gossip is used for failure detection, the Gossiper class maintains a loss of nodes that are alive and dead.
How gossip protocol works:
**GossipDigestSync
GossipDigestAck
GossipDigestAck2**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hq0oyxzu347m46kp5oas.png)
 

**Gossip Detection**
When the gossiper determines that another endpoint is dead, it convicts that endpoint by marking it dead in the local list and logging that fact. Cassandra has a failure detection algorithm known as Phi Accrual Failure Detector.

- Failure detection is based on 2 primary ideas.
- Failure detction should be flexible which is achieved by decoupling it from the application being monitored.
It does not depend on the heartbeat protocol. It has a number as suspicion level which represents the confidence level it has that node is dead.

**Snitches**
The job of a snitch is to provide information about the network topology so that Cassandra can efficiently route requests. The snitch will figure out nodes that are in relation to other odes. The snitch will determine relative host proximity for each node in a cluster and determines which nodes to read and write from.

**How Snitch participates in a read operation**
When Cassandra has to perform a read operation, it must contact a number of replicas determined by the consistency number. In order to support the maximum speed of reads, Cassandra selects a single replica to query for the full object and asks additional replicas for hash value in order to ensure the latest version of the requested data. The snitch helps to identify the replicas that will return the fasted and this is the replica that is queried for the full data.
The default snitch is topology unaware known as Simple Snitch.
Cassandra provides a pluggable way to describe the cluster topology, It also provides a feature called Dynamic Snitching that helps optimize the routing if reads and writes over time.
The dynamic snitching implementation uses a modified version of the gossip protocol. The dynamic snitch understands the network topology and performance of each of the nodes. It also keeps track of the nodes which are performing compaction. It avoids directing traffic to the nodes that are busy or performing poorly. Dynamic snitching uses a modified version of the Phi failure mechanism used by the gossip.
**Rings and Token**
Cassandra represents the data managed by a cluster as a ring. Each node in the ring is assigned one or more ranges of data described by a token, which determines its position in the ring. A node claims ownership of the range of values less than. or equal to each token and greater than the last token of the previous node. This is known as the token range. Data is assigned to nodes by using a hash function to calculate a token for the partition key.
**Virtual node**
Instead of assigning a single range to a node, a node is assigned multiple smaller ranges. Vnodes are useful in the case of systems where different machines have different computational resources available to them. A further advantage of the node is that they speed up some of the more heavyweight Cassandra operations such as bootstrapping a new node, decommissioning a node and repairing a node. This is because the load associated with the operation on multiple smaller ranges is spread more evenly across the nodes of the cluster.
**Partitioners**
A partitioner decides how data is distributed across the Cassandra cluster. The role of the partitioner is to compute the token based on the partition key columns. Any clustering columns that may be present in the primary key are used to determine the ordering of rows within a given node that owns the token representing that partition.
**Coordinator Node**
Whenever a client makes a request for any read and writes operation, the client is connected to any of the nodes which act as the coordinator node for that operation. The coordinator node creates the hash of the key and finds the node which has data for that token range. Among those nodes which nodes are to be selected for reading and writing are decided based on the data from Snitch. Then, the coordinator asks that particular node for the read/write operation.

[ video explanation of Cassandra Architecture ](https://youtu.be/JEwkI0W-wAk)