- What is? Redis
Redis By the Italians Salvatore Sanfilippo（ Net name ：antirez） Developed a memory cache database .Redis Its full name is ：Remote Dictionary Server（ Remote data services ）,bai The software uses C Language writing ,Redis It's a key-value The storage system , It supports rich data types , Such as ：string、list、set、zset(sorted set)、hash.
- Redis characteristic
Redis Using memory as data storage medium , So the efficiency of reading and writing data is very high , Far more than databases . To set and get a 256 Byte string as an example , It can read up to 110000 Time /s, Write as fast as 81000 Time /s.
Redis Follow memcache The difference is , Stored in the Redis The data in is persistent , After power failure or restart , And the data won't be lost . because Redis The storage of is divided into memory storage 、 Disk storage and log Three parts of the document , After restart ,Redis Data can be reloaded from disk into memory , These can be configured through configuration files , Because of this ,Redis To achieve persistence .
Redis Support master-slave mode , You can configure clusters , This is more conducive to supporting large projects , This is also Redis A highlight of .
- Redis Application scenarios , What can it do
Many languages support Redis, because Redis Exchange data fast , So it is often used to store some data that needs to be called frequently in the server , This can greatly save the time for the system to read the disk directly to get the data I/O expenses , More importantly, it can greatly improve the speed . Take a large website for example , such as a The front page of the website has 100 Ten thousand people visit , One of them is recommendation news .
Redis The application scenarios include ： Cache system （“ hotspot ” data ： High frequency reading 、 Low frequency writing ）、 Counter 、 Message queuing system 、 Ranking List 、 Social networks and real-time systems .
Application scenarios ：
Let's give a simple example to describe Hash Application scenarios of , For example, we need to store a user information object data , Contains the following information ：
user ID For searching key, Stored value User object contains name , Age , Birthday and other information , If you use ordinary key/value Structure to store , There are mainly the following 2 Storage methods
The first way is to make users ID As a search for key, Encapsulate other information into an object and store it in a serialized way , The disadvantage of this way is , Added serialization / The cost of deserialization , And when you need to modify one of the messages , You need to get the whole object back , And the modification operation needs to protect the concurrency , introduce CAS And so on .
The second method is to store as many members as the user information object key-value pair , Use users ID+ The name of the corresponding property is used as a unique identifier to get the value of the corresponding property , Although it saves serialization overhead and concurrency problems , But users ID For duplicate storage , If there's a lot of that data , Memory waste is still considerable .
that Redis Provided Hash It's a good solution to this problem ,Redis Of Hash It's actually internal storage Value For one HashMap, And provides direct access to this Map Member's interface , Here's the picture ：
in other words ,Key Still the user ID, value It's a Map, This Map Of key Is the attribute name of the member ,value It's property value , In this way, data can be modified and accessed directly through its internal Map Of Key(Redis Inside, inside Map Of key by field), That is, through key( user ID) + field( Property tags ) You can operate the corresponding attribute data , No need to store data repeatedly , It also doesn't cause serialization and concurrent modification control problems . It solved the problem very well .
If you query directly from the database , So it's going to cost more in one day 100 Ten thousand database requests . That was already said ,Redis Support rich data types , So it can be used Redis To complete , Save this hotspot data to Redis（ Memory ） in , When it's time to use , Directly from memory , Greatly improve the speed and save the cost of the server .
All in all ,Redis It's widely used , And it's very valuable , It's a powerful tool in the server , So from now on , Let's learn it step by step .