Learn these 15 points to get your Redis database in minutes

Source: Internet
Author: User
Tags auth benchmark delete key redis keep alive server port haproxy redis cluster

The original: Learn these 15 points, let you take a minute to win the Redis database

1. Introduction to Redis

REmote DIctionary Server (Redis) is a Key-value storage system written by Salvatore Sanfilippo. Redis is an open source API that is written in ANSI C, adheres to the BSD protocol, supports networks, can be persisted in memory, key-value databases, and provides multiple languages. It is commonly referred to as a data structure server, because the value can be a string (string), a hash (MAP), a list, a collection (sets), and an ordered collection (sorted sets).
We all know that Redis is a key-value-based no SQL database, so let's take a look at key-related knowledge points

1. Any binary sequence can be used as key
2, Redis has a uniform rules to design key
3, the maximum allowable length of key-value is 512MB

2. Supported languages:
ActionScript   Bash   C   C#   C++   Clojure   Common Lisp  Crystal  D  Dart Elixir   emacs lisp   Erlang    Fancy  gawk  GNU Prolog  Go  Haskell  Haxe  Io Java  Javascript  Julia  Lua  Matlab  mruby  Nim  Node.js  Objective-C  OCaml Pascal  Perl  PHP  Pure Data  Python  R  Racket  Rebol  Ruby  Rust Scala  Scheme  Smalltalk  Swift  Tcl  VB  VCL*
3. What are the application scenarios for Redis?

1, the most common is the session cache
2. Message Queuing, such as payment
3. Activity Leaderboard or Count
4, publish, subscribe to messages (message notification)
5. List of items, list of comments, etc.

4, Redis installation and installation of the approximate steps are as follows:

Redis is a C language, and installing Redis requires a C language compilation environment
If no GCC is required to install online: Yum install gcc-c++

First step: Get the source bundle: wget http://download.redis.io/releases/redis-3.0.0.tar.gz
Step two: Decompress Redis:tar zxvf redis-3.0.0.tar.gz
Step three: Compile. Enter the Redis source directory (CD redis-3.0.0). Execute make
Fourth step: Install. Make install Prefix=/usr/local/redis

The prefix parameter specifies the installation directory for Redis
5. Redis Data type

Redis supports a total of five data types

1, String (strings)
2, hash (hashed)
3. List (lists)
4. Set (SET)
5, Zset (sorted set ordered set)

String (String)

It is the most basic data type of Redis, a key corresponding to a value, it should be noted that a key value of the maximum storage 512MB.

128.127.0.0.1:6379> set key "hello world"OK127.0.0.1:6379> get key"hello world"127.0.0.1:6379> getset key "nihao""hello world"127.0.0.1:6379> mset key1 "hi" key2 "nihao" key3 "hello"OK127.0.0.1:6379> get key1"hi"127.0.0.1:6379> get key2"nihao"127.0.0.1:6379> get key3"hello"
Related Command Introduction

Set value (value) for a key
Get gets the value that corresponds to a key
Getset Set Value (value) for a key and return the corresponding value
Mset setting value (value) for multiple keys

Hash (hashed)

A Redis hash is a collection of key-value pairs, a string-type field and value mapping table that is suitable for storing objects

128.127.0.0.1:6379> hset redishash 1 "001"(integer) 1127.0.0.1:6379> hget redishash 1"001"127.0.0.1:6379> hmset redishash 1 "001" 2 "002"OK127.0.0.1:6379> hget redishash 1"001"127.0.0.1:6379> hget redishash 2"002"127.0.0.1:6379> hmget redishash 1 21) "001"2) "002"
Related Command Introduction

Hset the field in the hash corresponding to the key is configured as value, and if the hash is not saved, it is created automatically.
Hget get the value of field configuration in a hash
Hmset Batch configuration Multiple field values in the same hash
Hmget multiple field values for the same hash in bulk

List (lists)

is a redis simple list of strings, sorted by insert order

128.127.0.0.1:6379> lpush word  hi(integer) 1127.0.0.1:6379> lpush word  hello(integer) 2127.0.0.1:6379> rpush word  world(integer) 3127.0.0.1:6379> lrange word 0 21) "hello"2) "hi"3) "world"127.0.0.1:6379> llen word(integer) 3
Related Command Introduction

Lpush inserts an element to the left of the specified list, returning the length of the inserted list
Rpush inserts an element to the right of the specified list, returning the length of the inserted list
Llen returns the length of the specified list
Lrange returns the element value of the specified range in the specified list

Set (SET)

is an unordered collection of type string and cannot be duplicated

