Learn these 15 points to get your Redis database in minutes

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


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
#PREFIX参数指定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"
OK
127.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"
OK
127.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) 1
127.0.0.1:6379> hget redishash 1
"001"
127.0.0.1:6379> hmset redishash 1 "001" 2 "002"
OK
127.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 2
1) "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) 1
127.0.0.1:6379> lpush word  hello
(integer) 2
127.0.0.1:6379> rpush word  world
(integer) 3
127.0.0.1:6379> lrange word 0 2
1) "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) 1
127.0.0.1:6379> sadd redis redisset1
(integer) 1
127.0.0.1:6379> sadd redis redisset2
(integer) 1
127.0.0.1:6379> smembers redis
1) "redisset1"
2) "redisset"
3) "redisset2"
127.0.0.1:6379> sadd redis redisset2
(integer) 0
127.0.0.1:6379> smembers redis
1) "redisset1"
2) "redisset"
3) "redisset2"
127.0.0.1:6379> smembers redis
1) "redisset1"
2) "redisset3"
3) "redisset"
4) "redisset2"
127.0.0.1:6379> srem redis redisset
(integer) 1
127.0.0.1:6379> smembers redis
1) "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) 1
127.0.0.1:6379> zadd nosql 0 002
(integer) 1
127.0.0.1:6379> zadd nosql 0 003
(integer) 1
127.0.0.1:6379> zcount nosql 0 0 
(integer) 3
127.0.0.1:6379> zcount nosql 0 3
(integer) 3
127.0.0.1:6379> zrem nosql 002
(integer) 1
127.0.0.1:6379> zcount nosql 0 3
(integer) 2
127.0.0.1:6379> zscore nosql 003
"0"
127.0.0.1:6379> zrangebyscore nosql 0 10
1) "001"
2) "003"
127.0.0.1:6379> zadd nosql 1 003
(integer) 0
127.0.0.1:6379> zadd nosql 1 004
(integer) 1
127.0.0.1:6379> zrangebyscore nosql 0 10
1) "001"
2) "003"
3) "004"
127.0.0.1:6379> zadd nosql 3 005
(integer) 1
127.0.0.1:6379> zadd nosql 2 006
(integer) 1
127.0.0.1:6379> zrangebyscore nosql 0 10
1) "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) 1
127.0.0.1:6379> exists key1
(integer) 1
127.0.0.1:6379> exists key100
(integer) 0
127.0.0.1:6379> get key
"nihao,hello"
127.0.0.1:6379> get key1
"hi"
127.0.0.1:6379> del key1
(integer) 1
127.0.0.1:6379> get key1
(nil)
127.0.0.1:6379> rename key key0
OK
127.0.0.1:6379> get key
(nil)
127.0.0.1:6379> get key0
"nihao,hello"
127.0.0.1:6379> type key0
string

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 0
OK
127.0.0.1:6379> info
# Server
redis_version: 3.0.6
redis_git_sha1: 00000000
redis_git_dirty: 0
redis_build_id: 347e3eeef5029f3
redis_mode: standalone
os: Linux 3.10.0-693.el7.x86_64 x86_64
arch_bits: 64
multiplexing_api: epoll
gcc_version: 4.8.5
process_id: 31197
run_id: 8b6ec6ad5035f5df0b94454e199511084ac6fb12
tcp_port: 6379
uptime_in_seconds: 8514
uptime_in_days: 0
hz: 10
lru_clock: 14015928
config_file: /usr/local/redis/redis.conf
------------------- Omit the N line
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> MULTI
OK
127.0.0.1:6379> set key key1
QUEUED
127.0.0.1:6379> get key
QUEUED
127.0.0.1:6379> rename key key001
QUEUED
127.0.0.1:6379> exec
1) OK
2) "key1"
3) OK
10. Redis Security Configuration


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


# requirepass foobared
Remove comment # to configure password
The query without password is as follows
127.0.0.1:6379> CONFIG GET requirepass
1) "requirepass"
2) ""
After the password is configured, authentication is required
127.0.0.1:6379> CONFIG GET requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379> AUTH foobared #Authentication
OK
127.0.0.1:6379> CONFIG GET requirepass
1) "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 #If more than 1 key is modified within 900 seconds, start the snapshot save
3, save #If more than 10 keys are modified within 300 seconds, start the snapshot save
4, Save 10000 #If more than 10,000 keys are modified within 60 seconds, 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 #Enable AOF persistent storage
Appendfsync always #Write to disk immediately after receiving a write command, the least efficient, the best effect
Appendfsync everysec #Write to disk once per second, efficiency and effect centered
Appendfsync no #Fully dependent on OS, the efficiency is best, the effect cannot guarantee


