用强化学习优化NGINX缓存淘汰,命中率最高提升146%。
Cold-RL: Learning Cache Eviction with Offline Reinforcement Learning for NGINX
- 用离线强化学习替代传统LRU,基于六维特征动态选淘汰对象。
- 25MB缓存下命中率从14.36%升至35.38%,提升146%。
- 推理延迟低于500微秒,适合高要求的实时缓存系统。
Web代理如NGINX通常采用不考虑大小的最近最少使用(LRU)淘汰策略,在周期性突发流量和混合对象大小场景下易出现抖动。本文提出Cold-RL,一种集成于NGINX的可学习淘汰策略,将原有强制过期路径替换为由ONNX侧车服务的双网络深度Q网络,严格控制在微秒级延迟内。每次淘汰时,Cold-RL采样K个最久未使用对象,提取年龄、大小、命中次数、到达间隔时间、剩余TTL和上次源站响应时间共六个轻量特征,请求受害者位图;若500微秒内未完成决策,则立即回退至原生LRU。策略通过重放NGINX访问日志,在缓存模拟器中离线训练,奖励机制为:若保留对象在TTL过期前被再次命中,则得1分。在两个对抗性工作负载下与LRU、LFU、基于大小的策略、自适应LRU及混合基线对比,25MB缓存下命中率由0.1436提升至0.3538(+146%),100MB缓存下由0.7530升至0.8675(+15%),400MB时达到经典方法水平(约0.918)。推理开销低于2% CPU,95%分位淘汰延迟保持在预算内。据我们所知,这是首个满足严格服务级别目标的强化学习缓存淘汰策略集成于NGINX。
原文摘要 · Abstract (English)
Web proxies such as NGINX commonly rely on least-recently-used (LRU) eviction, which is size agnostic and can thrash under periodic bursts and mixed object sizes. We introduce Cold-RL, a learned eviction policy for NGINX that replaces LRU's forced-expire path with a dueling Deep Q-Network served by an ONNX sidecar within a strict microsecond budget. On each eviction, Cold-RL samples the K least-recently-used objects, extracts six lightweight features (age, size, hit count, inter-arrival time, remaining TTL, and last origin RTT), and requests a bitmask of victims; a hard timeout of 500 microseconds triggers immediate fallback to native LRU. Policies are trained offline by replaying NGINX access logs through a cache simulator with a simple reward: a retained object earns one point if it is hit again before TTL expiry. We compare against LRU, LFU, size-based, adaptive LRU, and a hybrid baseline on two adversarial workloads. With a 25 MB cache, Cold-RL raises hit ratio from 0.1436 to 0.3538, a 146 percent improvement over the best classical baseline; at 100 MB, from 0.7530 to 0.8675, a 15 percent gain; and at 400 MB it matches classical methods (about 0.918). Inference adds less than 2 percent CPU overhead and keeps 95th percentile eviction latency within budget. To our knowledge, this is the first reinforcement learning eviction policy integrated into NGINX with strict SLOs.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。