Redis database operations, persistence and memory management infrastructure Detailed Overview

Redis database operations, persistence and memory management infrastructure Detailed Overview

Foreword

The first part introduces the installation process and compile redis redis two commands commonly used tools, database-related operations of this article focuses on redis: CRUD etc., as well as brief understanding redis persistence principle and configuration operations.

Redis database operations

Redis database commonly used commands (usually upper or lower case) - is relatively simple, direct introduction grammar, try to validate their own course, these are just tip of the iceberg, for example, are not different types of data corresponding to the same operation, have research interests can understand.
A single database command:

set——写入数据
语法:set key value

get——读取数据
语法:get key

keys——查询命令
语法:keys [*?]
*——多次
?——几个问号表示匹配几位

exists——判断是否存在每个key
语法:exists key_name

del——删除key
语法:del key_name

type——查看key对应的value的数据类型
语法:type key_name

rename——重命名key(具备覆盖特点旧值覆盖目标值)
语法:rename src_keyname dest_keyname

renamenx——对已有的key重命名,且检测新名是否存在
语法:renamenx old_keyname new_keyname

dbsize——查看该数据库key的个数
语法:dbsize

Multiple database commands (the Redis default database index range 16 0 to 15, starts from 0)

select——数据库切换
语法:select index 

move——移动数据
语法:move key_name index

flushdb、flushall——清空当前数据库内数据(高危命令)
语法:flushdb

Redis persistence

1. What is persistence?

Is simply stored data, prevent data loss, errors and other scenarios lead to business problems.

All Redis data are stored in memory, and then from time to time saved to disk (this is known as "semi-persistent mode") by asynchronously; can also change each time data is written to a append only file (aof) inside (this is known as "full-persistent mode").

2. Why do you need to persist?

Since Redis data are stored in memory, if not configured persistent, restart the server after redis data on the whole is lost, it is necessary to open the persistence of redis function, save data to disk, when the redis server restarts, you can restore data from disk. redis persistence provides two ways, one is persistent RDB (Reids principle is the in-memory database record timing to dump the RDB persistent disk), the other is AOF (append only file) persistence ( Reids operating principle is to log additional way to write files).

2.1RDB persistence

Create a way to get a snapshot copy of all data Redis in a moment; RDB persistence means that within a specified time interval memory snapshot of the data set is written to disk, the actual operation is fork a child process, the first data set write temporary files after writing is successful, then replace the previous file, binary compressed storage.

2.2AOF persistence

AOF persisted in the form of log records processed by the server every write, delete, query operation is not recorded, the recorded text, you can open the file to see the detailed operating record.

2.3RDB AOF and the advantages and disadvantages

RDB advantage

Easy data backup (in particular, a large number)

Suitable for disaster recovery

Good performance

Start high efficiency

RDB disadvantage

Relatively high probability of data loss

Server running experiencing large data sets may stop (1s inside)

AOF advantage

Data security, support for multiple synchronization strategy

Additional writing, can guarantee data consistency

Using rewrite mechanisms to protect data more secure

According to data log can complete the reconstruction work

AOF disadvantage

A relatively large space

Restore large data sets relatively slow

3, how persistence?

RDB for persistence:

redis.conf default configuration

save 900 1 #——900s内,如果超过1个key被修改,则发起快照保存

save 300 10 #——300秒内,如果超过10个key被修改,则发起快照保存

save 60 10000 #——60秒内,如果1万个key被修改,则发起快照保存

AOF for persistence:

redis.conf Configuration

appendonly no  #——no 改为yes即开启AOF持久化
#开启AOF持久化启动服务端多了appendonly.aof文件
appendfsync always #——每次操作都会立即写入aof文件(效率最低)
appendfsync everysec #——每秒持久化(默认推荐选择)
appendfsync no #——不主动进行同步,默认30s一次

Finally, this paper gives a mind map redis corresponds to the end of the full memory management

Redis database operations, persistence and memory management infrastructure Detailed Overview

Guess you like

Origin blog.51cto.com/14557673/2481018