NOSQL Database Redis Series (1) – Introduction to Redis

I. Introduction to redis

(1) Introduction to Redis

Redis is completely open source and free, complies with the BSD protocol, and is a high-performance key-value database.

Redis and other key-value caching products have the following three characteristics:

  • Redis supports data persistence. The data in the memory can be saved in the disk, and it can be loaded again for use when restarting.
  • < span style="font-size: 18px; font-family: Microsoft YaHei;">Redis not only supports simple key-value type data, but also provides storage for list, set, zset, hash and other data structures.
  • < span style="font-size: 18px; font-family: Microsoft YaHei;">Redis supports data backup, that is, data backup in master-slave mode.

(2) What can Redis do

1. Cache< /span>

The caching mechanism is used in almost all large-scale websites. Proper use of caching can not only speed up data access, but also effectively reduce the pressure on back-end data sources. Redis provides key value expiration time settings, and also provides flexible control of maximum memory and elimination strategies after memory overflow. It can be said that a reasonable cache design can escort the stability of a website.

2. Ranking system

The ranking system exists in almost all websites, such as ranking by popularity According to the ranking list at the release time and the ranking list calculated according to various complex dimensions, Redis provides a list and an ordered set of data structures. The reasonable use of these data structures can easily build various ranking systems.

3. Counter application

The counter plays an important role in the website. The business website has the number of views. In order to ensure the real-time performance of the data, the operation of adding 1 is required for each playback and browsing. If the amount of concurrency is large, it is a challenge to the performance of traditional relational data. Redis naturally supports the counting function and the counting performance is also very good, which can be said to be an important choice for the counter system.

4. Social network

Likes/dislikes, fans, mutual friends/likes, push, pull-down refresh, etc. are social The necessary functions of the website, because social networking sites are usually visited relatively large, and traditional relational data is not suitable for storing this type of data, the data structure provided by Redis can implement these functions relatively easily.

5. Message Queue System

The message queuing system can be said to be a necessary basic component of a large website, because of its It has the characteristics of business decoupling and non-real-time business peak cutting. Redis provides publish and subscribe functions and blocking queue functions. Although is not powerful enough compared with professional message queues, it can basically satisfy general message queue functions.

< span style="font-size: 18px; font-family: Microsoft YaHei;">(four),RedisWhat not to do
actually with any technology Similarly, each technology has its own application scenarios and boundaries, that is to say, Redis is not a panacea. There are many problems that are suitable for it to solve, but there are also many problems. Suitable for the problem it solves. We can analyze from the perspective of data scale and data coldness and heat. From the perspective of data scale, data can be divided into large-scale data and small-scale data. We know that the data of Redis is stored in memory, although now The memory is already cheap enough, but if the amount of data is very large, for example, there are hundreds of millions of user behavior data every day, if you use Redis to store it, it will basically be a bottomless pit and economical The cost is quite high. From the perspective of hot and cold data, data is divided into hot data and cold data. Hot data usually refers to data that requires frequent operations, and vice versa, cold data. For example, for video websites, basic video information is basically in each business line. They are all data that are frequently manipulated, and the user’s viewing records are not necessarily the data that needs to be accessed frequently. I will not discuss the difference between the two data scales for the time being. From the perspective of the cold and hot data, the video information is hot data. User viewing records are cold data. If you put these cold data in Redis, it is basically a waste of memory, but for some hot data, you can put it in Redis accelerates reads and writes, and can also reduce the load on the back-end storage, which can be said to be twice the result with half the effort. Therefore, Redis is not a panacea. I believe that as we gradually learn Redis, we can Know the real usage scenarios of Redis.
span>

< span class="fontstyle0">(5), redis features< /s pan>

The reason why Redis is favored by so many companies is bound to be outstanding. Here are 8 important features of Redis.
1.Fast speed< br> Under normal circumstances, Redis executes commands very fast. The official figure is that the read and write performance can reach 100,000 per second. Of course, this also depends on the performance of the machine, but I will not discuss the difference in machine performance here. I will only analyze what makes Redis so fast. It can be roughly summarized into the following four points: All data in Redis is stored In memory,

  • Redis is implemented in C language, generally speaking The program implemented in C language is closer to the operating system, and the execution speed is relatively faster.
  • Redis uses a single-threaded architecture to prevent competition issues that may arise from multiple threads.
  • The author can say that the Redis source code is carefully polished. Someone once commented that Redis is low Some open source code that combines performance and elegance.

2.< strong>Data structure server based on key-value pairs
Almost all programming languages ​​provide similar The function of a dictionary, such as map in Java and dict in Python, is similar to this way of organizing data called a key-value-based way. Unlike many key-value databases, the value in Redis can not only be a string , And it can also be a specific data structure, which not only facilitates the development of many application scenarios, but also improves development efficiency. The full name of Redis is REmote Dictionary Server. It mainly provides five data structures: string, hash, list, set, and ordered set. At the same time, on the basis of string, it has evolved bitmaps (Bitmaps) and HyperLogLog. This kind of magical “data structure”, and with the continuous development of LBS (Location Based Service), Redis 3.2 version adds the function of GEO (geographical information positioning). In short, with the help of these data structures, Developers can develop various “interesting” applications.
3.Rich features
In addition to five data structures, Redis also provides many additional functions: it provides a key expiration function, which can be used to implement caching. Provides a publish and subscribe function, which can be used to implement the message system to support Lua script functions, and can use Lua to create new Redis commands. Provides a simple transaction function, which can guarantee transaction characteristics to a certain extent. Provides a pipeline (Pipeline) function, so that the client can transmit a batch of commands to Redis at once, reducing network overhead.

