Cache Learning---Redis

Source: Internet
Author: User
Tags data structures file size hash memcached memory usage redis redis server
Introduction

The official website publishes the data: reads the speed is 110,000 times/s, writes the speed is 81,000 times/s

Redis is a key-value storage system. Similar to memcached, it supports storing more value types, including string (string), list (linked list), set (set), Zset (sorted set– ordered collection), and hash (hash type). These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic. Based on this, Redis supports sorting in a variety of different ways. As with memcached, data is cached in memory to ensure efficiency. The difference is that Redis periodically writes the updated data to disk or writes the modified operation to the appended record file, and Master-slave (Master-Slave) synchronization is implemented on this basis. (from Baidu Encyclopedia) comparable memcache and Redis

Too lazy to write, copy from the ' Real Home ' blog
-Performance: There's no need to be overly concerned about performance, because both are already high enough. Since Redis uses only single cores, and memcached can use multicore, on average, Redis has a higher performance than memcached for storing small data on a per-core scale. In more than 100k of data, memcached performance is higher than Redis, although Redis has recently been optimized for the performance of storing big data, but it is slightly inferior to memcached. Having said so much, the conclusion is that no matter which one you use, the number of requests per second will not be a bottleneck. (such as bottlenecks may be in the network card)
-Memory usage efficiency: With simple key-value storage, memcached memory utilization is higher, and if Redis uses hash structure to do key-value storage, its memory utilization will be higher than memcached because of its combined compression. Of course, this is related to your application scenario and data characteristics.
-Data persistence: If you have requirements for data persistence and data synchronization, it is recommended that you choose Redis because neither of these features memcached. Choosing Redis is also wise, even if you just want the cached data to be not lost after upgrading or rebooting the system.
-Data structure: Of course, finally, you need to talk about your specific application needs. Redis has more data structures and supports richer data operations than memcached, usually in memcached, you need to get the data to the client for similar modifications and set it back. This greatly increases the number of network IO and the volume of data. In Redis, these complex operations are often as efficient as the general get/set. So, if you need caching to support more complex structures and operations, Redis is a good choice.
-Network IO Model aspect: Memcached is multi-threading, divided into listening thread, worker thread, introduce lock, bring the performance loss. Redis uses a single-threaded IO multiplexing model to maximize the speed advantage and provide a simpler computing capability
-Memory Management: Memcached uses a pre-allocated pool of memory to create a certain amount of wasted space, and when memory is still large, new data may be rejected, and Redis uses on-site memory to store data without culling any non-temporal data Redis is more suitable as a storage instead of a cache
-Data consistency: memcached provides CAS commands to guarantee. And Redis provides the functionality of a transaction that guarantees the atomicity of a sequence of commands, which is not interrupted by any action.Redis Data Types

Redis has 5 types of data: string (String), hash (hash table), list (linked list), set (set), Zset (ordered collection)
Redis encoding Mode: Raw (Simple dynamic string), int (Long Integer), HT (dictionary), LinkedList (double-ended linked list), Ziplist (compressed list), Skiplist (Skip table), Intset (integer collection) Type encoding method correspondence relationship

Simple Dynamic String

Linked list

Dictionary


The use of a Redis dictionary is the most basic implementation of the hash table, as shown
Redis Database Key space is a dictionary that holds all of the database key-value pairs

Key is the key of the database, as a string object
Value is the value of the database, which may be one of 5 data types
things Redis things consist of three stages: things start with commands multi command queued things queue FIFO (FIFO) things execute command exec
Redis things can not be rolled back, such as three commands 1, 2, 3, follow the new execution, when 2 occurs the problem is that 1 will not be rolled back persisted snapshot Rdb

Default persistence mode for use
Command SAVE BGSAVE
Save will block the server process until the Rdb file creation is complete
Bgsave will not block, will send a thread to implement

Redis defaults to Bgsave, with configuration files specifying the execution interval
Save 1 #900秒内有一次变更
Save #300秒内有10次变更
Save 10000 #60秒内有10000次变更

Boot server automatically import Rdb file, if aof file first import aof file aof (append only) persisted

Record database status by saving write commands performed by the Redis server
AOF persistence is divided into: command append, file Write file Synchronization command append: After the server executes a write command, appends the command to the AOF_BUF buffer end file in protocol format (plain text format) to the file synchronization: through the Servercron timer function, Call the Flushappendonlyfile function to determine whether the buffer contents are written to the AoF file

