春招面试模拟,如同雷同,纯属巧合!!!

  面试的大体流程:

  第一步:一般会有笔试题,也可能没有。有笔试题就要好好答了,因为会重视笔试结果,为了节约面试官时间,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

  

  

05-11 22:26