分布式项目 07.redis的内存和磁盘策略

Redis作为中间件,既可以作为缓存存储数据在内存中,也可以通过RDB和AOF两种模式进行数据持久化。RDB模式在满足特定条件(如指定时间内操作次数)时生成快照,保存到磁盘,适合对数据丢失容忍度较高的场景。AOF模式则记录所有操作日志,确保高数据持久性。内存策略包括LRU和LFU等算法,用于处理内存溢出问题。
摘要由CSDN通过智能技术生成

redis作为一个中间件,很多时候被视为一个缓存,缓存存在cache中,也就是高速内存。但是有时也需要可以长时间的保存数据,甚至打开项目的时候,可以在缓存中进行访问。

也就是把数据放在磁盘中。有两种策略:RDB模式和AOF模式,其中的RDB模式是redis默认的长时间保存数据的模式。

RDB模式:redis中的set操作,在规定的周期内执行了指定的次数时,redis
会自动的将内存中的数据持久化到硬盘中.并且后缀名为.rdb

优点: 使用单独子进程来进行持久化,主进程不会进行任何IO操作,保证了redis的高性能

缺点: RDB是间隔一段时间进行持久化,如果持久化之间redis发生故障,会发生数据丢失。所以这种方式更适合数据要求不严谨的时候

在redis.conf文件中:
在linux中打开redis.conf文件:

[root@localhost ~]# cd /
[root@localhost /]# ls
bin   dev  home  lib64       media  opt   root  selinux  sys  usr  webapps
boot  etc  lib   lost+found  mnt    proc  sbin  srv      tmp  var
[root@localhost /]# cd usr/local/src
[root@localhost src]# ls
jdk1.8  mysql  redis  tomcats
[root@localhost src]# cd redis
[root@localhost redis]# vim redis.conf

在redis.conf的文件中:第200行开始,关于RDB模式的save保存规定:

#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

save 900 1
save 300 10
save 60 10000
save 900 1
save 300 10
save 60 10000

save 900 1 在900秒内 执行1次set操作 持久化一次
save 300 10 在300秒内 执行10次set操作 持久化一次
save 60 10000 在60秒内 执行10000次set操作,持久化一次

保存的.rdb文件的文件名是:在redis.conf文件的第254行

# The filename where to dump the DB
dbfilename dump.rdb

默认的文件名是dump,位置在redis的文件中:
redis.conf文件中的第264行:文件在./,意思是说redis.conf的同级

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./

结果:

root@localhost ~]# cd /
[root@localhost /]# cd usr/local/src
[root@localhost src]# cd redis
[root@localhost redis]# ls
00-RELEASENOTES  CONTRIBUTING  deps      INSTALL   MANIFESTO  redis.conf  runtest-cluster   sentinel.conf  tests
BUGS             COPYING       dump.rdb  Makefile  README.md  runtest     runtest-sentinel  src            utils

持久化命令,可以直接用命令进行持久化操作
说明:在redis客户端中提供了持久化的命令.save和bgsave
命令说明:

  1. save命令
    主动持久化 只要数据备份,其他的线程处于阻塞状态.
  2. bgsave命令
    后台备份,不会造成线程阻塞.

AOF模式:
Append-only file,将“操作 + 数据”以格式化指令的方式追加到操作日志文件的尾部,在append操作返回后(已经写入到文件或者即将写入),才进行实际的数据变更,“日志文件”保存了历史所有的操作过程;

当server需要数据恢复时,可以直接replay此日志文件,即可还原所有的操作过程。AOF相对可靠,它和mysql中bin.log、apache.log、zookeeper中txn-log简直异曲同工。AOF文件内容是字符串,非常容易阅读和解析。

相关的规定在redis.conf文件中:
第700行,把appendonly no改成appendonly yes
第704行,文件的名称

# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.

appendonly yes

# The name of the append only file (default: "appendonly.aof")

appendfilename "appendonly.aof"

appendonly.aof文件在redis.conf的同级目录下

[root@localhost redis]# redis-cli -p 6379 shutdown
[root@localhost redis]# redis-server redis.conf
9265:C 16 May 2023 05:09:07.209 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9265:C 16 May 2023 05:09:07.209 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=9265, just started
9265:C 16 May 2023 05:09:07.209 # Configuration loaded
[root@localhost redis]# ls
00-RELEASENOTES  BUGS          COPYING  dump.rdb  Makefile   README.md   runtest          runtest-sentinel  src    utils
appendonly.aof   CONTRIBUTING  deps     INSTALL   MANIFESTO  redis.conf  runtest-cluster  sentinel.conf     tests

内存策略:
Redis中的数据都保持在内存中,如果不定期清理则必然会导致内存溢出.必然使用算法实现定期清理内存.
也就是操作系统的中的cache刷新算法:
LRU算法
LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰。该算法赋予每个页面一个访问字段,用来记录一个页面自上次被访问以来所经历的时间 t,当须淘汰一个页面时,选择现有页面中其 t 值最大的,即最近最少使用的页面予以淘汰。
访问到的那个cache块,t就0。

LFU算法
说明: redis5.0以后的版本才有LFU算法.
LFU(least frequently used (LFU) page-replacement algorithm)。即最不经常使用页置换算法,要求在页置换时置换引用计数最小的页,因为经常使用的页应该有一个较大的引用次数。但是有些页在开始时使用次数很多,但以后就不再使用,这类页将会长时间留在内存中,因此可以将引用计数寄存器定时右移一位,形成指数衰减的平均使用次数。

内存策略:
1.volatile-lru 设定了超时时间数据,之后采用LRU算法进行删除
2.allkeys-lru 全部数据,采用LRU算法进行内存数据的优化
3.volatile-lfu 设定了超时时间的数据,采用LFU算法进行删除
4.allkeys-lfu 所有的数据采用LFU算法实现数据删除
5.volatile-random 为设定超时时间的数据采用随机算法
6.allkeys-random 所有数据采用随机算法实现删除
7.volatile-ttl 将所有设定了超时时间的数据,利用ttl方式进行排序,将还没超时的数据提前删除
8.noeviction(默认策略)不采用任何的算法删除数据,如果将来内存溢出则报错返回

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值