In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Redis database
1.1 introduction to Redis
1.1.1 introduction
Redis is an open source, network-enabled, memory-based, optional persistence key-value pair (key-value) storage database written in ANSI C. From June 2015, the development of Redis was sponsored by Redis Labs, while from May 2013 to June 2015, its development was sponsored by Pivotal. Until May 2013, its development was sponsored by VMware. According to the monthly ranking website DB-Engines.com, Redis is the most popular key-value pair storage database.
Source: https://db-engines.com/en/ranking
Redis uses in-memory (In-Memory) dataset (DataSet).
Multiple data types are supported.
It runs on most POSIX systems, such as Linux, * BSD, OS X, etc.
1.1.2 Software acquisition and help
Official website: https://redis.io
Official version download address: http://download.redis.io/releases/
Redis Chinese Command reference: http://redisdoc.com
Chinese website 1: http://redis.cn
Chinese website 2: http://www.redis.net.cn
1.1.3 Redis featur
High-speed read and write, rich data types
Support persistence, multiple memory allocation and recovery strategies
Support for weak transactions, message queuing, message subscription
Support for high availability and distributed sharding clusters
1.1.4 comparison of enterprise cache database solutions
Memcached:
Advantages: high-performance read and write, single data type, support for client-side distributed clusters, consistent hash multi-core structure, high multi-thread read and write performance.
Disadvantages: no persistence, possible cache penetration in node failures, client-side implementation for distribution, difficulty in cross-room data synchronization, and high complexity of architecture expansion.
Redis:
Advantages: high-performance read and write, multi-data type support, data persistence, highly available architecture, support for custom virtual memory, support for distributed sharding clusters, high performance for single-thread read and write
Disadvantages: multithreaded read and write is slower than Memcached
Tair: official website: http://tair.taobao.org
Advantages: high-performance read and write, support for three storage engines (ddb, rdb, ldb), support for high availability, support for distributed shard clusters, and support for caching for almost all Taobao businesses.
Disadvantages: in the case of stand-alone, the read and write performance is slower than the other two products.
1.1.5 Redis application scenario
Data cache, web session cache (Session Cache)
Ranking application
Message queuing, publish subscription
Appendix-Enterprise applications of Redis
1.2 Redis simple deployment
1.2.1 typical installation-single instance
System environment description
[root@Redis ~] # cat / etc/redhat-release CentOS release 6.9 (Final) [root@Redis ~] # uname-r 2.6.32-696.el6.x86_64 [root@Redis ~] # sestatus SELinux status: disabled [root@Redis ~] # / etc/init.d/iptables statusiptables: Firewall is not running. [root@Redis ~] # hostname-I 10.0.0.186 172.16.1.186
Install redis
[root@Redis ~] # cd / usr/local/ [root@Redis local] # wget http://download.redis.io/releases/redis-3.2.10.tar.gz[root@Redis local] # tar xzf redis-3.2.10.tar.gz [root@Redis local] #\ rm redis-3.2.10.tar.gz [root@Redis local] # mv redis-3.2.10 redis [root@Redis local] # cd redis/ [root@Redis redis] # make
At this point, the redis installation is complete.
1.2.2 start the first redis instance
Create a client soft connection
[root@Redis src] # ln-s / usr/local/redis/src/redis-cli / usr/bin/
Simple startup methods, all using the default configuration
[root@Redis ~] # cd / usr/local/redis/src [root@Redis src] #. / redis-server &
Write a configuration file
1. Simplify the configuration file
[root@Redis redis] # cp redis.conf {, .bak} [root@Redis redis] # grep-Ev'^ $| # 'redis.conf.bak > redis.conf [root@Redis redis] # cp redis.conf / etc/
2. Edit the configuration file
[root@Redis ~] # cat / etc/redis.conf bind 127.0.0.1 10.0.0.186protected-mode yesport 6379tcp-backlog 511timeout 0tcp-keepalive 300daemonize yessupervised nopidfile / var/run/redis_6379.pidloglevel noticelogfile "/ var/log/redis_6379.log" dir / usr/local/redis/data/#
3. Write startup scripts (for CentOS 6.x)
View Code redis management script
Note: pay attention to the execution permissions since writing the script.
1.2.3 Redis multi-instance configuration
Note: after the completion of the multi-instance configuration based on the single instance configuration
Create and enter the program directory
[root@Redis redis] # mkdir / application/redis-p [root@Redis redis] # cd / application/redis/
Modify the configuration file
For i in 0 1 2 do # create a multi-instance (port naming) directory mkdir-p 638$ I # copy initiator to each instance\ cp / usr/local/redis/src/redis-server / application/redis/638$ I / # copy configuration files. Note: based on the single instance configuration, modify the program storage directory sed-I "/ dir/s#.*#dir / application/redis/638$ iUnip" / application/redis/638$ i/redis.conf # modify other port information sed-I "s 6379x638 $iUnig" / application/redis/638$ i/redis.conf # allow remote connection to redis sed-I'/ protected-mode/s#yes#no#g' / application/redis/638$ i/redis.confdone to launch instance for i in 0 1 2 do / application/redis/638$ i/redis-server / application/redis/638$ i/redis.confdone
Connect redis
[root@Redis redis] # redis-cli-h 10.0.0.186-p 637910.0.186 purl 6379 >
1.2.4 redis.conf configuration description
Whether to run in the background:
Daemonize no/yes
Default Port:
Port 6379
Whether the AOF log switch is on:
Appendonly no/yes
Log file location
Logfile / var/log/redis.log
RDB persistence data file:
Dbfilename dump.rdb
Specify IP to listen
Bind 10.0.0.51 ip2 ip3 ip4
Prohibit protected-mode
Protected-mode yes/no (protected mode, whether only local access is allowed)
Add requirepass {password}
Requirepass root
Use in redis-cli
Auth {password} for authentication
1.2.5 change configuration online
Get the current configuration
CONFIG GET *
Change the running configuration
CONFIG SET loglevel "notice"
Change password to be empty
10.0.0.186 config set requirepass > exit10.0.0.186:6379 > config get dir1) "dir" 2) "/ usr/local/redis/data"
1.3 Redis data persistence
1.3.1 persistence strategy
Redis provides several different levels of persistence: one is RDB, the other is AOF.
RDB persistence
A point-in-time snapshot (point-in-time snapshot) of a dataset can be generated within a specified time interval.
AOF persistence
Record all write commands executed by the server and restore the dataset by re-executing these commands when the server starts. All commands in the AOF file are saved in the Redis protocol format, and the new command is appended to the end of the file.
Redis can also rewrite (rewrite) the AOF file in the background so that the size of the AOF file does not exceed the actual size required to save the dataset state. Redis can also use both AOF persistence and RDB persistence. In this case, when Redis restarts, it gives priority to using the AOF file to restore the dataset because the dataset saved by the AOF file is usually more complete than the dataset saved by the RDB file.
You can even turn off persistence so that the data exists only while the server is running.
1.3.2 RDB persistence
Advantages of RDB
⚔ RDB is a very compact (compact) file that holds the dataset of Redis at some point in time. This kind of file is ideal for backup: for example, you can back up RDB files every hour for the last 24 hours, and also back up a RDB file every day of each month. In this way, even if you encounter problems, you can restore the dataset to a different version at any time.
⚔ RDB is ideal for disaster recovery (disaster recovery): it has only one file and the content is so compact that it can be transferred (after encryption) to another data center, or to Amazon S3.
⚔ RDB can maximize the performance of Redis: the only thing the parent process has to do when saving the RDB file is to fork out a child process, and then the child process will handle all the rest of the save work, and the parent process does not have to perform any disk ISCOO operations.
⚔ RDB recovers large datasets faster than AOF does.
Shortcomings of RDB
If you need to avoid losing data in the event of a server failure, RDB is not for you.
Although Redis allows you to set different save points (save point) to control how often the RDB file is saved, because the RDB file needs to save the state of the entire dataset, it is not an easy operation. So you may save the RDB file at least once in 5 minutes. In this case, in the event of a downtime, you may lose data for several minutes.
Each time the RDB is saved, the Redis needs to fork () a child process, and the child process does the actual persistence work. When the dataset is large, fork () can be very time-consuming, causing the server to stop processing the client in so-and-so milliseconds; if the dataset is very large and CPU time is very tight, the stop time may even be as long as a full second. Although AOF rewriting also requires fork (), there is no loss of data durability no matter how long the execution interval of AOF rewriting is.
1.3.3 AOF persistence
Advantages of AOF
Using AOF will make your Redis** more durable: you can use different fsync strategies: no fsync, fsync per second, fsync every time you write. Using the default per second fsync policy, Redis still performs well (fsync is processed by background threads, and the main thread will try its best to handle client requests). In the event of a failure, you will lose up to 1 second of data.
Redis can automatically rewrite AOF in the background when the AOF file becomes too large: the new AOF file after rewriting contains the minimum set of commands needed to recover the current dataset. The entire rewrite operation is perfectly safe because Redis continues to append commands to existing AOF files during the creation of new AOF files, and existing AOF files will not be lost even if downtime occurs during rewriting.
Once the new AOF file is created, Redis switches from the old AOF file to the new AOF file and starts appending the new AOF file.
The AOF file saves all writes to the database in an orderly manner, which are saved in the format of the Redis protocol, so the contents of the AOF file are easy to read and parse. Exporting (export) the AOF file is also very simple: for example, if you accidentally execute the FLUSHALL command, but as long as the AOF file is not rewritten, simply stop the server, remove the FLUSHALL command at the end of the AOF file, and restart Redis, and you can restore the dataset to the state it was before the FLUSHALL execution.
Shortcomings of AOF
For the same dataset, the volume of the AOF file is usually larger than that of the RDB file. Depending on the fsync strategy used, AOF may be slower than RDB.
In general, the performance of fsync per second is still very high, and turning off fsync can make AOF as fast as RDB, even under heavy loads. However, RDB can provide a more guaranteed maximum latency (latency) when dealing with large write loads.
AOF has had such bug in the past: due to individual commands, when the AOF file is reloaded, the dataset cannot be restored to what it was when it was saved. (for example, the blocking command BRPOPLPUSH has caused such a bug. Tests are added to the test suite: they automatically generate random, complex data sets and make sure everything is all right by reloading the data
Although this kind of bug is not common in AOF files, it is almost impossible for RDB to have this kind of bug by comparison.
1.3.4 how to choose which persistence method to use
In general, if you want to achieve data security comparable to PostgreSQL, you should use both persistence features.
If you are very concerned about your data, but can still withstand data loss within a few minutes, then you can only use RDB persistence.
There are many users who only use AOF persistence, but we do not recommend this approach: because regular RDB snapshots (snapshot) are very convenient for database backup, and RDB recovers datasets faster than AOF, in addition, using RDB can avoid the bug of the AOF programs mentioned earlier.
Note: for all the reasons mentioned above, redis may integrate AOF and RDB into a single persistence model in the future (this is a long-term plan).
1.3.5 Snapshots are persistent
By default, Redis saves the database snapshot in a binary file named dump.rdb. You can set Redis to save the dataset automatically when the condition of "at least M changes to the dataset in N seconds" is met.
You can also manually let Redis save the dataset by calling SAVE or BGSAVE.
For example, the following setting allows Redis to automatically save a dataset when it meets the condition that "at least 1000 keys have been changed in 60 seconds": save 60 1000
This persistence method is called snapshot snapshotting.
When Redis needs to save the dump.rdb file, the server does the following:
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.