如何阻止redis内存使用率随连接数的增加而增加

gojuced7  于 2021-06-10  发布在  Redis
关注(0)|答案(1)|浏览(584)

我们在aws上通过elasticache运行redis,当运行大量刚刚读取的lambda函数时,内存使用率会出现峰值。下面是 redis-cli --stat ```
------- data ------ --------------------- load -------------------- - child -
keys mem clients blocked requests connections
1002 28.11M 15 0 2751795 (+11) 53877
1002 28.07M 15 0 2751797 (+2) 53877
1002 28.07M 15 0 2751799 (+2) 53877
1002 28.11M 15 0 2751803 (+4) 53877
1002 28.07M 15 0 2751806 (+3) 53877
1001 28.11M 15 0 2751808 (+2) 53877
1007 28.08M 15 0 2751837 (+29) 53877
1007 28.08M 15 0 2751839 (+2) 53877
1005 28.10M 16 0 2751841 (+2) 53878
1007 171.68M 94 0 2752012 (+171) 53957
1006 545.93M 316 0 2752683 (+671) 54179
1006 1.07G 483 0 2753508 (+825) 54346
1006 1.54G 677 0 2754251 (+743) 54540
1006 1.98G 882 0 2755024 (+773) 54745
1006 2.35G 1010 0 2755776 (+752) 54873
1005 2.78G 1014 0 2756548 (+772) 54877
1005 2.80G 1014 0 2756649 (+101) 54877
1004 2.79G 1014 0 2756652 (+3) 54877
1008 2.79G 1014 0 2756682 (+30) 54877
1007 2.79G 1014 0 2756685 (+3) 54877

正如您所看到的,密钥的数量几乎是恒定的,但是随着客户机数量的增加,内存使用率会上升到2.8gb。这种内存模式是预期的吗?如果是的话,除了增加进程可用的ram数量之外,还有什么方法可以减轻它吗?
lambda客户机是用java编写的,使用的是5.2.1.release和spring数据redis 2.2.1.release
除非springdataredis中有一些额外的redis交互,否则客户机代码基本如下

public T get(final String label, final RedisTemplate<String, ?> redisTemplate) {
final BoundHashOperations<String, String, T> cache = redisTemplate.boundHashOps(REDIS_KEY);
return cache.get(label);
}

没有使用 `RedisTemplate#keys` 在我的代码库中,与redis的唯一交互是通过 `RedisTemplate#boundHashOps` 下面是 `redis-cli info memory` 扣球前后:

#### 之前

Memory

used_memory:31558400
used_memory_human:30.10M
used_memory_rss:50384896
used_memory_rss_human:48.05M
used_memory_peak:6498905008
used_memory_peak_human:6.05G
used_memory_peak_perc:0.49%
used_memory_overhead:4593040
used_memory_startup:4203584
used_memory_dataset:26965360
used_memory_dataset_perc:98.58%
allocator_allocated:32930040
allocator_active:34332672
allocator_resident:50593792
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:5140907060
maxmemory_human:4.79G
maxmemory_policy:volatile-lru
allocator_frag_ratio:1.04
allocator_frag_bytes:1402632
allocator_rss_ratio:1.47
allocator_rss_bytes:16261120
rss_overhead_ratio:1.00
rss_overhead_bytes:-208896
mem_fragmentation_ratio:1.60
mem_fragmentation_bytes:18826560
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:269952
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0


#### 之后

Memory

used_memory:4939687896
used_memory_human:4.60G
used_memory_rss:4754452480
used_memory_rss_human:4.43G
used_memory_peak:6498905008
used_memory_peak_human:6.05G
used_memory_peak_perc:76.01%
used_memory_overhead:4908463998
used_memory_startup:4203584
used_memory_dataset:31223898
used_memory_dataset_perc:0.63%
allocator_allocated:5017947040
allocator_active:5043314688
allocator_resident:5161398272
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:5140907060
maxmemory_human:4.79G
maxmemory_policy:volatile-lru
allocator_frag_ratio:1.01
allocator_frag_bytes:25367648
allocator_rss_ratio:1.02
allocator_rss_bytes:118083584
rss_overhead_ratio:0.92
rss_overhead_bytes:-406945792
mem_fragmentation_ratio:0.96
mem_fragmentation_bytes:-185235352
mem_not_counted_for_evict:0
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:4904133550
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0

9fkzdhlc

9fkzdhlc1#

在与aws support讨论过这一点之后,造成内存峰值的原因是1000个lambda客户机中的每个客户机都在用~5mb的数据填充一个读取缓冲区,因为我们存储在redis中的数据是大型序列化json对象。
他们的建议是:
在群集中添加2-3个副本,并将副本节点用于读取请求。您可以使用读卡器端点来平衡请求的负载
或使用参数控制客户端输出缓冲区,但请注意,如果达到缓冲区限制,客户端将断开连接。
客户端输出缓冲区限制正常硬限制>>如果客户端的输出缓冲区达到指定的字节数,则客户端将断开连接。默认值为零(无硬限制)。默认值为0,这意味着客户端可以使用尽可能多的内存。
客户机输出缓冲区限制正常软限制>>如果客户机的输出缓冲区达到指定的字节数,则客户机将断开连接,但前提是此情况持续时间为客户机输出缓冲区限制正常软限制秒。默认值为零(无软限制)。
客户端输出缓冲区限制正常软秒>>对于redis发布/订阅客户端:如果客户端的输出缓冲区达到指定的字节数,客户端将断开连接。默认值为0
考虑到这些约束和我们的使用概要,我们实际上要在这个示例中切换到使用s3。

相关问题