4.Simple and stable
The simplicity of Redis is mainly manifested in three aspects. First of all, the source code of Redis is very few. The code of the early version is only about 20,000 lines. After version 3.0, the code has been increased to about 50,000 lines due to the addition of the cluster feature. Compared with many NoSQL databases, the amount of code is relatively much less. It means that ordinary development and operation and maintenance personnel can “see through” it. Second, Redis uses a single-threaded model, which not only makes the Redis server-side processing model simple, but also makes client-side development easier. Finally, Redis does not need to rely on class libraries in the operating system (for example, Memcache needs to rely on system class libraries such as libevent). Redis itself implements event processing related functions. Although Redis is very simple, it does not mean that it is unstable. Taking thousands of Redis maintained by the author as an example, there has been no downtime due to Redis’s own bugs.
5.Multiple client languages
Redis provides a simple TCP communication protocol, many programming languages ​​can be easily connected to Redis, and because Redis is well received by the community It is widely recognized by major companies, so there are many client languages ​​that support Redis, almost covering mainstream programming languages, such as Java, PHP, Python, C, C++, Nodejs, etc.
6.Persistence
Generally speaking, it is not safe to put data in memory. Once the power is cut off or the machine fails, important data may be lost. Therefore, Redis provides two types of persistence. Method: RDB and AOF, that is, two strategies can be used to save the data in the memory to the hard disk (as shown in Figure 1-1), which ensures the durability of the data. In Chapter 5, we will persist Redis Give a detailed description.

share picture
Figure 1-1 Redis memory to disk persistence
7.Master-slave replication
Redis provides a replication function, which implements multiple Redis copies of the same data (as shown in Figure 1-2). The replication function is the basis of distributed Redis. In Chapter 6, we will give a detailed description of Redis replication.

share picture
8.Highly available and distributed
Redis officially provides a high-availability implementation Redis Sentinel from version 2.8, which can ensure the failure detection and automatic failover of Redis nodes. Redis has officially provided a distributed implementation of Redis Cluster from version 3.0. It is a true distributed implementation of Redis, providing high availability, read-write, and capacity scalability.

(6), Redis advantage

  • Extremely high performance-Redis can read 110,000 times/s, and write 81,000 times/s.
  • Rich data types – Redis supports Strings, Lists, Hashes, Sets and Ordered in binary cases Sets data type operation.
  • Atomic-All Redis operations are atomic, meaning that either succeeds or fails Not implemented at all. A single operation is atomic. Multiple operations also support transactions, that is, atomicity, packaged by MULTI and EXEC instructions.
  • Rich features – Redis also supports publish/subscribe, notification, key expiration and other features.

(7), Redis and others What is the difference between key-value storage?

  • Redis has a more complex data structure and provides The atomic operation of this is a different evolutionary path from other databases. Redis data types are based on basic data structures and are transparent to programmers without additional abstraction.
  • Redis runs in memory but can be persisted to disk, so it is necessary to perform different data sets The memory needs to be weighed when high-speed reading and writing, because the amount of data cannot be greater than the hardware memory. Another advantage of the in-memory database is that compared to the same complex data structure on the disk, it is very simple to operate in memory, so that Redis can do a lot of internally complex things. At the same time, in terms of disk format, they are generated in a compact manner because they do not require random access.

(eight), Redis in windows General operations under

Open a cmd In the window, use the cd command to switch to C:
edis and run redis-server.exe redis.windows.conf.

If you want convenience, you can change the path of redis Add it to the system’s environment variables, so that you don’t have to enter the path again. The redis.windows.conf can be omitted. If it is omitted, the default will be enabled. After inputting, the following interface will be displayed:

share picture< /span>

At this time, open another cmd window, do not close the original one, or you will not be able to access the server.

Switch to the redis directory and run redis-cli.exe -h 127.0.0.1 -p 6379.

set key-value pairs set myKey abc

Get the key-value pair get myKey

Share a picture

Second, RedisMajor version

