Redis

Redis OM

Introduction to Redis OM

As computer programmers who work with data, it is a regular assignment to arrange the data to comply with a specific requirement. Data structures make this task easy for developers by providing the proper ways to operate on data. Redis is one of the popular in-memory data stores which supports several robust data structures such as hashes, sorted sets, sets, lists, streams, etc. This opened the doors for implementing the advanced tools like message queues, caches, leaderboards, pub/sub systems, etc. more efficiently and with reduced cost.

The common problem is that it is costly and time-consuming to implement the previously mentioned tools from scratch. So, the development team came up with an abstraction layer to facilitate the mapping between the core Redis data structures and the programming language that they use. Even if a custom abstraction layer is implemented, it might introduce a considerable performance lag. Due to that, you will not be able to get the maximum performance out of Redis data structures.

Redis OM is an object mapper for Redis that helps the developers to work with Redis data structures within their programming environment without additional effort. It provides a higher-level abstraction layer for object mapping. Usually, a specific domain is modeled in an object-oriented way. So, Redis OM provides a set of language-specific client libraries which can be used to persist domain objects in the Redis data store and fetch those with a language-specific API.

Domain Object Mapping with Redis OM

The object mapping capability is one of the major features that come with Redis OM. The Redis OM abstraction layer makes the consumer’s life easy by providing a rich language-specific API. It involves a simple, less time consuming process to follow.

Let’s assume that we are working in an ERP domain and we need to store the employees in the Redis store. With Redis OM in place, we need to define first our “Employee” schema as follows. We will use the OM client library for Node.js here:

class Employee extends Entity {}
const schema = new Schema(Employee, {
  id: { type: 'string' },
  age: { type: 'number' },
  degrees: { type: 'string[]' }
});

The “Entity” and “Schema” classes come from the OM abstraction layer.

Next, an employee instance can be created as follows:

const employeeObj = employeeRepository.createEntity()
employeeObj.id = "1000"
employeeObj.age = 45
employeeObj.degrees = ['BSc', 'MSc', 'PHD']

const employeeObjId = await employeeRepository.save(album)

The “employeeObjId” holds the ULID of the created object that is URL-safe, globally unique, and base32-encoded. It is more similar to the usual UUIDs. Furthermore, the domain entities are serialized in two different ways. The basic objects with a uniform structure is serialized to a Redis hash. The complex objects with tens of fields and nested entities are converted to JSON structure using the RedisJSON module.

Querying Domain Objects with Redis OM

Redis OM provides a query API to fetch the persistent objects in the Redis store. This query API uses the RedisJSON and RedisSearch modules at its base implementation to provide indexing and querying for domain objects. The performance of the querying is efficient because they’re indexed by default.

We can query all the employee objects in the Redis store using the Node.js OM API as follows:

employeeRepository.search().return.all()

Furthermore, the querying can be done in different ways using the filtering options provided by Redis OM fluent query API. The “where” clause can be used as follows:

employeeRepository.search().where(<attribute>).eq(<value>).return.all()
employeeRepository.search().where(<attribute>).gt(<value>).return.all()
employeeRepository.search().where(<attribute>).lt(<value>).return.all()
employeeRepository.search().where(<attribute>).eq(<value>).return.all()

It releases a lot of burdens away from the developers by focusing more on application development.

Redis OM Client Libraries

By the time of writing this article, four main Redis OM client libraries are released for Java spring, .NET, Node.js, and Python languages. The Node.js library is implemented using the typescript and it supports both the typescript and Javascript domains. The .NET specific OM library enables the .NET developers to map and query the domain objects using LINQ.

Furthermore, the Redis OM Python library integrates with the FastAPI framework to provide the synchronous and asynchronous support. Java’s popular spring framework is integrated with the Redis OM to provide the probabilistic data structures.

Conclusion

In short, Redis OM provides a set of language-specific client libraries to map your domain objects to the Redis data structures. It is an abstraction layer that helps you to focus more on your application logic without worrying about how to map a Java or Node.js class to a Redis hash. As indicated, Redis OM focuses on object mapping and querying. The domain objects are serialized into hashes or JSON structures based on the complexity of the object. On the other hand, the querying API uses the RedisJSON and RedisSearch modules. Redis OM supports Java, Node.js, Python, and .NET client libraries.

About the author

Nimesha Jinarajadasa

Being a Full-stack Senior Software Engineer for more than five years, I love technology, as technology has the power to solve our many problems within just a minute. I try to learn more and create more opportunities for this new world.