SoFunction
Updated on 2025-04-22

Detailed explanation of several data types of Redis

Several data types of Redis

Redis provides a variety of data types to support different application scenarios. Each data type has its own specific operation mode and is also optimized in internal implementation to meet different business needs.

Here are several common data types supported by Redis:

1. String

describe:Strings are the most basic data type in Redis, similar to key-value pairs. Each string can store 512 MB of data (this value can be very large, depending on the machine's memory).

Common operations:

  • SET key value: Set a key-value pair.
  • GET key: Get the value of a key.
  • INCR key: Increase the key value by 1.
  • DECR key: Decrease the key value by 1.
  • APPEND key value: Append content after the string value.

Application scenarios:

  • Caching: storing user data, web page cache, etc.
  • Counter: count visits, likes, etc.

2. Hash

describe:Hash is a collection of key-value pairs that are suitable for storing multiple fields. For example, multiple attributes of a user (such as username, mailbox, etc.) can be stored in the same hash. The internal structure of hash is similar to that in JavaMapor Pythondict

Common operations:

  • HSET key field value: Set field value in hash.
  • HGET key field: Gets the value of the specified field in the hash.
  • HGETALL key: Gets all fields and values ​​in the hash.
  • HDEL key field: Delete the fields in the hash.

Application scenarios:

  • User information: Multiple attributes of a user, such as user name, age, email address, etc.
  • Configuration file: Stores application configuration information, etc.

3. List

describe:List is a simple string list that can be sorted in the order of insertion and deleted from both ends. It's similar to that in JavaLinkedListor Pythonlist, but it is more suitable for use in scenarios such as queues or stacks.

Common operations:

  • LPUSH key value: Insert one or more values ​​to the left of the list.
  • RPUSH key value: Insert one or more values ​​to the right of the list.
  • LPOP key: Removes and returns the left element of the list.
  • RPOP key: Removes and returns the right element of the list.
  • LRANGE key start stop: Gets the element in the specified range in the list.

Application scenarios:

  • Message queue: Implement asynchronous task queues to handle background tasks.
  • Recent items: Store the recently viewed products, history, etc. of the user.

4. Set

describe:A collection is an unordered collection of strings, and the elements in the collection are unique and repetition is not allowed. Collections provide efficient element joining, deleting, and finding operations.

Common operations:

  • SADD key member: Add one or more members to the collection.
  • SREM key member: Remove one or more members from the collection.
  • SMEMBERS key: Returns all members in the collection.
  • SISMEMBER key member: Determine whether the member is in the set.

Application scenarios:

  • Deduplication: Avoid duplicate elements such as user deduplication in the recommended system.
  • Friendship relationship: For example, friends relationships in social networks, members in the friend collection are unique.

5. Ordered Set

describe:Ordered set is aWeight (score), where each element is associated with a double-precision floating point value as weight and sorted from small to large by weight. Unlike ordinary sets, the position of an element in an ordered set is determined by its weight.

Common operations:

  • ZADD key score member: Add one or more members and their scores to the ordered set.
  • ZREM key member: Remove one or more members from the ordered collection.
  • ZRANGE key start stop: Gets the members of the specified range in the ordered set (sorted by fractions).
  • ZRANK key member: Returns the ranking of members (score sorting).

Application scenarios:

  • Ranking: It can be used to achieve game rankings, website visits and other scenarios.
  • Task Scheduling: Sort based on task priorities.

6. Bitmaps (Bitmaps)

describe:Bitmap is a data structure in bits, often used to store and operate large amounts of binary data. Each bit (0 or 1) represents a value, and the bitmap can be operated efficiently through the bit operation command provided by Redis.

Common operations:

  • SETBIT key offset value: Set the bit of the specified location.
  • GETBIT key offset: Get the bit of the specified location.
  • BITCOUNT key: Statistics the number of bits in the bitmap with 1.

Application scenarios:

  • User check-in system: Use bitmaps to represent user check-in records, saving storage space.
  • Bit operation: such as access log statistics, etc.

7. HyperLogLog

describe:HyperLogLog is a probability-based data structure used to estimate cardinality (i.e., the number of elements that do not repeat). It does not store the elements themselves, but only stores some simplified statistics, so it is very efficient for cardinal estimation of large amounts of data.

Common operations:

  • PFADD key element: Add an element to the HyperLogLog.
  • PFCOUNT key: Returns the cardinality estimate of the elements in HyperLogLog.

Application scenarios:

  • Cardinality estimation of big data: such as estimating the number of unique IPs visited by a website.
  • Low-precision but efficient deduplication.

8. Geospatial index (Geospatial)

describe:Redis provides geospatial capabilities to store and query geographic locations with latitude and longitude information. It is used internallyGeohashEncoding to represent geographical coordinates, thereby enabling efficient range query.

Common operations:

  • GEOADD key longitude latitude member: Adds a geographic location to the specified geospace.
  • GEODIST key member1 member2: Calculate the distance between two geographical locations.
  • GEORADIUS key longitude latitude radius: Returns all geographical locations within a given radius.

Application scenarios:

  • Mall and catering industry: Return to nearby shops or restaurants based on user location.
  • Logistics and Distribution: Positioning and calculating the shortest path to distribution.

Summarize

These data structures provided by Redis are very flexible to meet different business needs.

The design and implementation of each data structure has been carefully optimized to improve application performance and efficiency in different scenarios.

By choosing the right data structure, developers can achieve higher performance in different applications.

Common Redis data types include:

  • String
  • Hash
  • List
  • Set (Set)
  • Ordered Set
  • Bitmap (Bitmap)
  • HyperLogLog
  • Geospatial index (Geospatial)

The above is personal experience. I hope you can give you a reference and I hope you can support me more.