Redis Data Types
Strings, hashes, sets, sorted sets.
Overview
Redis ships several data types beyond strings, and picking the right one for each workload turns Redis from a key-value cache into a precision tool. Hashes serialise objects without copying full strings; sets answer membership questions in O(1); sorted sets handle leaderboards natively. Defaulting to strings for everything wastes memory and forces application-level work that Redis could do faster in C.
- Strings. Simple values, counters, atomic increments. The default that should not always be the default.
- Hashes. Object fields under one key. Update individual fields without rewriting the whole serialised object.
- Sets. Membership testing in O(1). Tag systems, deduplication, “is this user in the cohort” checks.
- Sorted sets plus other types. Leaderboards and time-ranged queries via score; lists, streams, bitmaps, and HyperLogLog cover further specialised cases.
The approach
Three habits make Redis usage idiomatic: pick types per workload rather than per habit, use hashes for objects to save memory, and document the rationale so future engineers do not regress to strings.
- Workload-driven type choice. Each access pattern picks the type that fits, not the type that is familiar.
- Hashes for objects.
HSETandHGETper field. Cuts memory and lets fields update independently. - Sorted sets for rankings.
ZADDwith score plusZRANGEfor top-N queries. Leaderboards run at memory speed. - Documented rationale plus per-key type. Per-key the type and the why; new engineers inherit the reasoning instead of guessing.
Why this compounds
Each correct type choice compounds across every access. Memory footprint shrinks; latency drops; the team’s caching mental model deepens. New services pick the right type from day one rather than defaulting to strings and migrating later.
- Performance improves. Right type matches workload. Single-millisecond operations stay sub-millisecond.
- Memory efficiency. Hashes and bitmaps use far less memory than serialised strings. Cluster size shrinks accordingly.
- Engineering culture matures. Workload-driven decisions replace tribal defaults. Code reviews start citing Redis types deliberately.
- Year-one investment, year-two habit. First deliberate type choice takes thought. By the third service, type selection is automatic.