Redis operation and maintenance routine operation

Redis is a high performance key-value database.

SET operation 110000 times per second; GET operations are 81000 per second.

The installation

The Window, the installation

Download: github.com/MSOpenTech/… .

Redis supports 32 and 64 bits. This needs to be selected according to the actual situation of your system platform, here we download redis-x64-XXx. zip to disk C, decompress, rename the folder Redis.

Open a CMD window and use the CD command to switch the directory to C:\redis. Run redis.exe redis.windows.conf.

If you want to make it easier, you can add the path of redis to the system environment variable, so that you do not have to enter the path again, the following redis.windows.conf can be omitted, if omitted, will enable the default.

Then open another CMD window, do not close the original, otherwise you will not be able to access the server.

Go to redis and run redis-cli.exe -h 127.0.0.1 -p 6379.

Under Linux installation

IO /download…

Download, unzip, and compile Redis

$wget http://download.redis.io/releases/redis-5.0.4.tar.gz $tar XZF redis - 5.0.4. Tar. Gz $cdRedis - 5.0.4 $makeCopy the code

To compile the Redis source, you need gCC-C ++ and TCL. Yum install -y GCC -c++ TCL yum install -y GCC -c++ TCL

Go to the decompressed SRC directory and run the following command to start Redis:

$ src/redis-server
Copy the code

You can use the built-in client to interact with Redis:

$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
Copy the code

Under the Ubuntu installation

To install Redi on Ubuntu, use the following commands:

$sudo apt-get update
$sudo apt-get install redis-server
Copy the code

Powered up

  • Startup configuration:echo "/usr/local/bin/redis-server /etc/redis.conf" >> /etc/rc.local

Opening firewall Ports

  • Add a rule:iptables -I INPUT -p tcp -m tcp --dport 6379 -j ACCEPT
  • Save rule:service iptables save
  • Restart iptables:service iptables restart

Redis usage and configuration

Start the

Start the Redis service

cd/ opt/redis/redis - 5.0.4 / SRC/redis server. -Copy the code

Start the Redis client

cd/ opt/redis/redis - 5.0.4 / SRC/redis - cliCopy the code

Check whether Redis is started

$ redis-cli
Copy the code

The command will open the following terminal:

Redis 127.0.0.1:6379 >Copy the code

127.0.0.1 is the local IP address and 6379 is the Redis service port. Now we enter the PING command.

Redis 127.0.0.1:6379 > ping PONGCopy the code

This shows that we have successfully started Redis.

Common configuration

The default Redis configuration file is the Redis. Conf file in the root directory.

To specify a specific file as the configuration file, run the./redis-server -c xxx.conf command

Each modification takes effect only after you restart the system.

Redis official default configuration:

  • Conf for Redis 2.8
  • Conf for Redis 2.6.
  • Conf for Redis 2.4.

Redis configuration parameters can be passed directly from the command line since Redis2.6. This approach can be used for testing. Redis configuration parameters can be passed directly from the command line since Redis2.6. This approach can be used for testing.

Set it to the daemon

Redis starts in non-daemon mode by default, and is normally set to daemonize yes

The remote access

Redis is bound to 127.0.0.1 by default, so that it can only be accessed locally. To allow remote access to Redis, you need to configure bind 0.0.0.0

Set the password

Redis does not require a password by default. If you need to set a password, do as follows:

  • protected-mode yes
  • requirepass

Configuration parameter table

