CodePark/ questions / Data types & commands
START PRACTICINGPRACTICE
Backend question · Redis & caching › Data types & commands

Name the main Redis data types and give a typical use case for each.

The main types are strings, hashes, lists, sets and sorted sets. There are also some special types.

Main types:

  • Strings hold text, numbers or binary data, up to 512 MB. Use them for cached JSON, counters with INCR, and lock tokens.
  • Hashes are maps of fields to values. Use them for a user profile or a shopping cart, where you update one field at a time.
  • Lists are ordered sequences. Adding and removing at both ends is fast. Use them for simple queues and recent-activity feeds.
  • Sets hold unique members with no order. Use them for tags, the users who liked a post, and finding members common to two sets.
  • Sorted sets hold unique members, ordered by a number called a score. Use them for leaderboards, priority queues, and time-ordered data such as sliding-window rate limits.

Special types:

  • Streams are append-only logs. Consumer groups let several workers share the events. Use them for event processing.
  • Bitmaps are bit operations on strings. They are good for flags like "was this user active today".
  • HyperLogLog estimates how many unique items there are, using about 12 KB.
  • Geospatial indexes store coordinates, so you can search within a radius.

JSON documents and full-text search come from the Redis Stack modules.

  • Strings are for cached values, counters and tokens.
  • Hashes are for objects, lists are for queues, and sets are for unique members.
  • Sorted sets are for leaderboards and time-ordered data.
  • Streams are for event logs with consumer groups.
  • Bitmaps, HyperLogLog and geospatial indexes cover special needs.