Redis as Object Storage
By: Thenusan Santhirakumar
Tags: Redislabs, Redis, In Memory, Object Storage, NoSQL

Redis Episode -5

Redis as Object Storage

As we discussed before the Redis is an in-memory database where we can store our data as key-value pairs. Redis has many features, like data persistence and high availability. I have decided to discuss how objects can be stored in the Redis database in this article. First, I am going to introduce you some new terms before jump deep into this topic.

Photo by Danny Feng on Unsplash

Object

Object means an abstract data type. An object may have various properties and methods and may even carry other objects as well. Objects are represented as classes in most programming languages. Objects provide a structured path to programming.

As the Redis is a key-value store, we may not simply store the complex objects directly. So there we need an approach to handle the objects to store in the Redis database.

Object Serialization

Serialization means transforming an object into a stream of bytes to store the object in memory. The main goal is to save the state of an object from being able to recreate it when required. The reverse process of serialization is called deserialization.

Clients

A client in programming is a computer program that depends upon another program to function. There are clients available for many technologies in all widely used programming languages, for example, MySQL has clients in JAVA, Python, NodeJS, etc. Likewise, the Redis also has clients in many different programming languages. When we are using clients, we don’t have to think too much about how the internal connections are happening. JAVA client for Redis is called Jedis. Jedis give us an excellent interface to use Redis very easily through JAVA. We may easily download the Jedis jar file from here.

Object Storing Procedure

In the Redis, Everything can be stored as only key-value pair format. Key must be unique and storing an object in a string format is not a good practice anyway. Objects are usually stored in a binary array format in the databases. Here we are also going to use the binary array format to store the object. Jedis allows us to work with the binary arrays using BinayJedis() constructor.

Redis Instance Creation

We are creating a BinaryJedis Object in a thread safety manner in this method. The “jedis” variable is referenced as BinaryJedis. The method ensures that there is only one object is created by calling this method. The new object is created when the “jedis” variable is null. Otherwise, the method returns the already created object. HOST and PORT are the host address and port number of the Redis deployment.

I am going to use apache’s serialization library to serialize and deserialize objects. You can see the apache’s documentation to get more knowledge on that library usage. Two methods shown below are used for serializing and deserializing.

I am choosing a simple object to demonstrate purposes.

Here, I define a sample class called human with two properties such as age and name. Below code is the demonstration part of storing and retrieving the sample object with the Redis.

Initially, We should get the jedis object through the getJedisInstance() method. After that, I create a sample object called h1 using the human class with parameters as 12 and “Alias”. Now the object is created. Then we need to serialize the object into a byte array format to be stored in the Redis using the BinaryJedis. We need to cast the object to the serializable interface to serialize the object. The key is converted to a byte array as the BinaryRedis set method requires the key and value to be in the form of the byte array. The object is stored in the Redis while the program executes this line.

jedis.set(“human_1”.getBytes(), h1_obj);

We need to use the key in the byte array format while retrieving the object from the Redis also. The object will be in the byte array format when we retrieve the object from the Redis database. We need to deserialize it to get the actual human object. We must cast it to the human object after deserialization.

object h2_obj = deserialize(jedis.get("human_1".getBytes()));

human h2 = (human)h2_obj;

We need to test whether both objects h1 and h2 are identical. So the get_age() method of the human class is called on top off h1, h2 and values can be compared. 12 will get printed in terminal twice while these lines are executed.

System.out.println(h1.get_age());

System.out.println(h2.get_age());

So we can store and retrieve an object with the Redis database successfully.

I believe that you understood the topic discussed today. If you have any concerns or any clarifications, don’t hesitate to contact me through the response section. Thank you for spending your precious time to read this article.

Cheers….

Photo by Jon Tyson on Unsplash

References