12. Redis Performance Test


Self-bringing relevant test tools



[[email protected] ~]# redis-benchmark --help
Usage: redis-benchmark [-h <host>] [-p <port>] [-c <clients>] [-n <requests]> [-k <boolean>]

 -h <hostname>      Server hostname (default 127.0.0.1)
 -p <port>          Server port (default 6379)
 -s <socket>        Server socket (overrides host and port)
 -a <password>      Password for Redis Auth
 -c <clients>       Number of parallel connections (default 50)
 -n <requests>      Total number of requests (default 100000)
 -d <size>          Data size of SET/GET value in bytes (default 2)
 -dbnum <db>        SELECT the specified db number (default 0)
 -k <boolean>       1=keep alive 0=reconnect (default 1)
 -r <keyspacelen>   Use random keys for SET/GET/INCR, random values for SADD
  Using this option the benchmark will expand the string __rand_int__
  inside an argument with a 12 digits number in the specified range
  from 0 to keyspacelen-1. The substitution changes every time a command
  is executed. Default tests use this to hit random keys in the
  specified range.
 -P <numreq>        Pipeline <numreq> requests. Default 1 (no pipeline).
 -q                 Quiet. Just show query/sec values
 --csv              Output in CSV format
 -l                 Loop. Run the tests forever
 -t <tests>         Only run the comma separated list of tests. The test
                    names are the same as the ones produced as output.
 -I                 Idle mode. Just open N idle connections and wait.

Examples:

 Run the benchmark with the default configuration against 127.0.0.1:6379:
   $ redis-benchmark

 Use 20 parallel clients, for a total of 100k requests, against 192.168.1.1:
   $ redis-benchmark -h 192.168.1.1 -p 6379 -n 100000 -c 20

 Fill 127.0.0.1:6379 with about 1 million keys only using the SET test:
   $ redis-benchmark -t set -n 1000000 -r 100000000

 Benchmark 127.0.0.1:6379 for a few commands producing CSV output:
   $ redis-benchmark -t ping,set,get -n 100000 --csv

 Benchmark a specific command line:
   $ redis-benchmark -r 10000 -n 10000 eval ‘return redis.call("ping")‘ 0

 Fill a list with 10000 random elements:
   $ redis-benchmark -r 10000 -n 10000 lpush mylist __rand_int__

 On user specified command lines __rand_int__ is replaced with a random integer
 with a range of values selected by the -r option.








Actual test executes 1 million requests simultaneously



[[email protected] ~]# redis-benchmark -n 1000000 -q
PING_INLINE: 152578.58 requests per second
PING_BULK: 150308.14 requests per second
SET: 143266.47 requests per second
GET: 148632.58 requests per second
INCR: 145857.64 requests per second
LPUSH: 143781.45 requests per second
LPOP: 147819.66 requests per second
SADD: 138350.86 requests per second
SPOP: 134282.27 requests per second
LPUSH (needed to benchmark LRANGE): 141302.81 requests per second
LRANGE_100 (first 100 elements): 146756.67 requests per second
LRANGE_300 (first 300 elements): 148104.27 requests per second
LRANGE_500 (first 450 elements): 152671.75 requests per second
LRANGE_600 (first 600 elements): 148104.27 requests per second
MSET (10 keys): 132731.62 requests per second








13. Redis backup and recovery Redis automatic backup has two ways
The first is to achieve backup through dump.rdb file
The second type uses aof files for automatic backup

dump.rdb backup
The default automatic file backup method of the Redis service (when AOF is not turned on), when the service starts, it will automatically load data from the dump.rdb file.

Specific configuration in redis.conf
save 900 1
save 300 10
save 60 10000









You can also manually execute the save command to implement manual backup


128.127.0.0.1: 6379> set name key
OK
127.0.0.1:6379> SAVE
OK
127.0.0.1:6379> set name key1
OK
127.0.0.1:6379> BGSAVE
Background saving started
When redis snapshot to dump file, dump.rdb file is automatically generated
# The filename where to dump the DB
dbfilename dump.rdb
-rw-r--r-- 1 root root 253 Apr 17 20:17 dump.rdb





SAVE command means use the main process to snapshot the current database to a dump file
BGSAVE command indicates that the main process will fork a child process for snapshot backup
The difference between the two backups is that the former will block the main process and the latter will not.

