Introduction to Redis

Leave your reply

What is Redis?

Learn more about Redis, and some examples of its use. Redis (which is officially pronounced "REdis") is a key-value store, not a relational database.

Redis is used by companies like Twitter, Flickr, GitHub, and Craigslist, and is consistently one of the top three most popular Docker images.


  • Install Redis on a Servidor Cloud with Linux

Key-Value Store

Unlike a database, Redis stores data in a flexible structure, similar to a dictionary or hash.

If a relational database is a bulldozer, a key-value store is a scalpel. Neither one is "better," but there are times when only one or the other will do. Redis is not an all-purpose solution, but there are many situations where it is the right tool for the job.

Redis is an excellent tool for storing session caches, queues, leaderboards, and other situations where you need to store large amounts of data, access it quickly, in a high-volume environment, but you don't necessarily need to keep the data forever.

Data Management

Because Redis stores its data in memory, it is exceptionally fast. Redis only writes to the disk for the purposes of data persistence.

This also allows Redis to easily handle large numbers, which makes it an excellent tool for working with big data. Each hash can hold over 4 billion key-value pairs.

There are five Redis data types. Learn which one is the best choice for your project in our article Redis Data Types.

When is a Key-Value Store Like Redis Useful?

Storing data in a relational database requires you to set up an inflexible structure, and comply with that structure whenever inserting or updating data. By comparison, a key-value store like Redis will allow you to insert data with as much flexibility as you need.

As an example, imagine you are creating a website for a local deli. The deli owner wants to be able to take sandwich orders through the website. You will be creating a form on the front end to capture the data and send it to a store on the back end.

The deli doesn't need to store these orders after they have been completed. They just want to take orders and make sandwiches, without a lot of extra fuss.

Relational Database Example

To do this with a relational SQL database, you would need to create a database with tables for the bread type, cheese, other toppings, etc. A separate table would contain the order.

The order form would specify all of the possible variations, and insert the data with a command like:

INSERT INTO sandwich_order (customer_name, customer_phone, customer_address, bread_type, meat_type, cheese_type, other_fillings_1, other_fillings_2, side_order, drink) VALUES ('J Doe', '206-555-1234', '123 Main Street', '2', '4', '3', '1', '6', '8', '11'); 

Wondering what kind of sandwich the customer wants? To send the entire text of the order to the deli's sandwich counter, your code will need to reference all the other tables with queries like:

SELECT bread_name FROM bread_info WHERE bread_type = "2";
SELECT meat_name FROM meat_info WHERE meat_type = "4";
SELECT cheese_name FROM cheese_info WHERE cheese_type = "3";
SELECT other_filling_name FROM other_fillings_info WHERE other_fillings_id = "1";
SELECT other_filling_name FROM other_fillings_info WHERE other_fillings_id = "6";
SELECT side_order_name FROM side_order_info WHERE side_order = "8";
SELECT drink_name FROM drink_info WHERE drink = "11";

If you create a separate table to store customer info, this will generate another flurry of SQL queries as well.

As you can see, this can quickly get clunky. What if someone wants a third extra filling? Or two different kinds of cheese? What if they don't want to give their phone number or address, but want to be notified by email that their sandwich is ready? You would need to add fields for each of these possibilities, and then use those fields all the time.

Not to mention the processing time it takes to look up and return all of those values.

This kind of database is an excellent solution for many situations. But in the case of sandwich orders, which are created and accessed on the fly, and don't need to be saved forever, you simply don't need the infrastructure of a full-scale SQL database.

For this deli, a fast, lightweight solution is better.

Key-Value Store Example

A key-value store is much more flexible. All you would need to do is create a form with all the possible fields, and pass along the data to a Redis hash. As a key, we will use sandwich plus the customer's name and timestamp.

For example:

hmset sandwich:john_doe-1467228882 bread sourdough meat ham cheese swiss side_dish chips drink lemonade

And let's say John's sister Jane places an order a few seconds later:

hmset sandwich:jane_doe-1467229035 bread "Toasted white" meat1 Ham meat2 "Smoked turkey" cheese Gouda notes "No mayo" toppings "Lettuce and tomato"

Retrieving the sandwich orders from the command-line interface is simple:> hvals sandwich:john_doe-1467228882
1) "sourdough"
2) "ham"
3) "swiss"
4) "chips"
5) "lemonade"> hvals sandwich:jane_doe-1467229035
1) "Toasted white"
2) "Ham"
3) "Smoked turkey"
4) "Gouda"
5) "No mayo"
6) "Lettuce and tomato"

Redis Use Cases

Redis is used when there are large volumes of data that needs to be collected and accessed on the fly, but doesn't necessarily need to be kept forever - so-called "hot data."

If processing speed is a big concern, then Redis is a very good candidate. Redis's use of memory means that it can quickly sift through large amounts of data to return results.

Some examples of Redis use include:

  • Twitter uses Redis to store tweets. Tweets are stored in a list, so that a user's latest tweets are always on display.
  • Leaderboards which display your score, your relative position in the scoring system, and system-wide high scores, are simple to implement with a Redis sorted set.
  • Reddit-like voting systems, where submitted articles are moved up or down a page based on the number of upvotes or downvotes.
  • Because Redis includes a simple way to allow data to expire, it is an excellent way to keep a rolling count of something.
  • Many online games use Redis to track user's scores and session data. Gaming is an excellent platform for Redis, because speed is more important than data persistence.


Redis Security

Security is always a big concern when handling and storing any kind of data. To learn the basics of Redis security, and how to harden your Redis installation, see our article Securing a Redis Installation. This tutorial includes step-by-step instructions on basic security "best practices," including how to set up a Firewall Policy, bind Redis to Localhost, configure AUTH, and rename or disable "dangerous" commands.

Distributed Data Storage and Processing with Redis Cluster

Redis Cluster is a distributed implementation of Redis which spreads the processing over multiple servers. This can greatly increase the throughput and stability of a Redis installation. Redis Cluster also automatically shards the data across all of the servers in the cluster, which reduces the number of possible failure points and makes the installation more fault-tolerant.