Skip to content

Cassandra Linear Scaling ​

Adding Nodes (Scale Out) ​

Linear scalability:

Kịch bản minh hoạ, không phải số đo: nếu partition key phân đều và client đủ tải, throughput ghi tăng gần tỉ lệ với số nút.
Số thật đổi theo phần cứng và workload: đo bằng cassandra-stress trên cụm của bạn.

Add node to cluster:

bash
# 1. Cài Cassandra trên nút mới, chép cassandra.yaml của cụm sang rồi sửa như khối yaml dưới
yaml
# cassandra.yaml của nút mới (cùng cluster_name với cụm)
cluster_name: 'MyCluster'
listen_address: 10.0.1.3
seed_provider:
  - class_name: org.apache.cassandra.locator.SimpleSeedProvider
    parameters:
      # nút đang chạy; không đưa nút mới vào seeds (seed không bootstrap)
      - seeds: "10.0.1.1,10.0.1.2"
bash
# 2. Chạy nút mới rồi theo dõi bootstrap (stream dữ liệu từ các nút khác)
cassandra
nodetool status
# 3. Khi mọi nút UN, chạy trên từng nút CŨ để bỏ dữ liệu không còn thuộc nút đó
nodetool cleanup

Kết quả thật khi nút mới đang join (Cassandra 5, cụm 3 nút Docker, nodetool status):

text
UN  172.24.0.2  95.8 KiB    16  100.0%  rack1
UN  172.24.0.3  119.65 KiB  16  100.0%  rack1
UJ  172.24.0.4  30.89 KiB   16  ?       rack1   <- nút mới (Up, Joining)
# Khi bootstrap xong, trạng thái chuyển sang UN (Up, Normal)

Token distribution:

Cassandra uses consistent hashing
- Each node owns token ranges
- Adding node splits existing ranges
- New node streams its ranges automatically; run `nodetool cleanup` on existing nodes afterwards

Replication Factor & Consistency Levels ​

Replication factor:

cql
CREATE KEYSPACE myapp WITH REPLICATION = {
  'class': 'NetworkTopologyStrategy',
'datacenter1': 3, -- 3 replicas in DC1
'datacenter2': 2 -- 2 replicas in DC2
};

Consistency levels (tunable):

Write Consistency       Meaning
─────────────────────────────────────────────────
ANY                    1 hint (fastest, least durable)
ONE                    1 replica acks
TWO                    2 replicas ack
THREE                  3 replicas ack
QUORUM                 (RF/2)+1 replicas ack
LOCAL_QUORUM           Quorum in local DC
EACH_QUORUM            Quorum in each DC
ALL                    All replicas ack (slowest, most durable)

Read Consistency       Meaning
─────────────────────────────────────────────────
ONE                    1 replica (fastest, may be stale)
TWO                    2 replicas (higher consistency)
QUORUM                 (RF/2)+1 replicas agree
LOCAL_QUORUM           Quorum in local DC
EACH_QUORUM            Quorum in each DC
ALL                    All replicas agree (slowest, strongest)

Strong consistency formula:

Write CL + Read CL > RF = Strong consistency

Examples (RF=3):
- Write QUORUM (2) + Read QUORUM (2) = 4 > 3 ✅ Strong
- Write ONE (1) + Read ALL (3) = 4 > 3 ✅ Strong
- Write ONE (1) + Read ONE (1) = 2 < 3 ❌ Eventual

Multi-Datacenter Replication ​

Setup:

cql
CREATE KEYSPACE myapp WITH REPLICATION = {
  'class': 'NetworkTopologyStrategy',
'US-East': 3, -- 3 replicas in US East
'EU-West': 3 -- 3 replicas in EU West
};

-- Total: 6 replicas (3 per DC)

Consistency levels for multi-DC:

cql
-- cqlsh; cần cụm đủ nút cho RF 3 mỗi DC. Đã chạy OK CONSISTENCY LOCAL_QUORUM và EACH_QUORUM (đọc) trên cassandra:5, cụm 1 DC
USE myapp;
CREATE TABLE IF NOT EXISTS users (user_id int PRIMARY KEY, name text);

-- Ghi chỉ chờ DC cục bộ (nhanh)
CONSISTENCY LOCAL_QUORUM;
INSERT INTO users (user_id, name) VALUES (1, 'An');
-- Ghi 2/3 nút ở DC cục bộ, bất đồng bộ sang DC xa

-- Đọc chỉ từ DC cục bộ (nhanh)
SELECT * FROM users WHERE user_id = 1;
-- Đọc 2/3 nút ở DC cục bộ (có thể cũ nếu bản ghi mới ở DC xa)

-- Ghi chờ mọi DC (bền)
CONSISTENCY EACH_QUORUM;
INSERT INTO users (user_id, name) VALUES (2, 'Binh');
-- Ghi 2/3 nút ở MỖI DC (chậm hơn, nhất quán toàn cầu)

Vnodes (Virtual Nodes) ​

Traditional tokens (old):

Node 1 owns token range 0-33
Node 2 owns token range 34-66
Node 3 owns token range 67-99

Problem: Adding node 4 only rebalances from node 3

Vnodes (default, modern):

Each node owns 16 virtual nodes (mặc định từ 4.0; trước đó 256)
- Node 1: vnodes scattered across ring
- Node 2: vnodes scattered across ring
- Node 3: vnodes scattered across ring

Adding node 4: Rebalances from ALL nodes (even distribution)

Configuration:

yaml
# cassandra.yaml
num_tokens: 16 # Default since 4.0 (with allocate_tokens_for_local_replication_factor: 3)

Phải nhớ:

  • Thêm nút: nút mới không nằm trong seeds, chờ UN rồi chạy nodetool cleanup trên nút cũ.
  • Nhất quán mạnh khi Write CL + Read CL > RF; ưu tiên LOCAL_QUORUM cho multi-DC.
  • Tăng throughput theo số nút chỉ khi partition key phân đều; đo bằng cassandra-stress.