Recovery examples


# Note that you must specify a directory here, not a file name.
dir / usr / local / redisdata /
Backup file storage path
127.0.0.1:6379> CONFIG GET dir
1) "dir"
2) "/ usr / local / redisdata"
127.0.0.1:6379> set key 001
OK
127.0.0.1:6379> set key1 002
OK
127.0.0.1:6379> set key2 003
OK
127.0.0.1:6379> save
OK








Back up the backup file to another directory



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





delete data



128.127.0.0.1:6379> del key1
(integer) 1
127.0.0.1:6379> get key1
(nil)





Shut down 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




Sign in to see if data is restored



[[email protected] ~]# redis-cli -a foobared
127.0.0.1:6379> mget key key1 key2
1) "001"
2) "002"
3) "003"





AOF automatic backup
The redis service is disabled by default.

The relevant parameters of the configuration file have been described in detail previously.
AOF file backup is to back up all historical records and executed commands. It is very similar to mysql binlog. When restoring, it is to re-execute the command executed before. Please note that before the restoration, it needs to be manually deleted like the database restoration. Commands that have been executed del or incorrectly.

AOF is different from dump backup
1.AOF file backup is different from dump file backup
2. The service reads files in different priorities, and will start according to the following priorities
If only AOF is configured, load AOF file to restore data on restart
If both RBD and AOF are configured, only AOF files are loaded to recover data at startup
If only RBD is configured, a dump file will be loaded to restore data at startup

Note: As long as aof is configured, but there is no aof file, the database started at this time will be empty. 14. Introduction to Redis Production Optimization 1. Memory Management Optimization
  hash-max-ziplist-entries 512
  hash-max-ziplist-value 64
  list-max-ziplist-entries 512
  list-max-ziplist-value 64
When the members and values of the list are not too large, they will be stored in a compact format with relatively small memory overhead. When running Redis in a Linux environment, if the system's memory is relatively small, automatic backup may fail at this time and need to be modified The vm.overcommit_memory parameter of the system. This parameter is the memory allocation strategy of the Linux system.

 0 means that the kernel will check if there is enough available memory for the process to use; if there is enough available memory, the memory application is allowed; otherwise, the memory application fails and an error is returned to the application process.
 1 means that the kernel allows all physical memory to be allocated, regardless of the current memory status.
 2 means the kernel allows allocating more memory than the sum of all physical memory and swap space
The official Redis explanation is that it is recommended to modify the value of vm.overcommit_memory to 1, which can be modified in the following ways:
(1) Edit /etc/sysctl.conf, change vm.overcommit_memory = 1, and then sysctl -p to make the configuration file take effect
(2) sysctl vm.overcommit_memory = 1
(3) echo 1> / proc / sys / vm / overcommit_memory

2.Memory pre-allocation 3.Persistence mechanism
 Timed snapshots: inefficient, data will be lost
 AOF: maintain data integrity (the number of one instance should not be too large, 2G maximum)
Optimization summary
1) Select the appropriate data type according to business needs
2) When the business scenario does not require persistence, close all persistence methods (using SSD disks to improve efficiency)
3) Do not use virtual memory, write AOF in real time per second
4) Do not let the physical memory usage of the server where REDIS is located exceed 3/5 of the total memory
5) To use maxmemory
6) Large amounts of data use multiple redis instances separately by business

15.Redis cluster application
A cluster is a collection of multiple redis instances to achieve the same business needs, or to achieve high availability and load balancing.

What are the cluster solutions? ?

1. haproxy + keepalived + redis cluster
1) Realize master-slave replication and read-write separation through redis configuration files
2) Through the configuration of haproxy, load balancing is achieved, and it will be removed from the cluster in time when it fails.
3) Use keepalived to achieve high availability of the load

2.Redis official Sentinel cluster management tool

1) sentinel is responsible for monitoring, alerting and automatic failover of the master and slave services in the cluster
2) The redis cluster is responsible for providing external services

3.Redis Cluster
Redis Cluster is a distributed solution for Redis. It was officially launched in Redis 3.0 and effectively addresses the needs of Redis distribution. When encountering single machine memory, concurrency, traffic and other bottlenecks, the Cluster architecture can be used to achieve the purpose of load balancing.

1) Official recommendation, no doubt.
2) Decentralization, the cluster can increase a maximum of 1000 nodes, and the performance linearly expands as the nodes increase.
3) Convenient management. You can add or remove nodes later, move slots, and so on.
4) Simple and easy to use.

Learn these 15 points and let you win the 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.