What is Redis?
Redis (Remote Dictionary Service), remote dictionary service
It is an open source database C language that supports network, memory-based and persistent, and provides APIs in multiple languages
Redis is free and open source. It is one of the most popular Nosql technologies. It is also known as a structured database.
There are so many languages supported by Redis
Redis will periodically write updated data to disk or write modification operations to appended record files, and implement master-slave (master-slave synchronization) on this basis.
What can Redis do?
Redis can do a lot of things, let's list some:
- In-memory storage, persistence. Data is stored in memory. If the service crashes or the server goes down, the data in the memory will be lost. Persistence is very important. The persistence in Redis includes RDB, AOF
- Can be used for caching, Redis is very efficient
- Do publish-subscribe system
- Do map information analysis
- Do counters, timers, etc.
What are the features of Redis
- Diverse data types
- Persistence
- cluster
- Transaction processing
Materials needed to learn Redis
- Official website: https://redis.io/
- Chinese website: http://www.redis.cn/
- download link:
How to install Redis
Install Redis
1. Download the Windows Redis installation package on the official website: https://github.com/tporadowski/redis/releases
2. Unzip the installation package
Unzip the zip installation package Redis-x64-5.0.10.zip, this compressed package now has 14 M
- redis-server
redis server
- redis-client
redis client
- redis-check-aof and redis-check-rdb
Persistence tool for redis
- redis-benchmark
Detection tool for redis
3. Open redis-server and enter the server, you can see the following interface
4. Open the client redis-cli, you can try to enter the ping
command, and the result is PONG
indicating that the connection is successful
Tried set key
and get key
, no problem
Here is just a brief introduction to the installation and use of redis under Windows. In fact, most of the time we still operate under Linux, and the official website of Redis also strongly recommends that we use it under Linux.
Install Redis
1. Download the installation package from the official website: https://redis.io/
redis-6.2.5.tar.gz and put it in your own linux environment
2. Unzip, tar xvf redis-6.2.5.tar.gz
You can also extract it to the specified directory, such as tar xvf redis-6.2.5.tar.gz -C /usr/local/redis
3. Enter redis-6.2.5, compile the source code and execute make && make install
4. Set background startup
Modify redis.conf in the directory
5. Start redis-server and start redis-cli to connect to the server
root@iZuf66y3tuzn4wp3h02t7pZ:/# redis-server /usr/local/redis/redis-6.2.5/redis.conf
root@iZuf66y3tuzn4wp3h02t7pZ:/# redis-cli -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379>hmset config name xiaomotong age 18
127.0.0.1:6379> hgetall config
1) "name"
2) "xiaomotong"
3) "age"
4) "18"
nice , the installation is ok, and the setting value is also ok
7. Check whether the reids-server process exists
ps aux |grep redis
Check if the process exists
8. Close the redis service
127.0.0.1:6379> shutdown
not connected>
not connected>
root@iZuf66y3tuzn4wp3h02t7pZ:/# ps aux |grep redis
root 15903 0.0 0.0 14436 1012 pts/0 S+ 22:38 0:00 grep --color=auto redis
How to test redis performance
Use redis-benchmark to test redis performance, the following parameters can be used
Options | describe | Defaults |
---|---|---|
-h | Specify the server hostname | 127.0.0.1 |
-p | Specify the server port | 6379 |
-s | Specify the server socket | |
-c | Specify the number of concurrent connections | 50 |
-n | Specify the number of requests | 10000 |
-d | Specifies the data size of the SET/GET value in bytes | 3 |
-k | 1=keep alive 0=reconnect | 1 |
-r | SET/GET/INCR uses random keys, SADD uses random values | |
-P | Pipe <numreq> requests | 1 |
-q | Force quit redis. Only show query/sec values | |
--csv | Output in CSV format | |
*-l* (lowercase letter of L) | Generate loops that execute tests permanently | |
-t | Run only a comma-separated list of test commands. | |
*-I* (capital letter of i) | Idle mode. Only open N idle connections and wait. |
started testing
# redis-benchmark -h localhost -p 6379 -c 1000 -n 1000000
You can see the following print information
26.27 seconds for one million requests
1000 clients concurrently
Specify the set data size in bytes of 3 bytes
From the above figure, we can see that the performance of this redis test can handle 38066.23 requests per second, which is quite powerful for single-machine redis
redis basics
Redis has 16 databases by default, we can view the definitions in the redis.conf file
# vim /usr/local/redis/redis-6.2.5/redis.conf
select
uses the 0th database by default, we can use the 061deed026cba8 command to switch the database
select
You can switch the database select
127.0.0.1:6379> select 3
OK
127.0.0.1:6379[3]> DBSIZE
(integer) 0
127.0.0.1:6379[3]> select 0
OK
127.0.0.1:6379> dbsize
(integer) 5
It can be seen that database No. 0 has data, and database No. 3 has no data yet.
keys
You can keys *
command. Generally, this command is rarely used, because when the amount of data is large, using this command will be very slow.
127.0.0.1:6379> keys *
1) "config"
2) "key:__rand_int__"
3) "counter:__rand_int__"
4) "myhash"
5) "name"
flushdb
Clear the key and value of the current database
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> keys *
(empty array)
flushall
Clear all database keys and values
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> dbsize
(integer) 0
127.0.0.1:6379[2]> set name xiaomotong
OK
127.0.0.1:6379[2]> select 0
OK
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> select 2
OK
127.0.0.1:6379[2]> dbsize
(integer) 0
So why is the default port number of redis 6379?
Because the author of redis is a fan of female star MERZ, MERZ corresponds to 6379 on the 9-square mobile phone keyboard
redis is single threaded
Redis is single-threaded, but it does not affect his speed. Officials say that redis is based on memory operations. CPU is not the bottleneck of redis, but machine memory and network bandwidth are the bottlenecks of redis.
Redis is written in C language, and the official data provided by redis is 100,000+ QPS
Why is redis single thread so fast?
1. Does a high-performance server have to be multi-threaded? This is a misunderstanding, single thread can be very fast
2. Is the efficiency of multi-threading necessarily higher than that of single-threading? This is also a misunderstanding, multi-threading will have CPU context switching, which is very time-consuming
Efficiency Comparison : CPU > Memory > Hard Disk
redis core:
Redis puts all data in memory, so single-threaded operation of data will be very fast and efficient, while multi-threading will cause CPU context switching. For memory, the efficiency of the system without context switching is the highest of
Welcome to like, follow, favorite
Friends, your support and encouragement are the motivation for me to persist in sharing and improve quality
Okay, here it is this time
Technology is open, and our mentality should be open. Embrace change, live in the sun, and move forward.
I'm little devil boy Nezha , welcome to like, follow and collect, see you next time~
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。