10. RDB endurance

RDB file creation and loading

The SAVE instruction will block the Redis server process until the RDB file is created. During the server process is blocked, the server cannot process any command requests

The BGSAVE command will spawn a child process, which is responsible for creating the RDB file, and the server process (parent process) continues to process the command request.

def SAVE():
	#创建RDB文件
	rdbSave()
	
def BGSAVE():
	# 创建子进程
	pid=fork()
	
	if pid == 0:
		# 子进程负责创建RDB文件
		rdbSave()
		# 完成之后向父进程发送信号
		signal_parent()
	elif pid>0:
		# 父进程继续处理命令请求,并通过轮询等待子进程的信号
		handle_request_and_wait_signal()
	else:
		# 处理出错情况
		handle_fork_error()

Automatic interval saving

The server automatically executes a BGSAVEcommand at regular intervals , and multiple storage conditions can be set. As long as any one of the conditions is met, the server will execute the BGSAVEcommand

save 900 1
save 300 10
save 60 10000

服务器在900秒内进行了至少1次修改
服务器在300秒内进行了至少10次修改
服务器在60秒内进行了至少10000次修改

Set storage conditions

struct redisServer
{
	struct saveparam * saveparams;
	long long dirty; //距离上一次成功执行BGSAVE之后,进行修改的次数
	time_t lastsave; //上一次成功执行BGSAVE的时间
};

struct saveparam
{
	time_t seconds;
	int changes;
}

Check whether the storage conditions are met

It is executed every 100ms by defaultserverCron()

def serverCron():
	# 遍历所有保存条件
	for saveparam in server.saveparams:
		save_interval=unixtime_now()-server.lastsave
		
		#如果数据库状态的修改次数超过条件所设置的次数
		#并且距离上次保存的时间超过条件所设置的时间
		#那么执行保存操作
		if server.dirty >= saveparam.changes and save_interval>saveparam.seconds:
			BGSAVE()

RDB file structure

Overview

  1. REDIS, through these five characters, the program can quickly check whether the loaded file is an RDB file when loading a file
  2. db_version, records the version number
  3. databases, contains any number of databases and their key-value pair data.
  4. EOF, marking the end of the body content of the RDB file
  5. check_sum, checksum, check whether the RDB file is wrong or damaged

databases

Each non-empty database saves three parts:

  1. SELECTDB constant, indicating that a database number will be read next
  2. db_number, after reading in, call the SELECT command to switch the database, so that the key-value pair read later can be loaded into the correct database
  3. key_value_pairs, saves all key-value pair data in the database. Contains expiration time.

key_value_pairs

  1. TYPE records the type of value, which represents an object type or underlying code.
  2. key is always a string object
  3. The value saves the content of the corresponding type according to the TYPE instruction
  4. EXPIRETIME_MS constant, which means that an expiration time in milliseconds will be read later
  5. ms, save the expiration time of the key-value pair

Guess you like

Origin blog.csdn.net/weixin_42249196/article/details/108634882
RDB