Redis Study Notes 1 1. Introduction: in the past few years, NoSQL databases have become synonymous with high concurrency and massive data storage solutions, the corresponding products have also sprung up. However, only a few products stand out, such as Redis, MongoDB, BerkeleyDB, and CouchDB. Because each product has
Redis Study Notes 1 1. Introduction: in the past few years, NoSQL databases have become synonymous with high concurrency and massive data storage solutions, the corresponding products have also sprung up. However, only a few products stand out, such as Redis, MongoDB, BerkeleyDB, and CouchDB. Because each product has
Redis study Note 1
I. Introduction:
In the past few years, NoSQL databases have become synonymous with high concurrency and massive data storage solutions, and their corresponding products have sprung up. However, only a few products stand out, such as Redis, MongoDB, BerkeleyDB, and CouchDB. Because each product has different features, there are also some differences in their application scenarios. The following is a simple description:
1 ). berkeleyDB is a very popular open-source embedded database that can be used for storage engines in more cases. For example, BerkeleyDB was used as the storage engine of MySQL before it was acquired by Oracle, Which is foreseeable, this product has excellent concurrency scalability, supports transactions and nested transactions, massive data storage and other important features, and has extremely high availability value in the storage of real-time data. However, it should be noted that the license of this product is GPL, which means that it is not used for free in all cases.
2) MongoDB is defined as the Oriented-Document database server. Unlike BerkeleyDB, this database can run independently and provide related data services as other relational database servers. We can learn from the official documentation of this product that MongoDB is mainly applicable to highly concurrent forums or blog websites, these websites have the following features: High concurrent access, multi-read and less-write, large data volumes, simple logical relationships, and document data as the main data source. Like BerkeleyDB, the License of this product is GPL.
3). Redis is a typical NoSQL database server. Compared with BerkeleyDB, it can run independently on its own server host as a service program. In many cases, Redis is regarded as a Key/Value database server, but this is not the case. In the current version, in addition to Key/Value, Redis also supports data structures such as List, Hash, Set, and Ordered Set. Therefore, Redis is more widely used. The official Redis website also clarified this misunderstanding. Different from the above two products, Redis's License is Apache License, which is completely free for the moment.
4). memcached: data cache server. Why should we explain this product here? It is very simple, because I think it is most similar to Redis in use. After all, this is a blog on Redis Technology series. In view of this, we will briefly compare the two products. First, let's talk about the biggest difference between them. memcached only provides the data cache service. Once the server goes down, all the data cached in the memory will also disappear, therefore, we can see that memcached does not provide any form of data persistence, while Redis provides such a function. In addition, Redis provides a richer data storage structure, such as Hash and Set. There are two main similarities between them. One is completely free, and the other is that they provide extremely similar command forms.
Ii. Advantages of Redis:
1 ). compared with other NoSQL products, Redis is extremely easy to use. Therefore, for developers who have experience using similar products, one or two days, you can even use Redis to build your own platform in a few hours.
2) while solving many general problems, it also provides related solutions for some personalized problems, such as index engines, statistics rankings, and Message Queue services.
Iii. Main Problems in the current version of Redis:
1). the official version does not support the Windows platform. The published official version only supports Unix and MacOSX platforms.
2). No cluster support is provided. However, according to the official website, this feature is expected to be added to version 2.6.
3) in the Publication/subication function, if the master node is down, the slave cannot be automatically upgraded to the master node.
Iv. Comparison with relational databases:
In Redis of the current version (2.4.7), five different data types are supported, of which only one type is supported. The string type can be considered as the Key-Value structure, other data types are applicable to scenarios with their respective features. For details, we will describe them in the blog post on this series.
Compared with relational databases, Redis does not provide good support for complex logical relationships because of its relatively simple storage structure. However, in Redis scenarios, we can achieve a significant improvement in efficiency. Even so, Redis still provides some basic concepts that databases should possess. For example, you can choose to open different databases in the same connection. However, the difference is that, databases in Redis are named by numbers. By default, the opened database is 0. If the program intends to switch the database during running, you can use the select command of Redis to open other databases, such as select 1. If you want to switch back to the default database, you only need to execute select 0.
In terms of data storage, Redis follows the mainstream idea of the existing NoSQL database, that is, the Key is the unique identifier of Data Retrieval. We can simply think of it as the index Key in a relational database, value is the primary object for data storage. Each Value is associated with a Key, which is like the location where the physical data in the index is stored in the data table. In Redis, Value is treated as a binary byte stream used to store data in any format, such as Json, XML, and serialized object byte streams, therefore, we can think of it as a BLOB field in RDB. It can be seen that during data query, we can only use the Key as the condition for our query. Of course, we can also use some techniques in Redis to use the Value as the Key of other data, we will introduce this knowledge in our blog.
5. How to persist memory data:
By default, Redis will refer to the number of data changes in the current database. After reaching a certain threshold, it will store the database snapshots to the disk, you can set this threshold through the configuration file. In general, we can also set Redis to be regularly saved. If more than 1000 of the key data is modified, Redis performs data persistence every 60 seconds. By default, if there are nine or less data changes, Redis will persist every 15 minutes.
From the above solution, we can see that if this method is used, Redis's operation efficiency will be very efficient, that is, whenever new data changes occur, only the cache data in the memory changes, and such changes will not be immediately persisted to the disk, thus avoiding disk IO in most modification operations. However, things often have two sides. In this method, we have indeed improved the efficiency, but have lost the data reliability. If the server where Redis is located goes down before the memory snapshot is persisted to the disk, the modified data not written to the disk will be lost. To ensure high data reliability, Redis also provides another data persistence mechanism-Append mode. If the Redis server is configured with this method, it will be immediately persisted to the disk whenever any data modification occurs.