Cassandra write/read protocol between nodes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Apache Cassandra is a highly scalable, distributed NoSQL database that is designed to handle large amounts of data across many commodity servers without a single point of failure. A fundamental aspect of how Cassandra manages this scalability and ensures reliability is through its write and read protocols, specifically how data is written to and read from multiple nodes in a cluster. This article delves into the technical details of these protocols.
Write Protocol in Cassandra
Cassandra uses a write protocol called write ahead log and partitioned row store. When a write operation is performed, the data is first written to a commit log, ensuring durability. Simultaneously, the data is written to an in-memory structure called a memtable. Eventually, data in the memtable is flushed to the disk in a structure called an SSTable.
Write Path Steps:
- Client Request: A client sends a write request to a coordinator node.
- Commit Log Update: The coordinator logs the write request to its commit log to ensure data durability.
- Memtable Update: The write request updates the memtable.
- Replication: The coordinator forwards the write request to replica nodes based on the replication factor and the consistency level specified.
- Acknowledgments: The coordinator node waits for acknowledgments from the replica nodes. Once the required number of acknowledgments (determined by the consistency level) are received, the write is considered successful.
Read Protocol in Cassandra
For read operations, Cassandra follows a somewhat more complex path, primarily because it needs to ensure that the read data is the most recent version among all the replicas.
Read Path Steps:
- Client Request: A client sends a read request to the coordinator node.
- Read Requests to Replicas: The coordinator node forwards the request to all replica nodes that hold a part of the requested data.
- Data Collection and Response: Each replica node checks both the memtable and SSTables for the requested data. It might also involve a merging process of data found in different SSTables and resolving different versions based on timestamps. The replicas then send their responses back to the coordinator.
- Data Aggregation: The coordinator node aggregates responses, resolves any discrepancies using the configured consistency level, and sends the most recent data back to the client.
Consistency Levels
The consistency of a read or write operation is determined by the consistency level, which specifies how many replicas need to respond to a request before it is considered successful. Common consistency levels in Cassandra are:
- ONE: Only one replica must respond successfully.
- QUORUM: A majority of the replica nodes (i.e., more than half) must agree on the read or write operation.
- ALL: All replicas must respond successfully.
Impact of the Consistency Level
The choice of consistency level affects the availability and data accuracy:
- Higher consistency levels (e.g., ALL) provide greater assurance that all nodes see the same data at the expense of availability (more nodes need to be up and responsive).
- Lower consistency levels (e.g., ONE) increase availability but with a risk that read data might not be the latest.
Summary Table: Key Cassandra Protocols
| Protocol Aspect | Description | Impact |
| Write Ahead Log | Ensures data durability by logging writes before executing them. | High durability. In case of node failure, data can be restored. |
| Memtable and SSTable | Temporary and permanent storage structures for new writes. | Memtables improve write efficiency; SSTables ensure long-term persistence. |
| Replication | Data is replicated across nodes based on the replication factor. | Enhances data availability and fault tolerance. |
| Consistency Levels | Determines the number of replicas that must acknowledge a read or write. | Balances between data consistency and availability. Allows adaptation to application needs. |
Conclusion
Understanding the protocols for data writing and reading in Cassandra is crucial for effectively designing and maintaining a Cassandra cluster. The configuration of replication and consistency levels significantly influences the performance, data accuracy, and availability of the Cassandra database.
Cassandra’s ability to finely tune these parameters allows it to adapt to various application requirements, whether it is fast write operations, robust data accuracy, or high availability.

