当前位置:首页>Linux>Linux cassandra.yaml 配置文件深度解析(下)

Linux cassandra.yaml 配置文件深度解析(下)

  • 2026-08-23 16:53:11
Linux cassandra.yaml 配置文件深度解析(下)

Linux cassandra.yaml 配置文件深度解析(下)

一、内存管理参数(最重要调优区)

Cassandra是内存敏感型数据库,Heap设置不当会导致频繁GC、OOM或性能雪崩。

1.1 Heap大小(在cassandra-env.sh中设置,非yaml)

虽不在yaml,但必须提及:

MAX_HEAP_SIZE="16G"
HEAP_NEWSIZE="4G"
  • 生产黄金法则:Heap不超过物理内存的50%,且不超过31GB(避免Compressed Oops退化)
  • 推荐公式:Heap = min(物理内存/2, 16-24GB)
  • 5.0建议:12-16GB(Trie Memtable更省内存)

1.2 memtable_allocation_type

memtable_allocation_type:heap_buffers# heap_buffers / offheap_buffers / offheap_objects
  • heap_buffers(默认):Memtable在Heap内,简单但易GC
  • offheap_buffers:堆外(Direct Memory),减少GC压力
  • offheap_objects:完全堆外对象(5.0推荐,内存利用率最高)

生产推荐:offheap_objects(需Java 11+,配合足够Direct Memory)

1.3 memtable_heap_space_in_mb / offheap_space_in_mb

memtable_heap_space_in_mb:2048# Heap内Memtable上限(仅heap模式)
memtable_offheap_space_in_mb:8192# 堆外Memtable上限(推荐)
  • 控制Memtable总内存占用
  • 生产公式:memtable_space ≈ 预计峰值写QPS × 平均行大小 × 100ms(flush阈值)
  • 推荐:堆外设为Heap的1-2倍(如Heap 16G → offheap 16-32G)

1.4 memtable_flush_writers 与 flush行为

memtable_flush_writers:8# 根据磁盘数设置
  • Memtable flush到SSTable的并行线程数
  • 推荐:等于Data磁盘数(NVMe多则更高)

1.5 memtable_cleanup_threshold

memtable_cleanup_threshold:0.3# 触发清理旧Memtable比例
  • 避免内存碎片,5.0 Trie结构下可调低

二、Compaction(压缩)策略与调优

Compaction是将多个SSTable合并、删除墓碑、回收空间的过程,是Cassandra磁盘IO的主要来源。

2.1 concurrent_compactors

concurrent_compactors:8# 默认CPU核数/4
  • Compaction并行线程数
  • 生产推荐:SSD环境设为CPU核数;HDD设为2-4
  • 过高会导致IO风暴,过低导致SSTable堆积、读放大

2.2 compaction_throughput_mb_per_sec

compaction_throughput_mb_per_sec:64# 全局Compaction限速(MB/s)
  • 限制Compaction总吞吐,避免影响正常读写
  • 生产实践:白天64-128,夜间调高或0(不限速跑Major Compaction)

动态调整:nodetool setcompactionthroughput

2.3 表级Compaction策略(Keyspace创建时指定,非全局yaml)

虽不在yaml全局,但运维需了解:

  • SizeTieredCompactionStrategy (STCS):默认,适合写多读少
  • LeveledCompactionStrategy (LCS):读多场景,读延迟稳定
  • TimeWindowCompactionStrategy (TWCS):时间序列数据神器
  • UnifiedCompactionStrategy (UCS):5.0新策略,自动适应混合负载

生产选择指南:

场景
推荐策略
理由
通用写密集
STCS
写放大低
读密集/低延迟要求
LCS
读放大低(L0-S7层级)
时间序列/TTL数据
TWCS
自动按时间窗压缩,空间回收高效
混合负载(5.0+)
UCS
自适应,减少手动调优

运维注意:策略不可在线更改,需重建表或用sstableloader


三、Cache与索引配置

3.1 key_cache_size_in_mb

