Redis Data Types

Leave your reply


Learn how to choose the right Redis data type for your project, and why. Redis comes with several useful data types, which can make it difficult to decide which one to use.


  • A Servidor Cloud running Linux (CentOS 7 or Ubuntu 14.04)
  • Redis installed and running. See our article on Installing Redis on a IONOS Linux Servidor Cloud if needed.

Overview of Redis Data Types

Although you may expect that your project will determine the data type you use, the reverse can also be true: The data type drives the project. Each Redis data type has its own strengths and weaknesses, and is suited for a particular kind of project.

If you have experience with various computer languages, you will be familiar with terms like "hash" and "string." However, Redis uses these data types in ways you may not expect.

Redis is a "key-value store," not a relational database like MySQL or PostgreSQL. This means that it stores data based on keys, and nothing else. Thus, the way you choose to store the data will have a significant effect on how your project behaves, and how you interact with the data.

There are five Redis data types:

  • String
  • Hash
  • List
  • Set
  • Sorted Set


Strings are the simplest Redis data type. A string stores data in key/value pairs.


Key: "j doe"
Value: "07/20/2016 1:43PM"

Strings can be up to 512 megabytes in size. There are a few things you can do with strings, including cut, remove, or replace data based on its position in the string.

Redis allows you to work with strings on a bit-by-bit basis without requiring any extra bitshifting gymnastics in your code. This makes strings convenient for things like basic counters, user login tracking by ID number, and so forth.

Being able to work with a string using its individual bits also makes it a compact way to store data. You can store approximately 10 million single-bit data points in a 1.2MB string.


In Redis, a hash is a collection of key/value pairs. Each key and value in a hash is a string.

Hashes are an excellent way to store a lot of small pieces of data. Although a hash can support over 4 billion key/value pairs, Redis optimizes the efficiency of relatively small hashes (those with fewer than about 100 fields). This makes it very fast to work with smaller hashes.


Key: User ID
  "username" => "j doe"
  "address" => "123 Main St"
  "city" => "Anytown, USA"
  "email" => ""
  "last login date" => "07/20/2016 1:43PM"


A Redis list is a key/value pair where the value is a collection of items, ordered by their insertion order. Lists are often used by programmers who need a queueing system. You can insert and delete things from the head or tail of a list, and use push or pop functions to move the queue along.

Twitter is probably the most well-known example of Redis list functionality. The "10 most recent tweets" list for a user is a list. New tweets are pushed to the head of the list, and the oldest tweet is popped off.


Key: "jdoe_twitter"
Value: ["Never mind, I found it", "Did someone eat it? Rude!", "Has anyone seen my sandwich? I left it in the break room fridge."]

The user sends an update a few minutes later. It is pushed to the head of the list, and the oldest entry is popped off the end:

Key: "jdoe_twitter"
Value: ["Oops, it wasn't mine. Sorry to whoever it belonged to, I owe you a sandwich.", "Never mind, I found it", "Did someone eat it? Rude!"]


A set is a collection of unique strings. Each string can only exist once in any given set, no matter how many times you add it.

Redis sets are very fast, which makes them an excellent way to check for membership in a set. There are also several interesting random functions you can perform, if you want to randomly remove or replace an item from a set.


Key: "administrators"
Value: ("jdoe", "alice", "bob")

Key: "banned users"
Value: ("evil_jdoe", "meanguy53", "i_am_a_troll")

Sets are often used to create simple card games. Because each string will be unique, you don't need to worry about accidentally handing someone two Ace of Spades cards. And the random features are perfect for card games, allowing you to deal out a random card, shuffle the hand, return cards to the deck, etc.

Sorted Sets

A sorted set is a set where the strings are sorted by a score which you specify. Because of these properties, sorted sets are often used for leaderboards to track players' points or levels in a game.


Key: "Leaderboard: Total Achievement Points"
Value: ("Jdoe" [2], "Alice" [43], "Bob" [3553])

Bob is obviously leading the pack, here. The leaderboard will be displayed on the website as:

Bob: 3553 AP
Alice: 43 AP
Jdoe: 21 AP

Let's say Alice completes an achievement worth 20 points. The value "Alice" [63] is added to the sorted set, and the leaderboard on the website is instantly updated accordingly:

Bob: 3553 AP
Alice: 63 AP
Jdoe: 21 AP 

Scores can be repeated, but each string can only exist once per set. If you insert the same string twice, it will only exist in the set once, although its score will be set to the more recent entry.

For example, imagine you insert the value "jdoe" into a sorted set with a sorting score of 4. Later you insert the value "jdoe" but with a sorting score of 7. The value "jdoe" will remain unique, but the sorting score will be changed. Thus, this has the effect of updating the score for "jdoe" to 7.