Code Reference

Lists & Sets

Redis · Reference cheat sheet

Lists & Sets

Redis · Reference cheat sheet


📋 Overview

Lists are ordered sequences (queues, timelines). Sets are unordered unique collections (tags, membership). Both are foundational for queues and relationships.

🔧 Core concepts

List commandsRole
LPUSH / RPUSHPush left/right
LPOP / RPOPPop left/right
LRANGESlice
LLENLength
BLPOP / BRPOPBlocking pop
Set commandsRole
SADD / SREMAdd/remove members
SISMEMBERMembership test
SMEMBERSAll members
SINTER / SUNION / SDIFFSet algebra
SCARDCount

💡 Examples

Simple queue:

LPUSH jobs '{"id":1}'
BRPOP jobs 5

Tags set:

SADD post:9:tags redis cache
SISMEMBER post:9:tags redis
SINTER user:1:tags user:2:tags

Recent items (list trim):

LPUSH feed:42 itemA
LTRIM feed:42 0 99

⚠️ Pitfalls

  • SMEMBERS / large LRANGE can block — use SSCAN / limit ranges.
  • Lists allow duplicates; sets do not — pick the right structure.
  • Blocking pops need careful timeouts in app servers to avoid stuck workers.

On this page