This book covers the use of Redis, an in-memory database/data structure server.

open all | close all

4.4.1 Defining users and their inventory

We’ll start by showing some structures that define our users and their inventory. User
information is stored as a HASH, with keys and values that store user attributes like
name, funds, and anything else. A user’s inventory will be a SET that holds unique
identifiers for each item, which can be seen in figure 4.2.

Figure 4.2Example user inventory and user information. Frank has 43 e-dollars and an item that he’s considering selling from his inventory.

Our requirements for the market are simple: a user can list an item for a given price,
and when another user purchases the item, the seller receives the money. We’ll also
say that the part of the market we’ll be worrying about only needs to be ordered by
selling price. In chapter 7, we’ll cover some
topics for handling other orders.

Figure 4.3Our basic marketplace that includes an ItemA being sold by user 4 for 35 e-dollars

To include enough information to sell a
given item in the market, we’ll concatenate the
item ID for the item with the user ID of the
seller and use that as a member of a market
ZSET, with the score being the item’s selling
price. By including all of this information
together, we greatly simplify our data structures
and what we need to look up, and get the
benefit of being able to easily paginate
through a presorted market. A small version of
the marketplace is shown in figure 4.3.

Now that we know what structures our marketplace uses, let’s list items in the market.