You are previewing Redis Cookbook.

Redis Cookbook

Cover of Redis Cookbook by Tiago Macedo... Published by O'Reilly Media, Inc.

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


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.


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.


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 ...

The best content for your career. Discover unlimited learning on demand for around $1/day.