key_cache_size_in_mb:512# Partition Key缓存
  • 缓存热门Partition位置,减少Bloom Filter IO
  • 推荐:100-1024MB,根据内存余量

3.2 row_cache(已废弃)

5.0完全移除row_cache(之前缓存整行数据,内存效率低)。

迁移建议:使用应用层缓存(如Redis)或Storage-Attached Indexing(SAI,5.0+)

3.3 file_cache_size_in_mb

file_cache_size_in_mb:2048# OS Page Cache预留(5.0+)
  • Cassandra不依赖OS Cache,建议设小值,让OS有更多Page Cache

3.4 bloom_filter_fp_chance

表级参数,但全局默认:

bloom_filter_fp_chance:0.01# 误判率,默认0.01
  • 低值:准确但SSTable头部大;高值:节省空间但多IO
  • 读密集表设0.001,写密集设0.1

四、并发与请求控制参数

4.1 concurrent_reads / concurrent_writes

concurrent_reads:64# 推荐:磁盘数 × 32(SSD)
concurrent_writes:64# 推荐:CPU核数 × 4
  • 读:建议SSD 64-128,HDD 16-32
  • 写:通常高于读,因为写路径简单

公式:

  • concurrent_reads ≈ 预期读QPS / 2000(单线程处理能力)
  • concurrent_writes ≈ 预期写QPS / 10000

4.2 concurrent_counter_writes

concurrent_counter_writes:32
  • Counter表专用,单独调高

4.3 native_transport_max_threads

native_transport_max_threads:128# CQL请求处理线程
  • 过高导致上下文切换开销

五、Hinted Handoff 与故障恢复

5.1 hinted_handoff_enabled

hinted_handoff_enabled:true
max_hint_window_in_ms:10800000# 3小时默认
  • 节点宕机时存储Hint
  • 生产建议:短窗口(1-3小时),避免Hint堆积

5.2 hinted_handoff_throttle_in_kb

hinted_handoff_throttle_in_kb:4096
  • Hint递交限速

六、读写路径高级调优

6.1 counter_cache_size_in_mb

counter_cache_size_in_mb:256
  • Counter表缓存

6.2 internode_compression

internode_compression:all# all / dc / none
  • 节点间流量压缩,跨DC必开

6.3 stream_throughput_outbound_megabits_per_sec

stream_throughput_outbound_megabits_per_sec:400
  • Streaming(扩容/修复)限速

七、生产级cassandra.yaml高级模板(结合上篇)

以下为生产参考(Heap 16G、SSD、写密集场景):

# 内存管理
memtable_allocation_type:offheap_objects
memtable_offheap_space_in_mb:16384
memtable_heap_space_in_mb:4096
memtable_flush_writers:8

# Compaction
concurrent_compactors:8
compaction_throughput_mb_per_sec:64

# Cache
key_cache_size_in_mb:1024
file_cache_size_in_mb:1024
bloom_filter_fp_chance:0.01# 表级覆盖

# 并发
concurrent_reads:96
concurrent_writes:96
native_transport_max_threads:256

# Hinted Handoff
hinted_handoff_enabled:true
max_hint_window_in_ms:7200000# 2小时
hinted_handoff_throttle_in_kb:8192

# 其他
internode_compression:all
stream_throughput_outbound_megabits_per_sec:800

八、常见调优问题与案例

  1. 频繁GC暂停 → 原因:Heap过大或memtable_heap模式 → 解决方案:切换offheap_objects,降低Heap到16G
  2. 读延迟高 → 检查SSTable数量(nodetool cfstats),Compaction落后 → 提高concurrent_compactors或用LCS
  3. 写延迟突增 → Memtable flush队列积压 → 增加memtable_flush_writers或分离CommitLog盘
  4. 磁盘空间爆炸 → 墓碑堆积 + STCS → 切换TWCS或定期repair
  5. 扩容慢 → Streaming限速低 → 临时调高stream_throughput

最新文章

随机文章