Redis Learning Notes Collation

Source: Internet
Author: User
Tags allkeys redis sort volatile
Redis Notes Grooming

One, Redis features

Second, Redis application scenario

Third, Redis operations and optimization

One, Redis features

1, Redis is an open-source, high-performance Key-value database. It runs in memory but can be persisted to disk.

2, the advantages of Redis:

L Read and write fast, very high performance. (Memory-based operation, single thread eliminates the context switch between threads) Read 11w/s,write 8w/s.

L Atomicity, all operations are atomic.

L supports multiple data types: String, list, set, sort set, hash, Hyperloglogs, Geo.

L Support Transaction (every instruction within a transaction executes, regardless of whether there is an error in the middle, no rollback mechanism), pub/sub, Lua script, key expiration and other features

L Redis Sentinel offers highly available support for master/slave,cluster failover

L Support Data Persistence

Second, Redis application scenario

1. Cache

2. MQ

Using the list data structure, Lpush adds data to the end of the team, lpop the data from the team head

3. Ranking

Using the sort set data structure, add a score to each key by Zadd, get the top 10 data using Zrang when ranking

4. Distributed lock

Using the string data structure, the SETNX command adds a key only if the key does not exist. The approximate flow chart is as follows:

1) setnx (lockname,expiretime) Get lock

2) Successful end, failure continued

3) Get (lockname) Get expiretime, judge whether Expiretime is out of date,

4) Not expired, expired, continued

5) Getset (lockname,expiretime1), get lock and reset expiration time, Judge ReturnValue

6) returnvalue!= Expiretime get lock failed to retry. returnvalue== Expiretime acquires the lock successfully.

5. Paging optimization

When the paging data volume is large, such as the number of pages more than hundreds of pages, the list data structure can be cached by the latest N records, through the Lpush insert data, through the LTrim to maintain the length of the list chain list.

6. Common friends, possible friends

A set data structure is implemented to determine the difference between A and B sets by Sdiff, and returns all elements in set a that are different from those in the collection A. Gets the intersection of a A/b two sets through sinter.

7. Implement class Bitmap algorithm

By Hyperloglog data structure, Pfadd inserts the dataset, if the data set changes, returns 1, no change returns 0.

Third, redis operation and maintenance

1, master-slave replication

1) config configuration implementation

Slaveof <masterIP> <port>

Master exists password: Masterauth <password>

2) The concept of a backlog of space:

The cache space for commands that are designed to hold data modifications. When a request to write data is entered, the corresponding command is saved in that space for synchronization with the slave server. This space is not sent directly to slave and is designed to accommodate a master-slave disconnection.

3) Synchronization Process

Slave first connects to master, initiates a partially synchronized request (PSYNC command: Master_runid the last connected host identity and offset backlog space offsets), Master accepts the request for data validation to see if partial synchronization is satisfied. Does not satisfy the return full amount of synchronization, the host returns +fullresync Master_runid offset (slave receives and records Master_runid and offset, and prepares to receive the Rdb file) and then starts BGSAVE generate the Rdb file, after BGSAVE ends, Transferred to the slave, the Rdb file is first sent to the slave, the slave receives the RDB file to disk, and then loaded into memory.  The master then sends the data in the backlog to slave. This completes the full-volume synchronization.

The data synchronization is then pushed directly to slave by the master with data write operations.

2. Data elimination mechanism

When the memory of the instance reaches the maximum amount of memory set, the elimination mechanism of the data:

L VOLATILE-LRU: Pick the least recently used data from the set of data sets (Server.db[i].expires) that have expired time

L VOLATILE-TTL: Select the data that will expire from the set of expired data sets (Server.db[i].expires)

L Volatile-random: Choose data culling from data set (Server.db[i].expires) with set expiration time

L ALLKEYS-LRU: Select the least recently used data culling from the dataset (Server.db[i].dict)

L Allkeys-random: Choose data culling from data set (SERVER.DB[I].DICT)

L no-enviction (Expulsion): Prohibit eviction of data, exceeding maximum request direct return error

As a further indication, Redis does not guarantee that all keys will be recycled until they expire.

Redis executes commands to detect whether the memory used is over-Mem_tofree = Mem_used-server.maxmemory calculates the memory to be freed. The space is then freed according to the data-culling mechanism, and the data Change message is published locally (AOF persistent) and slave (master-slave connection).

3. Persistence strategy

The persistence strategy includes two ways of Rdb and aof. The main difference between AOF persistence and RDB persistence is that the former records changes to the data, while the latter preserves the data itself.

Two persistence modes can be turned on at the same time, in which case, when Redis restarts, it takes precedence to load the AoF file for recovery, as the AoF file is typically saved in a dataset that is more complete than the data set saved by the Rdb file.

If you want to switch a running Redis database from an RDB to a aof, we recommend that you first use dynamic switching and then modify the configuration file to restart the database. (You cannot directly modify the configuration file and restart the database, otherwise the data in the database is empty.) )

1) RDB Policy:

Dump the memory snapshot to disk for backup save, set by Conf file, trigger condition "N seconds data set has at least M changes". During backup, the main process fork out a child process for bgsave operations, the new RDB file is replaced with the old RDB file after the backup completes, and the old Rdb file is deleted.

Use the Copy-on-write method for backup, which may consume a large amount of host memory during the backup process. Avoid an RDB operation at the same time when there are multiple instances on the same host.

Advantages:

Files are kept compact, saving all data at a point in time, easy to back up, and easy to transfer.

When recovering a large data set, the RDB will be faster.

Disadvantages:

Because it is a backup every once in a while, you may lose a few minutes of data in the event of an unplanned outage of redis.

L The RDB often fork the sub-process to save the data, when the data set is large, the fork process will be time-consuming, this may cause the main process can not respond to the client's request

2) AOF strategy:

Recording each operation to the server is saved to the end of the aof file (Fsync), and Fsync is also done by fork a child process. When the aof file is too large, Redis overrides the AoF file (bgrewrite). Set by the Conf file, with the default of Fsync per second. When AOF is first opened, the AoF file is initialized to aof the existing datasets into the AoF file.

Advantages:

Based on the use of different fsync strategies, you can guarantee the fewest lost data. 1 seconds of data loss by default

L AOF is just an append file operation that can be repaired with the redis-check-aof tool for some reason when the full write command is not executed.

L AOF files are easy to read and easy to modify manually

Disadvantages:

L The same amount of data, the AoF file is larger than the Rdb file.

When the amount of data is large, the Redis reboot takes longer than the RDB.



Related references:

http://wiki.jikexueyuan.com/project/redis/master-slave-replication.html

http://www.redis.cn/topics/persistence.html

http://www.cnblogs.com/0201zcr/p/5942748.html







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.