Reader small image

You're reading from  Seven NoSQL Databases in a Week

Product typeBook
Published inMar 2018
PublisherPackt
ISBN-139781787288867
Edition1st Edition
Right arrow
Authors (2):
Sudarshan Kadambi
Sudarshan Kadambi
author image
Sudarshan Kadambi

Sudarshan has a background in Distributed systems and Database design. He has been a user and contributor to various NoSQL databases and is passionate about solving large-scale data management challenges.
Read more about Sudarshan Kadambi

Xun (Brian) Wu
Xun (Brian) Wu
author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu

View More author details
Right arrow

Chapter 4. Redis

Redis is a popular data store utilized by businesses of all sizes, from start-ups to large enterprises. It can function as a data store, cache layer, and a message broker. Redis mainly holds its data in memory, making operations lightning fast.

In this chapter, we will explore and learn about several aspects of Redis, including:

  • Key features
  • Appropriate use cases
  • Data modeling
  • Anti-patterns
  • How to leverage Redis with different tools and languages such as:
    • redis-cli
    • Lua
    • Python
    • Java

After reading through this chapter, you will gain an insight into how Redis works, and how to leverage it to solve interesting problems. You will learn how to install and configure Redis, as well as how to back up and restore its persistence layer. Application examples will help you to round out your approach to Redis. These will provide demonstrations on several topics, including querying Redis from the application layer and utilizing the publish/subscribe features.

Let's start with a quick introduction...

Introduction to Redis


REmote DIctionary Server (Redis) is an open source, key-value, single-threaded, in-memory data store that is commonly referred to as a data structure server.[9] It is capable of functioning as a NoSQL database, key/value store, a cache layer, and a message broker (among other things). Redis is known for its speed, as it can store complex data structures in memory, and serve them to multiple users or applications.

Redis was primarily designed to serve as an in-memory cache, intended to support atomic operations on a single server. It was written (in C) by Salvatore Sanfilippo, who used it to replace the MySQL instance running at his start-up. Clustering options are available (as of Redis 3.0) with the advent of Redis Cluster. It is important to note that in terms of distributed systems, these two configurations do behave differently:

A diagrammatic representation of the CAP theorem, using corners of a triangle to denote the design aspects of consistency, availability,...

What are the key features of Redis?


Redis has many features that make it an attractive option for an application data store. Its penchant for performance makes it a favorite of developers. But there are several other additional points of functionality that make Redis unique.

Performance

The underlying idea behind Redis is very straightforward: to read and write as much data as possible in RAM. As the majority of the operations do not include disk or network I/O, Redis is able to serve data very quickly.

Tunable data durability

The underlying architecture that allows Redis to perform so well is that data is both stored and read from RAM. The contents of RAM can be persisted to disk with two different options:

  • Via a forked snapshot process which creates an RDB file
  • Using Append-only Files (AOF), which saves each write individually

While using the AOF option has a direct impact on performance, it is a trade-off that can be made depending on the use case and the amount of data durability that an application...

Appropriate use cases for Redis


Redis is known for being a very fast data store. And as developers, who doesn't want the fastest data store? The far more complicated question is: when is Redis the right choice for a data store? Let's take a look at some project aspects that could help determine when a use case might work well with Redis.

Data fits into RAM

If your entire dataset can fit into memory, then Redis may be a good choice. Using Redis in this scenario should drastically limit (or possibly eliminate) latency due to disk I/O.

Data durability is not a concern

In looking at your intended dataset objectively, what would happen if it was lost? If your tenant application is simply caching data that is helpful, but not necessarily mission-critical, then Redis might be a good solution. Or if you can reload data for your Redis instance from another source of truth system quickly, then Redis may also be a good fit for your application.

For example, if your application is simply using Redis as an...

Data modeling and application design with Redis


Redis performs quite well without a whole lot of configuration or tuning. However, there are things that can be done on the application side to ensure that your instance will continue to perform well.

Taking advantage of Redis' data structures

Redis ships with tools and types that are intended to make development easier. It has structures to simplify tasks, such as maintaining a queue of sequential events, or lists of unique items (ordered or unordered). Chances are that one of Redis' delivered types will suit how your application needs to store or manage data.

Queues

Unlike many data stores, Redis can easily support queue-like functionality. Updates and deletes can be performed with minimal overhead, and it has data types available to work with Last In First Out (LIFO) and First In First Out (FIFO) queuing scenarios. Redis can also keep the number of items in a queue at the desired size, as well as provide methods for adding an item to the top...

Redis anti-patterns


Redis is a favorite data store of many developers due to its raw performance. However, it is important to make sure that it is being used properly, otherwise, problems can and will occur.

Dataset cannot fit into RAM

