Redis Tutorial 0/42 lessons ~6 min read Lesson 7

    Redis Data Types Overview

    Redis is not a dumb string cache.

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Redis is not a dumb string cache. It offers strings, lists, sets, sorted sets, hashes, streams, bitmaps, HyperLogLog, and geospatial indexes — each optimized for specific access patterns.

    Picking the wrong type wastes memory and forces O(N) commands where O(1) was available. A JSON string for a session object prevents partial field updates; a set when you need ranking misses ZREVRANGE.

    This overview maps business problems to Redis types so you choose correctly before writing code.

    Understanding the topic

    Key concepts

    • String — binary-safe blob, counters (INCR), cache entries.
    • Hash — field map for objects (session, profile).
    • List — ordered sequence, queue (LPUSH/BRPOP).
    • Set — unique unordered members, tags, graph edges.
    • Sorted set — member + score, leaderboards, delayed queues.
    • Stream — append-only log with consumer groups (Kafka-lite).
    • Bitmap / HLL / GEO — specialized analytics and location.
    text
    flowchart LR
    Cache[String] --> API
    Session[Hash] --> Auth
    Queue[List] --> Worker
    Tags[Set] --> Filter
    Rank[ZSet] --> Board
    Events[Stream] --> Consumer

    Step-by-step explanation

    1. Every key has exactly one type; TYPE command returns it.
    2. Wrong-type commands error (WRONGTYPE Operation against a key).
    3. Redis converts encodings internally as collections grow/shrink.
    4. OBJECT ENCODING reveals internal representation.
    5. MEMORY USAGE estimates bytes for capacity planning.

    Syntax reference

    Common commands

    • TYPE before operating on unknown keys.
    • MEMORY USAGE — plan shard sizing.
    • WRONGTYPE means you picked the wrong API for that key.
    bash
    TYPE mykey
    OBJECT ENCODING mykey
    MEMORY USAGE mykey
    # Quick reference
    # String: SET GET INCR
    # Hash: HSET HGET
    # List: LPUSH BRPOP
    # Set: SADD SISMEMBER
    # ZSet: ZADD ZRANGE
    # Stream: XADD XREADGROUP

    Informative example

    Same user modeled three ways — only hash fits partial updates efficiently:

    bash
    # String blob — rewrite entire JSON on one field change
    SET user:1 '{"name":"Ada","plan":"pro"}'
    # Hash — update one field
    HSET user:1 plan enterprise lastLogin "2026-07-01"
    # Wrong: Set loses field semantics
    SADD user:1 name:Ada plan:pro

    Use hash when you read/update individual fields. String JSON is OK for read-mostly cache blobs.

    Real-world use

    Real-world use cases

    • String — API response cache, feature flag value.
    • Hash — shopping cart line attributes, user session.
    • List — lightweight job queue between services.
    • Set — unique visitors per article, mutual followers.
    • Sorted set — game leaderboard, priority task queue.
    • Stream — order events, audit trail with consumer groups.

    Best practices

    • Match type to read/write pattern, not ORM habit.
    • Run TYPE in runbooks when debugging legacy keys.
    • Prefer hash over JSON string for mutable objects.
    • Use streams over pub/sub when you need retention.
    • Document type conventions in team key naming guide.
    • Sample MEMORY USAGE on representative keys before launch.

    Common mistakes

    • Everything stored as JSON strings.
    • Using list as set (duplicates allowed).
    • Sorted set when set suffices (extra memory for scores).
    • Pub/sub when streams needed for replay.

    Advanced interview questions

    Q1BeginnerName five Redis data types.
    String, list, set, sorted set, hash — plus stream, bitmap, HLL, geo.
    Q2BeginnerWhen use hash vs string?
    Hash for field-level read/update; string for opaque cache blobs or counters.
    Q3IntermediateHow check a key's type?
    TYPE key; OBJECT ENCODING for internal format.
    Q4IntermediateList vs stream for messaging?
    List is simple queue; stream adds persistence, consumer groups, and replay.
    Q5AdvancedDesign data model for e-commerce cart?
    Hash per cartId for line items; string for product cache; ZSET for recently viewed.

    Summary

    Redis types are purpose-built — not interchangeable.

    Ready to mark this lesson complete?Track your journey across the entire course.