Introduction to Redis data types and persistence

Table of contents

Basic operations of 9 data types in Redis

1. String

2. List

3. Hash

4. Set

5. Sorted Set

6. Bitmap

7. Geospatial

8. Timeout (HyperLogLog)

9. Stream

Redis persistence: RDB and AOF

RDB persistence

AOF persistence

Summarize:


Basic operations of 9 data types in Redis

Redis is a high-performance key-value storage database that supports the storage and operation of multiple data types. In Redis, there are 9 basic data types, and each type has corresponding operation commands. This blog will introduce the 9 data types of Redis and their basic operations to help readers fully understand the data storage and processing capabilities of Redis.

1. String

String is the simplest Redis data type, which can store any type of data, including strings, integers, and floating point numbers. Commonly used operation commands are:

  • SET key value: Set the specified key name to the corresponding value.
  • GET key: Get the value of the specified key name.
  • INCR key: Increase the integer value corresponding to the specified key name by 1.
  • DECR key: Decrease the integer value corresponding to the specified key name by 1.

2. List

A list is an ordered collection of strings that can store multiple elements of the same or different types. The operation commands of the list are:

  • LPUSH key value: Insert the value value into the head of the list key.
  • RPUSH key value: Insert the value value into the end of the list key.
  • LPOP key: Remove and return the head element of the list key.
  • RPOP key: Remove and return the tail element of the list key.

3. Hash

A hash is a collection of key-value pairs that can be used to store objects. The hash operation commands are:

  • HSET key field value: Set the field field in the hash table key to the corresponding value.
  • HGET key field: Get the value of the specified field in the hash table key.
  • HDEL key field1 field2 ...: Delete one or more fields in the hash table key.

4. Set

A set is an unordered collection of strings where each element is unique and no duplication is allowed. The collection operation commands are:

  • SADD key member1 member2 ...: Add one or more elements to the collection key.
  • SMEMBERS key: Get all elements in the collection key.
  • SREM key member1 member2 ...: Remove one or more elements from the collection key.

5. Sorted Set

Sorted sets are similar to sets, but each element has a score that can be used for sorting. The operation commands for ordered sets are:

  • ZADD key score1 member1 score2 member2 ...: Add one or more elements with scores to the ordered set key.
  • ZRANGE key start stop: Get the elements ranked between start and stop in the ordered set key in order of scores from small to large.

6. Bitmap

Bitmap is a special data type that can operate on binary bits. The bitmap operation commands are:

  • SETBIT key offset value: Set the offset offset of the bitmap corresponding to the key name to the specified value value.
  • GETBIT key offset: Get the offset value of the bitmap corresponding to the key name.

7. Geospatial

The geographical location data type is used to store geographical location information and can be used to query and calculate geographical location. The geographical location operation commands are:

  • GEOADD key longitude latitude member: Add the specified geographical location information to the key.
  • GEODIST key member1 member2 [unit]: Calculate the distance between two locations.

8. Timeout (HyperLogLog)

HyperLogLog is used for cardinality estimation and can count the number of unique elements in a data set. The timeout operation commands include:

  • PFADD key element1 element2 ...: Add one or more elements to HyperLogLog.
  • PFCOUNT key: Counts the number of unique elements in HyperLogLog.

9. Stream

Stream is a new data type introduced in Redis 5.0 version, which is used to record multiple events efficiently. The stream operation commands are:

  • XADD streamName ID field1 value1 field2 value2 ...: Add one or more events to the stream streamName.
  • XREAD COUNT count STREAMS streamName startID: Read the specified number of events from the stream streamName.

The above are the 9 basic data types of Redis and their basic operations. Through these data types and corresponding commands, we can realize various complex data storage and processing functions.

Redis persistence: RDB and AOF

Redis supports two persistence mechanisms: RDB (Redis Database) and AOF (Append Only File). These two mechanisms ensure that the Redis server can restore previously stored data after restarting.

RDB persistence

RDB persistence is to save Redis data in memory to a binary file on the hard disk in the form of a snapshot. RDB persistence can be enabled through the configuration file redis.conf.

The steps to enable RDB persistence are as follows:

  1. Open the redis.conf file.
  2. Search and find the "save" configuration item. Here you can set the trigger conditions for persistence. For example, save 900 1 means that if at least 1 key is modified within 900 seconds, RDB persistence will be triggered.
  3. Multiple save conditions can be set to adapt to different persistence strategies.

AOF persistence

AOF persistence appends Redis write operations to the AOF file in the form of logs, recording the status changes of the Redis server in real time. AOF persistence can be enabled through the configuration file redis.conf.

The steps to enable AOF persistence are as follows:

  1. Open the redis.conf file.
  2. Search and find the "appendonly" configuration item, and set it to "yes" to enable AOF persistence.

Check persistence files

In order to confirm whether RDB and AOF persistence takes effect, you can check whether the corresponding persistence file is generated in the directory where the Redis server is located.

The default name of RDB files is "dump.rdb" and the default name of AOF files is "appendonly.aof".

Execute the ls command in the directory where the Redis server is located to check whether these two files are generated.

By enabling RDB and AOF persistence, we can ensure that the Redis server can restore previously stored data after restarting, ensuring data security and stability.

Summarize:

As a high-performance key-value storage database, Redis supports the storage and operation of multiple data types. This article introduces the 9 basic data types of Redis and their basic operations, as well as the two persistence mechanisms of RDB and AOF. By mastering this knowledge, readers can better utilize Redis to process data and ensure data persistence.

Using RDB and AOF persistence can ensure data security and prevent data loss. In actual applications, you can choose an appropriate persistence mechanism according to specific needs and configure the corresponding persistence parameters reasonably. The persistence mechanism is an important feature of Redis. I hope this article will help readers understand and use Redis persistence.

Guess you like

Origin blog.csdn.net/weixin_62304542/article/details/131866586