Skip to content

Redis data structures and Operations

Redis (Remote Dictionary Server) is an open-source, in-memory data structure store used as a database, cache, and message broker^[001-TODO__28490作日誌寫入機制.md]. It is renowned for its high performance and flexibility, supporting a wide variety of abstract data structures^[001-TODO__28490作日誌寫入機制.md].

Unlike traditional relational databases that rely on tables and rows, Redis organizes data into key-value pairs where the value can be one of several specific data types^[001-TODO__28490作日誌寫入機制.md]. Below is an overview of the core data structures and common operations.

Key-Value Model

The fundamental unit of storage in Redis is a Key-Value pair^[001-TODO__28490作日誌寫入機制.md]. * Key: A binary safe string, typically used to uniquely identify the data. * Value: The data itself, which can be of different types (Strings, Lists, Sets, etc.).

Core Data Structures

Strings

Strings are the most basic Redis data type^[001-TODO__28490作日誌寫入機制.md]. They are binary-safe and can contain any data, such as text, serialized objects, or integers^[001-TODO__28490作日誌寫入機制.md]. * Use Cases: Caching HTML fragments, storing counters, or session management. * Common Operations: * SET: Assigns a value to a key. * GET: Retrieves the value of a key. * INCR/DECR: Atomically increments or decrements a counter stored in a string^[001-TODO__28490作日誌寫入機制.md].

Lists

Lists are collections of string elements sorted by insertion order^[001-TODO__28490作日誌寫入機制.md]. They are implemented using Linked Lists. * Use Cases: Implementing queues (FIFO), stacks (LIFO), or message queues^[001-TODO__28490作日誌寫入機制.md]. * Common Operations: * LPUSH/RPUSH: Insert an element to the head (left) or tail (right) of the list. * LPOP/RPOP: Remove and return an element from the head or tail. * LRANGE: Retrieve a range of elements from the list^[001-TODO__28490作日誌寫入機制.md].

Sets

Sets are unordered collections of unique strings^[001-TODO__28490作日誌寫入機制.md]. * Use Cases: Tracking unique items (e.g., unique visitors to a website), managing tags, or computing commonalities between groups^[001-TODO__28490作日誌寫入機制.md]. * Common Operations: * SADD: Add one or more members to a set. * SREM: Remove one or more members from a set. * SMEMBERS: Retrieve all members of a set. * SISMEMBER: Check if a specific element is a member of the set^[001-TODO__28490作日誌寫入機制.md].

Hashes

Hashes are maps between fields and string values^[001-TODO__28490作日誌寫入機制.md]. They are ideal for representing objects. * Use Cases: Storing user profiles (e.g., name, email, age) or product details^[001-TODO__28490作日誌寫入機制.md]. * Common Operations: * HSET: Set the value of a field in the hash. * HGET: Get the value of a specific field. * HGETALL: Get all fields and values in the hash^[001-TODO__28490作日誌寫入機制.md].

Sorted Sets (ZSets)

Sorted Sets are similar to sets, but every member is associated with a score (a floating-point number)^[001-TODO__28490作日誌寫入機制.md]. Elements are ordered by this score. * Use Cases: Leaderboards, priority queues, or rate limiting^[001-TODO__28490作日誌寫入機制.md]. * Common Operations: * ZADD: Add a member with a specified score. * ZRANGE: Retrieve members within a range of scores (by rank). * ZRANGEBYSCORE: Retrieve members within a specific score range^[001-TODO__28490作日誌寫入機制.md].

  • [[Database Normalization]]
  • [[Memoization]]
  • Pub/Sub

Sources

  • 001-TODO__28490作日誌寫入機制.md