The initial Redis

  • REmote DIctionary Server(Redis) is a key-value storage system written by Salvatore Sanfilippo that is a cross-platform non-relational database.

  • Redis is an open source ANSI C language, BSD protocol compliant, network-enabled, memory based, distributed, optional persistence key-value database storage, and provides a multi-language API.

  • Redis is often referred to as a data structure server because values can be strings, hashes, lists, sets, and sorted sets.

Recommend a blog about Redis: The easy to understand Introduction to Redis

Redis Installation (on Linux)

The installation

  1. Download the Redis installation package from the official website

  1. Move the installation package to the opt directory
Sudo mv redis - 6.2.2. Tar. Gz/optCopy the code
  1. Unzip the Redis installation package
Sudo tar - ZXVF redis - 6.2.2. Tar. GzCopy the code
  1. Access the decompressed Redis file and view the Redis configuration file
cdRedis - 6.2.2 lsCopy the code

  1. Install the base environment command (install GCC) and check whether the installation was successful
yum install gcc-c++
gcc -v
Copy the code
  1. Precompile and compile installation
make           # precompiled
make install   # build install
Copy the code

  1. And because the default Redis installation directory is/usr/local/binGo to the directory and create a foldermyconfig
sudo mkdir myconfig
Copy the code

  1. Copy the Redis configuration file tomyconfig
Sudo cp /opt/redis-6.2.2 /redis.conf myconfigCopy the code

  1. Modify the redis.conf configuration to enable Redis to start in the background (redis does not start in the background by default)
vim redis.conf
Copy the code

Use the test

  1. Start the service from the specified configuration file
redis-server myconfig/redis.conf
Copy the code

  1. Client access
redis-cli -p 6379
Copy the code

  1. throughpingTest the connection

  1. throughSet Attribute name Attribute valueStore value
  2. throughThe get property nameThe values

  1. Check whether the Redis process is started
ps -ef|grep redis
Copy the code

  1. Stop the Redis service
shutdown
exit
ps -ef|grep redis   # Check again to confirm closing
Copy the code

There is a bug in the installation

–> < p style = “max-width: 100%; clear: both; min-height: 1em

The performance testredis-benchmark

  1. This command is executed in the redis directory, not in the Redis client
  2. Performance test tool parameters

  1. The instance test
  • Test content: 100 concurrent processing of 100,000 requests
  • After the service is enabled, enter a value on the terminalredis-benchmark -c 100 -n 100000

Basic knowledge of

  1. Redis has 16 databases installed by default and uses database 0 by default

  1. Set a value to the database, value

  1. Query all values of the databasekeys *

  1. Conversion databaseSelect the database to which you want to convert

  1. Deletes a value from the database
  • flushdbClearing the current database

  • flushallClearing all databases

Extracurricular supplementary

Redis is single-threaded:

With Redis, there is almost no CPU bottleneck; Redis is largely limited by memory and network. On a normal Linux system, for example, Redis can handle a million requests per second using Pipelining, so if the application uses mainly O(N) or O(log(N)) commands, it hardly consumes much CPU.

  • In fact, Redis is based on memory operation, redis is to put all the data in memory.
  • High maintainability after using single thread.
  • Although multithreaded model has excellent performance in some aspects, it introduces the uncertainty of program execution order, brings a series of problems of concurrent read and write, increases the system complexity, and may cause performance loss caused by thread switching, even locking and unlocking, deadlock.
  • Redis has very high processing performance through AE event model and IO multiplexing, so there is no need to use multithreading. The single-threaded mechanism greatly reduces the complexity of the internal implementation of Redis. Lazy Rehash, Lpush and other “thread-unsafe” commands of Hash can be executed without locking.

Multithreading was introduced after Redis6.0

The reason:

  1. Redis keeps all the data in memory, and the memory response time is about 100 nanoseconds. For small packets, the Redis server can handle 80,000 to 100,000 QPS, which is the limit of Redis processing. For 80% of companies, single-threaded Redis is sufficient.

  2. Twenty percent of companies, with increasingly complex business scenarios that may require hundreds of millions of transactions, need larger QPS.

  3. Using multiple threads can make full use of server CPU resources, currently the main thread can only use one core

  4. Multithreading tasks can share the load of Redis synchronous IO reads and writes

Although Redis introduces multithreading, it is turned off by default.

There is also a blog about default multithreading initialization parameters.