Redis (Remote Dictionary Server), that is, the remote dictionary service, is an open source service using ANSI C language Write, support network, memory based and persistent log type, key value database , and provides API s in multiple languages
Redis's role:
- In memory storage, persistence (RDB and AOF)
- Efficient and can be used for caching
- Publish subscribe system, queue
- Map information analysis
- Counters, timers (e.g. views)
Advantages of Redis:
- High performance database expansion (non relational database expansion)
- High availability (clusters can be built for convenient management)
- High performance (based on memory reading and writing, high efficiency, non relational database, high efficiency of reading, writing and storage)
Reasons for high efficiency in Redis single thread
- Redis reads and writes based on memory. Multithreading and single thread do not affect the reading and writing efficiency of redis
- When the CPU implements multithreading, it will consume a lot when switching the context, while Redis is a piece of CPU operating a piece of memory. There is no need to switch the context and there is no loss.
Redis installation under windows
- Because the environment under Linux is officially recommended, here is only a brief description of the windows environment, which can be used for testing. The operation is roughly the same as that of Linux.
-
Select the appropriate version for installation.
-
Unzip and start
-
Use the ping command to test whether the connection is successful
Redis installation under Linux
-
Select the appropriate version for installation. Be careful not to download too old versions or beta versions.
-
Upload to linux server
Select a tool to upload the installation package.
-
Unzip using the command
[root@localhost redis]# tar -zxvf redis-5.0.7.tar.gz
-
C language environment installation (the existing C environment does not need to be installed, and redis compilation needs to depend on this environment)
[root@localhost redis]# yum install gcc-c++
-
Compile redis and enter the directory decompressed by redis for compilation
[root@localhost redis-5.0.7]# make
-
Install redis
[root@localhost redis-5.0.7]# make install
After installation, the redis startup file will be in / usr/local/bin
-
Copy the configuration file under the redis directory conf
[root@localhost bin]# mkdir myconf [root@localhost bin]# cp /windows-datas/redis/redis-5.0.7/redis.conf myconf
-
Modify the configuration file. Here, only the startup mode is changed to background startup.
# NotAL ##################################### # By default Redis does not run as a daemon. Use 'yes' if you need it. # Note that Redis will write a pid file in /var/run/redis.pid when daemonized. daemonize no #Change to yes to start in the background
-
Start redis service
[root@localhost bin]# redis-server myconf/redis.conf #The configuration file needs to be specified for startup
-
Start the redis client to connect to the redis service
[root@localhost bin]# redis-cli -p 6379 #Connect the redis service of the specified port
-
Test connection
127.0.0.1:6379> ping PONG
-
Close connection
127.0.0.1:6379> shutdown #Turn off redis service not connected> exit #Close connection
Redis expansion
Redis benchmark
- After redis is installed, the test tool is also installed
Test tool instruction parameters
Serial number | option | describe | Default value |
---|---|---|---|
1 | -h | Specify the server host name | 127.0.0.1 |
2 | -p | Specify server port | 6379 |
3 | -s | Specify server socket | |
4 | -c | Specifies the number of concurrent connections | 50 |
5 | -n | Specify the number of requests | 10000 |
6 | -d | Specifies the data size of the SET/GET value in bytes | 2 |
7 | -k | 1=keep alive 0=reconnect | 1 |
8 | -r | SET/GET/INCR uses random keys and Sadd uses random values | |
9 | -P | Pipeline requests | 1 |
10 | -q | Force to exit redis. Show only query/sec values | |
11 | –csv | Export in CSV format | |
12 | *-L * (lowercase letter of L) | Generate a loop and permanently execute the test | |
13 | -t | Run only a comma separated list of test commands. | |
14 | *-i * (capital letter of i) | Idle mode. Open only N idle connections and wait. |
Simple test example
# Test 10000 requests under 20 concurrent [root@localhost bin]# redis-benchmark -h localhost -p 6379 -c 20 -n 10000 ====== PING_INLINE ====== 10000 requests completed in 0.18 seconds 20 parallel clients 3 bytes payload keep alive: 1 99.34% <= 1 milliseconds 99.97% <= 2 milliseconds 100.00% <= 2 milliseconds 54945.05 requests per second ====== SET ====== #Performance of set 10000 requests completed in 0.14 seconds 20 parallel clients 3 bytes payload keep alive: 1 99.89% <= 1 milliseconds 100.00% <= 1 milliseconds 68965.52 requests per secon ......
Database Basics
Redis has 16 databases by default. Similar to the array, the subscript starts from zero. The zero database is used by default initially
# View and set the number of databases [root@localhost bin]# cat myconf/redis.conf | grep database # Set the number of databases. The default database is DB 0, you can select # dbid is a number between 0 and 'databases'-1 databases 16 # Switch database 127.0.0.1:6379> select 10 OK 127.0.0.1:6379[10]> # View the number of key s in the current library 127.0.0.1:6379[10]> dbsize (integer) 0 # Clear all key s in the current database 127.0.0.1:6379[10]> flushdb OK # Clear all key s of all databases 127.0.0.1:6379[10]> flushall OK