Redis Study Guide: From entry to mastery, let you easily master NoSQL database

Redis is currently one of the most popular NoSQL databases. It has high performance, high availability and rich data structure characteristics. This article will introduce you to Redis related concepts, data types, commands, and some usage scenarios, help you fully understand Redis, and provide some examples for reference.

I. Overview

Redis is an open source high-performance NoSQL database that supports a variety of data structures, such as strings, hash tables, lists, sets, and ordered sets. Redis uses memory as a data storage medium and writes data to disk asynchronously to ensure data reliability. When reading data, Redis performs non-blocking I/O operations, enabling high-concurrency, low-latency read and write operations.

The advantages and disadvantages of Redis are as follows:

advantage:

  1. High performance: The read and write speed of Redis is very fast, which can reach tens of thousands of read and write performance per second.

  2. Scalability: Redis can expand data storage capacity and read and write performance through master-slave replication and clustering.

  3. Rich data structures: Redis supports multiple data structures, and you can choose the appropriate data type according to different scenarios.

  4. Data persistence: Redis supports two persistence methods, RDB and AOF, to ensure data reliability.

shortcoming:

  1. Limited memory: Redis's data storage is based on memory, so the data storage capacity is relatively limited.

  2. Does not support complex queries: Redis does not support complex query statements similar to SQL.

  3. Data loss: If the Redis master node fails, the data that is not persisted at the same time may be lost.

2. Data type

1. String

The string type is the most basic data type in Redis, which can store any type of data, such as numbers, text, and binary data. In string operations, Redis provides a large number of APIs, such as get, set, append, delete, increase and decrease, and so on.

For example:

GET key: Get the value of the specified key.

SET key value: Set the value of the key to value.

INCR key: Add 1 to the value corresponding to the key, and return the new value.

2. Hash table (hash)

A hashtable type is a data structure for storing key-value pairs, where the key is an element in the hashtable and the value is the element associated with the key. Hashtable types can store complex data structures like JSON objects or similar. In hash table operations, Redis provides many APIs such as get, set, delete, increase and decrease.

For example:

HSET key field value: Set the value of the specified field in the hash table of the specified key to value.

HGET key field: Get the value of the specified field in the hash table of the specified key.

HDEL key field: Delete the specified field in the hash table of the specified key.

3. List (list)

A list type is an ordered data structure in which elements are sorted in the order of insertion and can be repeated. Redis provides many APIs for manipulating list types, such as inserting and deleting elements at the head or tail of the list, getting and modifying elements, and so on.

For example:

LPUSH key value1 value2: inserts two elements, value1 and value2, at the head of the list with the specified key.

LPOP key: Delete the head element of the list with the specified key and return the deleted element.

LRANGE key start stop: Returns the element at the index position from start to stop in the list of the specified key.

4. Collection (set)

A collection type is an unordered data structure in which elements are not repeated. Redis provides a wealth of APIs for operating collection types, such as adding, deleting, getting elements, taking intersections, unions, and so on.

For example:

SADD key member1 member2: Add two elements member1 and member2 to the set of the specified key.

SREM key member: Deletes the member element from the set of the specified key.

SMEMBERS key: Returns all elements in the set of the specified key.

5. Ordered set (sorted set)

An ordered collection type is an ordered data structure in which the elements are not repeated, and each element is attached with a score for sorting. Redis provides many APIs for manipulating ordered collection types, such as adding, deleting, getting elements, getting elements based on score range, and so on.

For example:

ZADD key score1 member1 score2 member2: Add two elements to the ordered set of the specified key, namely score1 and member1, score2 and member2.

ZREM key member: Delete the member element from the ordered set of the specified key.

ZRANGEBYSCORE key min max: Returns the elements whose scores are in the range [min, max] in the ordered set of the specified key.

3. Commonly used commands

1. Basic commands

GET key: Get the value of the specified key.

SET key value: Set the value of the key to value.

DEL key1 key2 ... keyN: delete multiple specified keys.

EXISTS key: Determine whether the specified key exists.

2. Hash table commands

HSET key field value: Set the value of the specified field in the hash table of the specified key to value.

HGET key field: Get the value of the specified field in the hash table of the specified key.

HDEL key field: Delete the specified field in the hash table of the specified key.

HGETALL key: Returns all key-value pairs in the hash table of the specified key.

3. List command

LPUSH key value1 value2: inserts two elements, value1 and value2, at the head of the list with the specified key.

LPOP key: Delete the head element of the list with the specified key and return the deleted element.

LRANGE key start stop: Returns the element at the index position from start to stop in the list of the specified key.

4. Collection (set)

A collection type is an unordered data structure in which elements are not repeated. Redis provides a wealth of APIs for operating collection types, such as adding, deleting, getting elements, taking intersections, unions, and so on.

For example:

SADD key member1 member2: Add two elements member1 and member2 to the set of the specified key.

