Manually creating a Redis schema

This graphic outlines our basic scenario of a simple two-product online storefront from which we will construct our Redis schema.

Manually creating a Redis schema

Imagine that you have an online storefront selling paper products and you want to manage different stationary products that are offered for sale. Embedded within this seemingly simple business need, the following narrative starts with these separate steps:

  1. An online customer comes to our website looking to buy paper stationery.
  2. We offer two choices of paper stationery: a blue rectangle package of 20 sheets printed on rice paper and a red square, and a 15-sheet stationery package also printed on rice paper.
  3. The basic entity in our example is a package of stationery that has three basic properties: color, height, width, and number of sheets. (Until we start selling paper stationery made out of non-rice paper, we will ignore material as a property. Another future enhancement would be to add a more friendly, human name for each stationery package).
  4. Managing a small inventory of these two types of stationery, we record a sale when a customer purchases a paper package from our website, noting the time and the amount received, as well as decreasing the inventory by the number of packages sold.

The first step in manually creating a Redis schema is establishing a global stationery counter appended to a stationery prefix for the type and brand of stationery that we are offering for sale. We'll store the color and dimension properties as fields in a stationery:{id-counter} hash and store the number of sheets in a separate string value in the stationery:{id-counter}:sheets key. These data structures can be demonstrated with the following commands by using the Redis CLI program connecting to an instance of Redis running on a local host:

127.0.0.1:6379> INCR global:stationery1

The returned integer 1 will be used as the id counter for the first stationery:

127.0.0.1:6379> HMSET stationery:1 color blue width '30 cm' height '40 cm'OK

To associate the number of sheets for the stationery:1:sheets set, we use the INCREBY command:

127.0.0.1:6379> INCRBY stationery:1:sheets 20(integer) 20

Now, we call the INCR command again to generate the second id counter for our second stationery type, populate the hash, and increment by 15 the number of sheets:

127.0.0.1:6379> INCR global:stationery(integer 2)127.0.0.1:6379> HMSET stationery:2 color red width '45 cm' height '45 cm'15127.0.0.1:6379> INCRBY stationery:2:sheets 15(integer) 15

Next, the inventory of stationery packages for a particular type is stored using the following key pattern, stationery:<stationery id>:inventory, with the value in that key being a simple integer representing all of the available packages for that type of stationery. This can be illustrated through the following Redis-CLI commands where an initial inventory of 250 packages is set as an integer:

127.0.0.1:6379> SET stationery:1:inventory 250

When one or more packages are sold, the integer value stored in stationery:1:inventory is decremented by the number of packages. Likewise, when new packages of the stationery arrive from the distributor, the key is incremented by the number of new stationery packages.

127.0.0.1:6379> DECR stationery:1:inventory 127.0.0.1:6379> INCRBY stationery:1:inventory 10

Sales information for each package is stored as a sorted set by using a Unix timestamp (an integer that represents the time since the epoch) as the score and the amount of sale as the value in a sorted set row. Using the Redis key of stationery:1:sales for the sorted set, the Redis command for documenting a twenty-dollar sale from the Redis CLI would be as follows:

127.0.0.1:6379> ZADD stationery:1:sales 1430861194 20.00

Even with this simplistic example, having a common pattern for the Redis keys gives the Redis application a method of relating information between our data in an online store for stationery.

The following diagram shows how the information for each stationery type is clustered and how sales and other information can quickly be extracted from the datastore by using Redis.

Manually creating a Redis schema