春招必问的redis持久化(RDB AOF),你能答上来么?
春招面试模拟,如同雷同,纯属巧合!!!
面试的大体流程:
第一步:一般会有笔试题,也可能没有。有笔试题就要好好答了,因为会重视笔试结果,为了节约面试官时间,HR可能先会看,不合格直接让你走人了。
第二步:开始面试,面试官会让你先来个自我介绍,他在看你的简历。可能有人会有疑问?为什么简历上都写的很清楚还让我做自我介绍?因为面试官不会提前看你的简历,他要有时间去看简历。所以自我介绍,不宜过长或过短,要重点突出,怎么重点突出?提前看好岗位要求,要求都是入职之后用到的技术,所以面试官会看重那些!!!自我介绍结束,步入正题。
面试官会问:之前的公司用过redis么?
面试者有两类回答:
面试者甲:没用过,但自己学过,下载过源码,自己部署安装过,基本的命令像string/hash/lsit/set/zset,都熟悉;
面试官(心想虽然没用,但动手能力很强,也很好学,不错):redis是内存数据库,那它怎么进行持久化的?
面试官甲(心想没注意看啊,不知道啊):...
面试者乙:用过,比较熟悉
面试官会接着问:持久化方式有哪些?
面试者乙:RDB和AOF
面试官:原理?区别?优缺点说一下吧?
面试官乙:...
要是是你去面试,你能回答上来么?
下面让我为你一一揭晓答案!!!
RDB持久化(Redis DataBase)
redis是内存数据库,一旦服务器进程退出,服务器中的数据库状态也会消失不见。重点介绍save和bgsave命令。
RDB文件的创建与载入
save命令会阻塞Redis服务器进程,直到RDB文件创建为止,在服务器进程阻塞期间,服务器不能处理任何命令请求。
bgsave命令派生出子进程,,然后由子进程创建RDB文件,父进程继续处理请求。
创建RDB文件实际rdb.c/rdbLoad函数完成的
自动间隔性保存
redis服务器会通过用户配置save选项,每隔一段时间去执行一下bgsave命令;默认的配置文件redis.conf,关于rdb部分的配置如下:
################################ SNAPSHOTTING ################################
#
# Save the DB on disk:
#
# 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 sec ( min) if at least key changed
# after sec ( min) if at least keys changed
# after sec if at least 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
save
save # By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-writes-on-bgsave-error yes # Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes # Since version of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around %) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes # The filename where to dump the DB
dbfilename dump.rdb # 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 ./
rdb
主要有三条策略,满足任意一个,就会执行bgave命令:
服务器在900秒之内,对数据库至少修改了1次
服务器在300秒之内,对数据库至少修改了10次
服务器在60秒之内,对数据库至少修改了10000次
优缺点
优点:
适合大规模的数据恢复
对数据完整性和一致性要求不高
缺点:
会丢失最后一次修改的数据
fork会产生额外消耗
AOF持久化(Append Only File)
与RDB通过键值对来记录数据库状态不同,AOF是通过Redis服务器所执行的写命令来记录数据库状态的。
AOF持久化的实现
AOF持久化功能的实现可以分为命令追加(append)、文件写入、文件同步(sync)三个步骤。
aof在redis.conf配置文件的:
############################## APPEND ONLY MODE ############################### # 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 no # The name of the append only file (default: "appendonly.aof") appendfilename "appendonly.aof" # The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log. Slow, Safest.
# everysec: fsync only one time every second. Compromise.
#
# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec. # More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "everysec". # appendfsync always
appendfsync everysec
# appendfsync no # When the AOF fsync policy is set to always or everysec, and a background
# saving process (a background save or AOF log background rewriting) is
# performing a lot of I/O against the disk, in some Linux configurations
# Redis may block too long on the fsync() call. Note that there is no fix for
# this currently, as even performing fsync in a different thread will block
# our synchronous write() call.
#
# In order to mitigate this problem it's possible to use the following option
# that will prevent fsync() from being called in the main process while a
# BGSAVE or BGREWRITEAOF is in progress.
#
# This means that while another child is saving, the durability of Redis is
# the same as "appendfsync none". In practical terms, this means that it is
# possible to lose up to seconds of log in the worst scenario (with the
# default Linux settings).
#
# If you have latency problems turn this to "yes". Otherwise leave it as
# "no" that is the safest pick from the point of view of durability. no-appendfsync-on-rewrite no # Automatic rewrite of the append only file.
# Redis is able to automatically rewrite the log file implicitly calling
# BGREWRITEAOF when the AOF log size grows by the specified percentage.
#
# This is how it works: Redis remembers the size of the AOF file after the
# latest rewrite (if no rewrite has happened since the restart, the size of
# the AOF at startup is used).
#
# This base size is compared to the current size. If the current size is
# bigger than the specified percentage, the rewrite is triggered. Also
# you need to specify a minimal size for the AOF file to be rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase # Specify a percentage of zero in order to disable the automatic AOF
# rewrite feature. auto-aof-rewrite-percentage
auto-aof-rewrite-min-size 64mb # An AOF file may be found to be truncated at the end during the Redis
# startup process, when the AOF data gets loaded back into memory.
# This may happen when the system where Redis is running
# crashes, especially when an ext4 filesystem is mounted without the
# data=ordered option (however this can't happen when Redis itself
# crashes or aborts but the operating system still works correctly).
#
# Redis can either exit with an error when this happens, or load as much
# data as possible (the default now) and start if the AOF file is found
# to be truncated at the end. The following option controls this behavior.
#
# If aof-load-truncated is set to yes, a truncated AOF file is loaded and
# the Redis server starts emitting a log to inform the user of the event.
# Otherwise if the option is set to no, the server aborts with an error
# and refuses to start. When the option is set to no, the user requires
# to fix the AOF file using the "redis-check-aof" utility before to restart
# the server.
#
# Note that if the AOF file will be found to be corrupted in the middle
# the server will still exit with an error. This option only applies when
# Redis will try to read more data from the AOF file but not enough bytes
# will be found.
aof-load-truncated yes
aof
AOF重写
随着服务器时间的流逝,文件的体积越来越大,体积过大的AOF文件对redis服务器、甚至整个宿主计算机造成影响。并且AOF文件的体积越大,使用AOF文件进行数据还原所需的时间越多。
为了解决AOF文件体重膨胀的问题,redis提供了AOF文件重写(rewrite)的功能。
触发机制
redis会记录上次重写时AOF的大小,默认配置是当AOF文件大小是上次rewrite后大小的一倍且大于64M;默认配置如下:
  auto-aof-rewrite-percentage 100
  auto-aof-rewrite-min-size 64mb
