The core data types and what each is for

Redis Course · lesson 2 of 19 · 5 min read

Choosing the right Redis type removes application code rather than adding it.

Open this lesson in the learning hub

Key points

  • A string holds text, a serialised blob or a counter, and tops out at 512 MB per value.
  • A hash stores fields under one key, so HGET user:1000 email reads one field without fetching the rest.
  • A list is a linked sequence with LPUSH and BRPOP, which makes a blocking work queue in two commands.
  • A set gives set membership plus server side SINTER, SUNION and SDIFF across keys.
  • A sorted set keeps members ordered by a float score, which is what a leaderboard or a delay queue needs.
  • Small collections are stored as a packed listpack until hash-max-listpack-entries (default 128) is passed.

Example

SET   page:home:etag "W/9f2c"            # string
HSET  user:1000 name Ada email ada@x.io  # hash
LPUSH queue:email "{\"to\":\"ada\"}"       # list, pop with BRPOP
SADD  post:42:tags redis cache latency   # set
ZADD  leaderboard 4820 ada 3910 bob      # sorted set
XADD  orders * id 991 total 240          # stream

OBJECT ENCODING user:1000   # "listpack" while small, "hashtable" once it grows

Pick the type that already does the operation you were about to write in application code.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Redis Course course, and every lesson in it is listed on the Redis Course contents page.