128.127.0.0.1:6379> sadd redis redisset(integer) 1127.0.0.1:6379> sadd redis redisset1(integer) 1127.0.0.1:6379> sadd redis redisset2(integer) 1127.0.0.1:6379> smembers redis1) "redisset1"2) "redisset"3) "redisset2"127.0.0.1:6379> sadd redis redisset2(integer) 0127.0.0.1:6379> smembers redis1) "redisset1"2) "redisset"3) "redisset2"127.0.0.1:6379> smembers redis1) "redisset1"2) "redisset3"3) "redisset"4) "redisset2"127.0.0.1:6379> srem redis redisset(integer) 1127.0.0.1:6379> smembers redis1) "redisset1"2) "redisset3"3) "redisset2"
Related Command Introduction

Sadd adds a string element to the set set of key corresponding to successfully returning 1 if the element exists return 0
Smembers returns all the elements in the specified collection
Srem Delete an element of the specified collection

Zset (sorted set ordered set)

is an ordered collection of string types and cannot be duplicated
Each element in the sorted set needs to specify a fraction, and the elements are sorted in ascending order based on fractions
Sort Ascending in dictionary order if multiple elements have the same score
The sorted set is therefore ideal for ranking

128.127.0.0.1:6379> zadd nosql 0 001(integer) 1127.0.0.1:6379> zadd nosql 0 002(integer) 1127.0.0.1:6379> zadd nosql 0 003(integer) 1127.0.0.1:6379> zcount nosql 0 0 (integer) 3127.0.0.1:6379> zcount nosql 0 3(integer) 3127.0.0.1:6379> zrem nosql 002(integer) 1127.0.0.1:6379> zcount nosql 0 3(integer) 2127.0.0.1:6379> zscore nosql 003"0"127.0.0.1:6379> zrangebyscore nosql 0 101) "001"2) "003"127.0.0.1:6379> zadd nosql 1 003(integer) 0127.0.0.1:6379> zadd nosql 1 004(integer) 1127.0.0.1:6379> zrangebyscore nosql 0 101) "001"2) "003"3) "004"127.0.0.1:6379> zadd nosql 3 005(integer) 1127.0.0.1:6379> zadd nosql 2 006(integer) 1127.0.0.1:6379> zrangebyscore nosql 0 101) "001"2) "003"3) "004"4) "006"5) "005"
Related Command Introduction

Zadd add 1 or more elements to a specified sorteset
Zrem Remove 1 or more elements from a specified sorteset
Zcount view the number of elements within a specified fraction range in a specified sorteset
Zscore to view the elements of a specified fraction in a specified sorteset
Zrangebyscore View all elements within a specified fraction range in a specified sorteset

6. Key-Value related commands
128.127.0.0.1:6379> exists key(integer) 1127.0.0.1:6379> exists key1(integer) 1127.0.0.1:6379> exists key100(integer) 0127.0.0.1:6379> get key"nihao,hello"127.0.0.1:6379> get key1"hi"127.0.0.1:6379> del key1(integer) 1127.0.0.1:6379> get key1(nil)127.0.0.1:6379> rename key key0OK127.0.0.1:6379> get key(nil)127.0.0.1:6379> get key0"nihao,hello"127.0.0.1:6379> type key0string

exists confirm if key exists
Del Delete key
Expire setting the key expiration time (in seconds)
Persist removal of the key expiration time configuration
Rename Rename key
Type of the return value of type

7. Redis Service-related commands
128.127.0.0.1:6379> select 0OK127.0.0.1:6379> info# Serverredis_version:3.0.6redis_git_sha1:00000000redis_git_dirty:0redis_build_id:347e3eeef5029f3redis_mode:standaloneos:Linux 3.10.0-693.el7.x86_64 x86_64arch_bits:64multiplexing_api:epollgcc_version:4.8.5process_id:31197run_id:8b6ec6ad5035f5df0b94454e199511084ac6fb12tcp_port:6379uptime_in_seconds:8514uptime_in_days:0hz:10lru_clock:14015928config_file:/usr/local/redis/redis.conf-------------------省略N行127.0.0.1:6379> CONFIG GET 0(empty list or set)127.0.0.1:6379> CONFIG GET 15(empty list or set)

Slect Select Database (database number 0-15)
Quit Quit connection
Info information and statistics on access to services
Monitor real-time monitoring
Config get get service configuration
FLUSHDB Delete key in the currently selected database
Flushall Delete key from all databases

8. Redis Publish and subscribe

Redis Publishing and Subscription (PUB/SUB) is a message communication pattern in which one party sends information and a party receives it.
is three clients subscribing to the same channel at the same time

When a new message is sent to Channel 1 o'clock, the message is sent to the three clients that subscribed to it

9. Redis Transaction

