01Choose a Redis type for each workload: session token with TTL, profile fields, online users, leaderboard, delayed jobs, replayable audit log. Justify each by dominant operation.introScenarioYou are designing the storage layout for a web application with six distinct workloads.AUse a hash for the session token so the TTL can live on one field, a list for online users, and Pub/Sub channels for the audit log.BUse a sorted set for everything that is ordered or unique — online users, leaderboard, delayed jobs, and the audit log keyed by timestamp, with scores doubling as deduplication keys for repeated events.CUse a JSON string for the profile fields, and a list for both delayed jobs and the audit log, popping entries as they replay.DUse string for the session token, hash for profile fields, set for online users, sorted set for leaderboard or delayed jobs, and stream for replayable audit logs.
02When does a hash reduce keyspace overhead compared with many string keys, and when might separate keys still be useful?introScenarioA teammate proposes folding fifty `user:42:*` string keys into a single `user:42` hash to save memory.AHashes are always cheaper because field lookups are O(1) while top-level key lookups must scan the keyspace.BHashes only save memory once the combined fields exceed about a megabyte; below that, both layouts cost the same.CA hash reduces overhead when many fields belong to one entity; separate keys can still help independent TTLs, routing, or access patterns.DSeparate keys are never useful once a hash exists, since per-field TTLs can be emulated with a second hash of deadlines at no cost beyond a little extra memory for the bookkeeping.
03Why does a sorted set need both uniqueness and ordering by score?appliedScenarioYou are explaining the design of the leaderboard type to someone who has only used lists and sets.AUniqueness prevents hash collisions in the underlying table, while ordering falls out of insertion order.BA sorted set needs unique members so updates replace the right item and scores so the structure can answer ordered or ranked queries.CScores already guarantee uniqueness, so member uniqueness is redundant and kept only for API compatibility, a holdover from when sorted sets were built on plain sets.DOrdering exists so members can be retrieved alphabetically, and the score is an optional display value.
04Compare list, stream, and Pub/Sub for a worker system where consumers may disconnect.appliedScenarioYou are picking the backbone for a job system whose worker processes crash and reconnect regularly.ALists model simple queues, streams model durable replay with acknowledgements, and Pub/Sub models live delivery only.BPub/Sub fits best, because messages queue per subscriber while it is disconnected; lists and streams require polling.CLists and streams are equivalent for workers, with streams adding only an alternative syntax for popping entries.DStreams push to every consumer the way Pub/Sub does, so consumer groups exist mainly to slow producers down.
05Which type choice would you use for a replayable audit log, and why would Pub/Sub be the wrong fit?advancedScenarioAn auditor requires that every recorded event can be re-read later, in order, by any number of independent readers.AUse Pub/Sub with a persistence flag enabled, since channels can be configured to retain history for late subscribers for a configurable retention period per channel.BUse a list with RPOPLPUSH so every replay moves entries onto a backup list, preserving them for the next reader.CUse a stream for a replayable audit log because entries persist by ID; Pub/Sub would drop messages for disconnected consumers.DUse a sorted set scored by timestamp, which replays like a stream and also deduplicates identical events automatically.