Redis series (I) introduction to redis


It is more or less used in NoSQL development, and it is also a necessary knowledge point for interview. I’ve asked every interview in recent days. However, I feel that the answer is not good, and there are still many knowledge points that need to be sorted out. Here, I’ll comb through several redis notes, and then add the above test questions.

Redis series:

  1. Redis series (I) introduction to redis
  2. Redis series (II) 8 data types of redis
  3. Redis series (III) integration of redis transactions and spring boot
  4. Redis series (IV) redis profile and persistence
  5. Redis series (V) publish subscribe mode, master-slave replication and sentinel mode
  6. Redis series (VI) redis’s cache penetration, cache breakdown and cache avalanche
  7. Redis series (VII) redis interview questions
  8. Redis Command Reference

1. What is NoSQL

NoSQL = not only SQL (not just SQL)

Relational databases: tables, rows, columns

Generally refers to the of non relational database, with the birth of Web 2.0 Internet! The traditional relational database is difficult to deal with the Web 2.0 era! Especially large-scale and highly concurrent communities! Many insurmountable problems have been exposed. NoSQL is developing very rapidly in today’s big data environment. Redis is the fastest growing technology and we must master it now!

There are many data types, such as user’s personal information, social networks, and geographic location. The storage of these data types does not need a fixed format! It can be expanded horizontally without redundant operations. MapUse key value pairs to control.

Features of NoSQL

1. Easy to expand (there is no relationship between data, so it is easy to expand)

2. Large amount of data and high performance (redis writes 80000 times and reads 110000 times a second. NoSQL cache is a fine-grained cache with high performance)

3. The data types are diverse (there is no need to design the database in advance, and it can be used as needed)

Comparison between traditional RDBMS and NoSQL

Traditional RDBS
-Structured organization
-Data and relationships exist in separate tables
-Operation language, database definition language
-Strict consistency
-Basic transaction
- ...
-Not just data
-There is no fixed query language
-Key value pair storage, column storage, document storage, graphic database
-Final consistency
-Cap theorem and base (remote multi activity)
-High performance, high availability and high scalability

2. Four categories of NoSQL

KV key value pair: redis

Document database (like JSON and bson):

  • MongoDB:
    • Mongodb is aDistributed file storageThe database, written in C + +, is mainly used to process a large number of documents.
    • Mongodb is aProducts between relational database and non relational data! Mongodb is the most functional of non relational data and most like relational database.
  • ConthDB

Column storage database:

  • HBase
  • distributed file system

Figure relational database:

  • Instead of storing graphics, he puts relationships, such as circle of friends, social networks, advertising recommendations
  • Neo4j、InfoGrid

3. Getting started with redis

Redis (remote dictionary server), i.e. remote dictionary service!

It is an open source log and key value database written in ANSI C language, supports network, can be based on memory and can be permanently brushed, and provides APIs in multiple languages. Free and open source! Is one of the most popular NoSQL technologies, also known as institutional database.

Redis’s role

1. In memory storage, persistence (RDB and AOF)

2. Efficient and can be used for caching

3. Publish subscribe system

4. Map information analysis

5. Counters, timers (e.g. views)

Redis features:

1. Diverse data types

2. Persistence

3. Cluster

4. Business

Redis official website:

Redis Chinese website:

4. Redis installation started

Download the compressed package from the official website and upload it to the server

[[email protected] opt]# ll
-rw-r--r-- 1 root root 2002463 Apr 29 00:18 redis-5.0.8.tar.gz
[ [email protected]  opt]# tar -zxvf redis-5.0.8.tar.gz  		#  decompression
[ [email protected]  redis-5.0.8]# yum install gcc-c++ 			#  Increase environmental support
[ [email protected]  redis-5.0.8]# make 					   #  install
[[email protected] redis-5.0.8]# make install
[ [email protected]  redis-5.0.8]# cd /usr/local/bin 		    #  Enter the default installation directory of redis
[[email protected] bin]# ls
jemalloc-config  jeprof            luajit        mcrypt    redis-benchmark  redis-check-rdb  redis-sentinel      libmcrypt-config  luajit-2.0.4  mdecrypt  redis-check-aof  redis-cli        redis-server

Copy profile:

[[email protected] bin]# mkdir kconfig
[[email protected] bin]# cp /opt/redis-5.0.8/redis.conf kconfig/
[[email protected] bin]# cd kconfig/
[[email protected] kconfig]# ls

Modify the configuration file for background startup:

[[email protected] kconfig]# pwd
[[email protected] kconfig]# vim redis.conf

Modify configuration item:

  • daemonizebyyes

  • Comment outbind

  • modifyprotected-mode no

Start redis

