编程知识 cdmana.com

"Attack redis" 1. First meeting redis


  Vaguely remember that year, I was just a young man in the industry 、 At that time, the hair was still so thick 、 There was no check inch shirt at that time ..., Think of the thick hair that used to be , The corner of the eye is always full of tears ( Stop fighting , Stop fighting , I am writing ).
A few days after I was on the job , My good brother, who has a few hairs on his head, came to me and said , Here's an old project , Get familiar with , We will start to develop new requirements tomorrow . No room for me to refute , What can I do , That's to say , Meng Xin San Lian ( well 、 well 、 well ), It's like the way my former leader is in front of the leader ( Hope not to be seen by him ).
Open the project with tears , One by one, new technologies are coming , ah , I vomited. . But when I face Baidu input Redis When the , I was shocked , From then on, I went on the road of losing my hair without looking back .
Backhand is a Antirez(Redis author )yyds.

First time to know

 Redis It's based on Key value pair (key-value) Of NoSQL database , Unlike many key value pair databases ,Redis The value in can be string( character string )、hash( Hash )、list( list )、set( aggregate )、zset( Ordered set )、Bitmaps( Bitmap )、HyperLogLog( Base Statistics )、GEO( Location of geographic information ) And other data structures and algorithms , therefore Redis It can satisfy many application scenarios .
And because Redis All data operations are in memory , So its read-write performance is amazing . More Than This ,Redis The data in memory can also be saved to the hard disk in the form of snapshot and log , So in the event of a similar power failure or machine failure , Data in memory does not “ The loss of ”. In addition to the above functions ,Redis Key expiration is also provided 、 Publish subscribe 、 Business 、 Assembly line 、Lua Additional functions such as scripts .
 Is it

Little story

  It's my favorite part again .2008 year ,Redis The author of antirez(Salvatore Sanfilippo) In the development of a LLOOGG The website of , 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 I'm not rich enough , 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 On , Maybe he didn't even think of ,Redis So popular later .

Related links

Official website
The author blog
Source code address


1. Fast reading and writing

  The official figure is that the read-write performance can reach 10 ten thousand / second , Of course, it also depends on the configuration of the server .
Redis The reason for high read-write performance :
1). Based on memory operation .
2). use C Language implementation ,C Language implementation of the program “ distance ” The operating system is closer , The execution will be relatively faster .
3). Using single thread plus I/O Multiplexing model , It avoids the concurrency competition in multithreading environment .
4). The author's own strict polishing of the source code . The amount of code is very small compared with other databases .

2. Store based on key value pairs

  according to key-value The mapping relation generation of a dictionary is similar to the function of a dictionary , Such as Java( The best language in the world , Not one of them. ) Medium Map.
and Redis The value in can be more than a string , It can also be a specific data structure or algorithm

3. Diversified functions

  In addition to providing commonly used 5 Basic data type in , It also provides the following functions :
1). Key expired , Can be used to implement caching 、 Distributed locks, etc .
2). Provides basic publish and subscribe capabilities , Can be used to implement messaging systems , But the general system will choose to use rabbitMQ Specialized message middleware .
3). Support Lua Script function , image Redisson The bottom layer of the distributed lock framework is Lua Script to achieve .
4). Support persistence ,Redis It's memory based , So it provides AOF and RDB There are two ways to persist .
5). Support high availability ,Redis Cluster mode provides master-slave replication 、 Sentinel mode and cluster Pattern , Of course, the master-slave and the sentry are usually used together .
6). wait ( The dog's head protects his face ).... The follow-up will come one by one , Good brother, don't worry

Use scenarios

1. What can be done

1). cache , This feature is the main reason most systems use it , You can cache hotspot data , Sharing database pressure .
2). Ranking system , Provide lists and ordered collections , We can reasonably use this data structure to achieve ranking .
3). Counter application , Like counting traffic or playing ,Redis Natural support counting function, and counting performance is also very good .
4). Social networks , Fabulous / Step on 、 fans 、 Common friends / like 、 push 、 Pull down and refresh are necessary functions of social networking sites ,Redis The data structure provided can realize these functions relatively easily .
5). Message queuing system , be based on Redis Publish subscribe , It can realize the basic message queue function , However, it is recommended to use specialized message oriented middleware .

2. Nothing to do

 Redis It's based on pure memory operation , Although the memory is cheap enough now , But when the amount of data is large , Use redis The economic cost of storage will be great . in addition , If it is based on master-slave replication for high availability , The master database is copied from the slave database , It means that the amount of data that can be stored is limited .
The second thing is , In hot and cold data ( visit ) Speak up , Storing hot data can improve the throughput of the system , And if you store cold data, it's basically a waste of memory .


  stay 2020 The last month of the year , The idea of blogging came into being . After a series of struggles, I decided to keep writing .
On the one hand, in the process of writing a blog, you can find your own blind spot for a certain knowledge point , Deepen the understanding of technical points , Improve the depth of technical capability .
Next, learn 、 It's totally different to understand a technical point and feed it back through word output , Can greatly improve my language skills .
Finally, through sharing, I can communicate with many giants ( Later, I called my brother ) Make a collision ( thinking , The body can't beat ), Every time after the collision, there is always a rush , One word is wonderful .
And then I'll share it later Redis This piece of stuff , Basically, it's from 0 To 1 such , To exaggerate, it's from entry to mastery ( Yeah, yeah , Don't make faces , Don't make faces ), I hope you like it .
Last, last , I'm an entertainment blogger , Welcome to Collision ( Meat bomb impact ), To rush ......

That's all for this issue , If there is something wrong, please leave a message in the comments area , in addition $\color{#FF0000}{ Please pay attention to 、 Please thumb up }$

Next :Redis Installation and configuration


Scroll to Top