Redis Persistence and Data Types

redis persistence

Redis is a memory-level caching program, that is, redis uses memory to cache data, but it can save the data in memory to hard disk according to certain strategies, so as to achieve the purpose of data persistence. redis supports two different ways of data persistence mechanism, namely RDB. And OF

RDB mode:

RDB: Time-based snapshots, which only keep the latest snapshots, are characterized by faster execution speed, and the disadvantage is that data not snapshotted between the last snapshot and the current time point may be lost.

The specific process of RDB implementation is that Redis first fork s out a sub-process from the main process. Using the write-time replication mechanism, the sub-process saves the memory data as a temporary file, such as dump.rdb.temp. When the data is saved, it replaces the last saved RDB file, and then closes the sub-process, so that it can be saved. The data saved in every RDB snapshot is complete, because there may be a sudden power failure when directly replacing RDB files, which may lead to the loss of data when the RDB files have not been saved completely and then suddenly shut down to save, so that the process of each generated RDB file can be backed up manually. Samples can maximize the preservation of historical data.

Advantages and disadvantages of RDB model

Advantage:

RDB snapshot saves data at a certain time point. It can customize time point backup by script execution of bgsave (non-blocking) or save (blocking) commands. It can retain multiple backups. When problems arise, it can be restored to versions at different time points. - The performance of o can be maximized, because the only thing the parent process needs to do when saving RDB files is fork to produce a child process, and then the - operation will have this child process operation, the parent process does not need any IO operation.

RDB recovers faster than AOF in large amounts of data, such as several G data.

Disadvantages:

Failure to save data from time to time will result in loss of memory data from the last RDB backup to the current one

When the amount of data is very large, it takes a little time to fork from the parent process, maybe milliseconds or seconds or minutes, depending on disk IO performance.

AOF Model

AOF: Adding operations to a specified log file in order of operation is characterized by relatively high data security. The disadvantage is that even if some operations are repeated, all operations will be recorded.

Like RDB, AOF uses write-time replication mechanism. By default, AOF uses fsync once a second. The commands to be executed are stored in the AOF file. Thus, even if the redis server fails, data within one second will be lost at most. Different fsync policies can be set, or commands can be executed every time. When fsync is executed, fsync executes threads in the background, so the main thread can continue to process the user's normal requests without being affected by the IO written to the AOF file.

Advantages and disadvantages of AOF model:
The file size of AOF is larger than that of RDB format. According to the fsync policy used (fsync is redis in synchronous memory, all modified files are transferred to storage device), the default is append fsync everysec, that is, fsync is executed once a second.

redis data type

String:

String is the most common and commonly used data type in all programming languages, and it is also one of the most basic data types in redis, and all key types in redis are strings.

Get the difference set of the set:
Difference sets: Elements that already belong to A but not B are called differences between A and B.

# 127.0.0.1:6379> SDIFF set1 set2
1)"v1"
2)"v3"

Get the intersection of collections:
Intersection: Elements that already belong to A and B are called intersections of A and B.

# 127.0.0.1:6379> SINTER set1 set2
1)"v4"
2)"v2"

Get the union of sets:
Unification: Elements that already belong to A or B are called unions of A and B.

# 127.0.0.1:6379> SUNION set1 set2
1)"v2"
2)"v4"
3)"v1"
4)"v3"

Sorted set

Redis ordered set is also a set of string-type elements, and no duplicate members are allowed. The difference is that each element will be associated with a double (double-precision floating-point) type score. redis is used to sort the members of the set from small to large through the score. The members of ordered set are unique. However, scores can be repeated. Collections are implemented by hash tables. Therefore, the complexity of adding, deleting and searching is O(1). The largest number of members in a collection is 2 ^ 32 - 1 (4294967295, each set can store more than 4 billion members).