aof file load Restore

Create a virtual client without a network link, execute the command in the AoF file
Note: Redis commands can only perform aof overrides in the client context why you need to override

AOF command Append causes the AoF file to become larger, affecting the performance of the Redis server is a single thread, to prevent aof overrides when blocking the Redis main thread, Redis will aof overrides to the child process execution, so that the Redis server will not be affected by processing command requests AoF Rewrite, create a new aof file, read the current value from the Redis database, use a command to record the key value pair in the AoF rewrite, the server is still processing the request, in order to prevent data inconsistency, add a aof rewrite buffer, waiting for the child process to finish is to send a signal to the server, This will block the server, write the aof rewrite buffer content to the new aof file, modify the file name to replace the original aof file, complete the aof rewrite trigger time

Conditions that trigger AOF background overrides 1, aof overrides can be triggered manually by the user by calling Bgrewriteaof.

2, the server in the case of aof function, will maintain the following three variables: Record the current aof file size variable aof_current_size. The variable aof_rewrite_base_size aof file size is recorded after the last aof rewrite. Growth percent variable AOF_REWRITE_PERC.

Each time the Servercron (server normal operation) function executes, it checks to see if all of the following conditions are met and, if all is satisfied, triggers an automatic aof rewrite operation:
1), no Bgsave command (RDB persistence)/aof persistence in execution;
2), no bgrewriteaof in progress;
3), the current aof file size is greater than server.aof_rewrite_min_size (default is 1MB), or redis.conf configured auto-aof-rewrite-min-size size;
4), the ratio between the current aof file size and the size of the last rewrite is equal to or equal to the specified growth percentage (the Auto-aof-rewrite-percentage parameter is set in the configuration file, not set to default to 100%)

If the previous three conditions are satisfied and the current aof file size is greater than the specified percentage for the last aof override, the automatic aof override is triggered. Copy The slaveof command allows one server to replicate a server that is replicated by another server called the primary server (master) to replicate the server called from the server (slave) Redis replication feature into synchronization, command propagation synchronization implementation

Command Propagation

After the master-slave server is consistent, after the primary server executes the write command, the command is sent to the slave server to ensure consistent state
Note: The old version of sync Copy, can not solve the problem of the disconnection of the connection (the connection will be copied once, the performance is very low), the new version of the copy using Psync to increase the partial copy function, through the offset implementation (see: Redis Design and implementation of the second edition) Sentinel

is a solution implementation of Redis high availability

A Sentinel system consisting of one or more sentinel instances, monitoring the slave servers of any number of primary and primary servers, and then upgrading from the server to the new primary server when the primary server goes offline (see: Redis Design and Implementation Second Edition) cluster

Redis uses the cluster, there is no concept of the database, the cluster will be the entire database into 16,384 slots (slots), each node management 0-16384 slots, when all slots have node processing when the cluster is on-line state, otherwise the cluster is offline status

Compute key belongs to that slot algorithm

CRC16 (Key) & 16383 CRC16 (key) calculates CRC-16 checksum, & 16383 calculates a slot number of 0-16383 as key to publish a subscription

Not used (see details: Redis Design and Implementation Second Edition) common commands for redis commands

Keys key*
TTL key
Type key
Del key
Exists key
Info Print Redis Server information
FLUSHDB Delete all keys for the current DB
Flushall Delete all key strings for all DB , hash, list, set, Zset ordered set, etc.

command can refer to https://www.cnblogs.com/luoxn28/p/5790313.html redis usage Scenario normal string cache (e.g. token, user ID, session, etc.) counter ranking

Assume a real-time arena rating, scoring after the player finishes, and dynamically displaying the player's leaderboard
Zadd rank Biki Zhibin Ming Fen 98 Cat
(integer) 5
Show top three players with highest score
Zrevrange Rank 0 2 withscores
1) "Biki"
2) 100.0
3) "Cat"
4) 98.0
5) "Zhibin"
6) 87.0 distributed lock

Implemented by the SETNX command
The success setting value returns 1, otherwise returns 0

127.0.0.1:6379> setnx Test Value
(integer) 1
127.0.0.1:6379> setnx test value
(integer) 0

Note: Using Redis to implement distributed locks requires setting the expiration time to prevent deadlocks

Resources
Official website https://redis.io/
Redis Design and Implementation (second edition)
http://blog.csdn.net/hguisu/article/details/8836819
Https://baike.baidu.com/item/Redis/6549233?fr=aladdin

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.