O'Reilly logo

Redis Cookbook by Fred Oliveira, Tiago Macedo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 3. Leveraging Redis

In this chapter, we’ll look into how we can leverage Redis’s data structures, speed, and flexibility to create complex systems and functionality, typically in a fraction of the time we’d spend doing the same with a RDBMS. We’ll start by looking at ways to store simple data sets, and work up from there in terms of complexity and interest.

Using Redis as a Key/Value Store

Problem

Most applications need to store temporary data about usage, configuration, or other relevant information that might not be a great fit for the fixed structure of relational databases. Traditionally, developers have resorted to hacking a table structure to accommodate this data and using MySQL or another RDBMS to store it. In this recipe, we’ll look at how we can use Redis and its built-in data types to store application data in a lighter, faster, and looser manner.

Solution

Redis positions itself not simply as a key/value store but as a server for data structures as well. This means that on top of typical key/value store functionality, it gives you several ways to store and manipulate application data. We’ll use these structures and commands to store application sample data: as examples, we’ll store usage counters in regular keys, user objects in Redis hashes, and a circle-of-friend implementation (like Google+) using sets.

Discussion

Storing application usage counters

Let’s begin by storing something quite basic: counters. Imagine we run a business social network and want to track profile/page ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required