Redis draws on the Linux operating system for The naming rule of the version number: If the second digit of the version number is an odd number, it is an unstable version (for example, 2.7, 2.9< span class="fontstyle1">, 3.1), if it’s an even number, it’s the stable version (e.g. 2.6, 2.8, 3.0, 3.2 ). The current odd version is the next stable version of the development version
book, for example, 2.9version is 3.0The development version of the version. So we usually choose the even version of Redis in the production environment. If you want to know and use some new features in advance, you can choose the latest
odd version . The version that introduces a technology is a must-have content for many technical books, and it is usually easy for readers to ignore, but as you learn this technology in depth, you will feel that Be prepared to feel cordial, and usually pay attention to the features of the new version. This section will focus on RedisExplain some important versions and features in the development process.
1.Redis2.6
Redis2.6in 2012 was officially released in 2012. It has gone through 17 versions, until 2.6. The 17 version, compared with
Redis2.4, the main features are as follows:
1) The server supports Lua scripts.
2) Remove virtual memory related functions.
3) Let go of hard-coded restrictions on the number of client connections.
4) The expiration time of the key supports milliseconds.
5) The slave node provides read-only functions.
6) Two new bitmap commands: bitcount and bitop.
7) enhanced the function of redis-benchmark: support customized compression Test, CSVoutput and other functions
.
8) Increment commands based on floating point numbers: incrbyfloatand hincrbyfloat.
9) redis-cliyou can use –evalParameter implementation LuaScript execution.
10) shutdown command enhancements.
11) infocan followsection< span class="fontstyle1"> output, and added some statistical items.
12) Refactored a lot of core code, all cluster related codes have been removed, clusterFunction
will be the biggest highlight of the 3.0 version.
13) sort order optimization.
2.Redis2.8
Redis2.8in 2013year11month22day Officially released, it has gone through 24 versions, to 2.8.24version,
Compared with Redis2.6, the main features are as follows:
1) Add part The function of master-slave replication, to a certain extent, reduces the pressure on the system caused by
frequent full replication RDBdue to network problems.
2) Try to support IPv6.
3) can be set via the config setcommandmaxclients.
4) You can use the bindcommand to bind multiple IP address.
5) Redis sets the obvious Process name, it is convenient to use the ps command to view the system process.
6) config rewriteThe command can be config set persists to Redis configuration file.
7) The pubsub command has been added for publish and subscribe.
8) Redis SentinelThe second edition, compared to Redis2.6 of Redis Sentinel, this version has
become production available.
3.Redis3.0
Redis3.0in 2015year4month1day Officially released, as of the completion of this book, it has reached the 3.0.7 version,
compared to Redis2.8The main features are as follows:
Note
Redis3.0The biggest change is the addition of< span class="fontstyle0">RedisDistributed implementation of Redis Cluster, which fills in
RedisOfficially there is no gap in distributed implementation. Redis Cluster has experienced 4 years before it was officially released
for a reason, specifically You can refer to the development log of Redis Cluster
(http://antirez.com/news/79).
1) Redis Cluster: Redis< The official distributed implementation of span class="fontstyle1">.
2) New embedded stringobject encoding results, optimized memory access for small objects , The speed is greatly improved under certain
workloads.
3) lruThe algorithm is greatly improved.
4) migrateconnects to the cache to greatly increase the speed of key migration.
55
5) migrate Command two new parameters copy and replace.
6) New client pause command to stop processing clients within a specified time End request.
7) bitcountcommand performance improvement.
8) config setSettingsmaxmemory< In span class="fontstyle1">, you can set different units (previously only words
section), such as config set maxmemory1gb.
9) Redis Small adjustments to the log: The log will reflect the current instance Role (masteror
slave).
10) incrcommand performance improvement.
4.Redis3.2
Redis3.2201656日正式发布, 相比于Redis3.0主要特征如下:
1) 添加GEO相关功能。
2SDS在速度和节省空间上都做了优化。
3) 支持用upstart或者systemd管理Redis进程。
4) 新的List编码类型: quicklist
5) 从节点读取过期数据保证一致性。
6) 添加了hstrlen命令。
7) 增强了debug命令, 支持了更多的参数。
56
8Lua脚本功能增强。
9) 添加了Lua Debugger
10config set支持更多的配置参数。
11) 优化了Redis崩溃后的相关报告。
12) 新的RDB格式, 但是仍然兼容旧的RDB
13) 加速RDB的加载速度。
14spop命令支持个数参数。
15cluster nodes命令得到加速。
16Jemalloc更新到4.0.3版本。
5.Redis4.0
可能出乎很多人的意料, Redis3.2之后的版本是4.0, 而不是3.43.6
3.8。一般这种重大版本号的升级也意味着软件或者工具本身发生了重大变
革, 下面列出Redis4.0的新特性:
1) 提供了模块系统, 方便第三方开发者拓展Redis的功能, 更多模块详
见: http://redismodules.com
2PSYNC2.0: 优化了之前版本中, 主从节点切换必然引起全量复制的
问题。
3) 提供了新的缓存剔除算法: LFULast Frequently Used) , 并对已有
算法进行了优化。
4) 提供了非阻塞delflushall/flushdb功能, 有效解决删除bigkey可能造
成的Redis阻塞。
5) 提供了RDB-AOF混合持久化格式, 充分利用了AOFRDB各自优
势。
6) 提供memory命令, 实现对内存更为全面的监控统计。
7) 提供了交互数据库功能, 实现Redis内部数据库之间的数据置换。
8Redis Cluster兼容NATDocker.
< /span>

参考资料:Redis开发与运维

分享图片

分享图片

Leave a Comment

Your email address will not be published.