Chapter 14: Redis

Now that you have successfully leveraged two major databases with Node.JS—MongoDB and MySQL—it’s time learn about Redis.

Redis is a database, but it would be more accurately described as a datastructure server, a definition that resonates more with MongoDB than MySQL.

Instead of interacting with rows in tables or documents in collections, you always access data in Redis through keys. As a result, you can think of Redis as a key-value store like the following JavaScript object:


    ‘key’: ‘some value’

  , ‘key.2’: ‘some other value’


But because it’s a datastructure server, the value is not always a simple string. The following are the fundamental data types that you can assign to keys in Redis:





sorted set

A fundamental difference between Redis and MongoDB, however, is that Redis document structure is always flat. Even though a key, for example, can contain something that resembles a JavaScript object very closely—a hash—that hash can’t contain nested data structures like it could in MongoDB.

Another fundamental difference is how you persist data. Redis is designed to be an extremely fast in-memory store with configurable persistence to disk. It’s important to remember persistence to disk matters because anything stored in memory is volatile and susceptible to be lost upon a system crash or reboot.

What this means is that Redis usage and configuration should be carefully reviewed prior to its deployment for sensitive information ...

Get Smashing Node.js: JavaScript Everywhere, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.