Module 03 · Introducing Apache Cassandra: An Open-Source NoSQL Database
Reading: Architecture of CassandraCassandra 的架构
Cassandra is a popular open-source distributed NoSQL database system known for its scalability, high performance, and fault tolerance. While it doesn't have a traditional architectural structure like a building, it does have a robust design that allows it to handle large amounts of data across multiple nodes. After reading this document, you will have a basic understanding of the components.
Cassandra 是一个流行的开源分布式 NoSQL 数据库系统,以其可扩展性、高性能和容错性而闻名。虽然它没有像建筑物那样的传统架构结构,但它具有强大的设计,能够跨多个节点处理大量数据。阅读本文档后,您将对各个组件有一个基本的了解。
Apache Cassandra topology Apache Cassandra 拓扑
Cassandra is a distributed system architecture. The basic component of Cassandra's architecture is the standalone unit, node. It is also known as a single Cassandra. Nodes can be added or removed without affecting the system's availability. Each node operates independently and communicates with other nodes through a peer-to-peer protocol.
Cassandra 是一个分布式系统架构。Cassandra 架构的基本组件是独立单元,即节点。它也被称为单个 Cassandra。节点可以添加或删除,而不会影响系统的可用性。每个节点独立运行,并通过点对点协议与其他节点通信。

As well as being a distributed system, Cassandra is designed to be a peer-to-peer architecture, with each node connected to all other nodes. Every Cassandra node can run all database operations and handle client requests independently, eliminating the need for a primary node.
除了作为分布式系统外,Cassandra 还被设计为一种点对点架构,其中每个节点都与所有其他节点相连。每个 Cassandra 节点可以独立运行所有数据库操作并处理客户端请求,从而消除了对主节点的需求。

