Reader small image

You're reading from  Redis Stack for Application Modernization

Product typeBook
Published inDec 2023
PublisherPackt
ISBN-139781837638185
Edition1st Edition
Right arrow
Authors (2):
Luigi Fugaro
Luigi Fugaro
author image
Luigi Fugaro

Luigi Fugaro's first encounter with computers was in the early 80s when he was a kid. He started with a Commodore Vic-20, passing through a Sinclair, a Commodore 64, and an Atari ST 1040, where he spent days and nights giving breath mints to Otis. In 1998, he started his career as a webmaster doing HTML, JavaScript, Applets, and some graphics with Paint Shop Pro. He then switched to Delphi, Visual Basic, and then started working on Java projects. He has been developing all kinds of web applications, dealing with backend and frontend frameworks. In 2012, he started working for Red Hat and is now an architect in the EMEA Middleware team. He has authored WildFly Cookbook and Mastering JBoss Enterprise Application Platform 7 by Packt Publishing.
Read more about Luigi Fugaro

Mirko Ortensi
Mirko Ortensi
author image
Mirko Ortensi

Mirko Ortensi earned a degree in Electronic Engineering and a Master's degree in Software Engineering. Mirko's career has spanned several roles from Software Engineering to Customer Support, particularly centered around distributed database systems. As a Senior Technical Enablement Architect at Redis, Mirko shares technical knowledge about Redis's products and services.
Read more about Mirko Ortensi

View More author details
Right arrow

Redis Stack as a Document Store

The traditional data modeling technique of using Redis’ core data types is possible with Redis Stack, which means that we can model an object using the popular Hash data structure, for example. This chapter presents Redis Stack’s capability to perform real-time queries and searches against the Hash data type, with the advantages of secondary indexing and additional search features. After that, we will discover how data modeling can be addressed using the JSON data structure. We will present different search and query features, from full-text to tagging, from aggregation to auto-completion, so that you understand how to use Redis Stack as a document store with advanced features.

By the end of this chapter, you will know how to rethink the data model of your application and use Redis Stack data structures to perform real-time queries and searches. You will also know how to use Hash and JSON data structures to store, query, and search documents...

Technical requirements

To follow along with the examples in this chapter, you will need the following:

  • Install Redis Stack Server 7.2 or a later version on your development environment. Alternatively, you can create a free Redis Cloud subscription to achieve a free plan and use a managed Redis Stack database.
  • The dataset that will be used in the examples is a conversion of the popular MySQL World database to Redis Hashes.
  • We will introduce data modeling through Hash and JSON data structures with examples that can be tested using a simple KnowledgeBase dataset.
  • Find and download all the datasets from this book’s repository if you’d like to test the examples that we’ll propose in this chapter: https://github.com/PacktPublishing/Redis-Stack-for-Application-Modernization.

Storing and querying documents in Redis Stack

The usual approach to organizing information that helps to describe a system is to identify the entities in the specific business domain and the relationships interconnecting them. Examples of entities could be companies and employees, and the relationship interconnecting them would describe the employee as part of the headcount. Other examples include universities and students, cars and their components, and so on. This high-level description is referred to as the conceptual data model, where we describe the things that are interesting for the domain we are considering.

Once this synthetic description has been completed, we refine it into a logical data model by describing all the elements in detail. Here, the entities and relationships are defined more specifically, with attributes, keys, and data types (for example, strings or integers). Finally, when the domain description is completed and we need a concrete implementation to manage...

Working with Hashes

The capabilities we’ve introduced so far have been explored through concise examples using cities and countries from the World dataset. In this section, we’ll consider a full-fledged use case, such as an application that stores and searches documents: a knowledge base (along the same lines, we could think of a learning management system, a blogging platform, or a generic and extensible content management system (CMS) or even a customer relationship management (CRM) tool). This kind of application works pretty well to present a walk-through of Redis Stack when used as a document store as we need solid classification and search capabilities to provide the best user experience, together with friendly user flows and, as usual, the real-time performance that is only possible when all the data is stored in the main memory. Let’s proceed to imagine what data we would store in our knowledge base to model a document:

  • The title.
  • The content...

