1. The concept of redis
Redis (Remote Dictionary Server), that is, remote dictionary service, is an open source written in ANSI C language , supports the network, can be memory-based or persistent, log-type, Key-Value database , and provides APIs in multiple languages. The description of redis on the redis official website is as follows (Redis is an open source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings , hashes , lists , sets , sorted sets with range queries, bitmaps , hyperloglogs , geospatial indexes , and streams . Redis has built-in replication , Lua scripting, LRU eviction , transactions , and different levels of on-disk persistence , and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster .) We can extract the key points: redis is based on opensource, complies with the BSD protocol, and uses c language Store data in the form of key-value written, stored in the database in memory, and redis supports persistent data storage
2. The role and advantages of redis
We know that the mainstream database in the current market is MySQL, and we think about the following questions: What is the relationship between redis and MySQL? What are the limitations of MySQL in the current market? What advantages does redis have over MySQL?
Let's compare MySQL and redis:
In a high-concurrency market environment, MySQL's data processing has gradually entered a bottleneck period. It cannot process huge amounts of data in a timely manner, and cannot meet the needs of users in some application scenarios. Compared with MySQL, redis first compares it from the hardware aspect : MySQ data is stored in the hard disk, while the data in redis is stored in memory. Therefore, redis has a faster speed for reading data. From the perspective of storage structure, redis stores data in the form of key-value, query Data has higher efficiency; from the perspective of database type, redis is a NOSQL type database, which is different from MySQL's relational database (RDBMS).
Through the comparison between the two, we give a general description of the role and advantages of redis:
redis role
1. Redis provides distributed caching technology, which is a guard with a knife in front of mysql (reducing the pressure on MySQL to process data) (when a request is sent to the database, it will reach redis before the request reaches MySQL, and query whether the data in redis If it exists in redis, it will return directly. If it does not pass through MySQL, if it does not exist in redis, it will go to MySQL to query. After the query is successful in MySQL, the data will be returned, and the data will also be loaded into redis at the same time. The next request will be directly in redis get data in)
2. redis supports memory storage and persistence (RDB+AOF), and redis supports asynchronously writing internal data to the hard disk without affecting the continued service (when redis has abnormal conditions such as power failure, loading data from the hard disk will not and then through MySQL).
3. Provide high-availability architecture collocation, such as stand-alone, master-slave, sentinel, cluster, etc.
4. Provide solutions for cache penetration, breakdown and avalanche
5. Provide distributed locks
6. Queue
Reids provides list and set operations, which makes Redis a good message queuing platform to use.
We often use the queue function of Reids to limit purchases. For example, during holidays or promotional periods, carry out some activities to restrict user purchase behavior, and limit the number of purchases that can be made today or only once within a period of time. It is also more suitable for application.
7. Ranking + likes
In Internet applications, there are various rankings, such as the monthly sales rankings of e-commerce websites, the gift rankings of social APPs, the voting rankings of small programs, and so on. The zset data type provided by Redis can quickly implement these complex leaderboards. For example, a novel website ranks novels, and recommends top-ranked novels to users based on the rankings
Advantages of redis:
- Extremely high performance - Redis can read at 110,000 times/s and write at 81,000 times/s
- Redis has rich data types, not only supports simple key-value type data, but also provides storage of data structures such as list, zset, set, and hash
- Redis supports data persistence, which can save the data in the memory to the disk, and can be loaded again for use when restarting
- Redis supports data backup, that is, data backup in master-slave mode
- Generate a dump.rpb file (can be changed in the configuration file) by default in the same directory as redis.conf
3. New features of redis7
Four. The use of redis
The use of redis we are talking about here is the use of Linux environment
4.1 Redis installation
1. The installation of redis requires a 64-bit centos operating environment, so use the following instructions to test our centos environment
The result of getconf LONG_BIT returns 32 to represent 32 bits, and returns 64 to represent 64 bits
2. Redis needs to install a compilation environment that depends on gcc. We use the following instructions to test:
gcc -v View gcc version
yum -y install gcc-c++ install c++ library environment
.3. Drag the previously downloaded redis-7.0.11.tar.gz (of course you may download other versions) directly into the /opt folder in Linux (usually put into the /opt folder, this folder is a custom folder) yes, then unzip the folder
tar -zxvf redis-7.0.11.tar.gz /opt directory decompression
After decompressing the folder, a file directory such as redis-7.0.11 will be generated:
Enter the file directory after decompression:
cd redis-7.0.11
install redis
make && make install
Enter the default installation directory and view the files related to redis
cd /usr/local/bin #Enter the default folder of the installation
ll #View the files and directories in the current directory
The following redis-related file results can be observed:
4. Switch back to the redis-7.0.11 file directory, create the myredis file directory, and copy the redis.conf file in the redis-7.0.11 file directory to the myredis file directory (because we want Modify the configuration file, so it is best to save the original configuration file)
mkdir /myredis #Create the file directory of myredis in the redis-7.0.11 directory
cp redis.conf /myredis/redis7.conf #Copy the default configuration file
5. Modify the copied configuration file: use the /+ keyword
in the bottom line mode to search for the specific content of the configuration file, and modify the content after locking the specific content:
Example:
vim /myredis/redis7.conf
// linux search word esc + / search word + enter in vim
6. Modify the redis.conf configuration file, make sure it takes effect after the modification, and remember to restart the redis server
1. Change the default daemonize no to daemonize yes
2. Change the default protected-mode yes to protected-mode no
3. Change the default bind 127.0.0.1 to directly comment out (the default bind 127.0.0.1 can only be accessed locally) or change the cost Machine IP address, otherwise it will affect the remote IP connection
4. Add the redis password to requirepass to the password you set yourself
7. Start the redis server and client
redis-server /root/opt/redis-7.0.11/myredis/redis7.conf #start service
Why start using it from the root directory? Because if we start using it from the current directory, it may report an error that the file or directory cannot be found (pro-test)
ps -ef | grep redis | grep -v grep #Test whether the startup is successful
Test Results:
8. Connection service
redis-cli -a set password -p 6379
ping test to get pongExecuting redis on the first line has a warning warning. The following instructions can not display warnings
password set by redis-cli -a -p 6379 2>/dev/null
4.2 Use of redis (forever helloworld)
Enter the following code:
set k1 helloWorld
OK
get k1
"helloWorld"
4.3 Shutdown of redis
close service
In Linux redis: shutdown Direct shutdown
of a single instance Remote shutdown: redis-cli -a password shutdown
Multi-instance remote shutdown, specified port number shutdown: redis-cli -p 6379 shutdown
4.4 uninstallation of redis
uninstall redis
ls -l /usr/local/bin/redis-* View all redis files
rm -rf /usr/local/bin/redis-* delete all files