Configuration items instructions
daemonize no Redis does not run as a daemon by default. This configuration item can be modified by using yes to enable daemons (Windows does not support daemons as no).
pidfile /var/run/redis.pid Pid is written to the /var/run/redis.pid file by default when Redis is running as a daemon, which can be specified by pidfile
port 6379 The default port is 6379. In my blog post, I explained why I chose 6379 as the default port, because 6379 corresponds to MERZ on the phone keys, and MERZ is named after The Italian singer Alessia MERZ
The bind 127.0.0.1 IP address of the bound host
timeout 300 The value 0 indicates that this function is disabled
loglevel notice Redis supports four log levels: debug, verbose, NOTICE, and warning. The default log level is NOTICE
logfile stdout The logging mode is standard output by default. If Redis is configured to run in daemon mode and the logging mode is configured as standard output, the logs will be sent to /dev/null
databases 16 Set the number of databases. Default is 0. You can specify the database ID on the connection using the SELECT command
save <seconds> <changes>The Redis default configuration file provides three conditions:save 900 1,save 300 10,save 60 10000Represents 1 change in 900 seconds (15 minutes), 10 changes in 300 seconds (5 minutes), and 10,000 changes in 60 seconds, respectively. Specifies how many updates to synchronize data to data files over a period of time
rdbcompression yes Redis uses LZF compression. If you want to save CPU time, you can disable this option, but it will cause database files to become huge
dbfilename dump.rdb Specifies the local database file name. The default is dump.rdb
dir ./ Specify the directory to store the local database
slaveof <masterip> <masterport> Set If Slav is used on the host, set the IP address and port number of the master service. When Redis starts, the master service automatically synchronizes data with the Slav service
masterauth <master-password> Password used by the Slav service to connect to the master when password protection is enabled for the Master service
requirepass foobared Set the Redis connection password. If the password is configured, the client needs to use the AUTH command to provide the password when connecting to Redis. The password is disabled by default
maxclients 128 The maximum number of client connections that Redis can open at a time is the maximum number of file descriptors that the Redis process can open at the same time. If maxClients 0 is set, there is no limit. Redis closes new connections and returns a Max number of clients reached error message to the client
maxmemory <bytes> Specifies the maximum memory limit for Redis. When Redis starts up, it loads data into memory. When the maximum memory is reached, Redis tries to clear expired or expiring keys first. The new VM mechanism of Redis will store keys in memory and values in swap
appendonly no Specifies whether to log after each update operation. Redis writes data to disk asynchronously by default. If not enabled, data may be lost for a period of time in the event of a power outage. Because redis itself synchronizes data files according to the above save conditions, some data will only exist in memory for a period of time. The default value is no
appendfilename appendonly.aof Appendone.aof specifies the update log file name. The default is appendone.aof
appendfsync everysec Specifies the update log condition with three optional values:no: waiting for the operating system to synchronize data cache to disk (fast)always: means to manually call fsync() after each update operation to write data to disk (slow, safe)everysec: indicates synchronization once per second (compromise, default)
vm-enabled no The default value is no. For a brief introduction, the VM mechanism stores data in paging mode. Redis swaps the less visited pages, i.e. cold data, to disk, and automatically swaps the more visited pages out of disk to memory (I will analyze the VM mechanism of Redis in detail in the next article).
vm-swap-file /tmp/redis.swap The default value is/TMP /redis.swap. This file cannot be shared by multiple Redis instances
vm-max-memory 0 All data larger than vM-max-memory is stored in virtual memory. No matter how small the VM-max-memory setting is, all index data is stored in memory (Redis index data is keys). In fact, all values exist on disk. The default value is 0
vm-page-size 32 The Redis swap file is divided into many pages. An object can be stored on multiple pages, but a page cannot be shared by multiple objects. Vm-page-size is set according to the size of the stored data. The page size is best set to 32 or 64bytes; Use a larger page if you are storing large objects, or use the default if in doubt
vm-pages 134217728 Set the number of pages in the swap file. Since the page table (a bitmap indicating that a page is free or used) is placed in memory, every 8 pages on disk consumes 1byte of memory.
vm-max-threads 4 Set the number of threads that can access the swap file. It is best not to exceed the number of cores on the machine. If set to 0, all operations on the swap file are serial and may cause a long delay. The default value is 4
glueoutputbuf yes This parameter specifies whether to combine small packets and send them as one packet when replying to clients. This parameter is enabled by default
hash-max-zipmap-entries 64 hash-max-zipmap-value 512 Specifies that a special hash algorithm is used when the number of elements exceeds a certain threshold or when the largest element exceeds a threshold
activerehashing yes Rehash specifies whether to enable rehash, which is enabled by default.
include /path/to/local.conf The inclusion of additional profiles allows you to use the same profile across multiple Instances of Redis on the same host, while each instance has its own specific profile

Redis cluster usage and configuration

Redis 3.0 supports cluster mode.

Cluster planning

