Appearance
Cassandra Production Setup
Production Checklist
cassandra.yaml (production):
yaml
# Cluster
cluster_name: 'production_cluster'
num_tokens: 16 # Default since 4.0 (with allocate_tokens_for_local_replication_factor)
partitioner: org.apache.cassandra.dht.Murmur3Partitioner
# Seeds (vài nút ổn định, không phải mọi nút)
seed_provider:
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
- seeds: "10.0.1.5,10.0.1.6,10.0.1.7"
# Listen addresses
listen_address: 10.0.1.5 # This node's IP
rpc_address: 10.0.1.5
broadcast_rpc_address: 10.0.1.5
# Data directories (separate disk for commitlog)
data_file_directories:
- /data/cassandra/data
commitlog_directory: /data/cassandra/commitlog # Fast SSD!
saved_caches_directory: /data/cassandra/saved_caches
# Memory
# Heap đặt trong jvm-server.options hoặc jvm17-server.options (-Xms và -Xmx bằng nhau); từ 4.0 không còn jvm.options.
# Cỡ heap: đo GC trên tải thật, không có con số cố định.
# Commitlog
commitlog_sync: periodic
commitlog_sync_period: 10000ms # 10s (balance: durability + performance)
commitlog_segment_size: 32MiB
# Compaction
compaction_throughput: 16MiB/s # Throttle to avoid I/O spikes
# Read/write timeouts
read_request_timeout: 5000ms
write_request_timeout: 2000ms
range_request_timeout: 10000ms
# Snitch (bắt buộc: thiếu thì Cassandra không khởi động)
endpoint_snitch: GossipingPropertyFileSnitch
# Authentication
authenticator: PasswordAuthenticator # Enable auth
authorizer: CassandraAuthorizerConnection settings:
javascript
// Node.js (cassandra-driver); minh hoạ: IP, user và mật khẩu là ví dụ
const cassandra = require('cassandra-driver')
const client = new cassandra.Client({
contactPoints: ['10.0.1.5', '10.0.1.6', '10.0.1.7'],
localDataCenter: 'dc1',
// Auth
authProvider: new cassandra.auth.PlainTextAuthProvider('app_user', 'password'),
// Connection pool
pooling: {
coreConnectionsPerHost: {
[cassandra.types.distance.local]: 2,
[cassandra.types.distance.remote]: 1
},
maxConnectionsPerHost: {
[cassandra.types.distance.local]: 8,
[cassandra.types.distance.remote]: 2
}
},
// Query options
queryOptions: {
consistency: cassandra.types.consistencies.localQuorum, // Production default
prepare: true // Use prepared statements
},
// Retry policy
policies: {
retry: new cassandra.policies.retry.RetryPolicy()
}
})
client.connect().then(() => console.log('Cassandra connected'))Monitoring Queries
1. Check node status:
bash
nodetool status
# Output:
# UN = Up + Normal (healthy)
# DN = Down
# UJ = Up + Joining (bootstrapping)
# UL = Up + Leaving (decommissioning)
# Example:
# Datacenter: dc1
# Status=Up/Down
# State=Normal/Leaving/Joining/Moving
# Address Load Tokens Owns (effective) Host ID Rack
# UN 10.0.1.5 847GB 16 33.2% abc123 rack1
# UN 10.0.1.6 823GB 16 33.4% def456 rack1
# UN 10.0.1.7 891GB 16 33.4% ghi789 rack1
# Alert if:
# - Any node DN (down)
# - Load imbalance >20% (streaming issue or hotspot)2. Check read/write latency:
bash
nodetool tablestats myapp.users
# Key metrics:
# - Local read latency: 1.23 ms (trung bình cục bộ của nút; p99 xem nodetool tablehistograms)
# - Local write latency: 0.47 ms
# - Bloom filter false ratio: 0.02 (alert if >0.05)
# - Compacted partition maximum bytes: 2847234 (alert if >100MB)
# Target:
# - Read: <10ms p99
# - Write: <5ms p993. Check pending compactions:
bash
nodetool compactionstats
# Output:
# pending tasks 47 (đo: định dạng cột, kèm dòng concurrent compactors)
# Alert if >100 pending (compaction not keeping up)4. Check repair status:
bash
# Chạy trên MỌI nút (-pr chỉ sửa phạm vi chính của nút đó).
# Từ 4.0 repair mặc định là incremental; thêm --full để repair đầy đủ (đo: nodetool help repair có -full).
nodetool repair -pr myapp
# crontab của mỗi nút (hằng tuần):
# 0 2 * * 0 nodetool repair -pr myapp
# Repair is CRITICAL in Cassandra:
# - Fixes inconsistencies from eventual consistency
# - MUST run within gc_grace_seconds (default 10 days; đo: 864000)5. Check tombstone warnings:
bash
# Check Cassandra logs:
grep -i "tombstone" /var/log/cassandra/system.log # dòng cảnh báo/huỷ khi vượt tombstone_warn_threshold / tombstone_failure_threshold; đối chiếu tên lỗi trong log của bạnGỡ lỗi từng truy vấn trong cqlsh:
cql
TRACING ON;
SELECT * FROM myapp.users WHERE user_id = 'abc123';
-- Tìm dòng: "Read X live rows and Y tombstone cells" (đo: "Read 1 live rows and 0 tombstone cells")
-- Cảnh báo ở tombstone_warn_threshold (mặc định 1000); truy vấn bị HUỶ ở tombstone_failure_threshold (mặc định 100000)Backup Strategies
1. Snapshot backup:
bash
# Chụp snapshot một bảng (đo trên 5.0: -kt keyspace.table)
nodetool snapshot -t backup-20250109 -kt myapp.users
# Thư mục bảng có hậu tố UUID: /data/cassandra/data/<keyspace>/<table>-<uuid>/snapshots/<tag>/
cd /data/cassandra/data/myapp
tar -czf /backup/cassandra-20250109.tar.gz users-*/snapshots/backup-20250109
# Clear old snapshots:
nodetool clearsnapshot -t backup-202501092. Incremental backup:
yaml
# cassandra.yaml:
incremental_backups: trueFile incremental là hard link, Cassandra không tự xoá: phải dọn sau khi sao lưu xong, nếu không đĩa đầy dần.
bash
# Đường dẫn bảng có hậu tố UUID: /data/cassandra/data/<keyspace>/<table>-<uuid>/backups/
# Backup incremental files daily:
rsync -av /data/cassandra/data/myapp/users-*/backups/ /backup/incremental/3. Restore procedure:
bash
# Khôi phục khi nút đang chạy (đo trên 5.0: TRUNCATE rồi nạp lại snapshot, đếm lại đủ 3 dòng).
# Không xoá thư mục dữ liệu bằng rm -rf.
# 1. Giải nén snapshot ra thư mục tạm
mkdir -p /tmp/restore
tar -xzf /backup/cassandra-20250109.tar.gz -C /tmp/restore
# 2. Nạp lại sstable vào bảng (nodetool refresh đã deprecated, dùng import)
nodetool import myapp users /tmp/restore/users-*/snapshots/backup-20250109
# 3. Với keyspace RF > 1: repair để đồng bộ các bản sao (-pr chỉ xử lý primary range của nút này; muốn đồng bộ mọi bản sao của dữ liệu vừa nạp, chạy repair không -pr ở nút đó hoặc chạy -pr trên mọi nút; chưa đo vì RF=1 trong lab)
nodetool repair -pr myappSecurity Hardening
1. Enable authentication:
yaml
# cassandra.yaml:
authenticator: PasswordAuthenticator
authorizer: CassandraAuthorizerTạo user (cqlsh; đo trên 5.0: GRANT nhiều quyền trong một câu chạy được):
cql
-- Mặc định có superuser cassandra/cassandra: đổi mật khẩu ngay
ALTER ROLE cassandra WITH PASSWORD = 'strong-superuser-password';
-- Create users (keyspace myapp phải tồn tại):
CREATE ROLE app_user WITH PASSWORD = 'strong-password' AND LOGIN = true;
GRANT SELECT, MODIFY ON KEYSPACE myapp TO app_user;
-- Read-only user:
CREATE ROLE readonly_user WITH PASSWORD = 'password' AND LOGIN = true;
GRANT SELECT ON KEYSPACE myapp TO readonly_user;2. Enable TLS/SSL:
yaml
# cassandra.yaml:
client_encryption_options:
enabled: true
optional: false # Require TLS
keystore: /etc/cassandra/conf/keystore.jks
keystore_password: keystore-password
truststore: /etc/cassandra/conf/truststore.jks
truststore_password: truststore-password
# Internode encryption (between Cassandra nodes):
server_encryption_options:
internode_encryption: all
keystore: /etc/cassandra/conf/keystore.jks
keystore_password: keystore-password
truststore: /etc/cassandra/conf/truststore.jks
truststore_password: truststore-password3. Network security:
bash
# Firewall (iptables):
# 7000 = Internode communication
# 9042 = CQL native protocol
iptables -A INPUT -p tcp --dport 7000 -s 10.0.1.0/24 -j ACCEPT # Cassandra cluster
iptables -A INPUT -p tcp --dport 9042 -s 10.0.2.0/24 -j ACCEPT # App servers
iptables -A INPUT -p tcp --dport 7000 -j DROP
iptables -A INPUT -p tcp --dport 9042 -j DROPPhải nhớ (Cassandra Production Setup):
- Cassandra 5.0: khoá cấu hình có đơn vị (
read_request_timeout: 5000ms); tên cũ*_in_msbáo deprecated, thiếuendpoint_snitchthì không khởi động (đo). - Thư mục bảng có hậu tố UUID (
users-<uuid>); khôi phục snapshot bằngnodetool import(đo: đếm lại đủ 3 dòng). - Đổi mật khẩu superuser mặc định
cassandra/cassandra(đo: đăng nhập được) trước khi mở cổng 9042.