{ Lightweight Storage with Redis. }

Objectives:

By the end of this chapter, you should be able to:

  • Explain what Redis is and how to use it
  • Compare and contrast data structures that Redis provides
  • Highlight use cases for databases like Redis

Introduction

From the docs

Redis is an open source, in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

Memory vs Disk

Redis typically holds the whole dataset in memory. Persistence is now achieved in two different ways: one is called snapshotting, and is a semi-persistent durability mode where the dataset is asynchronously transferred from memory to disk from time to time, written in RDB dump format.

A safer alternative is AOF, an append-only file (a journal) that is written as operations modifying the dataset in memory are processed. Redis is able to rewrite the append-only file in the background in order to avoid an indefinite growth of the journal.

By default, Redis writes data to a file system at least every 2 seconds, with more or less robust options available if needed. In the case of a complete system failure on default settings, only a few seconds of data would be lost.

An advantage of in memory databases is that the memory representation of complex data structures is much simpler to manipulate compared to the same data structure on disk, so Redis can do a lot, with little internal complexity. RAM is incredibly fast, but usually quite limited and expensive.

You can read more about it here

What Redis Offers

Although Redis is often called a key-value store, it does not simply just store keys and values. Redis provides access to many different kinds of data structures to serve different use cases. The values that Redis can store are far more than just "strings" and include

  • Lists (implemented as Linked Lists)
  • Sets
  • Sorted Sets
  • Hashes
  • and more!

Depending on the use case, each of these data structures aim to solve different problems with the optimal time and space complexities.

Installing

  1. brew install redis
  2. brew services start redis
  3. redis-cli (exit to get out)

Useful Redis commands

Where Redis is used

Some of the most common use cases for Redis include:

  • page caching
  • session caching
  • a message queue
  • leaderboards

Quite a few companies use Redis at very large scale including Twitter, GitHub, Pinterest, and Craigslist. You can read more about how these companies are using Redis here

Additional Resources

Redis Introduction
Try Redis
Redis in Action eBook
The Little Redis Book

When you're ready, move on to Authentication with Passport Local

Continue

Creative Commons License