[[email protected] bin]# ls
jemalloc-config  jeprof   libmcrypt-config  luajit-2.0.4  mdecrypt         redis-check-aof  redis-cli       redis-server      kconfig  luajit            mcrypt        redis-benchmark  redis-check-rdb  redis-sentinel
[ [email protected] Bin]# redis server kconfig / redis.conf # start redis through a specific configuration file
8178:C 29 Apr 2020 08:43:40.043 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
8178:C 29 Apr 2020 08:43:40.043 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=8178, just started
8178:C 29 Apr 2020 08:43:40.043 # Configuration loaded

Connect and test using the client:

[[email protected] bin]# redis-cli -p 6379> ping
PONG> set name test
OK> get name

View redis process: open another window

[[email protected] ~]# ps -ef|grep redis
root      8179     1  0 08:43 ?        00:00:00 redis-server
root      8210  3437  0 08:44 pts/1    00:00:00 redis-cli -p 6379
root      8306  8281  0 08:48 pts/2    00:00:00 grep --color=auto redis

Exit redis:> shutdown 		#  Close redis 
not connected> exit 				#  sign out
[[email protected] bin]# ps -ef|grep redis
root      8330  3437  0 08:49 pts/1    00:00:00 grep --color=auto redis
[[email protected] bin]#

The cluster test may be started with a single machine and multiple redis later.

5. Redis performance test

Redis benchmark is a built-in stress testing tool on the official website.

Serial number option describe Default value
1 -h Specify the server host name
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 Transmission through pipelinerequest 1
10 -q Force to exit redis. Show only query / sec values
11 –csv Export in CSV format
12 -l Generate a loop and permanently execute the test
13 -t Run only a comma separated list of test commands.
14 -I Idle mode. Open only n idle connections and wait.


#Test 100 concurrent connections and 100000 requests
redis-benchmark -h localhost -p 6379 -c 100 -n 100000

Analysis examples:

====== SET ======
  100000 requests completed in 1.97 seconds 		  #  Write test on 100000 requests
  100 parallel clients 							#  100 concurrent clients 
  3 bytes payload 								#  Write 3 bytes at a time
  keep alive: 1 									#  There is only one server to handle these requests, that is, stand-alone performance

19.56% <= 1 milliseconds
98.70% <= 2 milliseconds
99.82% <= 3 milliseconds
99.85% <= 9 milliseconds
99.94% <= 10 milliseconds
99.95% <= 126 milliseconds
99.96% <= 127 milliseconds
100.00% <= 127 milliseconds 					#  All requests were processed in 127 milliseconds
50761.42 requests per second 				#  50761.42 requests processed per second

6. Basic knowledge

Redis has 16 databases by default, which can be viewed in the configuration file.

[[email protected] bin]# vim kconfig/redis.conf
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT  where
# dbid is a number between 0 and 'databases'-1
databases 16

The default is 0.

useselectTo switch databases, use thedbsizeView database size:

[[email protected] bin]# redis-cli -p 6379> ping
PONG> select 3 		#  Switch database
OK[3]> dbsize 		#  View database size
(integer) 0[3]> set name jack
OK[3]> dbsize
(integer) 1[3]>

View all in the databasekey[3]> keys *
1) "name"[3]>

Clear current database:[3]> flushdb
OK[3]> keys *
(empty list or set)

Clear all databases:> flushall
OK> keys *
(empty list or set)

7. Redis is single threaded. What’s so fast (interview question)

Redis is fast. Officials say that redis is based on memory operation. CPU is not redis’s performance bottleneck. Redis’s performance bottleneck is the machine’s memory and network bandwidth. Since it can be implemented by single thread, single thread is used.

Redis is written in C language. The official data provided is 100000 + QPS, which is no worse than memecache, which also uses key value.

Why is redis single thread so fast?

1. Myth 1: high performance servers must be multilinear?

2. Myth 2: multi thread must be more efficient than single thread?

To understand the execution efficiency, CPU > memory > hard disk

Core: redis puts all data in memory. It is said that using a single thread to operate has the highest execution efficiency. During the execution of multiple threads, CPU context switching is required, which is a time-consuming operation. For the memory system, if there is no context switching, the efficiency is the highest. Multiple reads and writes are on one CPU. In the case of memory, this is the best solution.

Redis adopts network IO multiplexing technology to ensure the high throughput of the system when multiple connections are made.Reference articles

The next note will introduce the five major data types and three special types of redis.

Recommended Today

Lua language novice simple tutorial

1、 Foreword Lua is a lightweight and compact scripting language, which is written in standard C language and open in the form of source code. Its design purpose is to be embedded in the application, so as to provide flexible expansion and customization functions for the application. Lua can be applied in game development, independent […]