Redis transactions can execute multiple commands at once, send EXEC commands to queue caches before they are sent, end transactions, perform transactional operations after receiving the EXEC command, and if a command fails, other commands can continue to execute, and during a transaction execution, Requests submitted by other clients are not inserted into the list of commands executed by the transaction.

Three stages of a business experience

Start Transaction (command: Multi)
Command execution
End Transaction (command: EXEC)

128.127.0.0.1:6379> MULTIOK127.0.0.1:6379> set key key1QUEUED127.0.0.1:6379> get keyQUEUED127.0.0.1:6379> rename key key001QUEUED127.0.0.1:6379> exec1) OK2) "key1"3) OK
10. Redis Security Configuration

You can improve security by modifying the configuration file device password parameters

# requirepass foobared  去掉注释#号就可以配置密码没有配置密码的情况下查询如下127.0.0.1:6379> CONFIG GET requirepass1) "requirepass"2) ""配置密码之后,就需要进行认证127.0.0.1:6379> CONFIG GET requirepass(error) NOAUTH Authentication required.127.0.0.1:6379> AUTH foobared #认证OK127.0.0.1:6379> CONFIG GET requirepass1) "requirepass"2) "foobared"
11. Redis Persistence

Redis persistence is available in two ways: snapshotting (snapshot), append-only file (AOF)

Snapshotting (snapshot)

1. Writes the data stored in memory to a binary file as a snapshot, as in the default Dump.rdb
2, save 1 #900秒内如果超过1个Key被修改, start the snapshot save
3, save #300秒内如果超过10个Key被修改, start the snapshot save
4, Save 10000 #60秒内如果超过10000个Key被修改, start the snapshot save

Append-only file (AOF)

1. When using aof persistence, the service appends each received write command to the file via the Write function (appendonly.aof)
2, AOF persistent storage mode parameter description
AppendOnly Yes #开启AOF持久化存储方式
Appendfsync always #收到写命令后就立即写入磁盘, the least efficient, the best effect
Appendfsync everysec #每秒写入磁盘一次, efficiency and effect centered
Appendfsync no #完全依赖OS, the efficiency is best, the effect cannot guarantee

12. Redis Performance Test

Self-bringing relevant test tools

[[email protected] ~]# redis-benchmark--helpusage:redis-benchmark [-H 
The actual test executes 1 million requests simultaneously
[[email protected] ~]# redis-benchmark -n 1000000 -qPING_INLINE: 152578.58 requests per secondPING_BULK: 150308.14 requests per secondSET: 143266.47 requests per secondGET: 148632.58 requests per secondINCR: 145857.64 requests per secondLPUSH: 143781.45 requests per secondLPOP: 147819.66 requests per secondSADD: 138350.86 requests per secondSPOP: 134282.27 requests per secondLPUSH (needed to benchmark LRANGE): 141302.81 requests per secondLRANGE_100 (first 100 elements): 146756.67 requests per secondLRANGE_300 (first 300 elements): 148104.27 requests per secondLRANGE_500 (first 450 elements): 152671.75 requests per secondLRANGE_600 (first 600 elements): 148104.27 requests per secondMSET (10 keys): 132731.62 requests per second
13, Redis Backup and recovery Redis automatic backup There are two ways

The first is to use the Dump.rdb file for backup
The second uses the AOF file for automatic backup

Dump.rdb Backup

The Redis service defaults to Automatic file backup (AOF is not enabled) and automatically loads data from the Dump.rdb file when the service starts.

具体配置在redis.confsave 900 1 save 300 10 save 60 10000

You can also manually perform the Save command to make a manual backup

128.127.0.0.1:6379> set name keyOK127.0.0.1:6379> SAVEOK127.0.0.1:6379> set name key1OK127.0.0.1:6379> BGSAVEBackground saving startedredis快照到dump文件时,会自动生成dump.rdb的文件# The filename where to dump the DBdbfilename dump.rdb-rw-r--r-- 1 root root   253 Apr 17 20:17 dump.rdb

The Save command indicates that the current database is being snapped to the dump file using the main process
The Bgsave command indicates that the main process will fork a child process for a snapshot backup
The two types of backups are different, the former blocking the main process, and the latter does not.

recovery example
# Note that you must specify a directory here, not a file name.dir /usr/local/redisdata/备份文件存储路径127.0.0.1:6379> CONFIG GET dir1) "dir"2) "/usr/local/redisdata"127.0.0.1:6379> set key 001OK127.0.0.1:6379> set key1 002OK127.0.0.1:6379> set key2 003OK127.0.0.1:6379> saveOK

Back up backup files to a different directory