How do the nodes in this peer-to-peer architecture know to which node to route a request without a primary node? What if a certain node is down or up? Through the gossip protocol.
在这种点对点架构中,节点如何在没有主节点的情况下知道将请求路由到哪个节点?如果某个节点宕机或启动了怎么办?通过八卦协议。
The gossip protocol enables nodes to exchange details and information, updating each node about the status of all other nodes. A node performs gossip communications with up to three other nodes every second. The gossip messages follow a specific format and use version numbers to communicate efficiently. So, each node can build the entire metadata of the cluster (which nodes are up/down, what the tokens allocated to each node are, and so on).
八卦协议使节点能够交换详细信息和数据,更新每个节点关于所有其他节点状态的信息。每个节点每秒与最多三个其他节点进行八卦通信。八卦消息遵循特定格式,并使用版本号进行高效通信。因此,每个节点可以构建整个集群的完整元数据(包括哪些节点是宕机/运行中、分配给每个节点的令牌等)。
Components of a Cassandra node Cassandra 节点的组成部分
Several components in Cassandra nodes are involved in the write and read operations. Some of them are listed next:
Cassandra 节点的几个组件参与写入和读取操作。其中一些组件列举如下:
Memtable 内存表
Memtables serve as in-memory structures within Cassandra, buffering write operations before being written onto disk. Typically, each table has an active Memtable. Eventually, these Memtables are flushed to disk, transforming into immutable SSTables (Sorted String Tables).
内存表是 Cassandra 中的内存结构,用于在写入磁盘之前缓冲写入操作。通常,每个表都有一个活跃的内存表。最终,这些内存表会被刷新到磁盘上,转化为不可变的排序字符串表(SSTables)。
The triggering of Memtable flushes can occur through various methods:
Memtable 刷新的触发可以通过多种方法进行:
- Exceeding a predefined threshold for Memtable memory usage.
超过内存表内存使用量的预定义阈值。 - Approaching the maximum size of the CommitLog, which prompts Memtable flushes to free up CommitLog segments.
接近 CommitLog 的最大大小,这会触发 Memtable 刷写以释放 CommitLog 段。 - Setting specific time intervals to trigger flushes on a per-table basis.
设置特定的时间间隔,以在每张表的基础上触发刷写。
These triggers initiate the process where the buffered data in Memtables is persisted onto disk as SSTables, ensuring data durability and efficient retrieval in the Cassandra database system.
这些触发器启动了将 Memtable 中的缓冲数据持久化到磁盘作为 SSTables 的过程,确保数据持久性和在 Cassandra 数据库系统中的高效检索。
Commit log
Commit logs in Cassandra function as append-only logs, capturing all local mutations on a specific Cassandra node. Before you write data to a Memtable, you must record it in a commit log. This process ensures durability in the event of an unexpected shutdown. Upon restarting, any mutations in the commit log are applied to the Memtables, guaranteeing data consistency and recovery in the Cassandra database system.
Cassandra 中的提交日志充当仅追加的日志,捕获特定 Cassandra 节点上的所有本地变更。在将数据写入 Memtable 之前,你必须将其记录在提交日志中。这个过程确保了在意外关机的情况下数据的持久性。在重新启动时,提交日志中的任何变更都会被应用到 Memtables,从而保证 Cassandra 数据库系统中的数据一致性和恢复。
SSTables
SSTables (Sorted String Tables) are the immutable data files in Cassandra, storing data persistently on disk. These files are created by flushing memtables or streaming from other nodes. When you generate SSTables, Cassandra initiates the compaction processes to merge multiple SSTables into one. You should be able to see the new SSTable while the older SSTables become eligible for removal.
SSTables(排序字符串表)是 Cassandra 中不可变的数据文件,将数据持久地存储在磁盘上。这些文件由刷新 Memtables 或从其他节点流式传输创建。当你生成 SSTables 时,Cassandra 会启动合并多个 SSTables 为一个的压缩过程。你应该能够在新的 SSTable 出现时,使旧的 SSTables 变得有资格被移除。
An SSTable comprises various distinct components stored in separate files, some of which include:
一个 SSTable 由多个不同的组件组成,这些组件存储在不同的文件中,其中一些包括:
Data.db: This file contains the actual data stored by Cassandra.
Data.db:该文件包含 Cassandra 实际存储的数据。
Index.db: An index file that maps partition keys to specific positions within the Data.db file, aiding in efficient data retrieval.
Index.db:一个索引文件,将分区键映射到 Data.db 文件中的特定位置,有助于高效地检索数据。
Summary.db: This file provides a sample subset (typically every 128th entry) of the information contained in the Index.db file, offering an overview for quicker data access.
Summary.db:该文件提供了一个示例子集(通常是每 128 条记录中的一个),包含在 Index.db 文件中存储的信息,为快速数据访问提供概览。
Filter.db: Cassandra employs a Bloom Filter in this file, which serves as a probabilistic data structure, assisting in determining if a partition key exists in the SSTable without requiring a disk seek.
Filter.db:Cassandra 在此文件中使用布隆过滤器,这是一种概率性数据结构,有助于确定分区键是否存在于 SSTable 中,而无需进行磁盘查找。
CompressionInfo.db: This file holds metadata regarding the offsets and lengths of compressed chunks within the Data.db file, facilitating the decompression of stored data.
CompressionInfo.db:该文件包含有关 Data.db 文件中压缩块偏移量和长度的元数据,以促进存储数据的解压缩。
These distinct files within an SSTable collectively form an organized structure that enables efficient data storage, indexing, retrieval, and compression within the Cassandra database system.
这些不同的文件在 SSTable 中共同形成了一个有组织的结构,使 Cassandra 数据库系统能够高效地存储、索引、检索和压缩数据。
Write process at node level 节点级别的写入过程
Writes are distributed across nodes in the cluster, utilizing a coordinator node that manages the operation and replicates data to appropriate replicas based on the configured consistency level.
写入操作在集群中的节点之间分布,利用一个协调节点来管理操作,并根据配置的一致性级别将数据复制到适当的副本。
- Logging data in the commit log
在提交日志中记录数据 - Writing data to the Memtable
将数据写入 Memtable - Flushing data from the Memtable
从 Memtable 中刷新数据 - Storing data on disk in SSTables
在磁盘上以 SSTables 形式存储数据

Read at node level 节点级别读取
While writes in Cassandra are very simple and fast operations done in memory, the read is a bit more complicated since it needs to consolidate data from memory (Memtable) and disk (SSTables). Since you can fragment data on disk into several SSTables, the reading process needs to identify which SSTables most likely contain info about the querying partitions. The Bloom Filter information makes this selection through the following steps:
虽然 Cassandra 中的写入操作非常简单且快速,是在内存中完成的,但读取操作则稍微复杂一些,因为它需要从内存(Memtable)和磁盘(SSTables)中整合数据。由于数据可以在磁盘上分散到多个 SSTables 中,因此读取过程需要识别哪些 SSTables 最有可能包含查询分区中的信息。Bloom Filter 信息通过以下步骤进行这一选择:
- Checks the Memtable 检查 Memtable
- Checks Bloom filter 检查布隆过滤器
- Checks partition key cache, if enabled
检查分区键缓存,如果已启用 - If the partition is not in the cache, the partition summary is checked
如果分区不在缓存中,则检查分区摘要 - Then, the partition index is accessed
然后,访问分区索引 - Locates the data on the disk
定位磁盘上的数据 - Fetches the data from the SSTable on disk
从磁盘上的 SSTable 中获取数据 - Data is consolidated from Memtable and SSTables before being sent to the coordinator
数据在发送给协调器之前由 Memtable 和 SSTables 进行合并

