Basic architecture of key-value database

Redis is a typical key-value database. To design a key-value database, you need to consider what kind of data is stored in it, what kind of operations can be performed on the data, that is, the data model and operation interface, and you must also consider whether the key-value pairs are stored in memory or external memory, and what access mode is used.

The data model of the key-value database is the key-value model, and the key consideration is the type supported by value. Redis is widely used because Redis supports various types of values ​​such as String, hash table, list, and collection.

The operating interface of the database is nothing more than CRUD:

  • PUT / SET: increase or update key-value;
  • GET: Use the key to query the corresponding value;
  • SCAN: Query the corresponding value according to the range of a key;
  • DELETE: Use key to delete the entire key-value.

The advantage of storing the key-value pairs in the memory is the fast access speed, but once the machine is powered off, all data will be lost. Stored in external memory, although data loss can be avoided, the access speed will be limited by the access speed of the disk. At this time, the overall performance of the key-value database is reduced.

Therefore, the problem of storing key-value pairs in memory and external storage mainly depends on the application scenario. For example, the cache requires fast access speed and allows data loss. Therefore, it is better to use memory to store the key-value pair data in the cache scenario. Redis as a cache database is a very wide application scenario.

A key-value database usually includes four parts: access framework, index module, operation module and storage module. 

There are usually two access modes:

  • It is used by external applications through the function library call;
  • Provide external key-value pair operations in the form of Socket communication through the network framework.

Redis is accessed through the network framework, so Redis can be accessed as a basic network service, which expands the scope of application of Redis.

The Redis data model has rich value types, so there are more operation interfaces. For example, the operation list uses LPUSH/LPOP, and the operation set uses SADD/SREM.

Redis persistence supports log (AOF) and snapshot (RDB). These two persistence methods have different advantages and disadvantages, which will affect the access performance and reliability of Redis.

The Redis index module uses a hash table as an index, because its key value data is stored in memory, and the high-performance random access of the memory can well match the operation complexity of the hash table O(1).

 

Guess you like

Origin blog.csdn.net/qq_39074984/article/details/113735087