← 返回
内容创作
中文
Cassandra
Design Cassandra tables, write efficient queries, and avoid distributed database pitfalls.
设计 Cassandra 表,编写高效查询,避免分布式数据库陷阱。
ivangdavila
内容创作
clawhub
v1.0.0 1 版本 100000 Key: 无需
#latest
概述
Data Modeling Mistakes
- Design tables around queries, not entities—denormalization is mandatory, not optional
- One table per query pattern—Cassandra has no JOINs; duplicate data across tables
- Partition key determines data distribution—all rows with same partition key on same node
- Wide partitions kill performance—keep under 100MB; add time bucket to partition key if growing
Primary Key Traps
PRIMARY KEY (a, b, c): a is partition key, b and c are clustering columnsPRIMARY KEY ((a, b), c): (a, b) together is partition key—compound partition key- Clustering columns define sort order within partition—query must respect this order
- Can't query by clustering column without partition key—unlike SQL indexes
Query Restrictions
WHERE must include full partition key—partial partition key fails unless ALLOW FILTERINGALLOW FILTERING scans all nodes—never use in production; redesign table instead- Range queries only on last clustering column used—
WHERE a = ? AND b > ? works, WHERE a = ? AND c > ? doesn't IN on partition key hits multiple nodes—expensive; prefer single partition queries
Consistency Levels
QUORUM for most operations—majority of replicas; balances consistency and availabilityLOCAL_QUORUM for multi-datacenter—avoids cross-DC latencyONE for pure availability—may read stale data; fine for caches, bad for critical reads- Write + read consistency must overlap for strong consistency—
QUORUM + QUORUM safe
Tombstones (Silent Performance Killer)
- DELETE creates a tombstone, not actual deletion—tombstones persist until compaction
- Mass deletes destroy read performance—thousands of tombstones scanned per query
- TTL also creates tombstones—don't use short TTLs with high write volume
- Check with
nodetool cfstats -H table—Tombstone columns show problem
Batch Misuse
- UNLOGGED BATCH is not faster—use only for atomic writes to same partition
- LOGGED BATCH for multi-partition atomicity—adds coordination overhead
- Don't batch unrelated writes—hurts coordinator; send individual async writes
- Batch size limit ~50KB—larger batches fail or timeout
Anti-Patterns
- Secondary indexes on high-cardinality columns—scatter-gather query, slow
- Secondary indexes on frequently updated columns—creates tombstones
SELECT *—always list columns; schema changes break queries- UUID as partition key without time component—random distribution, hot spots during bulk loads
Lightweight Transactions
IF NOT EXISTS / IF column = ?—uses Paxos, 4x slower than normal write- Serial consistency for LWTs—
SERIAL or LOCAL_SERIAL - Don't use for counters or high-frequency updates—contention kills throughput
- Returns
[applied] boolean—must check if operation succeeded
Collections and Counters
- Sets/Lists/Maps stored with row—can't exceed 64KB, no pagination
- List prepend is anti-pattern—creates tombstones; use append or Set
- Counters require dedicated table—can't mix with regular columns
- Counter increment is not idempotent—retry may double-count
Compaction Strategies
SizeTieredCompactionStrategy (default)—good for write-heavy, uses more disk spaceLeveledCompactionStrategy—better read latency, higher write amplificationTimeWindowCompactionStrategy—for time-series with TTL; reduces tombstone overhead- Wrong strategy for workload = degraded performance over time
Operations
nodetool repair regularly—inconsistencies accumulate without repairnodetool status shows cluster health—UN (Up Normal) is good, DN is down- Schema changes propagate eventually—wait for
nodetool describecluster to show agreement - Rolling restarts: one node at a time, wait for UN status before next
版本历史
共 1 个版本
-
v1.0.0
当前
2026-03-29 03:06 安全 安全
安全检测
腾讯云安全 (Sanbu)
安全,无风险
查看报告
🔗 相关推荐
content-creation
fly0pants
广告情报与应用数据分析助手,支持搜索广告素材、分析应用排名、下载量、收入及市场洞察,用于广告素材和竞品分析。
★ 294
📥 136,362
ai-intelligence
ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,350
📥 317,461
content-creation
biostartechnology
消除AI写作痕迹,使文本更自然真实。基于维基百科"AI写作特征"指南,识别并修正夸张象征、宣传用语、肤浅-ing分析、模糊归因、破折号滥用、三项排比、AI词汇、负面平行结构及冗长连接词等模式。
★ 858
📥 199,020