Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 42 ยท Specialized Data Types

Choosing the Right Data Type

Learn to model data in Redis by access pattern: pick between strings, hashes, lists, sets and sorted sets, design keys, and avoid KEYS and huge keys.

Redis gives you a dozen data types, and the same information can often be stored in several of them. A user profile can be one JSON string or one hash. A feed can be a list or a sorted set. A "who visited today" record can be a set, a bitmap or a HyperLogLog. None of these choices is wrong in the abstract, but each one makes some operations cheap and others painful. This lesson gives you a method for choosing.

The method is the opposite of what relational databases teach. In SQL you model the data first and ask questions later, because joins and indexes can answer almost any question. Redis has no joins and no ad-hoc queries, so you model the questions first: which reads and writes must be fast, and in what shape does the answer need to come back? The data type is whatever makes those exact operations a single command.

As everywhere in this track, real Redis commands are read-only blocks. The runnable examples are JavaScript stand-ins that compute the same results, so you can see the trade-offs in numbers.

Start from the access pattern

Before picking a type, write down the operations as plain sentences. For a blog, they might be:

  1. Show one post by its id.
  2. Show the ten newest posts.
  3. Show every post that has the tags redis and cache.
  4. Count a view, and show the most viewed posts.

Each sentence points to a structure. Sentence 1 wants a lookup by key. Sentence 2 wants an order by time. Sentence 3 wants an intersection. Sentence 4 wants a counter plus a ranking. That is a hash per post, a sorted set scored by timestamp, one set per tag, and another sorted set scored by views.

Ask the question first

Lookup by id A string or a hash.
Newest first A sorted set scored by time.
Has all these tags Sets and SINTER.
Top N A sorted set scored by count.

The access pattern picks the type, not the shape of the data.

1 / 2

The types side by side

This table is a starting point, not a rulebook. "Typical cost" describes the commands you would use most.

Type Best for Typical cost Watch out for
String Single values, counters, cached blobs, flags O(1) get and set Rewriting a whole blob to change one part
Hash An object with fields you read or change separately O(1) per field Very large hashes make HGETALL slow
List Queues, stacks, recent items O(1) at both ends Access by index in the middle is O(N)
Set Unique members, tags, membership tests, intersections O(1) add and test SMEMBERS on a huge set blocks the server
Sorted set Rankings, time-ordered feeds, range queries O(log N) add Scores are floating point numbers
Stream Event logs, consumer groups O(1) append Needs trimming to stay bounded
Bitmap Yes/no per numeric id O(1) per bit Sparse, huge ids waste memory
HyperLogLog Approximate unique counts O(1) Cannot list the members
Geo Nearby search O(log N) It is a sorted set underneath

Two rules of thumb settle most decisions. First, if you only ever fetch or replace the value as a whole, a plain string is simplest and fastest. Second, as soon as you want to touch part of the value (one field, one member, one end), a structure that supports that part directly beats storing a blob and doing the work in your application.

Example: one profile, two models

Suppose a profile has a name, an email, a plan and a login counter, and the login counter changes constantly. Stored as a JSON string, every change reads the whole document, edits it in your application and writes the whole document back. Stored as a hash, a change is one field. The next example counts the bytes sent and received in both models.

JS JSON string vs hash: bytes per update

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The numbers are the point: the hash moves a few dozen bytes where the JSON string moves the whole document twice. The JSON route also has the lost-update problem from What Is Redis?, because the read and the write are separate commands. That does not make JSON strings bad. If the profile is small and always replaced as a unit, one SET and one GET are perfect, and the Redis Stack JSON lesson covers a type that edits parts of a document in place.

Key design

Keys are your only index, so naming them well is part of the model.

  • Put the identifier in the key, the facts in the value. Use user:42:profile, not one giant users hash that holds every user. A single key per entity lets you expire, delete and move entities independently.
  • Use colons from general to specific, as in cart:9f3a:items. Pick one convention and keep it everywhere.
  • Keep keys short enough to be cheap, long enough to be readable. Millions of keys with 80-character names waste memory for no benefit.
  • Add a version when the stored format may change, for example v2:product:1001. Changing the prefix retires old entries without a migration (more in Caching Patterns).
  • Wrap related keys in a hash tag when you will use them together in Cluster, for example {user:42}:cart and {user:42}:profile. The braces make both keys land in the same slot (see Redis Cluster).

A worked modeling exercise

Back to the blog. Four questions became four structures. The example below stores posts the Redis way and answers each question. Every answer prints the real command it corresponds to, so you can read the model as a script of Redis calls.

JS Modeling a blog by access pattern

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that the model duplicates information: a post id appears in the hash, in the timeline, in the view ranking and in each tag set. That is normal in Redis. You pay a little memory so that every question is one cheap command, and the application keeps the copies in step when a post changes (in a transaction or a script when they must stay consistent).

Antipatterns to avoid

Huge keys. A single hash, list or set with millions of members is awkward: reading it all blocks the server, deleting it with DEL can stall everything while memory is freed (UNLINK frees it in the background), and in Cluster one slot carries the whole load. Split by a sensible bucket, such as per day or per user, and let old buckets expire.

Using KEYS in production. KEYS pattern walks every key in one command, and because commands run one at a time, nothing else is served meanwhile. SCAN returns a small batch per call plus a cursor, so other clients keep working between calls. Compare them:

JS KEYS vs SCAN: work per command

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The sketch simplifies one thing: real SCAN walks the internal hash table, not an ordered array, and can return a key twice if the table is resized mid-scan, so your code must tolerate duplicates. The batching idea is exactly the same.

One key as the whole database. Putting every user in one hash called users makes it impossible to expire a single user and creates a hot spot. Prefer one key per entity.

Storing what you can compute. If a total can be a counter that you increment, do not store the list and count it on every request.

Treating Redis as the only copy. Unless you have designed persistence and backups for it (see Persistence), keep a source of truth elsewhere for data you cannot lose.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What should you decide first when modeling data in Redis?

    Redis has no joins or ad-hoc queries, so you model the questions (access patterns) first and pick the type that makes each one a single command.
  2. 2. A profile has one field that changes on every request and a few that rarely change. Which model fits best?

    A hash updates a single field atomically without reading or rewriting the rest of the object.
  3. 3. Why is KEYS a bad idea on a busy production server?

    Commands run one at a time. KEYS holds the main thread for the whole walk, while SCAN does a small batch per call.
  4. 4. Which structure answers 'show the ten newest items' most directly?

    A sorted set orders members by score, so reading the highest timestamps is a range query on the end.
  5. 5. Is it a mistake to store the same post id in several structures?

    Redis models trade a little memory for fast, single-command answers. Keep the copies in sync with a transaction or a script when needed.

Summary

  • Model the questions first: list the reads and writes, then pick the type that makes each one cheap.
  • Use a string for whole values and counters, a hash for objects with separately changing fields, a list for ends-of-sequence work, a set for unique members and intersections, and a sorted set for rankings and time order.
  • Put the identifier in the key, use colon-separated names, and add a version prefix when formats may change.
  • Duplicating small ids across structures is normal; keep them consistent with transactions or scripts.
  • Avoid huge keys and KEYS in production: split big collections and use SCAN, UNLINK and expiry.

Next, learn how to make several commands succeed or fail together in Transactions: MULTI, EXEC and WATCH.