Go beyond strings — atomic counters with INCR, store objects in hashes, and build a leaderboard with sorted sets. The three data types that cover most real backend caching work.
incr increases a number by one and returns the new value in a single atomic step — even if a hundred requests hit it at once, none of them lose a count. That makes it perfect for view counts, "likes," or anything you tally. You never read-then-write yourself, which is exactly where race conditions creep in.
// Count a page view. The key is created at 0 on first call.
const views = await redis.incr('post:7:views') // 1, then 2, then 3...
// Jump by more than one:
await redis.incrby('post:7:views', 10)
// Read the current count without changing it (comes back as a string):
const current = Number(await redis.get('post:7:views'))A hash is a key that holds multiple field/value pairs — like a small object living under one Redis key. Use it when you want to cache a record without serializing the whole thing to JSON. hset writes fields, hgetall reads them all back as an object, and you can read or update a single field without touching the rest.
// One key, many fields — like caching a user row.
await redis.hset('user:42', {
name: 'Ada Lovelace',
email: 'ada@example.com',
plan: 'pro',
})
const user = await redis.hgetall('user:42')
// { name: 'Ada Lovelace', email: 'ada@example.com', plan: 'pro' }
// Read or update just one field — no need to rewrite the object:
const plan = await redis.hget('user:42', 'plan') // 'pro'
await redis.hset('user:42', 'plan', 'enterprise')A sorted set keeps members ordered by a numeric score, and Redis maintains that order for you on every write. It is the go-to for leaderboards, "top N," or any ranking. zadd sets a score, zincrby bumps it, and zrevrange pulls the highest scores back already sorted — no ORDER BY, no re-sorting in your app.
// score = points; member = player
await redis.zadd('leaderboard', 100, 'ada', 90, 'alan')
await redis.zincrby('leaderboard', 25, 'alan') // alan now 115
// Top 3, highest first, with their scores attached:
const top = await redis.zrevrange('leaderboard', 0, 2, 'WITHSCORES')
// ['alan', '115', 'ada', '100', ...]
// One player's rank (0 = first place):
const rank = await redis.zrevrank('leaderboard', 'ada') // 1