[[email protected] ~]# ll /usr/local/redisdata/total 4-rw-r--r-- 1 root root 49 Apr 17 21:24 dump.rdb[[email protected] ~]# dateTue Apr 17 21:25:38 CST 2018[[email protected] ~]# cp ./dump.rdb /tmp/

Delete data

128.127.0.0.1:6379> del key1(integer) 1127.0.0.1:6379> get key1(nil)

Turn off the service and copy the original backup file back to the Save backup directory

[[email protected] ~]# redis-cli -a foobared shutdown[[email protected] ~]# lsof -i :6379[[email protected] ~]# cp /tmp/dump.rdb /usr/local/redisdata/cp: overwrite ‘/usr/local/redisdata/dump.rdb’? y[[email protected] ~]# redis-server /usr/local/redis/redis.conf &[1] 31487

Log in to see if data is restored

[[email protected] ~]# redis-cli -a foobared127.0.0.1:6379> mget key key1 key21) "001"2) "002"3) "003"
AOF Automatic Backup

The Redis service turns off this configuration by default

###### APPEND ONLY MODE ##########appendonly no# The name of the append only file (default: "appendonly.aof")appendfilename "appendonly.aof"# appendfsync alwaysappendfsync everysec# appendfsync no

The relevant parameters of the configuration file are described in detail earlier.
AOF file backup, is to back up all the history and executed commands, and MySQL binlog very similar, in the recovery is to re-hold the command before a time, it is important to note that before the recovery, and database recovery need to manually delete the executed del or misoperation command.

AOF is different from dump backup

1. aof file backup is different from dump file backup
2, the service read the file priority order, will be started according to the following priority level
If only AOF is configured, the AoF file recovery data is loaded when the reboot is restarted
If both RBD and AOF are configured, startup is to load only aof file recovery data
If only RBD is configured, dump file recovery data will be loaded at startup

Note: As long as the AOF is configured, but there is no aof file, this time the database started will be empty 14, Redis production optimization introduction 1, Memory management optimization
  hash-max-ziplist-entries 512  hash-max-ziplist-value 64  list-max-ziplist-entries 512  list-max-ziplist-value 64

List members and values are not too large when the compact format to store, relative memory overhead is also small, when running Redis in the Linux environment, if the system memory is relatively small, this time the automatic backup may fail, need to modify the system's Vm.overcommit_ memory parameter, which is the allocation policy for the Linux system.

 0 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。 1 表示内核允许分配所有的物理内存,而不管当前的内存状态如何。 2 表示内核允许分配超过所有物理内存和交换空间总和的内存

The official Redis note is that it is recommended to modify the value of Vm.overcommit_memory to 1, which can be modified in several ways:
(1) Edit/etc/sysctl.conf, change Vm.overcommit_memory=1, and sysctl-p make the configuration file effective
(2) Sysctl Vm.overcommit_memory=1
(3) echo 1 >/proc/sys/vm/overcommit_memory

2, memory pre-allocation 3, persistence mechanism
 定时快照:效率不高,会丢失数据 AOF:保持数据完整性(一个实例的数量不要太大2G最大)
Optimization Summary

1) Choose the right data type according to your business needs
2) Turn off all persistence methods (using SSD disks to improve efficiency) when the business scenario does not need to be persisted
3) Do not use virtual memory to write aof in real time every second
4) Do not allow the server on which Redis resides to use more than 3/5 of the total memory
5) to use MaxMemory
6) Large data volumes use multiple Redis instances separately by business

15. Redis Cluster Application

Clustering is the pooling of multiple Redis instances for the same business requirements or for high availability and load balancing

What are the cluster scenarios??

1. Haproxy+keepalived+redis Cluster

1) master-slave Copy, read/write separation via Redis configuration file
2) through the configuration of the haproxy, to achieve load balancing, when the fault is also in time from the cluster T in addition to
3) Use of keepalived to achieve high load availability

2. Redis Official Sentinel Cluster management tool


1) Sentinel is responsible for monitoring, alerting and automatic failover of the master-slave service in the cluster
2) Redis cluster is responsible for providing services externally

3. Redis Cluster

Redis cluster is a distributed solution for Redis and is officially launched in the Redis 3.0 release, effectively addressing the needs of redis distributed. When encountering single-machine memory, concurrency, traffic and other bottlenecks, the cluster architecture can be used to achieve load balancing purposes.

1) Official recommendation, no doubt.
2) to center, the cluster can increase the maximum of 1000 nodes, the performance increases linearly with the increase of the node.
3) Easy to manage, can be added or removed from the node, mobile sub-groove and so on.
4) Simple, easy to get started.

Learn these 15 points to get your Redis database in minutes

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.