Data distribution 数据分布
Partitioner: Cassandra uses a partitioner to distribute data across nodes. This consistent hashing algorithm ensures an even data distribution across the cluster, preventing hotspots and facilitating horizontal scaling.
分区器:Cassandra 使用分区器在节点间分配数据。这种一致性哈希算法确保数据在集群中均匀分布,防止热点并促进水平扩展。

Token Ring: Cassandra employs a token ring mechanism, assigning a range of tokens to each node in the cluster. Each token corresponds to a particular data range, allowing nodes to locate and manage data efficiently.
令牌环:Cassandra 采用令牌环机制,为集群中的每个节点分配一系列令牌。每个令牌对应特定的数据范围,使节点能够高效地定位和管理数据。
Data model 数据模型
Column family data model: Cassandra follows a column-family-based data model, organizing data into rows and columns. It stores data in tables composed of rows indexed by a unique key. Each row contains columns with different attributes.
列族数据模型:Cassandra 遵循基于列族的 数据模型,将数据组织为行和列。它将数据存储在由唯一键索引的行组成的表中。每行包含具有不同属性的列。

Replication and consistency 复制与一致性
Replication: Data replication is configurable in Cassandra. Each piece of data is replicated across multiple nodes (replication factor) to ensure fault tolerance and high availability, even in node failures.
复制:在 Cassandra 中,数据复制是可配置的。每条数据都会在多个节点(复制因子)之间进行复制,以确保容错和高可用性,即使在节点故障的情况下也是如此。
Consistency levels: Cassandra offers tunable consistency levels for read and write operations. Users can choose between different consistency levels, balancing consistency and performance based on application requirements.
一致性级别:Cassandra 为读写操作提供了可调节的一致性级别。用户可以根据应用需求在不同的 consistency levels 之间进行选择,以平衡一致性和性能。
Read and Write Operations 读写操作
Distributed writes: Writes are distributed across nodes in the cluster, utilizing a coordinator node that manages the operation and replicates data to appropriate replicas based on the configured consistency level.
分布式写入:写入操作在集群节点间分布,利用协调节点管理操作,并根据配置的一致性级别将数据复制到适当的副本。
Read optimization: Cassandra supports efficient reads by allowing read operations from multiple replicas. It uses the "hinted handoff" mechanism to ensure consistency in case some replicas are temporarily unavailable.
读取优化:Cassandra 通过允许从多个副本进行读取操作来支持高效的读取。它使用"提示传递"机制来确保在部分副本暂时不可用时的一致性。
Architecture Components 架构组件
Gossip protocol: Cassandra uses the gossip protocol for inter-node communication. Nodes exchange information about the cluster's state, such as membership changes or node failures, ensuring each node stays updated.
广播协议:Cassandra 使用广播协议进行节点间通信。节点交换集群状态信息,如成员变更或节点故障,确保每个节点保持更新。
Compaction and compression: Cassandra periodically performs compaction to merge and organize data files, optimizing storage and improving performance. It also supports data compression to reduce disk usage.
压实和压缩:Cassandra 会定期执行压实操作以合并和组织数据文件,优化存储并提高性能。它还支持数据压缩以减少磁盘使用。
Snitches and load balancing: Snitches determine network topology, optimizing data replication and routing. Load balancing ensures that queries are evenly distributed across nodes to prevent uneven distribution.
Snitches 和负载均衡:Snitches 确定网络拓扑,优化数据复制和路由。负载均衡确保查询均匀分布在节点上,防止分布不均。
Security 安全
Security features: Cassandra offers security features like authentication, encryption, and access control mechanisms to ensure data integrity, confidentiality, and protection against unauthorized access.
安全功能:Cassandra 提供认证、加密和访问控制机制等安全功能,以确保数据完整性、保密性,并防止未经授权的访问。
Summary 摘要
Cassandra's architecture is designed for high availability, fault tolerance, scalability, and efficient data management across distributed environments, making it a powerful choice for handling large-scale data-intensive applications.
Cassandra 的架构专为高可用性、容错性、可扩展性和分布式环境下的高效数据管理而设计,使其成为处理大规模数据密集型应用的强大选择。