SREM key member: Deletes the member element from the set of the specified key.

SMEMBERS key: Returns all elements in the set of the specified key.

5. Ordered set (sorted set)

An ordered collection type is an ordered data structure in which the elements are not repeated, and each element is attached with a score for sorting. Redis provides many APIs for manipulating ordered collection types, such as adding, deleting, getting elements, getting elements based on score range, and so on.

For example:

ZADD key score1 member1 score2 member2: Add two elements to the ordered set of the specified key, namely score1 and member1, score2 and member2.

ZREM key member: Delete the member element from the ordered set of the specified key.

ZRANGEBYSCORE key min max: Returns the elements whose scores are in the range [min, max] in the ordered set of the specified key.

4. Advanced Features

1. Affairs

Redis supports transactional operations, a transaction is a collection of commands that will be executed as a unit. In a transaction, if any command fails to execute, the entire transaction will fail and all commands will not be executed.

Transactional operations require the use of MULTI, EXEC, DISCARD, and WATCH commands.

For example:

1) Open the transaction

MULTI

2) Add transaction command

SET key1 value1

SET key2 value2

3) Submit the transaction

EXEC

2. Publish and subscribe

Redis supports publish/subscribe mode, that is, a client can send messages to other clients.

The publisher publishes a message to a specified channel through the PUBLISH command, and the subscriber subscribes to a channel's message through the SUBSCRIBE command.

For example:

1) The publisher publishes the message:

PUBLISH channel1 "hello world"

2) Subscriber subscribes to channel:

SUBSCRIBE channel1

3) The publisher publishes the message again, and the subscriber will be able to receive the message:

PUBLISH channel1 "redis is awesome!"

3. Master-slave replication

Master-slave replication is an important feature of Redis. It can synchronize the data of one Redis node to other nodes, so as to realize data backup and read-write separation.

In master-slave replication, the master node is responsible for writing data, and the slave node is responsible for reading data. The master node synchronizes the written data to the slave node, and the slave node writes asynchronously after receiving the data from the master node.

Master-slave replication can be started through configuration files or command line parameters, or configured through commands after Redis has started.

For example:

1) Turn on master-slave replication on the master node:

slaveof 127.0.0.1 6379

2) Start reading data on the slave node:

get key

4. Cluster

Redis cluster is a solution for multi-node Redis deployment, which can realize automatic data distribution and high availability.

In a Redis cluster, data can be distributed to different nodes, thereby improving the concurrency of data access. At the same time, when a node fails, other nodes can take over its work to ensure the availability of the system.

A Redis cluster requires at least 3 Redis instances to form, one of which is the master node, and the other nodes are slave nodes.

For example:

1) Build a Redis cluster:

redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002

2) Perform operations in the Redis cluster:

redis-cli -c -p 7000

get key

5. Practical cases

1. Verification code cache

Verification codes are a common verification method for applications. Each verification code is used only once. As the number of users increases and the frequency of visits increases, the traffic for generating and verifying verification codes will easily become a bottleneck.

Storing verification codes in Redis can greatly improve the speed of verification code generation and verification.

For example:

1) Generate verification code:

code = generate_code()

#在Redis中存储验证码,有效期为60秒

redis.setex("verify_code:user_id", 60, code)

2) Verify the verification code:

code = request.form['code']

if redis.get("verify_code:user_id") == code:

    return "success!"

2. High-frequency data statistics

In Internet applications, it is often necessary to perform high-frequency statistics on certain data, such as daily user PV, UV, order quantity, and so on.

Using Redis can quickly realize the statistics of high-frequency data, such as using counters to store the counters of each statistical item in Redis.

For example:

1) Statistics of daily user PV:

today = datetime.now().strftime("%Y-%m-%d")

redis.incr("PV:" + today)

2) Obtain daily user PV:

today = datetime.now().strftime("%Y-%m-%d")

pv = redis.get("PV:" + today)

return pv

3. Cache

Redis's high performance and memory storage characteristics make it a very suitable database for caching.

Using Redis as a cache database can store frequently accessed data in memory to improve read speed and reduce access pressure on back-end storage.

For example:

1) Read cache:

def get_user(id):

    user = redis.get("user:" + id)

    if user is None:

        # 从后端加载用户信息

        user = load_user(id)

        # 将用户信息存储到Redis中,有效期为1小时

        redis.setex("user:" + id, 3600, user)

    return user

2) Update the cache:

def update_user(id, name):

    # 更新后端存储

    update_backend(id, name)

    # 更新缓存

    redis.set("user:" + id, name)

6. Summary

Redis is a high-performance NoSQL database with rich data structures, high scalability, and various advanced features, which can be applied in many business scenarios.

This article starts with the basic theory of Redis, expounds the characteristics, data structure and commands of Redis, and provides practical cases, hoping to help readers better understand and use Redis.

Guess you like

Origin blog.csdn.net/canshanyin/article/details/130598802