当前位置:网站首页>Redis learning - 01 introduction, installation and configuration
Redis learning - 01 introduction, installation and configuration
2022-06-26 12:35:00 【Preserved egg is very white】
Redis brief introduction
Redis The origin of
2008 year , A start-up in Italy Merzia Launched a model based on MySQL Web site real time statistics system based on LLOOGG, But it didn't take long , The founder of the company Salvatore Sanfilippo Then the MySQL Disappointed with the performance of , So he decided to do it himself LLOOGG Customize a database , And in 2009 Development completed in , This database is Redis.
however Salvatore Sanfilippo It's not enough to just Redis be used for LLOOGG This product , But I want more people to use it , So in the same year Salvatore Sanfilippo take Redis Open source release , And start and Redis Another major code contributor to Pieter Noordhuis Go on together Redis Development of , Until today, .
Salvatore Sanfilippo I didn't think of , In just a few years ,Redis We have a huge user base . At home, such as BAT、 Sina weibo 、 Knowledge and so on , Abroad is like GitHub、Stack Overflow、Flickr Are all Redis Users of .
Development timeline :
VMware Company from 2010 Started sponsoring Redis Development of ,Salvatore Sanfilippo and Pieter Noordhuis Also in the same year 3 The month and 5 Month join VMware, Full time development Redis
and 2013 year 5 Month to 2015 year 6 During the month , It was developed by Pivotal sponsorship
2015 year 6 month ,Redis Labs Sponsor for further development until now
Redis Code is hosted in GitHub On (https://github.com/redis/redis), Development is very active .
What is? Redis
The following is the official document explanation .
Redis It's a use ANSI C Write open source 、 Support network 、 Memory based 、 Optional persistent key value pair to store database .
It can meet the storage requirements of different scenarios by providing a variety of key data types , for example :
- string - character string
- hash - Hash
- list - list
- set - aggregate
- Sort set with range query
- bitmap - Bitmap
- hyperlogolog - Super log
- geospatial index - Geospatial index
- stream - flow
Redis With built-in replication function , Parsing performed Lua Script ,LRU Cache control , Transactions and different levels of disk persistence , And pass Redist Sentinel and Redis Cluster Automatic partitioning provides high availability .
Redis Storage structure of
There is a data structure in most programming languages : Dictionaries , For example code dict["key"] = "value"
in :
dict
Is a dictionary structure variablekey
Is the key namevalue
Is the key value
In the dictionary, we can get or set the key value corresponding to the key name , You can also delete a key .
Redis yes REmote DIctionary Server( Remote dictionary server ) Abbreviation , It stores data in a dictionary structure , And allow other applications to pass through TCP The protocol reads and writes the contents of the dictionary .
Redis The key value in the dictionary can be a string , It can also be other data types . The more common ones are :
type | explain |
---|---|
Strings | character string |
Hashes | Hash / hash , Is the content that consists of fields associated with values . Fields and values are strings . This is related to Ruby or Python Hashes are very similar . Be similar to JavaScript Object structure in . |
Lists | list , A collection of string elements sorted by insertion order . They are basically linked lists . |
Sets | A collection of unordered string elements , The data in the set is not duplicated |
Sorted sets | And Sets similar , But each string element is associated with a floating-point value called a fraction . Elements are always sorted by their scores , Therefore, Sets Different , You can retrieve a series of elements ( for example , You may ask : Before you give it to me 10 First or last 10 name ) |
Memory storage and persistence
Redis All data in the database is stored in memory . Relative to disk , Memory data read / Write much faster , So we usually use Redis Do cache database , On an ordinary computer ,Redis Can read and write more than... In a second 10 Ten thousand key values .
Redis The performance test on the official website shows that , stay Linux 2.6、Xeon X3320 2.5 GHz Server ,50 In the case of concurrent requests 100000 Time ,SET Operation can reach 110000 Time /s,GET Operation can reach 81000 Time /s
There is also a problem storing data in memory , For example, the data in the memory will be lost after the program exits . however Redis Provides support for persistence , That is, the data in memory can be asynchronously written to the hard disk , At the same time, it does not affect the continued service .
Rich in functions
Redis Although it was developed as a database , But because it provides a wealth of functions , More and more people use it as a cache 、 Queue system, etc .
1、 As a caching system
Redis You can set the lifetime for each key , When the lifetime expires, it will be deleted automatically . This feature, combined with excellent performance, makes Redis Can be used as a cache . As a caching system ,Redis It can also limit the maximum space occupied by data , After the data reaches the space limit, the unnecessary keys can be eliminated automatically according to certain rules .
2、 As a queue system
besides ,Redis The list type key of can be used to implement the queue , It also supports blocking reading , It is easy to implement a high-performance priority queue .
3、“ Release / subscribe ” function
At the same time on a higher level ,Redis And support “ Release / subscribe ” Message mode for , You can build chat rooms and other systems based on this .
Simple and stable
Even if the function is rich , It is difficult to attract people if it is too complex to use .Redis The intuitive storage structure makes it possible to Redis The interaction is very simple .
stay Redis Use command to read and write data , A command statement is to Redis Equivalent to SQL Language for relational databases . For example, in a relational database, you want to get posts In table id by 1 The record of title Fields can be used as follows SQL Statements for :
SELECT title FROM posts WHERE id=1 LIMIT 1
Corresponding , stay Redis The name of the key to be read in is post: 1 Hash type of key title Value of field , You can use the following statement to implement :
HGET post:1 title
among ,HGET It's an order ,post:1
Is the key name ,title
Is the data field to be read .
Redis Provides 250 Multiple orders , It sounds a lot , But there are only a few dozen commonly used , And every command is easy to remember .Redis The list of commands :Command reference – Redis
Redis Provides clients in dozens of different programming languages (https://redis.io/clients), These libraries are well encapsulated Redis The order of , Make in a program with Redis It's easier to interact .
Redis Application scenarios
Redis It's a Key-Value The storage system , In most cases, it is because of its high-performance characteristics , Used as a cache , Here's an introduction Redis Frequently encountered usage scenarios .
The use scenario of a product must be based on the characteristics of the product , Let's start with a list of Redis Characteristics :
- Excellent reading and writing performance
- Persistence
- Rich data types
- Single thread
- Data automatically expires
- Publish subscribe
- Distributed
Here we go through several scenarios , Different dimensions Redis Application .
1、 Cache system
Caching is now used by almost all large and medium-sized websites , The proper use of caching can not only improve the speed of website access , It also greatly reduces database stress .Redis Key expiration is provided , It also provides a flexible key elimination strategy , therefore , Now? Redis It's used a lot for caching .
2、 Ranking List
Many websites have leaderboard applications , Such as Jingdong's monthly sales list 、 The new ranking of goods by time, etc .Redis The provided ordered collection data class can realize all kinds of complex leaderboard applications .
3、 Counter
What is a counter , For example, the number of visits to products on e-commerce websites 、 The number of videos played on video websites, etc . In order to ensure that the data is effective , Every time you browse, you have to give +1, When the amount of concurrency is high, it is undoubtedly a challenge and pressure to request database operations every time .Redis Provided incr To implement the counter function , Memory operations , Very good performance , Great for these counting scenarios .
4、 Distributed session
Cluster mode , In the case of few applications, it is generally used that the container comes with session Copy function can satisfy , As applications increase, in relatively complex systems , It's usually built to Redis And so on session service ,session No longer managed by containers , But by the session Service and memory database management .
5、 Distributed lock
Distributed technology is used in many Internet companies , The technical challenge of distributed technology is concurrent access to the same resource , Such as the overall situation ID、 Reduce inventory 、 Second kill and other scenes , The pessimistic lock of the database can be used in the scenario with small amount of concurrency 、 Optimistic lock to achieve , But in the case of high concurrency , It is not ideal to use database lock to control concurrent access of resources , Greatly affected the performance of the database . You can use Redis Of setnx Function to write distributed locks , If set, return to 1 Indicates that the lock is obtained successfully , Otherwise, the lock acquisition fails , There are more details to consider in practical application .
6、 Social networks
give the thumbs-up 、 Step on 、 Focus on / Be focus on 、 Mutual friends and so on are the basic functions of social networking sites , Social networking sites tend to get a lot of traffic , And traditional relational database types are not suitable for storing this type of data ,Redis Provided hash 、 Data structures such as collections can easily implement these functions .
7、 Latest list
Redis List structure ,LPUSH You can insert a content in the head of the list ID As a keyword ,LTRIM Can be used to limit the number of lists , So the list is always N individual ID, No need to query the latest list , Direct basis ID Go to the corresponding content page .
8、 The messaging system
Message queue is a necessary middleware for large websites , Such as ActiveMQ、RabbitMQ、Kafka And other popular Message Queuing Middleware , It is mainly used for business decoupling 、 Peak shaving and asynchronous processing of low real-time services .Redis Provides a release / Subscription and blocking queue function , Can realize a simple message queue system . in addition , This can't be compared with professional message middleware .
Example : Secshahe Redis The combination of
Seckill is a common marketing mode in the Internet system , As a developer , In fact, I don't like such activities , Because non-technical personnel can not understand the technical difficulty , As a result, there are always some deviations in resource coordination . In fact, the common problems of secsha include :
- Too high concurrency leads to program blocking .
- Inventory cannot be effectively controlled , Oversold occurs .
In fact, there are basically two solutions to these problems :
- Try to cache the data , Block the direct interaction between users and the database .
- Lock to avoid oversold .
Now let's explain , If we do a second kill now , that ,Redis How they should be used together ?
- Preheat data in advance , Put in Redis
- Product list put Redis List
- Detailed data of goods Redis Hash preservation , Set expiration time
- Inventory data of goods Redis Sorted Set preservation
- User's address information Redis Set preservation
- The order is generated by deducting inventory Redis Manufacturing distributed locks , Inventory synchronous deduction
- Shipment data after order generation , produce Redis List, Processing through message queuing
- After the second kill , And then Redis Data and database synchronization
The above is a brief second kill system and Redis A combined solution , Of course, it may be introduced in practice HTTP cache , Or use message docking MQ Alternatives, etc , There will also be business omissions , This is just a hope that it can attract jade .
Related resources
- Official website :https://redis.io/
- GitHub Warehouse :https://github.com/redis/redis
- Interactive learning Redis( Set up Redis learning environment ):https://try.redis.io/
- Redis Chinese net ( unofficial ):http://www.redis.cn/
- Redis Command Reference :http://doc.redisfans.com/
Redis install
About Redis Version of
Redis Learn from it Linux The naming rules of operating system for version number :
- If the second digit of version number is an odd number , It's an unstable version (Unstable), for example 2.7、2.9、3.1
- If the second digit of the version number is even , The stable version (Stable), for example 2.6、3.0、3.2
The current odd version is the next stable development version , for example 2.9 The version is 3.0 Development version of version .
Even versions of... Are usually selected in the production environment Redis, The odd version is used to learn and experience some new features in advance .
obtain Redis The way
obtain Redis There are many ways :
- Install on your own computer
- Install on virtual machine
- Install to the remote server
- from Docker Hub obtain Redis Of Docker Mirror image
- …
stay macOS Install in Redis
stay macOS There are two ways :
- Mode one : Compilation and installation , Same as Linux
- Mode two ( recommend ): Use Homebrew install
macOS Software package management tools under the system Homebrew A newer version of Redis package , You can use it directly to install Redis, There is no need for Linux The trouble of manual compilation on .
1、 install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2、 adopt Homebrew install Redis
brew install redis
stay Windows Install in Redis
Redis The government does not support Windows.
2011 Microsoft direction Redis Submitted a patch , In order to make Redis Can be in Windows Under the compiler run . But it was rejected by the author , The reason is in the server field Linux It has been widely used , Give Way Redis Can be in Windows It is not so important to run under . also Redis There are many operating system related features , compatible Windows It will consume too much energy and affect Redis Development of .
For all that , Microsoft has released a software that can be used in Windows Under the Redis edition , But this project is no longer maintained .
If you really want to Windows Learn to use Redis, You can try Memurai, It's a Redis for Windows substitute , Its core is based on Redis Source code and fully compatible Redis, But the project has not been officially recognized by Microsoft .
stay Linux Install in Redis
Use the following command to download 、 Extract and compile Redis:
# download Redis Source compression package
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
# Or use this address to download , This address always points to the latest stable version
# wget http://download.redis.io/redis-stable.tar.gz
# Decompress the package
tar xzf redis-6.2.6.tar.gz
# Get into Redis Source directory
cd redis-6.2.6
# Compilation and installation
make
Now the compiled binary executable can be src
Found in the directory . Execute the following file to run Redis:
# Run with full path Redis
src/redis-server
# Ctrl+C stop it Redis, The following describes the stop command
If you want to use it directly Redis Command to run Redis The server , You need to copy the executable to a directory in the environment variable , You do not need to add its full path to execute the executable file located in this directory .
This command can be copied manually or executed directly in the source directory :
make install
This order will Redis Binary executable copied to /usr/local/bin
Directory , PATH Environment variables are configured by default /usr/local/bin
Catalog .
# Direct operation Redis
redis-server
Redis function 、 Stop and connect
function Redis
The compiled Redis Source directory src
There will be the following executable files in the folder :
Executable file | explain |
---|---|
redis-server | Redis The server |
redis-cli | And Redis Interactive command line interface client tools |
redis-benchmark | Redis Performance testing tools |
redis-check-aof | AOF File repair tool |
redis-check-rdb | RDB File checking tool (3.0 And earlier redis-check-dump) |
redis-sentinel | Sentry mode tool ( Monitoring and failover ) |
in addition , Install by compiling the source code , It will also generate a in the root directory of the source code
redis.conf
Configuration file for .
The most common of these executables is redis-server
and redis-cli
.
Direct operation redis-server
You can start Redis The server :
redis-server
Redis By default 6379
port , Can pass --port
Parameter specifies the boot port :
redis-server --port 1234
Direct operation redis-server
It will run at the front desk Redis service , Occupy the command line window .
have access to --daemonize
Parameter and set to yes
, To run on the back end Redis:
redis-server --daemonize yes
see Redis Running state :
# View the current contents redis The process of
# If you can see it redis-server The process of , Express Redis The server is running
ps -ef | grep -i redis
stop it Redis
in consideration of Redis It is possible that data in memory is being synchronized to the hard disk , force finish Redis process ( for example Ctrl + C) May cause data loss .
So stop correctly Redis The way is to Redis send out SHUTDOWN command :
# Be careful : It's used here redis-cli instead of redis-server
redis-cli shutdown
When Redis perform SHUTDOWN After the command , Disconnect all clients first , Then perform persistence according to the configuration , Finally, complete the launch .
Redis Can be handled properly SIGTERM The signal , So use kill Redis Process PID It can also end normally Redis, Effect and delivery SHUTDOWN command :
# Stop by process number Redis
kill -9 280697
Can pass ps -ef | grep -i redis
Check to see if there are any after the stop redis-server process .
Connect Redis
redis-cli yes Redis Built in command line based Redis Client tools , It is also learning and testing Redis An important tool for .
function redis-cli
You can connect to the database :
redis-cli
Connect by default 127.0.0.1:6379
The database of , You can also specify the server address and port :
redis-cli -h 127.0.0.1 -p 1234
Under normal circumstances , At this time, it is connected Redis database , You can execute the following test command :
# PING If the command outputs PONG Indicates that the connection is normal
127.0.0.1:6379> PING
PONG
# towards Redis Store a key by foo, The value is bar The data of
127.0.0.1:6379> SET foo bar
OK
# Reading data
127.0.0.1:6379> GET foo
"bar"
# Query all data key
127.0.0.1:6379> KEYS *
1) "foo"
disconnect :
- command :
quit
- Shortcut key :
Ctrl + C
Redis To configure
As described above, you can use redis-server Start parameter of --port
Set up Redis The port number of the service , besides Redis Other configuration options are also supported , For example, whether to enable persistence 、 Log level, etc .
There are several ways to Redis To configure :
- Command line arguments
- The configuration file
- Change while the server is running Redis To configure
Passing parameters through the command line
The simplest way is to start redis-server Directly pass the command parameters :
redis-server --port 6379 --host 127.0.0.1
The configuration file
There are many options that can be configured , It is not convenient to set these options through startup parameters , therefore Redis It supports setting these options through the configuration file .
Redis Provides a configuration file modular redis.conf, Located in the root path of the source directory .
This file is just a template for a configuration file , The way to make the configuration effective is to pass the path of the configuration file as a startup parameter to redis-server:
redis-server < Profile path >
Passing a configuration option with the same name through the startup parameter will overwrite the corresponding parameter in the configuration file , for example :
redis-server < Configure as read path > --port 3000
It is recommended to put the configuration file in a unified directory ( for example
/etc/redis
), Named after port number ( for example6379.conf
).Copy the configuration file module to the target file :
cp <redis Source code root >/redis.conf <redis Profile directory >/6379.conf
Change while the server is running Redis To configure
You can also do it in Redis Run through redis-cli Of CONFIG SET The command does not restart Redis Dynamic modification in the case of part Redis To configure :
CONFIG SET logLevel warning
Be careful : During operation, only part Modify the configuration item .
Again , It can also be used at runtime CONFIG GET Command to get Redis Current configuration :
CONFIG GET logLevel
Redis Multiple databases in
adopt redis-server Start a Redis Service is a Redis example .
One Redis The example provides several dictionaries for storing data , The client can specify which dictionary to store the data in . This is similar to the well-known idea that multiple databases can be created in a relational database , So each dictionary can be understood as a separate database .
Redis The default support 16 A database , They are numbered :0
、1
、2
、…、14
、15
- Reids Custom database names are not supported
- Because each database is named after a number , So developers must be clear about which database stores which data
- Parameters can be configured by
databases
Modify the number of supported databases
Each database is independent , That is to say 0 Data inserted into database No , stay 1 No. database is inaccessible .
The client and Redis Automatically select after the connection is established 0 The database , have access to SELECT
Command to switch the database :
# Connect Redis Default connected 0 The database
redis-cli
127.0.0.1:6379> SET foo bar
OK
127.0.0.1:6379> GET foo
"bar"
127.0.0.1:6379> SELECT 1
OK
# 1 No. database does not store foo
127.0.0.1:6379> GET foo
(nil)
127.0.0.1:6379> KEYS *
(empty array)
# The switched database number exceeds the set value , You're going to report a mistake
127.0.0.1:6379> SELECT 16
(error) ERR DB index is out of range
Redis Setting different access passwords for each database is not supported , So a client either has access to all databases , Or none of them .
Multiple databases are not completely isolated , for example FLUSHALL
The command can empty a Redis Data in all databases in the instance .
in summary , These databases are more like a namespace , and Not suitable for storing data from different applications , For example, it is not suitable to use 0 No. database storage A Application data , While using 1 No. database storage B Application data , This is not recommended !!
Different applications should use different Redis Instance storage data .
because Redis Very light weight , An empty one Redis The memory occupied is only 1 MB about , So don't worry about more than one Redis Instances will take up a lot of extra memory .
边栏推荐
- 2022 China smart bathroom cabinet Market Research and investment Competitiveness Analysis Report
- Installing MySQL under Linux (RPM package installation)
- Nodejs get get/post request parameters
- [solved] data duplication or data loss after laravel paginate() paging
- Refined operation, extending the full life cycle value LTV
- Five trends of member management in 2022
- Which is safer and better for great wisdom to open an account
- 程序员必备,一款让你提高工作效率N倍的神器uTools
- Analysis report on the "fourteenth five year plan" and investment prospect of China's pharmaceutical equipment industry 2022-2028
- [redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
猜你喜欢
Comparison of latest mobile phone processors in 2020 (with mobile phone CPU ladder diagram)
Build Pikachu shooting range and introduction
PHP laravel+gatewayworker completes im instant messaging and file transfer (Chapter 1: basic configuration)
1、 MySQL introduction
老司机总结的12条 SQL 优化方案(非常实用)
环形队列php
栈,后入先出
Vscode solves the problem of Chinese garbled code
PHP uses laravel pay component to quickly access wechat jsapi payment (wechat official account payment)
Installing MySQL under Linux (RPM package installation)
随机推荐
Redis cannot connect to the server through port 6379
Is it safe to open a securities account
What are the top ten securities companies? Is it safe to open a mobile account?
Laravel subdomain accesses different routing files and different modules
Function collapse and expansion shortcut keys in vscode (latest and correct)
redis通过6379端口无法连接服务器
Scala-day02- variables and data types
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
Laravel uses find_ IN_ The set() native MySQL statement accurately queries whether a special string exists in the specified string to solve the problem that like cannot be accurately matched. (resolve
Demand scale forecast and investment competitiveness analysis report of China's new material market 2022-2028
Thinkphp5 query report: sqlstate[hy093]: invalid parameter number
"Pinduoduo and short video speed version", how can I roast!
Is it safe to open a securities account in general
关于NaN的一些总结
Polarismesh series articles - concept series (I)
Hello! Forward proxy!
UDP协议详解[通俗易懂]
Ad - update the modified PCB package to the current PCB
Analysis report on dynamic research and investment planning suggestions of China's laser medical market in 2022
7-1 数的范围