解决 Dragonfly 报错 OOM command not allowed when used memory 内存驱逐失败修复
排查 兼容 Redis 协议的现代多线程缓存 Dragonfly 在内存用尽时未能按预期释放空间,写入直接报 (error) OOM command not allowed when used memory > 'maxmemory',分析 驱逐策略与线程共享内存池。
问题现象
向 Dragonfly 实例持续高频压测灌入缓存数据,当内存达到限额后,客户端开始报错:
(error) OOM command not allowed when used memory > 'maxmemory'. Dragonfly is configured to not evict keys or unable to evict enough items.
触发根因
- Dragonfly 默认的
maxmemory_policy被设置为noeviction,达到内存顶峰时直接拒绝所有写入; - 数据集中所有 Key 均未设置过期时间(TTL),且配置了仅驱逐过期键策略(
volatile-lru/volatile-ttl),导致无键可驱; - 多线程架构下分配器在短时间内并发激增,驱逐工作流来不及回收足够内存块。
解决步骤
启动参数显式启用 cache_mode 或全量 LRU 驱逐策略
调整 Dragonfly 启动参数:
dragonfly --maxmemory=8gb --cache_mode=true --maxmemory_policy=allkeys-lru
确保数据写入时附带合理 TTL 或批量清理无用数据
避免不可回收的孤儿冷数据永久挤占内存。
在线动态配置当前运行实例的驱逐规则
通过 redis-cli 发送指令:
redis-cli -p 6379 CONFIG SET maxmemory-policy allkeys-lru
验证确认
继续压测验证:
内存达到 8GB 阈值后,Dragonfly 后台高效多线程驱逐最旧冷 Key,写入 QPS 保持在数十万级且零 OOM 报错。


