What are the core data types supported by Redis, and what is each one typically used for?
Short answer
Redis supports strings for simple text or numeric values, hashes for storing objects with multiple fields, lists for ordered collections that support fast additions at either end, sets for unordered unique collections, and sorted sets for collections ordered by a score, each…
Advertisement
AModel answer
Redis supports strings for simple text or numeric values, hashes for storing objects with multiple fields, lists for ordered collections that support fast additions at either end, sets for unordered unique collections, and sorted sets for collections ordered by a score, each suited to different kinds of application needs.
Example
SET user:1:name 'John' HSET user:1 name 'John' age 30 LPUSH recent:views 'product:456' SADD tags:post:1 'redis' 'database' ZADD leaderboard 100 'player1'
Real-world example
A gaming application uses strings for simple session tokens, hashes for player profile data, lists for a recent activity feed, sets for tracking unique achievements, and sorted sets for a live leaderboard, choosing the right type for each specific need.
Likely follow-ups
- How do you decide which data type is right for a specific use case?
- Can a single Redis key hold more than one data type at the same time?
Related topics