2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Redis (RemoteDictionaryServer) is an open source NoSQL database written in C. Redis runs on memory and supports persistence. It uses key-value storage and is an indispensable part of the current distributed architecture.
NoSQL (NoSQL= Not Only SQL), which means "not only SQL", is a general term for non-relational databases. The mainstream NoSQL databases include Redis, MongBD, Hbase, CouhDB, etc. The storage methods, storage structures and usage scenarios of the above non-relational databases are completely different. So we think it is a collection of non-relational databases, rather than a general term like relational databases. In other words, databases other than mainstream relational databases can be considered non-relational. NOSQL databases are considered to be the next generation of database products due to their advantages such as non-relational, distributed, open source and horizontal expansion.
A relational database is a structured database that is built on the basis of a relational model and is generally record-oriented. It uses mathematical concepts and methods such as set algebra to process data in the database. A relational model refers to a two-dimensional table model, so a relational database is a data organization consisting of two-dimensional tables and the connections between them. In the real world, various connections between various entities can be represented by a relational model. SQL statements (standard data query language) are a language based on relational databases, which are used to perform retrieval and operations on data in relational databases.
With the rise of Web2.0 websites, relational databases have exposed many difficult-to-solve problems, such as the "three highs" problem, when dealing with Web2.0 websites, especially Web2.0 purely dynamic websites with massive data and high concurrency SNS (Social Networking Services).
Web2.0 websites will generate dynamic pages and provide dynamic information in real time based on the user's personalized information, so it is impossible to use dynamic page staticization technology. Therefore, the concurrent load of the database is very high, generally reaching more than 10,000 read and write requests per second. Relational databases can barely support tens of thousands of query requests, but if there are tens of thousands of write data requests, the hard disk I0 will no longer be able to bear it. For ordinary BBS websites, there are often high-concurrency write data requests.
SNS websites like Facebook and Friendfeed generate a large amount of user dynamic information every day. For example, Friendfeed generates no less than 250 million user dynamic information per month. For a relational database, executing SQL queries on a table containing 250 million records is very inefficient.
In the Web architecture, databases are the most difficult to scale horizontally. When the number of users and traffic of application systems increases day by day, databases cannot simply expand their performance and load capacity by adding hardware and server nodes like Web services. Especially for some websites that need to provide services to the outside world 24 hours a day, database upgrades and expansions are often accompanied by downtime maintenance and data migration, and the workload is very huge.
Relational databases and non-relational databases have their own characteristics and application scenarios. The close combination of the two will bring new ideas to the development of Web2.0 databases. Let the relational database focus on relationships, and the non-relational database focus on storage. For example, in a MySQL database environment with read-write separation, frequently accessed data can be stored in a non-relational database to improve access speed.
It has extremely high data reading and writing speeds, with data reading speed reaching up to 110,000 times/s and data writing speed reaching up to 81,000 times/s.
It supports a variety of data types, not only simple key-value data, but also Strings, Lists, Hashes, Sets, and Ordered Sets.
It supports data persistence, and can save data in memory to disk, so that it can be loaded again for use when restarting.
Atomicity: All Redis operations are atomic.
Supports data backup, namely data backup in master-salve mode.
As a memory-based database, Redis is most commonly used for caching. In addition, common Redis application scenarios also include operations to obtain the latest N data, ranking applications, counter applications, storage relationships, real-time analysis systems, and log records.
[root@localhost ~]# systemctl stop firewalld //In the experimental environment, turning off the firewall has no effect. It is not recommended to turn it off in the production environment. Try to add firewall policies
[root@localhost ~]# setenforce 0 //Shut down the kernel, which will block the application from running
[root@localhost ~]# yum -y install gcc* zlib-devel //Download the tools needed to decompress the tarball
[root@localhost ~]#tar xvzf redis-4.0.9.tar.gz //redis is an open source and free application. You can download this package from the official website
[root@localhost ~]# cd redis-4.0.9/
[root@localhost redis-4.0.9]# make
Notice:
When making, the following error message may appear:
Solution 1: Usemake MALLOC=libc specifies the memory allocator to be libcCompile
Solution 2: make clean && makeke distclean
[root@localhost redis-4.0.9]# make PREFIX=/usr/local/redis install
[root@localhost ~]# ln -s /usr/local/redis/bin/* /usr/local/bin/
[root@localhost redis-4.0.9]# cd /root/redis-4.0.9/utils/
[root@localhost utils]# ./install_server.sh
Remark:
Config file: /etc/redis/6379.conf //Configuration file path
Log file: /var/log/redis_6379.log //Log file path
Data dir : /var/lib/redis/6379 //Data file path
Executable : /usr/local/redis/bin/redis-server // executable file path
Cli Executable : /usr/local/redis/bin/redis-cli //Client command line tool
[root@localhost utils]# netstat -anpt | grep redis
[root@localhost ~]#/etc/init.d/redis_6379 stop
[root@localhost ~]#/etc/init.d/redis_6379 start
[root@localhost ~]#/etc/init.d/redis_6379 restart
[root@localhost ~]#/etc/init.d/redis_6379 status
[root@localhost ~]#vim /etc/redis/6379.conf
bind 127.0.0.1 192.168.10.101 //Host address to listen to
port 6379 //port
daemonize yes // Enable the daemon process
pidfile /var/run/redis_6379.pid //Specify PID file
loglevel notice //Log level
logfile /var/log/redis_6379.log //Specify the log file
[root@localhost~]#/etc/init.d/redis_6379 restart
[root@localhost utils]# netstat -anpt | grep redis
Ø redis-server: a tool for starting Redis;
Ø redis-benchmark: used to detect the operating efficiency of Redis on the local machine;
Ø redis-check-aof: repair AOF persistence files;
Ø redis-check-rdb: repair RDB persistence files;
Ø redis-cli: Redis command line tool.
[root@localhost ~]# redis-cli
127.0.0.1:6379>
127.0.0.1:6379> ping
PONG
[root@localhost ~]#redis-cli -h 192.168.10.101 -p 6379
Ø help @<group> :Obtain<group> List of commands in ;
Ø help<command> : Get help for a command;
Ø help<tab> : Get a list of topics that may help.
Remark:
Ø help<tab> : Get a list of topics that may help.
After typing help, press the tab key
127.0.0.1:6379>help @list
127.0.0.1:6379>help set
redis-benchmark is the official Redis performance testing tool that can effectively test the performance of the Redis service.
Ø -h: specify the server host name;
Ø -p: specify the server port;
Ø -s: specifies the server socket;
Ø -c: specifies the number of concurrent connections;
Ø -n: specify the number of requests;
Ø -d: specifies the data size of the SET/GET value in bytes;
Ø -k:1=keep alive 0=reconnect;
Ø -r: SET/GET/INCR uses a random key, SADD uses a random value;
Ø -P: transfer through pipes<numreq> ask;
Ø -q: Force exit redis. Only query/sec value is displayed;
Ø --csv: Output in CSV format;
Ø -l: Generate a loop and execute the test forever;
Ø -t: only run the comma-separated list of test commands;
Ø -I: Idle mode. Only open N idle connections and wait.
[root@localhost ~]#redis-benchmark -h 192.168.10.101 -p 6379 -c 100 -n 100000 //Send 100 concurrent connections and 100,000 requests to the Redis server with IP address 192.168.10.101 and port 6379 to test performance
Remark:
Ø -h: specify the server host name;
Ø -p: specify the server port;
Ø -c: specifies the number of concurrent connections;
Ø -n: specify the number of requests;
====== MSET (10 keys) ======
100000 requests completed in 1.02 seconds
100 parallel clients ##100 concurrent connections
3 bytes payload
keep alive: 1
87.25% <= 1 milliseconds ##87.25% of the command execution time is less than or equal to 1 millisecond
99.90% <= 2 milliseconds
100.00% <= 2 milliseconds
97943.19 requests per second #Number of requests per second
[root@localhost ~]#redis-benchmark -h 192.168.10.101 -p 6379 -q -d 100
Remark:
Ø -h: specify the server host name;
Ø -p: specify the server port;
Ø -d: specifies the data size of the SET/GET value in bytes;
Ø -q: Force exit redis. Only query/sec value is displayed;
PING_INLINE: 121506.68 requests per second //How many PING operations are completed per second?
PING_BULK: 124378.11 requests per second
SET: 121654.50 requests per second //How many times per second SET key value is completed
GET: 122100.12 requests per second //How many times per second is the GET key value completed?
INCR: 118764.84 requests per second //How many atomic counts are completed per second
LPUSH: 112612.61 requests per second
RPUSH: 118623.96 requests per second
LPOP: 107874.87 requests per second
RPOP: 114416.48 requests per second
SADD: 123304.56 requests per second
HSET: 122249.38 requests per second
SPOP: 128040.97 requests per second
LPUSH (needed to benchmark LRANGE): 116686.12 requests per second
LRANGE_100 (first 100 elements): 40016.00 requests per second
LRANGE_300 (first 300 elements): 11991.85 requests per second
LRANGE_500 (first 450 elements): 7381.71 requests per second
LRANGE_600 (first 600 elements): 5230.67 requests per second
MSET (10 keys): 92421.44 requests per second //Multiple key v per secondlaueNumber of requests
[root@localhost ~]# redis-benchmark -t set,lpush -n 100000 -q //Test the performance of the Redis service on this machine when performing set and lpush operations.
Remark:
Ø -n: specify the number of requests;
Ø -q: Force exit redis. Only query/sec value is displayed;
Ø -t: only run the comma-separated list of test commands;
SET: 121951.22 requests per second
LPUSH: 127226.46 requests per second
Ø set: Store data. The basic command format is set key value。
Ø get: Get data, the basic command format is get key。
exist Redis In the database, key The relevant commands mainly include the following.
use keys The command can take a list of key values that match the rules, usually combined with*, ?, etc.
127.0.0.1:6379>set k1 1
OK
127.0.0.1:6379>set k2 2
OK
127.0.0.1:6379>set k3 3
OK
127.0.0.1:6379>set v1 4
OK
127.0.0.1:6379>set v5 5
OK
127.0.0.1:6379>KEYS *
1) "teacher"
2) "k1"
3) "k2"
4) "k3"
5) "v1"
6) "v5"
127.0.0.1:6379>set v22 5
OK
127.0.0.1:6379>KEYS v*
1) "v1"
2) "v5"
3) "v22"
127.0.0.1:6379>KEYS v?
1) "v1"
2) "v5"
127.0.0.1:6379>KEYS v??
1) "v22"
exists The command can determine whether the key value exists
127.0.0.1:6379>exists teacher
(integer) 1
The result is 1, indicating teacher The key is existence
127.0.0.1:6379>exists tea
(integer) 0
The result is 0, indicating tea Key does not exist
del The command can delete the specified key
127.0.0.1:6379>keys *
1) "teacher"
2) "v1"
3) "v22"
4) "k3"
5) "k1"
6) "k2"
7) "v5"
127.0.0.1:6379> del v5
(integer) 1
127.0.0.1:6379>get v5
(nil)
use type The command can be obtained key corresponding value Value Types
127.0.0.1:6379>type k1
string
Remark:
Data types supported by redis
rename The command is to key Rename
In actual use, it is recommended to use exists Command to view target key Whether it exists, and then decide whether to execute rename command to avoid overwriting important data
127.0.0.1:6379>keys v*
1) "v1"
2) "v22"
127.0.0.1:6379>rename v22 v2
OK
127.0.0.1:6379>keys v*
1) "v1"
2) "v2"
127.0.0.1:6379>get v1
"4"
127.0.0.1:6379>get v2
"5"
127.0.0.1:6379>rename v1 v2
OK
127.0.0.1:6379>get v1
(nil)
127.0.0.1:6379>get v2
"4"
renamenx The command is used to key Rename and check if the new name exists.
userenamenx When the command is rename, if the target key If it exists, it will not be renamed.
127.0.0.1:6379>keys *
1) "teacher"
2) "k3"
3) "k1"
4) "k2"
5) "v2"
127.0.0.1:6379>get teacher
"zhanglong"
127.0.0.1:6379>get v2
"4"
127.0.0.1:6379>renamenx v2 teacher
(integer) 0
127.0.0.1:6379>keys *
1) "teacher"
2) "k3"
3) "k1"
4) "k2"
5) "v2"
127.0.0.1:6379>get teacher
"zhanglong"
127.0.0.1:6379>get v2
"4
dbsize The command is used to view the current database key Number of.
127.0.0.1:6379> dbsize
(integer) 5
Redis Included by default without any changes 16 databases, the database names are numbers 0-15 To be named in sequence
(1)Switch to the sequence number 10 Database
127.0.0.1:6379>select 10
OK
(2)Switch to the sequence number 15 Database
127.0.0.1:6379[10]>select 15
OK
(3)Switch to the sequence number 0 Database
127.0.0.1:6379[15]>select 0
Redis The multiple databases are relatively independent to a certain extent. For example, in the database 0 Storage above k1 data, in other 1-15 It cannot be viewed in the database.
127.0.0.1:6379>set k1 100
OK
127.0.0.1:6379>get k1
"100"
127.0.0.1:6379>select 1
OK
127.0.0.1:6379[1]>get k1
(nil)
127.0.0.1:6379[1]>select 0 //Switch to the target database 0
OK
127.0.0.1:6379>get k1 //Check whether the target data exists
"100"
127.0.0.1:6379>move k1 1 //The database 0 middle k1 Move to Database 1 middle
(integer) 1
127.0.0.1:6379>select 1 //Switch to the target database 1
OK
127.0.0.1:6379[1]>get k1 //View the moved data
"100"
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> get k1 //In the database 0 Cannot be viewed in k1 Value
(nil)
Clear the current database data, use FLUSHDB
Command implementation; clear all database data, use FLUSHALL Command Implementation
Redis All data is stored in memory and then saved to disk asynchronously from time to time.(This is called“Semi-persistent mode”); You can also write every data change to a append only file(aof)in(This is called“Full persistence mode”)。
Since Redis data is stored in memory, if persistence is not configured,Redis After restarting, all data will be lost. Therefore, you need to turn on Redis The persistence function saves the data to disk. Redis After reboot, the data can be restored from the disk.Redis Provides two ways to persist, one is RDB(Redis DataBase) Persistence (the principle is to ReidsIn-memory database logging timing dump to disk RDB persistence), the other is AOF(append only file)Persistence (the principle is to Reids The operation log is written to the file in append mode).
The default method
RDB persistence is the process of generating a snapshot of the current process data and saving it to the hard disk. The RDB persistence process can be triggered manually or automatically.
Trigger mechanism: Manual trigger corresponds to the save and bgsave commands respectively
save command: blocks the current Redis server until the RDB process is completed. This may cause time blocking for instances with large amounts of memory. It is not recommended for online environments.
bgsave command: The Redis process executes the fork (function used to create a process) operation to create a child process. The RDB persistence process is the responsibility of the child process and ends automatically after completion. Blocking only occurs in the fork phase.
Advantages of RDB:
RDB is a compact and compressed binary file that represents a snapshot of Redis data at a certain point in time. It is very suitable for backup, full replication and other scenarios. For example, perform bgsave backup every 6 hours and copy the RDB file to a remote machine or file system for disaster recovery.
Redis loads RDB to restore data much faster than the AOF method.
Disadvantages of RDB:
RDB data cannot be persisted in real time or seconds. This is because bgsave creates a child process by forking each time it runs, which is a heavyweight operation and the cost of frequent execution is too high.
RDB files are saved in a specific binary format. There are multiple RDB versions in different formats during the evolution of Redis versions. There is a problem that the old version of Redis service is not compatible with the new version of RDB format.
AOF (append only file) persistence: Each write command is recorded in an independent log, and the command in the AOF file is re-executed when restarting to achieve the purpose of data recovery. The main function of AOF is to solve the real-time persistence of data, and it is currently the mainstream of Redis persistence.
The criteria for choosing between the two:
Sacrifice some performance in exchange for higher cache consistency (AOF),
When write operations are frequent, do not enable backup to obtain higher performance. save When you have time, make a backup (RDB)
Remark:
If a persistent file needs to be loaded after redies is restarted, the AOF file will be selected.
If RDB is enabled first and then AOF is enabled, and RDB is persisted first, the content in the RDB file will be overwritten by AOF.
[root@localhost ~]# vim /etc/redis/6379.conf
Open 6379.conf After the file, search save, you can see the configuration information as shown below.
exist Redis There are three synchronization modes in the configuration file, they are:
always: Synchronous persistence, every data change will be written to disk immediately
everysec: Default recommendation, asynchronous recording once per second (default value)
no: no synchronization, let the operating system decide how to synchronize
Ignore the last potentially problematic instruction
[root@localhost ~]#/etc/init.d/redis_6379 restart
To solve AOF To solve the problem of increasing file size, users can Redis send BGREWRITEAOFOrder.BGREWRITEAOF The command will be removed by AOF Redundant commands in the file are rewritten (rewrite)AOFFile, AOF The file size is as small as possible.
127.0.0.1:6379> bgrewriteaof
Background append only file rewriting started
# When the log is BGREWRITEAOF,ifno-appendfsync-on-rewriteIf set to yes, new write operations will not be synchronized with fsync, but will be temporarily stored in the buffer., to avoid disk IO operation conflicts, wait until the rewrite is complete before writing. The default value in Redis is no
no-appendfsync-on-rewrite no
# When the current AOF file size is twice the size of the AOF file at the last log rewrite, a BGREWRITEAOF operation occurs
auto-aof-rewrite-percentage 100
Remark:
100 meansThe growth ratio of the aof file refers to the growth ratio of the current aof file compared to the last rewrite.,100 is twice
#The minimum value for executing the BGREWRITEAOF command on the current AOF file to avoid frequent BGREWRITEAOF due to the small file size when Reids is just started
auto-aof-rewrite-min-size 64mb
192.168.9.236:7001> info memory
used_memory:1210776 #The size of the memory used.In bytes
used_memory_human:1.15M #With unit display,In M
used_memory_rss:7802880 # How much memory is used by redis from the operating system's perspective
used_memory_rss_human:7.44M # Display with unit
maxmemory:1073741824 # Maximum memory size
maxmemory_human:1.00G # Display with units
maxmemory-policy: Recycling strategy
Ø volatile-lru: It allows Redis Pick the least recently used key To delete
Ø volatile-ttl:Eliminate according to the expiration time of the key
Ø volatile-random: Randomly select data from the data set with set expiration time and eliminate it;
Ø allkeys-lru:use LRU The algorithm eliminates data from all data sets;
Ø allkeys-random: Randomly select data from the data set and eliminate them;
Ø noeviction: Disable data elimination (default value)
Remark:
Set the expiration time of the key
expire v1 10
The expiration time of v1 is 10 seconds
Remark:
when Redis Due to memory pressure, a key hour,Redis The first consideration is not to recycle the oldest data, but the least recently used data. key or about to expire key Randomly select one key, removed from the dataset
Find the requirepass keyword, followed by the password. It is commented out by default, which means that no password is required by default, as follows:
Open the comment, set it as your own password, and restart.
Use redis-cli to connect to redis and execute the following command
config set requirepass 123456
After the execution is completed, there is no need to restart. Exit the client and enter the password when you log in again.
[root@localhost bin]# ./redis-cli -a 123456
[root@localhost bin]# ./redis-cli127.0.0.1:6379> auth 123456
[root@localhost bin]# ./redis-cli -a 123456 shutdown
1) The password set by modifying the configuration file will take effect permanently; the password set by using the command will take effect temporarily and will become invalid after the restart.
2) To modify the password set in the configuration file, you need to restart the computer to take effect; the password set using the command will take effect after logging out and then logging in, and will become invalid after restarting the computer.
3) The command has a higher priority than the configuration file