悄悄告诉你们个小秘密:可能在面试时,面试官会吹嘘公司很牛,redis用的出神入化,当你入职之后,可以悄悄看看“auto-aof-rewrite-min-size 64mb”,默认64M大小,根本不够用,告诉发展的公司起码要3G起。
优缺点
优点:
配置灵活,可以选择多种方式进行持久化
缺点:
相同数据集的数据而言,aof文件要远大于rdb文件,恢复速度慢于rdb
春招必问的redis持久化(RDB AOF),你能答上来么?的更多相关文章
- Redis持久化rdb&aof
		
Redis持久化rdb&aof 前言 持久化:即把数据存储于断电后不会丢失的设备中,通常是硬盘 常见的持久化方式: 主从:通过从服务器保持持久化,如mongoDB的replication se ...
 - Redis持久化--RDB+AOF(转)
		
1.Redis两种持久化方式 RDB 执行机制:快照,直接将databases中的key-value的二进制形式存储在了rdb文件中 优点:性能较高(因为是快照,且执行频率比aof低,而且rdb文件中 ...
 - 第十章 Redis持久化--RDB+AOF
		
注:本文主要参考自<Redis设计与实现> 1.Redis两种持久化方式 RDB 执行机制:快照,直接将databases中的key-value的二进制形式存储在了rdb文件中 优点:性能 ...
 - [动图演示]Redis 持久化 RDB/AOF 详解与实践
		
Redis 是一个开源( BSD 许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件.它支持的数据类型很丰富,如字符串.链表.集 合.以及散列等,并且还支持多种排序功能. 什么叫持 ...
 - [动图演示]Redis 持久化 RDB/AOF 详解与实践【华为云技术分享】
		
Redis 是一个开源( BSD 许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件.它支持的数据类型很丰富,如字符串.链表.集 合.以及散列等,并且还支持多种排序功能. 什么叫持 ...
 - redis++:Redis持久化  rdb & aof  工作原理及流程图 (三)
		
RDB的原理: 在Redis中RDB持久化的触发分为两种:自己手动触发与Redis定时触发. 针对RDB方式的持久化,手动触发可以使用: 1):save:会阻塞当前Redis服务器,直到持久化完成,线 ...
 - redis持久化RDB与AOF
		
redis持久化 Redis是一种内存型数据库,一旦服务器进程退出,数据库的数据就会丢失,为了解决这个问题,Redis提供了两种持久化的方案,将内存中的数据保存到磁盘中,避免数据的丢失. RDB持久化 ...
 - Linux 安装redis  基本配置 发布订阅,安全配置,持久化 rdb ,aof
		
redis redis相关配置1.yum 源码 rpm yum 快速,间接,高效,解决依赖关系,(自动安装到某个路径,不可控),通过yum安装的软件查询命令 rpm -ql nginx yum源 ...
 - redis的持久化(RDB&AOF的区别)
		
RDB 是什么? 在指定的时间间隔内将内存中的数据集快照写入磁盘, 也就是行话讲的Snapshot快照,它恢复时是将快照文件直接读到内存里. Redis会单独创建(fork)一个子进程来进行持久化,会 ...
 
随机推荐
- C# 获取WebBrowser内容的高度
			
int webHeight =0; WebBrowser web =new WebBrowser(); web.Navigate("about:blank"); while (we ...
 - 【转】DB2数据库编目的概念以及对其的正确解析
			
此文章主要向大家描述的是DB2数据库编目的概念以及对DB2数据库编目的概念的正确理解,在DB2中编目(catalog)这个单词看似很难理解,我自己当初在学习DB2数据库的时候也常常被这个编目搞的很不明 ...
 - Spring Boot2 系列教程(二十一) | 自动配置原理
			
微信公众号:一个优秀的废人.如有问题,请后台留言,反正我也不会听. 前言 这个月过去两天了,这篇文章才跟大家见面,最近比较累,大家见谅下.下班后闲着无聊看了下 SpringBoot 中的自动配置,把我 ...
 - BFS - 求最短路径
			
Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...
 - Selenium的简单使用
			
selenium的使用对于新手来说十分友好,因为他避开了如今网络中的异步加载抓取的困扰,使得我们大部分的时间可以用于提取信息和存储中,下面就简单的列一些使用的代码,希望给同样初学的你有一定的参考价值. ...
 - python 面向对象-初识
			
一.分类 1.面向过程 2.面向函数 3.面向对象 二.类和对象 1.宏关 类是抽象的,对象是具体的 2.实例化 类->对象 3.类的作用 1)实例化成对象 实例化的过程,创建self对象,调用 ...
 - 10.JavaSE之包机制
			
包机制: 为了更好的组织类,Java提供了包机制,用于区别类名的命名空间 包语句的语法格式为: package pkg1[ . pkg2[ . pkg3...]]; package com.duan. ...
 - Python入门小建议
			
"java从心"正式更名"KEN DO EVERTHING".Ken(Can) Do Everything,无所不能 Believe youseft then ...
 - scikit-learn基础
			
一.scikit-learn基础 sklearn.ensemble模块有两种基于决策树的算法----随机森林和极端随机树
 - Java中SMB的相关应用
			
目录 SMB 服务操作 Ⅰ SMB简介 Ⅱ SMB配置 2.1 Windows SMB Ⅲ 添加SMB依赖 Ⅳ 路径格式 Ⅴ 操作共享 Ⅵ 登录验证 SMB 服务操作 Ⅰ SMB简介  SMB(全称 ...