Databases Reference
In-Depth Information
Designing a Redis data model
The most important rules of designing a Redis data model are: Redis does not
support ad hoc queries and it does not support relations in the same way than
relational databases. Thus, designing a Redis data model is a total different ballgame
than designing the data model of a relational database. The basic guidelines of a
Redis data model design are given as follows:
• Instead of simply modeling the information stored in our data model, we
have to also think how we want to search information from it. This often
leads to a situation where we have to duplicate data in order to fulfill the
requirements given to us. Don't be afraid to do this.
• We should not concentrate on normalizing our data model. Instead, we
should combine the data that we need to handle as an unit into an aggregate.
• Since Redis does not support relations, we have to design and implement
these relations by using the supported data structures. This means that
we have to maintain these relations manually when they are changed.
Because this might require a lot of effort and code, it could be wise to simply
duplicate the information instead of using relations.
• It is always wise to spend a moment to verify that we are using the correct
tool for the job.
NoSQL Distilled , by Martin Fowler contains explanations of
different NoSQL databases and their use cases, and can be
found at http://martinfowler.com/books/nosql.html .
As we learned in Chapter 1 , Getting Started , Redis supports multiple data structures.
However, one question remained unanswered: which data structure should we use
for our data? This question is addressed in the following table:
Data type
Description
String
A string is good choice for storing information that is already converted
to a textual form. For instance, if we want to store HTML, JSON, or
XML, a string should be our weapon of choice.
List
A list is a good choice if we will access it only near the start or end. This
means that we should use it for representing queues or stacks.
Set
We should use a set if we need to get the size of a collection or check if
a certain item belongs to it. Also, if we want to represent relations, a set
is a good choice (for example, "who are John's friends?").
Sorted set
Sorted sets should be used in the same situations as sets when the
ordering of items is important to us.
Hash
A hash is a perfect data structure for representing complex objects.
Search WWH ::




Custom Search