Redis for Developers: Data Structures and Use Cases
Redis is an in-memory data store known for sub-millisecond responses. It's more than a key-value cache: each data structure solves a specific class of problems elegantly.
Strings: cache values and counters
SET page:/pricing "<html>..." EX 300 # cache for 5 minutes
GET page:/pricing
INCR stats:signups:2026-10-11 # atomic counter
Hashes: objects
HSET user:42 name "Asha" plan "pro" credits 120
HGET user:42 plan
HINCRBY user:42 credits -5
Lists: simple queues and recent items
LPUSH recent:user:42 "post:991"
LTRIM recent:user:42 0 9 # keep the last 10
LRANGE recent:user:42 0 -1
Sets: unique membership
SADD online:users 42 77 105
SISMEMBER online:users 42
SINTER followers:42 followers:77 # mutual followers
Sorted sets: rankings and time-ordered data
ZADD leaderboard 1520 "asha" 1810 "ravi"
ZREVRANGE leaderboard 0 9 WITHSCORES # top 10
ZADD delayed:jobs 1760190000 "job:1" # score = run-at timestamp
Streams: event logs with consumer groups
XADD orders * id 42 total 1499
XREADGROUP GROUP billing worker-1 COUNT 10 STREAMS orders >
Using Redis from Node.js
import { createClient } from 'redis';
const redis = createClient({ url: process.env.REDIS_URL });
await redis.connect();
await redis.set('greeting', 'hello', { EX: 60 });
const value = await redis.get('greeting');
Remember
- Data lives in RAM, so size your instance for your dataset.
- Use clear key names with prefixes (
user:42,cache:product:7) and set TTLs on cache keys. - Configure persistence deliberately if Redis holds data you can't rebuild.
Was this article helpful?
Comments
No comments yet. Have a question or a tip? Be the first to share it.