Working with JSON

The JSON format does not need too much of an introduction. As a JavaScript native object, born for lightweight communication between web clients and servers, and adopted in general for electronic communications, it is supported by most client libraries and databases. Redis Stack does not make exceptions and extends the data modeling capabilities to JSON objects, together with the indexing features we’ve learned so far. Using the JSON data structure in Redis Stack, you can store, retrieve, and update JSON documents efficiently using the popular JSONPath syntax. The many commands to manipulate strings, counters, arrays, and object literals, and all the data stored in a JSON document, help address the requirements of several data modeling problems.

The JSONPath syntax

The JSONPath syntax helps with accessing a single element or multiple elements within a JSON document.

Using the JSON.* suite of commands together with JSONPath, you can work with the JSON...

Redis Stack as a recommendation engine

Typically, we would retrieve documents based on their data, which means that we would resort to different indexing methods to perform a search, such as TEXT, TAG, or NUMERIC. However, to provide realistic recommendations, we can’t just rely on the content or taxonomy of the information stored in a database – we must also rely on other methods that take into account the popularity and feedback from users who may have rated that content. This leads to the introduction of another variable: the relevance of the results. As an example, if a certain item is rated to be top-quality and affordable, our database should return this item rather than other items that are inferior or more expensive and sort the results by relevance.

In addition to searches based on the relevance of the documents, another type of recommendation can be based on the appearance of an item, or other properties that are also intrinsic, such as a textual description...

Redis Stack as a session store

Now that we have seen the principal features of the two main data structures that can be created, indexed, and searched in Redis Stack, let’s consider a conclusive example to understand what the Hash and JSON data structures offer to one of the most classical use cases: the session store. In Chapter 2, Developing Modern Use Cases with Redis Stack, we highlighted the importance of making session data available outside of the application server for different reasons, such as the scalability of the session store, high availability, load balancing, and, in the case of a session store that uses Redis as a backend, achieving real-time performance.

Redis offers many options to store and retrieve data efficiently. However, sessions store different types of data: metadata, lists, geographical locations, and entire objects. Finding the right data structure, using low-complexity data access patterns, and managing session expiration in a highly concurrent...

Summary

In this chapter, we dived into the modeling and indexing capabilities of Redis Stack. In particular, we experimented with the query syntax on some sample databases and learned about the possibilities that are offered by the search and aggregation features. In addition, we discovered several chances to enrich our applications using spellcheck, auto-completion, highlighting, summarizing, stemming, and more.

Once you were familiar with modeling, indexing, and searching through a database of Hash or JSON documents, you discovered how to implement a recommendation system using a scoring approach. We finished this chapter by modeling a session using Redis Stack’s capabilities, with examples and the trade-offs of using one data structure rather than another provided. While the many features considered in this chapter provide a rich application design and development experience, more features will be added to Redis Stack, such as the ability to index new geometries, or new...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Redis Stack for Application Modernization
Published in: Dec 2023Publisher: PacktISBN-13: 9781837638185
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
Luigi Fugaro

Luigi Fugaro's first encounter with computers was in the early 80s when he was a kid. He started with a Commodore Vic-20, passing through a Sinclair, a Commodore 64, and an Atari ST 1040, where he spent days and nights giving breath mints to Otis. In 1998, he started his career as a webmaster doing HTML, JavaScript, Applets, and some graphics with Paint Shop Pro. He then switched to Delphi, Visual Basic, and then started working on Java projects. He has been developing all kinds of web applications, dealing with backend and frontend frameworks. In 2012, he started working for Red Hat and is now an architect in the EMEA Middleware team. He has authored WildFly Cookbook and Mastering JBoss Enterprise Application Platform 7 by Packt Publishing.
Read more about Luigi Fugaro

author image
Mirko Ortensi

Mirko Ortensi earned a degree in Electronic Engineering and a Master's degree in Software Engineering. Mirko's career has spanned several roles from Software Engineering to Customer Support, particularly centered around distributed database systems. As a Senior Technical Enablement Architect at Redis, Mirko shares technical knowledge about Redis's products and services.
Read more about Mirko Ortensi