Since its inception at Facebook, Cassandra has become one of the most popular Apache projects. It is a widely-used open-source, high performance and distributed database and makes the lives of software engineers, data scientists, and web developers easier.
The users of this robust database solution include the likes of IBM, Facebook, Reddit, eBay, and Twitter.
However, to get familiar with Cassandra, you must know about its architecture and Apache Cassandra architecture can be a little tricky to understand.
That’s why we have created the following introductory guide on Cassandra architecture. It will make you familiar with all the necessary concepts of Cassandra architecture:
Basics of Cassandra Architecture
The Apache Cassandra architecture has no master or slave nodes. Instead, it has a ring-like architecture where the nodes are logically distributed in a ring. The architecture allows data to be distributed automatically across all nodes. Like HDFS, data in Cassandra gets replicated across the nodes for redundancy and is stored in memory. It uses the hash values of the keys to distribute data among the nodes in the cluster.
The Cassandra architecture allows the system to function irrespective of a single point of failure. This means if a cluster has a hundred nodes and one of them fails, the cluster would still be running. On the other hand, with Hadoop, the failure of a namenode would fail the entire system.
Cassandra architecture offers high scalability so a cluster can even have thousands of nodes. Moreover, you can add a new node to a cluster without interrupting its operations.
Apart from the features, we discussed above, Apache Cassandra architecture also supports multiple data centres and enables data replication across data centres.
Topology and Design
Cassandra architecture is based on a distributed system architecture. The simplest version of Cassandra can run on one machine and work well with basic testing. One Cassandra instance is called a node.
Cassandra offers horizontal scalability where you can add more than one node as a component of a cluster. It works with peer to peer architecture where each node is connected to every other node. Every Cassandra node performs all database operations and servers client requests without a master node.
This peer-to-peer distributed architecture ensures that a single point of failure doesn’t cause the entire system to fail. Clusters in Cassandra can communicate with each other for numerous functions. The following concepts are vital for such communication:
Gossip is a Cassandra protocol its nodes use for peer-to-peer communication. It informs a node about the other nodes’ states. Every second, one node performs gossip with up to three other nodes and every gossip message follows specific formats and version numbers to keep the communication efficient.
Every node in Cassandra configures a list of seeds which is a list of other nodes. A seed node doesn’t have any purpose other than bootstrapping a node when it first joins a cluster. After bootstrap, a node doesn’t require a seed on restarting. It’s best to use two or three seed nodes per Cassandra data centre and keep the seeds list uniform.
In Cassandra, data is stored in tables where every table is organized into rows and columns. Moreover, tables in Cassandra are grouped in keyspaces such as tables on customer data can all be grouped in one keyspace while tables on business transactions can be stored in another.
Every table has a primary key which is divided into clustering columns and partition key. Note that the clustering columns are optional. Cassandra uses the partition key to index the data. All the rows with a common partition key constitute one data partition, the basic unit for data partitioning.
Partitioning in Cassandra
In Cassandra, a partitioner converts partition keys into tokens. There are multiple partitioner options present in Cassandra where the Murmur3Partitioner is the default. Every token is assigned an integer value between -2^63 to +2^63-1, and the name of this range is the token range.
Every Cassandra owns a part of this range and it mainly possesses data related to the range. We use a token to locate the data precisely among the nodes.
You can understand the ownership system with this concept: If a cluster has only one node then that node would own the complete token range. With the addition of more nodes, the token range ownership would get split accordingly.
Cassandra simplifies the token calculation and assignment challenges by using virtual nodes, also known as Vnodes.
It divides a cluster into numerous virtual nodes to assign tokens and every physical node gets an equal quantity of Vnodes. The default number of Vnodes owned by a node is 256. You can set the same by using the num_tokens property. When you add a new node to a cluster, the token allocation algorithm would allocate the required tokens by using the Vnodes.
Cassandra replicates the data present in every keyspace with a replication factor. One primary replica of the data remains with the token owner node while the rest are placed by Cassandra on particular nodes through its replica placement strategy. Note that all replicas have equal importance for most database operations.
Two settings affect replica placement in Cassandra, which are snitch and the replication strategy. Snitch determines the data centre and the rack a node belongs to. They are responsible for informing Cassandra about the topology for keeping things efficient.
The replication strategy is set at the keyspace level and there are two of them: NetworkTopologyStrategy and simple strategy. The former is a rack and data centre aware while the latter is not.
Every distributed system works based on the CAP theorem. According to this theorem, any distributed system can properly deliver any two out of the three properties, Consistency, Availability, and Partition-tolerance.
In Cassandra, you can choose between availability and consistency. This means the data can either be highly consistent with lower availability or highly available with low consistency.
The concept of requesting a specific number of acknowledgements is called tunable consistency and you can apply it at the individual query level.
In this process, the data is written to a comment log on a disk and then sent to a responsible node according to the hash value.
After that, nodes write data to an in-memory table called memtable from where the data is written to a “sstable” in memory. Next, it gets updated to the actual table.
If the responsible node is down for some reason, the data gets written to another node.
Learn More About the Cassandra Architecture
Understanding Apache Cassandra architecture would help you understand how this solution works. By now, you also know why Cassandra stands out in the competition and why it’s so popular.
If you’re interested in learning more about databases, you can check out the following resources:
On the other hand, if you’re looking for a personalized learning experience, we recommend taking a data science course. At upGrad, we offer a PG Diploma in Data Science and a Master of Science in Data Science. These courses will teach you all the necessary skills to become a data science professional.
If you are interested to know more about Big Data Program, check out our PG Diploma in Software Development Specialization in Big Data program which is designed for working professionals and provides 7+ case studies & projects, covers 14 programming languages & tools, practical hands-on workshops, more than 400 hours of rigorous learning & job placement assistance with top firms.