服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Redis - 设置Redis最大占用内存的实现

设置Redis最大占用内存的实现

2022-10-18 15:52R先生专栏 Redis

本文主要介绍了设置Redis最大占用内存的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

Redis需要设置最大占用内存吗?如果Redis内存使用超出了设置的最大值会怎样?

打开redis配置文件

找到如下段落,设置maxmemory参数,maxmemory是bytes字节类型,注意转换。修改如下所示:

?
1
2
3
4
5
6
# In short... if you have slaves attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for slave
# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>
maxmemory 268435456

本机服务器redis配置文件路径:/usr/local/openresty/lualib/redis/redis.conf,由于本机自带内存只有4G,一般推荐Redis设置内存为最大物理内存的四分之三,所以设置3G,换成Byte是3221225472.

我们可以在CentOS下输入命令:find / -name redis查找redis目录:

?
1
2
3
[root@VM-8-8-centos ~]# find / -name redis
/etc/selinux/targeted/active/modules/100/redis
/usr/local/openresty/lualib/redis

Redis使用超过设置的最大值

如果Redis的使用超过了设置的最大值会怎样?让我们来改一改上面的配置,故意把最大值设为1个byte试试。

?
1
2
3
4
# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>
maxmemory 1

打开debug模式下的页面,提示错误:OOM command not allowed when used memory > ‘maxmemory’.

设置了maxmemory的选项,redis内存使用达到上限。可以通过设置LRU算法删除部分key,释放空间。默认是按照过期时间的,如果set时候没有加上过期时间就会导致数据写满maxmemory。

如果不设置maxmemory或者设置为0,64位系统不限制内存,32位系统最多使用3GB内存。

LRU是Least Recently Used 最近最少使用算法。

  • volatile-lru -> 根据LRU算法生成的过期时间来删除
  • allkeys-lru -> 根据LRU算法删除任何key
  • volatile-random -> 根据过期设置来随机删除key
  • allkeys->random -> 无差别随机删
  • volatile-ttl -> 根据最近过期时间来删除(辅以TTL)
  • noeviction -> 谁也不删,直接在写操作时返回错误 如果设置了maxmemory,一般都要设置过期策略。打开Redis的配置文件有如下描述,Redis有六种过期策略:
?
1
2
3
4
5
6
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations

那么打开配置文件,添加如下一行,使用volatile-lru的过期策略:

?
1
maxmemory-policy volatile-lru

保存文件退出,重启redis服务。

使用info命令查看Redis内存使用情况

如服务器Redis所在目录:/usr/local/openresty/lualib/redis/src

在终端输入./redis-cli,打开Redis客户端,输入info命令。

出来如下信息:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
[root@iZ94r80gdghZ src]# ./redis-cli
127.0.0.1:6379> info
# Server
redis_version:3.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:f07a42660a61a05e
redis_mode:standalone
os:Linux 3.10.0-327.10.1.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:2165
run_id:8ec8a8dc969d6e2f2867d9188ccb90850bfc9acb
tcp_port:6379
uptime_in_seconds:668
uptime_in_days:0
hz:10
lru_clock:15882419
config_file:/etc/redis/6379.conf
 
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
 
# Memory
used_memory:816232
used_memory_human:797.10K
used_memory_rss:7655424
used_memory_peak:816232
used_memory_peak_human:797.10K
used_memory_lua:36864
mem_fragmentation_ratio:9.38
mem_allocator:jemalloc-3.6.0
 
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1458722327
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
 
# Stats
total_connections_received:1
total_commands_processed:0
instantaneous_ops_per_sec:0
total_net_input_bytes:14
total_net_output_bytes:0
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
 
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
 
# CPU
used_cpu_sys:0.30
used_cpu_user:0.29
used_cpu_sys_children:0.00
used_cpu_user_children:0.00
 
# Cluster
cluster_enabled:0
 
# Keyspace
db0:keys=1,expires=1,avg_ttl=425280

其中used_memory:816232,仅用了0.7M左右。

到此这篇关于设置Redis最大占用内存的实现的文章就介绍到这了,更多相关Redis最大占用内存内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/7098612405365637128

延伸 · 阅读

精彩推荐
  • Redis关于redis状态监控和性能调优详解

    关于redis状态监控和性能调优详解

    Redis是一种高级key-value数据库。它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富。有字符串,链表、哈希、集合和有序集合5种。下面这...

    天生帅才4272019-11-08
  • Redis通俗易懂的Redis数据结构基础教程(入门)

    通俗易懂的Redis数据结构基础教程(入门)

    这篇文章主要介绍了通俗易懂的Redis数据结构基础教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    老錢11942021-07-28
  • Redis详解Redis 缓存删除机制(源码解析)

    详解Redis 缓存删除机制(源码解析)

    这篇文章主要介绍了Redis 缓存删除机制(源码解析),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    klew5832021-07-27
  • Redis简介Lua脚本与Redis数据库的结合使用

    简介Lua脚本与Redis数据库的结合使用

    这篇文章主要介绍了简介Lua脚本与Redis数据库的结合使用,Redis是基于主存的高性能数据库,需要的朋友可以参考下 ...

    goldensun8702019-10-26
  • Redisredis for windows 6.2.6安装包最新步骤详解

    redis for windows 6.2.6安装包最新步骤详解

    这篇文章主要介绍了redis for windows 6.2.6安装包全网首发,使用Windows计划任务自动运行redis服务,文章给大家讲解的非常详细,对大家的学习或工作具有一定的...

    冰河之刃9552022-10-17
  • RedisRedis migrate数据迁移工具的使用教程

    Redis migrate数据迁移工具的使用教程

    这篇文章主要给大家介绍了关于Redis migrate数据迁移工具的使用教程,文中通过示例代码介绍的非常详细,对大家的学习或者使用Redis具有一定的参考学习价...

    嘟噜聪4412020-09-01
  • Redisredis实现共同好友的思路详解

    redis实现共同好友的思路详解

    微信朋友圈大家都玩过吧,那么朋友圈的点赞、评论只能看到自己好友的信息是怎么操作的呢?下面通过本文给大家分享下此功能的实现流程,对redis实现...

    叁滴水3902021-08-06
  • RedisRedis 6.X Cluster 集群搭建

    Redis 6.X Cluster 集群搭建

    码哥带大家完成在 CentOS 7 中安装 Redis 6.x 教程。在学习 Redis Cluster 集群之前,我们需要先搭建一套集群环境。机器有限,实现目标是一台机器上搭建 6 个节...

    码哥字节14062021-04-07