In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
A brief introduction to Redis
Redis is completely open source and free, complies with the BSD protocol, and is a high-performance key-value database.
Redis and other key-value cache products have the following three features:
Redis supports data persistence. You can save the data in memory on disk and load it again when you restart it.
Redis not only supports simple key-value type data, but also provides storage of data structures such as list,set,zset,hash.
Redis supports data backup, that is, data backup in master-slave mode.
Redis advantage
Extremely high performance-Redis can read at a speed of 110000 times per second and write at a speed of 81000 times per second.
Rich data types-Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.
Atom-all operations of Redis are atomic, meaning either successful execution or failure to execute at all. A single operation is atomic. Multiple operations also support transactions, that is, atomicity, wrapped by MULTI and EXEC instructions.
Rich features-Redis also supports publish/subscribe, notification, key expiration, and so on.
Relational database and non-relational database
Relational database:
An institutionalized database, created on the basis of a relational model, generally oriented to records
Including oracle, mysql, sqlserver, db2
Non-relational databases:
Except for mainstream relational databases, unexpected databases are artificially non-relational.
Including redis, mongdb, hbase, couhdb
Background of non-relational database
High concurrent read and write requirements for database
Demand for efficient storage and access of massive data
Demand for high scalability and availability of database
Install and configure Redis
Install the necessary environment components and install redis
[root@localhost ~] # yum install gcc gcc-c++ make-y # # install environment components
[root@localhost ~] # mkdir / mnt/tools
[root@localhost ~] # mount.cifs / / 192.168.100.100/tools / mnt/tools/ # # Mount
Password for root@//192.168.100.100/tools:
[root@localhost ~] # cd / mnt/tools/redis/
[root@localhost redis] # ls
Redis-5.0.7.tar.gz
[root@localhost redis] # tar xf redis-5.0.7.tar.gz-C / opt/ # # decompress
[root@localhost redis] # cd / opt/
[root@localhost opt] # ls
Redis-5.0.7 rh
[root@localhost opt] # cd redis-5.0.7/
[root@localhost redis-5.0.7] # make # compilation
. / / omit the process
[root@localhost redis-5.0.7] # make PREFIX=/usr/local/redis/ install # # installation
. / / omit the process
Execute the configuration Redis profile script and configure it
[root@localhost redis-5.0.7] # cd utils/
[root@localhost utils] #. / install_server.sh # # execute script to configure
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] # # default port
Selecting default: 6379
Please select the redis config file name [/ etc/redis/6379.conf] # # configuration file
Selected default-/ etc/redis/6379.conf
Please select the redis log file name [/ var/log/redis_6379.log] # # Log File
Selected default-/ var/log/redis_6379.log
Please select the data directory for this instance [/ var/lib/redis/6379] # # data file
Selected default-/ var/lib/redis/6379
Please select the redis executable path [] / usr/local/redis/bin/redis-server
# # Executable File path
Selected config:
Port: 6379
Config file: / etc/redis/6379.conf
Log file: / var/log/redis_6379.log
Data dir: / var/lib/redis/6379
Executable: / usr/local/redis/bin/redis-server
Cli Executable: / usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied / tmp/6379.conf = > / etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@localhost utils] # ln-s / usr/local/redis/bin/ / usr/local/bin/ # # easy for system identification
[root@localhost utils] # netstat-ntap | grep 6379 # # View the listening port
Tcp 00 127.0.0.1:6379 0.0.0.0: LISTEN 18004/redis-server
[root@localhost utils] # / etc/init.d/redis_6379 stop # # close redis
Stopping...
Redis stopped
[root@localhost utils] # netstat-ntap | grep 6379 # # View the listening port
Tcp 0 0127.0.0.1 6379 127.0.0.1 TIME_WAIT-
[root@localhost utils] # / etc/init.d/redis_6379 start # # enable redis
Starting Redis server...
[root@localhost utils] # netstat-ntap | grep 6379
Tcp 0 0 127.0.0.1 6379 0.0.0.0 * LISTEN 18091/redis-server
Tcp 0 0127.0.0.1 6379 127.0.0.1 TIME_WAIT-
[root@localhost utils] #
[root@localhost utils] # vim / etc/redis/6379.conf # # modify the configuration file
Bind 127.0.0.1 192.168.52.149 # # set the listening address
[root@localhost utils] # / etc/init.d/redis_6379 restart # # restart the redis service
Stopping...
Redis stopped
Starting Redis server...
# basic operation of Redis database
[root@localhost utils] # redis-cli-h 192.168.52.149-p 6379 # # Log in to redis
192.168.52.149 6379 > help @ list # # get a list of help
BLPOP key [key...] Timeout
Summary: Remove and get the first element in a list, or block until one is available
Since: 2.0.0
.. / / omit part of the content
RPUSHX key value
Summary: Append a value to a list, only if the list exists
Since: 2.2.0
192.168.52.149 6379 > help set # # help help
SET key value [expiration EX seconds | PX milliseconds] [NX | XX] summary: Set the string value of a keysince: 1.0.0group: string
192.168.52.149 6379 > set name zhangsan # # set key-value pair
OK
192.168.52.149purl 6379 > set net www
OK
192.168.52.149 6379 > KEYS # # View all keys
1) "name"
2) "net"
192.168.52.149purl 6379 > KEYS nasty? # # check that the key begins with n and is followed by two characters
1) "net"
192.168.52.149 6379 > KEYS n # # View key starts with n
1) "name"
2) "net"
192.168.52.149 6379 > GET net # # View the value of the key
"www"
192.168.52.149 6379 > EXISTS net # # check whether the key exists
(integer) 1 # # 1 exists, 0 does not exist
192.168.52.149purl 6379 > EXISTS nat
(integer) 0
192.168.52.149 6379 > del net # # delete key
(integer) 1
192.168.52.149purl 6379 > KEYS
1) "name"
192.168.52.149 6379 > type name # # View the type of key
String
192.168.52.149 6379 > rename name N1 # # rename the key
OK
192.168.52.149purl 6379 > KEYS
1) "N1"
192.168.52.149 6379 > get N1
"zhangsan"
192.168.52.149 6379 > hset person score 80 # # using hash to establish key-value pairs
(integer) 1
192.168.52.149 6379 > hset person name zhangsan # # establish key-value pairs in hash mode
(integer) 1
192.168.52.149 6379 > hset person age 30 # # using hash to establish key-value pairs
(integer) 1
192.168.52.1490 6379 > KEYS
1) "person"
192.168.52.149 6379 > hget person name # # get the value of the key
"zhangsan"
192.168.52.149 6379 > hget person age # # get the value of the key
"30"
192.168.52.149 6379 > set name lisi # # get the value of the key
OK
192.168.52.149purl 6379 > KEYS
1) "name"
2) "person"
192.168.52.149 6379 > automatic deletion time of EXPIRE name 10 # # setting key is 10s
(integer) 1
View all keys in 192.168.52.149 6379 > KEYS # # 10s
1) "name"
2) "person"
Check all keys after 192.168.52.149 6379 > KEYS # # 10s
1) "person"
192.168.52.149 6379 > exit # # exit
Carry out pressure test
[root@localhost utils] # redis-benchmark-h 192.168.52.149-p 6379-c 100-n 100000
# # concurrently 100,000requests
= SET =
100000 requests completed in 1.14 seconds # # time spent on requests
100 parallel clients
3 bytes payload
Keep alive: 1
84.66% SELECT 10 # # enter the 11th library
OK
192.168.52.149 6379 [10] > KEYS
(empty list or set)
192.168.52.149 6379 [10] > SELECT 0 # # enter the first library
OK
192.168.52.149 6379 > MOVE N1 10 # # Mobile key pair to the 11th library
(integer) 1
192.168.52.149purl 6379 > KEYS
1) "mylist"
2) "counter:rand_int"
3) "key:rand_int"
4) "myset:__rand_int__"
192.168.52.149 6379 > SELECT 10 # # enter the 11th library
OK
192.168.52.149 6379 [10] > KEYS # # View key
1) "N1"
192.168.52.149 6379 [10] > GET N1
"zhangsan"
192.168.52.149 6379 [10] > FLUSHDB # # clear the data in the library
OK
192.168.52.149 6379 [10] > KEYS # # View all keys
(empty list or set)
192.168.52.149 6379 [10] > SELECT 0 # # switch to the first library
OK
192.168.52.149 6379 > KEYS # # View all keys
1) "myset:rand_int"
2) "mylist"
3) "key:rand_int"
4) "counter:__rand_int__"
192.168.52.149purl 6379 > exit
[root@localhost utils] #
Redis persistence
Redis is running in memory, and the data in memory is lost due to power outage.
In order to reuse Redis data later, or to prevent system failure, we need to write the data in Redis to disk space, that is, persistence
Persistence classification
RDB mode: to obtain copies of all the data in Redis at a certain time by creating snapshots
AOF mode: write the executed write command to the end of the file and log the changes in the data.
RDB persistence
Default persistence mode of Redis
Default file name dump.rdb
Trigger condition:
Performs a specified number of writes within a specified time interval (profile control)
Execute save or bgsave (asynchronous) commands
Execute the flushall command to empty all data in the database
Execute the shutdown command to ensure that the server shuts down properly and does not lose any data
Advantages and disadvantages:
Suitable for large-scale data recovery
If the business does not require high data integrity and consistency, RDB is a good choice.
The integrity and consistency of data is not high.
Memory consumption during backup
Configure RDB persistence
[root@localhost utils] # vim / etc/redis/6379.conf
# at least one write operation within 900 seconds
Save 900 1
# at least 10 write operations occur within 300 seconds
Save 300 10
# at least 10000 write operations occur within 60 seconds
Save 60 10000
# Snapshot operation will be triggered as long as one of them is satisfied. Note that all save entries indicate that RDB is closed.
# RDB file name
Dbfilename dump.rdb
# RDB file path
Dir / var/lib/redis/6379
# enable compression
Rdbcompression yes
AOF persistence
Redis is not enabled by default
Make up for the deficiency of RDB (data inconsistency)
Record each write in the form of a log and append it to the file
Redis restart will execute the write instruction from front to back according to the contents of the log file to complete the data recovery.
AOF persistent configuration
[root@localhost utils] # vim / etc/redis/6379.conf
# enable AOF persistence
Appendonly yes
# AOF file name
Appendfilename "appendonly.aof"
# always: synchronous persistence. Every time data changes, it is written to disk immediately.
# everysec: recommended by default, asynchronous records per second (default)
Appendfsync everysec
# no: if it is not synchronized, leave it to the operating system to decide how to synchronize
# ignore the last instruction that may be problematic
Aof-load-truncated yes
Rewriting Mechanism of AOF
The working principle of AOF is to append write operations to the file, so that there will be more and more redundant content in the file.
When the size of the AOF file exceeds the set threshold, Redis will compress the contents of the AOF file
AOF rewrite configuration
[root@localhost utils] # vim / etc/redis/6379.conf
# when BGREWRITEAOF the log, setting it to yes means that the new write operation does not synchronize the fsync
# it is only temporarily stored in the buffer to avoid disk I0 operation conflicts and write after the rewrite is completed. No is the default in redis
No-appendfsync-on-rewrite no
# BGREWRITEAOF occurs when the current AOF file size is twice the size of the AOF file in the last log rewrite
Auto-aof-rewrite-percentage 100
# the minimum value for the current AOF file to execute BGREWRITEAOF commands
# avoid frequent BGREWRITEAOF due to small file size when starting Reids
Auto-aof-rewrite-min-size 64mb
Memory fragmentation rate
● the memory value assigned by the system used_ memory rss divided by the memory value used by redis
Used _ memory calculates
● memory fragmentation is caused by inefficient allocation / recycling of physical memory by the operating system
Discontiguous physical memory allocation
● tracking memory fragmentation rates is very important to understand the resource performance of redis instances.
It is reasonable for the memory fragmentation rate to be slightly greater than 1, which indicates that the memory fragmentation rate is relatively low.
The memory fragmentation rate is more than 1.5, indicating that redis consumes 150% of the actual physical memory, of which 50% is the memory fragmentation rate.
If the memory fragmentation rate is less than 1, the Redis memory allocation exceeds the physical memory, and the operating system is swapping memory.
Recycle key
Ensure rational allocation of redis's limited memory resources
● when memory usage reaches the maximum set threshold, you need to choose a key recycling strategy.
By default, the recycling policy is to disable deletion
Modify the maxmemory-policy property value in the redis.conf configuration file
Volatile-lru: use the LRU algorithm to eliminate data from data sets with an expiration time set
Volatile-ttl: select data that is about to expire from a data set with an expiration time (recommended)
Volatile-random: randomly selects data to be eliminated from a set of data with an expiration time
Allkeys-lru: use the LRU algorithm to eliminate data from all data sets
Allkeys-random: data elimination by arbitrarily selecting from the data collection
No-enviction: ban phase-out data
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.