Redis' greatest asset is its speed, due to the fact that data is written to and read from RAM. If your dataset cannot fit into RAM, then disk I/O begins to enter the picture (as memory is swapped to disk) and performance can quickly degrade.

Note

Larger-than-RAM datasets were previously managed in Redis by a feature known as disk store, but it has since been deprecated.

Modeling relational data

Several good tools exist for storing, serving, and managing relational data; Redis is not one of them. As Redis requires a query-based modeling approach, reading relational data may require multiple operations and potentially application-side joins. Additionally, Redis is not an ACID-compliant database, which can further complicate write operations if the relational-like...

Redis setup, installation, and configuration


Redis is designed to run with a small footprint and provide quick access to its in-memory data. This allows it to be an effective data store on commodity hardware, cloud instances, and containers. Bearing these aspects in mind, there are a few hardware recommendations that make sense to follow.

Virtualization versus on-the-metal

As Redis IO indicates in its documentation,[6] it is preferable to deploy Redis on a physical machine over a VM. This is because a VM will have a higher intrinsic latency, or rather latency that we cannot improve upon with any amount of server or application configuration.

The redis-cli does have a means by which to measure intrinsic latency. Simply run the following on your Redis server (not the client), from the redis directory. It will measure latency on the machine (Redis does not need to be running) for a period of 30 seconds:

src/redis-cli --intrinsic-latency 30

Running this command (after installing Redis) will return...

Using Redis


Now that we have a running server, we will cover some simple examples to explore some of Redis' basic functionality. This section will introduce tools such as redis-cli, as well as examples for interacting with Redis via Python and Java.

redis-cli

Redis comes with the redis-cli command-line tool. This is a simple, yet powerful tool that allows you to write, query, and otherwise manage the key/values stored in your Redis instance. To run redis-cli (as demonstrated in the previous section), you can invoke it from the command line. To avoid the extra step of authentication, I'll send the password along with the -a flag:

src/redis-cli -a currentHorseBatteryStaple -n 0

You can also specify the database number with the -n flag. If you do not specify it, redis-cli will connect to the database 0 (zero) by default. Additionally, you can change databases with the SELECT command:

127.0.0.1:6379> SELECT 0

To start with, let's set a simple message in database 0. We will name the key packt:welcome...

Tips for success


  • Run Redis on Linux or BSD
  • Better performance on a physical machine, than on a VM
  • Be sure to open port 6379
  • If using Redis Cluster, then also open the cluster bus port, which has an offset of +10000 (default would be 16379)
  • Enable security
  • Enterprise support available from Redis Labs

Summary


In this chapter, the Redis data store was presented and discussed, along with acceptable features and use cases. Simple cases for using Redis were also shown, along with a description of what the coded examples intended to accomplish. Redis has been shown to perform very well, with configuration options for data expiration, as well as several available data types to assist with storage. It also has additional features that can be used for message brokering, counter types, and server-side scripting.

It is important to note that there are some cases where Redis may not perform optimally. Care should be taken to properly configure things such as security and THP, as well as taking care to follow good practices in the application code, such as avoiding the use of the KEYS command and unnecessary trips over the network. The intended dataset should also be inspected, so as to be sure that it fits into RAM, and that it is not relational in nature.

Redis is flexible, lightweight, easy to use...

References


  1. Grunwald A. (2017). Learn Redis the Hard Way (In production). Trivago Tech Blog. Retrieved on 20170617 from: http://tech.trivago.com/2017/01/25/learn-redis-the-hard-way-in-production
  2. Morgan-Walker D. (2016). A Speed Guide To Redis Lua Scripting. Compose - An IBM Company. Retrieved on 20170923 from: https://www.compose.com/articles/a-quick-guide-to-redis-lua-scripting/
  3. O'Rourke B.P. (2107). Lua: A Guide for Redis Users. Redis Green - Stovepipe Studios. Retrieved on 20170923 from: https://www.redisgreen.net/blog/intro-to-lua-for-redis-programmers/
  4. Redis.IO (2017). Redis Cluster Specification. Redis documentation. Retrieved on 20170617 from: https://redis.io/topics/cluster-spec
  5. Redis.IO (2017). FAQ - Redis. Redis documentation. Retrieved on 20170624 from: https://redis.io/topics/faq
  6. Redis.IO (2017). Redis latency problems troubleshooting. Redis documentation. Retrieved on 20170701 from: https://redis.io/topics/latency
  7. Redis.IO (2017). Pub/Sub. Redis documentation. Retrieved on 20171001...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Seven NoSQL Databases in a Week
Published in: Mar 2018Publisher: PacktISBN-13: 9781787288867
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Sudarshan Kadambi

Sudarshan has a background in Distributed systems and Database design. He has been a user and contributor to various NoSQL databases and is passionate about solving large-scale data management challenges.
Read more about Sudarshan Kadambi

author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu