Redis introduction and installation

Posted by mforan on Fri, 14 Jan 2022 21:11:50 +0100

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:

  1. In memory storage, persistence (RDB and AOF)
  2. Efficient and can be used for caching
  3. Publish subscribe system, queue
  4. Map information analysis
  5. Counters, timers (e.g. views)

Advantages of Redis:

  1. High performance database expansion (non relational database expansion)
  2. High availability (clusters can be built for convenient management)
  3. 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.
  1. Redis Download

    Select the appropriate version for installation.

  2. Unzip and start

  3. Use the ping command to test whether the connection is successful

Redis installation under Linux

  1. Download Redis

    Select the appropriate version for installation. Be careful not to download too old versions or beta versions.

  2. Upload to linux server

    Select a tool to upload the installation package.

  3. Unzip using the command

    [root@localhost redis]# tar -zxvf redis-5.0.7.tar.gz 
    

  4. 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++
    
  5. Compile redis and enter the directory decompressed by redis for compilation

    [root@localhost redis-5.0.7]# make
    
  6. Install redis

    [root@localhost redis-5.0.7]# make install
    

    After installation, the redis startup file will be in / usr/local/bin

  7. 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
    
  8. 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
    
  9. Start redis service

    [root@localhost bin]# redis-server myconf/redis.conf 	#The configuration file needs to be specified for startup
    
  10. 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
    
  11. Test connection

    127.0.0.1:6379> ping
    PONG
    
  12. 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 numberoptiondescribeDefault value
1-hSpecify the server host name127.0.0.1
2-pSpecify server port6379
3-sSpecify server socket
4-cSpecifies the number of concurrent connections50
5-nSpecify the number of requests10000
6-dSpecifies the data size of the SET/GET value in bytes2
7-k1=keep alive 0=reconnect1
8-rSET/GET/INCR uses random keys and Sadd uses random values
9-PPipeline requests1
10-qForce to exit redis. Show only query/sec values
11–csvExport in CSV format
12*-L * (lowercase letter of L)Generate a loop and permanently execute the test
13-tRun 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

Topics: Database Redis