Technology Encyclopedia Home >What data types and data structures does Redis support?

What data types and data structures does Redis support?

Redis supports several data types and data structures, which make it a versatile in-memory data store. Here are the primary ones:

Data Types:

  1. Strings: The basic data type in Redis, used to store text or binary data. For example, you can store a user's name or a small image.

    • Example: SET username "JohnDoe"
  2. Lists: Ordered collections of strings. Elements can be added or removed from either end of the list.

    • Example: LPUSH mylist "world" and RPUSH mylist "hello"
  3. Sets: Unordered collections of unique strings. Useful for tasks like managing unique visitors to a website.

    • Example: SADD myset "one" and SADD myset "two"
  4. Hashes: Maps between string fields and string values, similar to a dictionary in Python or a map in Java.

    • Example: HSET user:1000 name "John Doe" and HSET user:1000 email "johndoe@example.com"
  5. Sorted Sets: Similar to sets, but each element is associated with a score, which is used to sort the elements.

    • Example: ZADD myzset 1 "one" and ZADD myzset 2 "two"
  6. Bitmaps: Special strings where each bit can be independently set or cleared. Useful for tracking user activity or flags.

    • Example: SETBIT mybitmap 10 1
  7. HyperLogLogs: Probabilistic data structures used for estimating the cardinality of a set, i.e., the number of unique elements.

    • Example: PFADD myhyperloglog "one" and PFADD myhyperloglog "two"

Data Structures:

  • In-Memory Data Store: Redis stores all data in memory, which provides very fast access times.
  • Persistent Storage: Redis also supports persistence options like RDB snapshots and AOF logs to ensure data is not lost.

Use Case Example:

Imagine you are building a real-time analytics dashboard. You could use Redis lists to store and manage incoming data streams, sets to track unique users, and sorted sets to rank users based on activity levels. This combination allows for efficient data handling and quick retrieval needed for real-time analytics.

For deploying Redis in a scalable and reliable manner, you might consider using services like Tencent Cloud's Redis Cluster, which offers automated sharding, high availability, and failover capabilities to meet the demands of your applications.