Strings, Lists, and Hashes

[!NOTE] This module explores the core principles of Strings, Lists, and Hashes, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.

1. Strings: The Universal Building Block

A Redis String is binary-safe, meaning it can store text, serialized JSON, images, or raw bytes. It’s the most basic but versatile type.

Internals: SDS (Simple Dynamic String)

Redis does not use C-style strings (char*). It uses SDS.

  • O(1) Length: Stores length in a header.
  • Buffer Overflow Protection: Tracks free space.
  • Binary Safe: Can store null bytes \0.

Use Case: Distributed Rate Limiter

Prevent abuse by limiting API calls (e.g., 10 requests per minute).

public boolean allowRequest(String userId) {
    String key = "rate:" + userId;
    long current = jedis.incr(key);

    if (current == 1) {
        jedis.expire(key, 60); // Reset after 1 minute
    }

    return current <= 10;
}