Redis Data Types: An In-depth Guide to String and Hash Structures

Before diving into Redis's five fundamental data structures, it's essential to understand some core concepts that will provide a solid foundation for the following content. These include Redis's global commands, internal data structure encoding mechanisms, and its single-threaded command processing approach.

Redis Global Commands

Keys Command

The KEYS command returns all keys matching a specified pattern. It supports several wildcard patterns:

  • h?llo matches "hello", "hallo", and "hxllo"
  • h*llo matches "hllo" and "heeeello"
  • h[ae]llo matches "hello" and "hallo" but not "hillo"
  • h[^e]llo matches "hallo", "hbllo", etc., but not "hello"
  • h[a-b]llo matches "hallo" and "hbll"

Syntax:

KEYS pattern

Time complexity: O(N) where N is the number of keys in the database

Exists Command

The EXISTS command checks if a key exists in the database.

Syntax:

EXISTS key [key ...]

Time complexity: O(1)

Delete Command

The DEL command removes the specified key(s) from the database.

Syntax:

DEL key [key ...]

Time complexity: O(1)

Expire Command

The EXPIRE command sets a timeout on a key, after which the key will automatical be deleted.

Syntax:

EXPIRE key seconds

Time complexity: O(1)

TTL Command

The TTL command returns the remaining time to live of a key that has a timeout.

Syntax:

TTL key

Time complexity: O(1)

Type Command

The TYPE command returns the data type of the value stored at the specified key.

Syntax:

TYPE key

Time complexity: O(1)

Single-Threaded Architecture

Redis employs a single-threaded architecture to deliver high-performance database services. This design choice might seem counterintuitive, but Redis achieves impressive performance through several key optimizations:

  1. Pure in-memory operations: Redis stores all data in memory, providing extremely fast access times (around 100 nanoseconds).
  2. Non-blocking I/O: Redis uses epoll for I/O multiplexing, efficiently handling multiple connections without blocking.
  3. No thread contention: The single-threaded model eliminates the overhead of thread synchronization and context switching.

However, this architecture comes with a critical constraint: long-running commands can block all other operations. Therefore, Redis is optimized for fast command execution.

Redis Data Types and Internal Encoding

Redis provides five fundamental data types, each with multiple internal encoding implementations that optimize performance for different scenarios:

Data Type Internal Encodings
String raw, int, embstr
Hash hashtable, ziplist
List linkedlist, ziplist, quicklist
Set hashtable, intset
Zset skiplist, ziplist

String Data Type

The String type is Redis's most basic data structure. Key characteristics include:

  • All Redis keys are strings
  • String values can be simple strings, numbers, or binary data (up to 512MB)
  • Redis stores strings as binary data without character encoding processing

String Commands

Set Command

Sets the string value of a key.

Syntax:

SET key value [EX seconds|PX milliseconds] [NX|XX]

Time complexity: O(1)

Get Command

Retrieves the value of a key.

Syntax:

GET key

Time complexity: O(1)

MSet Command

Sets multiple keys to multiple values in a single atomic operation.

Syntax:

MSET key value [key value ...]

Time complexity: O(N) where N is the number of keys

MGet Command

Gets the values of all specified keys.

Syntax:

MGET key [key ...]

Time complexity: O(N) where N is the number of keys

Increment Commands

Redis provides several commands for atomic numeric operations:

  • INCR: Increments the integer value of a key by one
  • INCRBY: Increments the integer value of a key by the specified amount
  • DECR: Decrements the integer value of a key by one
  • DECRBY: Decrements the integer value of a key by the specified amount
  • INCRBYFLOAT: Increments the floating point value of a key by the specified amount

String Manipulation Commands

  • APPEND: Appends a value to a key
  • GETRANGE: Gets a substring of the string value of a key
  • SETRANGE: Overwrites part of a string at a specified offset
  • STRLEN: Gets the length of the string value of a key

String Internal Encoding

String values are encoded using one of three formats:

  • int: 8-byte long integers
  • embstr: Strings up to 39 bytes
  • raw: Strings larger than 39 bytes

String Use Cases

  • Caching: Storing frequently accessed data to reduce database load
  • Counting: Implementing counters for various metrics
  • Session storage: Managing user session data in distributed systems
  • Verification codes: Temporarily storing one-time use codes

Hash Data Type

The Hash type represents a collection of field-value pairs, similar to objects or dictionaries in programming languages.

Hash Commands

HSet Command

Sets the value of a field in a hash.

Syntax:

HSET key field value [field value ...]

Time complexity: O(1) for each field

HGet Command

Gets the value of a field in a hash.

Syntax:

HGET key field

Time complexity: O(1)

HExists Command

Checks if a field exists in a hash.

Syntax:

HEXISTS key field

Time complexity: O(1)

HDel Command

Deletes one or more fields from a hash.

Syntax:

HDEL key field [field ...]

Time complexity: O(1) per field

HGetAll Command

Gets all fields and values in a hash.

Syntax:

HGETALL key

Time complexity: O(N) where N is the number of fields

Other Hash Commands

  • HKEYS: Gets all field names in a hash
  • HVALS: Gets all values in a hash
  • HMGET: Gets the values of multiple fields
  • HLEN: Gets the number of fields in a hash
  • HSETNX: Sets a field only if it doesn't exist
  • HINCRBY: Increments the integer value of a field

Hash Internal Encoding

Hashes are encoded using either:

  • ziplist: For small hashes with small values (default: <512 fields, <64 bytes per value)
  • hashtable: For larger hashes or values exceeding size limits

Hash Use Cases

  • User information storage: Storing user profiles with multiple attributes
  • Caching complex objects: Efficiently caching structured data with partial updates

Tags: Redis Data Structures string hash Caching

Posted on Sat, 15 Aug 2026 16:37:50 +0000 by psychomossel