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

The Programmability of Redis Stack

In this chapter, we’ll explore Redis Stack’s programmability features and show you how they can be used in different situations. For quite a while, software engineers and database administrators have been making the most of stored procedures in relational databases. There are several good reasons for that: managing SQL procedures all in one place, easily troubleshooting procedure flows, staying separate from the chosen client library, and, most importantly, getting things done quickly.

Redis started off by allowing users to write Lua scripts and run them locally, giving them a taste of this capability. But with Redis Stack, server-side programming has been taken to a whole new level. You can now write intricate business logic in either Lua or JavaScript and execute it as close to your data as possible. There is no need for back-and-forth trips to a distant server.

In this chapter, we’ll show you how to implement complex...

Technical requirements

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

  • Install Redis Stack Server 7.2 or later on your development environment. Alternatively, you can create a free Redis Cloud subscription to get access to a free plan and use a managed Redis Stack database.
  • Create a clustered version to test the triggered remote functions using Redis Stack. You can refer to Chapter 12, Managing Development and Production Environments, to learn how to do it.
  • You can download and import the World dataset from this book’s repository if you’d like to test the examples that we propose in this chapter: https://github.com/PacktPublishing/Redis-Stack-for-Application-Modernization.

The single-threaded architecture

Redis incorporates a single-threaded architecture, which is a distinctive design in the realm of databases and caches. While “single-threaded” may suggest that only one thread runs within the Redis process, this assumption is not entirely accurate. A Redis server comprises multiple threads dedicated to specific tasks, such as key expiration, statistics, and threaded I/O to handle concurrent connections. However, the primary focus is on the main thread responsible for executing commands and managing data storage and retrieval. This single-threaded design ensures that commands are processed sequentially, avoiding complex synchronization issues such as deadlocks and long-running locks. By eliminating interaction between different clients, this implementation reduces the number of stages data and instructions must pass through and enables immediate access to data, a crucial aspect for real-time execution.

With the main thread executing...

Programming complex business logic with Redis Stack

To illustrate the programmability features of Redis Stack, let’s explore scenarios where we can solve problems through a sequence of actions. As an example, to identify click spam, we can employ various methods, such as counting events in a time span using the ZCOUNT method of the sorted set data structure. Using ZCOUNT, we can find the number of clicks from a user during a defined time window. If the count received surpasses a certain threshold, we identify it as anomalous and execute a series of actions, such as throttling user interactions or logging out the user, and maybe sending a warning notification. Another example where we can leverage Redis Stack’s programmability features is in an e-commerce application. When an order is placed, we may need to respond to the updated data and adjust the inventory count accordingly. By incorporating Redis Stack, we can efficiently handle such updates and ensure accurate inventory...

Lua scripting

With Redis, developers can write Lua scripts and execute them within Redis. Lua is a lightweight scripting language known for its simplicity and efficiency, and Redis has leveraged its power to perform complex operations on stored data since Redis 2.6.0. Lua scripting offers several benefits, such as allowing the execution of multiple commands atomically, meaning all the commands within a Lua script will be executed as a single unit of work, ensuring consistency. This is particularly useful to ensure they are executed without interruption or interference from other clients.

By executing Lua scripts, the network round trips between the application and the server are removed, improving performance. Lua scripts are sent to Redis as an atomic block of instructions and executed directly on the server, eliminating the need to send multiple commands and receive responses individually.

Redis provides a Lua interpreter with a set of Redis-specific Lua functions that allow...

Redis functions

Redis functions are an evolution of ephemeral scripting in Redis. They provide similar functionality as scripts but are considered first-class software artifacts within the database. Functions are managed and persisted by Redis, ensuring their availability through data persistence and replication. Unlike scripts, functions are declared before use and do not need to be loaded during runtime.

Here are some advantages of Redis functions over Lua scripts:

  • Persistence and replication: Redis manages functions as part of the database and stores them alongside the data itself ensuring their persistence (RDB snapshots and/or append-only files) and replication along with the data. Functions are considered integral components of the database, making them readily available without the need for external management.
  • Simplified development and code sharing: Functions belong to libraries, and libraries can contain multiple functions. Functions within the same library...

Triggers and functions

The latest addition to Redis Stack is the capacity to respond to specific events happening in your Redis database and define business logic that executes the desired actions. These events can be triggered by changes to the data, such as adding a new key-value pair. When an event occurs, the specified JavaScript function is automatically executed. However, this is not the only way to execute the desired functions: Redis Stack supports two types of triggers for execution while supporting manual user execution. For a summary, see the following:

  • User functions: Functions can be executed manually, using the TFCALL or TFCALLASYNC command
  • Keyspace triggers: These triggers are activated when there are changes to the data stored in Redis, and they execute the desired function
  • Stream triggers: Whenever new items are added to a Stream, these triggers are activated and execute the desired function

The advantage of using triggers is that you can keep...

Summary

In this chapter, we have summarized the evolution of the programmability features of Redis, since the first Lua scripting feature was added in Redis 2.6.0, passing through the introduction of Redis functions in Redis 7, up to the most recent additions to Redis Stack – the introduction of the V8 engine and the ability to write JavaScript functions.

While Lua scripts and functions (which allow complex actions as close to the data as possible to be written) are of great value to developers, JavaScript functions and the asynchronous nature of the V8 engine provide an unprecedented capability to offload part of the processing load to coroutines, while preserving a single-threaded data access model, which is the distinguishing feature of the Redis Server architecture. In addition, the support for clustered environments with remote functions expands the manageability of data when working with sharded databases. Finally, the real-time stream processing capability of Redis...

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