当前位置:网站首页>A complete tutorial for getting started with redis: getting to know redis for the first time

A complete tutorial for getting started with redis: getting to know redis for the first time

2022-07-04 22:59:00 Gu Ge academic

Redis [1] It's based on key value pairs (key-value) Of NoSQL database , With many key value pairs
The difference between databases is ,Redis The value in can be from string( character string )、hash( Hash )、
list( list )、set( aggregate )、zset( Ordered set )、Bitmaps( Bitmap )、
HyperLogLog、GEO( Location of geographic information ) And other data structures and algorithms , therefore
Redis It can satisfy many application scenarios , And because Redis All data will be stored in memory
in , So its read-write performance is amazing . More Than This ,Redis It can also store the data in memory
Save to hard disk in the form of snapshot and log , In this way, in the event of similar power failure or machine failure
Hou , Data in memory does not “ The loss of ”. In addition to the above functions ,Redis Keying is also provided
period 、 Publish subscribe 、 Business 、 Assembly line 、Lua Additional functions such as scripts . All in all , If in the right
Use the scene well Redis, It will be as invincible as a Swiss Army knife .
2008 year ,Redis The author of Salvatore Sanfilippo [2] In the development of a LLOOGG Net of
When standing , We need to implement a high-performance queue function , It started with MySQL To achieve ,
But then it turns out that no matter how optimized SQL Statement can not improve the performance of the website , Plus from
Be shy in your own pocket , So he decided to make one for himself LLOOGG The database of , This is
Redis The forerunner of . later ,Salvatore Sanfilippo take Redis1.0 Open source to GitHub [3]
On , Maybe he didn't even think of ,Redis So popular later .
If someone asks now Redis Who is the author of Redis, I think he can joke
Answer : Who else doesn't use Redis, Of course, it's just a joke , But from Redis The official company of
The statistic , Many heavyweight companies are using Redis, Such as foreign Twitter、
Instagram、Stack Overflow、GitHub etc. , There are more in China , If only from the volume
meter , Sina Weibo is arguably the largest in the world Redis Users , Besides Sina Weibo , And like ah

Ribaba 、 tencent 、 Baidu 、 sohu 、 Youku tudou 、 Meituan 、 millet 、 Vipshop and other companies are
Redis Users of . besides , Many open source technologies like ELK Wait until you have Redis As their group
An important part of the piece , and Redis The module system will be provided in future versions for third-party personnel
Realize function expansion , Give Way Redis To exert greater power . therefore , You can say that , Skillfully use
And operation and maintenance Redis It has become a necessary skill for development and operation personnel .
[1] http://redis.io
[2] http://antirez.com
[3] https://github.com/antirez/redis

1.2 Redis characteristic
Redis The reason why it is favored by so many companies , There must be something extraordinary , Here is about
Redis Of 8 There are two important characteristics .
1. Fast
Under normal circumstances ,Redis The execution of orders is very fast , The official figures are for reading and writing
Can achieve 10 ten thousand / second , Of course, it also depends on the performance of the machine , But let's not talk about machinability here
The difference in ability , Just analyze what makes Redis In addition, the speed , It can be roughly attributed to
Take the following four points :
·Redis All data is stored in memory , surface 1-1 It's Google 2009 Given in
Hardware execution speed at all levels , So putting data in memory is Redis The main reason for speed
because .
·Redis Yes, it is C The realization of language , Generally speaking C Language implementation of the program “ distance ” The operating system is more
near , The execution will be relatively faster .
·Redis A single threaded architecture is used , Prevent the possible competition problems caused by multi-threading .
· The author for Redis The source code can be said to be fine grinding , There has been comment Redis Is one of the few
Open source code that combines performance and elegance

Scale 1-1 The execution speed of hardware at all levels given by Google ​​​​​​ topic

 

2. Data structure server based on key value pairs
Almost all programming languages provide dictionary like functions , for example Java Inside map、
Python Inside dict, Similar to this way of organizing data is called key value based method , With a lot of
The key value is different from the database ,Redis The value in can be more than a string , And it can also be a
Body data structure , This can not only facilitate the development of many application scenarios , At the same time, it can also improve the opening
Hair efficiency .Redis The full name is REmote Dictionary Server, It mainly provides 5 It's a data node
structure : character string 、 Hash 、 list 、 aggregate 、 Ordered set , At the same time, it evolves on the basis of string
Bitmap out (Bitmaps) and HyperLogLog Two magical “ data structure ”, And with
LBS(Location Based Service, Location based services ) Continuous development ,Redis3.2 In the version
Add information about GEO( Location of geographic information ) The function of , In short, with the help of these data structures , open
The developer can develop various “ interesting ” Application .
3. Rich functions
except 5 Data structures ,Redis There are also many additional features :
· Key expiration is provided , Can be used to implement caching .
· Provides publish subscribe function , Can be used to implement messaging systems .
· Support Lua Script function , You can use Lua Create new Redis command .
· Simple transaction functionality is provided , To a certain extent, transaction characteristics can guarantee .
· It provides an assembly line (Pipeline) function , In this way, the client can send a batch of commands to
Redis, Reduced network overhead .
4. Simple and stable
Redis The simplicity of is mainly reflected in three aspects . First ,Redis The source code of is very few , Early versions
The code for is 2 About ten thousand lines ,3.0 After version, due to the addition of cluster feature , The code increases to 5 Line ten thousand
about , Compared with a lot of NoSQL For the database, the amount of code is relatively much less , That means ordinary
Our development and operation and maintenance personnel can “ Read and ” it . secondly ,Redis Using the single threaded model , such
Not only makes Redis The server-side processing model becomes simple , And it also makes client development simpler
single . Last ,Redis No need to rely on class libraries in the operating system ( for example Memcache Need to rely on
libevent Such a system class library ),Redis I realized the related functions of event processing .
Redis Although it's very simple , But that doesn't mean it's unstable . With thousands of... Maintained by the author Redis by
example , Not because Redis Oneself bug And the downtime .
5. Many client languages
Redis Simple is provided TCP Communication protocol , Many programming languages can easily access
Redis, And because of Redis Widely recognized by the community and major companies , So support Redis My guest
There are also many client languages , Almost covers the mainstream programming languages , for example Java、PHP、
Python、C、C++、Nodejs etc. [1] , The first 4 In this chapter, we will discuss Redis The client of   bright .
6. Persistence
Usually , It's not safe to put data in memory , In case of power failure or machine failure ,
Important data may be lost , therefore Redis There are two ways to persist :RDB and
AOF, That is, two strategies can be used to save the data in memory to the hard disk ( Pictured 1-1 Shown ), this
This ensures the persistence of data , The first 5 In this chapter, we will discuss Redis The persistence of .

7. Master slave copy
Redis Provides copy function , Implement multiple data of the same Redis copy ( Pictured 1-2 the
in ), The replication function is distributed Redis The basis of . The first 6 In this chapter, we will discuss Redis Details of replication
Detailed description .

 

 8. Highly available and distributed
Redis from 2.8 Version officially provides a high availability implementation Redis Sentinel, It can guarantee Redis
Node fault detection and automatic fault transfer .Redis from 3.0 The version officially provides a distributed implementation
Redis Cluster, It is Redis True distributed implementation , Provides high availability 、 Reading, writing and capacity
Extensibility .

原网站

版权声明
本文为[Gu Ge academic]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042229264371.html