A Redis cluster usually consists of multiple nodes, and the number of nodes must be at least six to ensure a complete high availability cluster.

Ideally, of course, all the nodes are on different machines. First of all, in terms of resources, I only get 3 servers when I deploy the Redis cluster. So, I plan to deploy 2 Redis nodes per server.

The deployment of

The Redis cluster node installation is the same as the single-node service, the only difference is the deployment mode.

Suppose the three servers have the following addresses:

  • Service A: 127.0.0.1
  • Service B: 127.0.0.2
  • Service C: 127.0.0.3

The distribution is as follows:

127.0.0.1 127.0.0.2 127.0.0.3
127.0.0.1:6381 127.0.0.2:6383 127.0.0.3:6385
127.0.0.1:6382 127.0.0.2:6384 127.0.0.3:6386

(1) Create a node directory

My personal preference is to put the software in /opt. On my machine, Redis is installed in /opt/ Redis /redis-5.0.4. Therefore, the following commands and configurations assume that the Redis installation directory is/ opt/ Redis /redis-5.0.4.

After ensuring that Redis is installed on the machine, run the following command to create the Redis cluster node instance directory:

  • 127.0.0.1
Sudo mkdir -p /opt/redis/redis-5.0.4/cluster/6381 sudo mkdir -p /opt/redis/redis-5.0.4/cluster/6382Copy the code
  • 127.0.0.2
Sudo mkdir -p /opt/redis/redis-5.0.4/cluster/6383 sudo mkdir -p /opt/redis/redis-5.0.4/cluster/6384Copy the code
  • 127.0.0.3
Sudo mkdir -p /opt/redis/redis-5.0.4/cluster/6385 sudo mkdir -p /opt/redis/redis-5.0.4/cluster/6386Copy the code

(2) Cluster node instance configuration

In each instance directory, create the redis.conf configuration file.

The example configuration template uses node 6381 as an example (replace port 6381 for other nodes) as follows:

# port
port 6381
# Bound host port (0.0.0.0 indicates that remote access is allowed)
bind 0.0.0.0
Start as a daemon
daemonize yes

Enable cluster mode
cluster-enabled yes
# Cluster configuration, the configuration file is automatically generated for the first timeCluster -- config file/opt/redis/redis - 5.0.4 / cluster / 6381/6381. ConfSet request timeout to 10 seconds
cluster-node-timeout 10000

# Enable AOF persistence
appendonly yes
# Data storage directoryDir/opt/redis/redis - 5.0.4 / cluster / 6381# process file
pidfile /var/run/redis-cluster/redis-6381.pid
# log fileLogfile/opt/redis/redis - 5.0.4 / cluster / 6381/6381. The logCopy the code

(3) Start Redis node

Redis utils/create-cluster contains a script named create-cluster, which can be used to create, start, stop, and restart a Redis node.

There are several key parameters in the script:

  • PORT=30000 – Initial port number
  • TIMEOUT=2000 – Timeout time
  • NODES=6 – Number of nodes
  • REPLICAS=1 – Number of backups

Each command item in the script iterates through the initial port number and the number of nodes set.

In the previous planning, node ports range from 6381 to 6386, so you need to set the PORT variable to 6380.

The script starts each Redis node by specifying command-line parameters to configure properties. So, we need to change it:

if [ "The $1"= ="start" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        echo "Starting $PORT"/ opt/redis/redis - 5.0.4 / SRC/redis server/opt/redis/redis - 5.0.4 / cluster /${PORT}/redis.conf
    done
    exit 0
fi
Copy the code

Ok, on each server, execute./create-cluster start to start the nodes.

Then, use the ps command to verify that the Redis process is working:

$ ps -ef | grep redis root 12036 1 12 16:26 ? 00:08:28 /opt/redis/redis-5.0.4/ SRC /redis-server 0.0.0.0:6381 [cluster] root 12038 1 0 16:26? 00:00:03 / opt/redis/redis - 5.0.4 / SRC/redis - server 0.0.0.0:6382 [their]Copy the code

(4) Start the cluster

You can run the redis-cli –cluster create command to automatically configure the cluster as follows:

$/opt/redis/redis-5.0.4/ SRC /redis-cli --cluster create 127.0.0.1:6381 127.0.0.1:6382 127.0.0.2:6383 127.0.0.2:6384 127.0.0.3 127.0.0.3:6385:6386 - cluster - replicas of 1Copy the code

If the startup is successful, the following information is displayed:

>>> Performing hashslots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 127.0.0.2:6384 to 127.0.0.1:6381 Adding Replica 127.0.0.3:6386 to 127.0.0.2:6383 Adding Replica 127.0.0.1:6382 to 127.0.0.3:6385 M: 75527 b790e46530ea271a5b78f9e0fd9030f68e0 127.0.0.1:6381 slots: [0-5460] (5461 slots) master S: 031 dd0fd5ad90fa26fcf45d49ad906d063611a6d 127.0.0.1:6382 replicates ebdd25005840da9ecbe07d937296a264206 53012 M: 0 cfbceec272b6ff70e1dfb5c5346a5cb2c20c884 127.0.0.2:6383 slots: [5461-10922] (5462 slots) master S: 016 ae9624202891cc6f2b480ff0634de478197fb 127.0.0.2:6384 replicates b790e46530ea271a5b78f9e0fd9030f68e0 75527 M: 53012 ebdd25005840da9ecbe07d937296a264206 127.0.0.3:6385 slots: [10923-16383] (5461 slots) master S: B6d70f2ed78922b1dcb7967ebe1d05ad9157fca8 127.0.0.3:6386 replicates 0 cfbceec272b6ff70e1dfb5c5346a5cb2c20c884 Can Iset the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting forthe cluster to join .... >>> Performing Cluster Check (using node 127.0.0.1:6381) M: 75527 b790e46530ea271a5b78f9e0fd9030f68e0 127.0.0.1:6381 slots: [0-5460] (5461 slots) master 1 additional up (s) M: 0 cfbceec272b6ff70e1dfb5c5346a5cb2c20c884 127.0.0.2:6383 slots: [5461-10922] (5462 slots) master 1 additional up (s) S: 016 ae9624202891cc6f2b480ff0634de478197fb 127.0.0.2:6384 slots: (0 slots) slave replicates 75527b790e46530ea271a5b78f9e0fd9030f68e0 M: 53012 ebdd25005840da9ecbe07d937296a264206 127.0.0.3:6385 slots: [10923-16383] (5461 slots) master 1 additional up (s) S: 031 dd0fd5ad90fa26fcf45d49ad906d063611a6d 127.0.0.1:6382 slots: (0 slots) slave replicates 53012ebdd25005840da9ecbe07d937296a264206 S: B6d70f2ed78922b1dcb7967ebe1d05ad9157fca8 127.0.0.3:6386 slots: (0 slots) slave replicates 0cfbceec272b6ff70e1dfb5c5346a5cb2c20c884 [OK] All nodes agree about slots configuration. >>> Checkfor open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Copy the code

(5) Routine maintenance

  • Shutting down the cluster –./create-cluster stop
  • Check whether the cluster is healthy (specify any node) :./redis-cli --cluster check <ip:port>
  • Try to repair the cluster node:./redis-cli --cluster fix <ip:port>

Redis command

For details about how to use the Redis command, see the official Redis command documentation

Move two cheat sheet figure, the site: www.cheatography.com/tasjaevan/c…

Pressure test

Refer to official documentation: How fast is Redis?

Redis comes with a performance testing tool: Redis-Benchmark

(1) Basic test

$ redis-benchmark -q -n 100000
Copy the code
  • -qThe command is executed silently
  • -n 100000100,000 requests

(2) Test the specified read and write instructions

$ redis-benchmark -t set,lpush -n 100000 -q
SET: 74239.05 requests per second
LPUSH: 79239.30 requests per second
Copy the code

(3) Test the specified read and write instructions in pipeline mode

redis-benchmark -n 1000000 -t set,get -P 16 -q
SET: 403063.28 requests per second
GET: 508388.41 requests per second
Copy the code

The client

RedisDesktopManager is recommended

The script

If you want to install a Redis single-node service foolishly, you can use my setup script

The resources

  • Redis official documentation
  • In-depth analysis of Redis series (3) – Redis cluster mode construction and detailed explanation of the principle