Learnitweb

Working with Redis Serialization and Codecs in Tests

In the previous lecture, we successfully created our first Redis test using a simple key-value pair. Hopefully, that exercise felt straightforward and intuitive.

Now, before we move on, let’s explore something important related to how Redis stores and retrieves data — serialization and codecs.


Understanding the Redis Documentation

Redis provides excellent official documentation that covers a wide range of examples and use cases. You can access it by visiting the Redis Java client documentation (Redisson or Lettuce depending on your setup) — for this tutorial, let’s assume you’re using Redisson.

When you open the documentation:

  1. Select English as the language.
  2. You’ll see a Table of Contents listing various sections and detailed examples.
  3. Among these, an important topic to focus on is Data Serialization.

Data Serialization in Redis

Serialization is the process of converting an object into a byte stream (or another transferable format) so it can be stored or transmitted. Deserialization is the reverse process — converting that byte stream back into an object.

Redis clients (like Redisson) come with several built-in Codec implementations that handle this serialization/deserialization process.

Here are some commonly used codecs:

  • StringCodec – Used to store and retrieve data as plain strings.
    Ideal for simple key-value operations like "name" -> "Sam".
  • LongCodec – Used when you want to store numeric values like Long types efficiently.
  • ByteArrayCodec – Used to store raw binary data directly.
  • JsonJacksonCodec – Converts Java objects to JSON and back using the Jackson library.
    Great for complex object serialization when you want human-readable storage.
  • AvroCodec – Used for serialization using the Avro format.
  • MessagePackCodec – Uses MessagePack, a lightweight binary serialization format that’s faster and more space-efficient than JSON.
  • ProtocolBuffersCodec – Uses Protocol Buffers (Protobuf), a format by Google that’s ideal for fast, compact, and schema-driven data serialization.

Each codec has its use case — for instance, if you’re dealing with structured Java objects, you might prefer JsonJacksonCodec or ProtocolBuffersCodec. For primitive types or simple data, StringCodec or LongCodec works perfectly.


Using Codecs in Your Test

Let’s revisit the Redis test we created earlier. Previously, we stored a simple key-value pair, but by default, Redis uses a built-in serialization mechanism. Now, we’ll explicitly specify a codec to control how data is stored.

You can do this by using the following syntax:

RBucket<String> bucket = redisson.getBucket("user", StringCodec.INSTANCE);
bucket.set("Sam");

Here, we’re telling Redis to use StringCodec for this particular bucket. That means the data will be stored as a plain string in Redis, which is easy to view and debug.


Observing the Difference

If you execute this test, you might not see any visible difference in the test output — everything should still pass. However, the underlying storage format in Redis will differ depending on the codec you use.

For instance:

  • Using StringCodec, the value will appear as a simple readable string in Redis (e.g., "Sam").
  • Using JsonJacksonCodec, the same data might appear as a JSON object (e.g., {"name":"Sam"}).
  • Using MessagePackCodec or ProtocolBuffersCodec, it will be stored as a binary sequence, which may not be human-readable.

Handling Connection Warnings

You might encounter warnings or messages like missing 4G or connectivity-related errors during testing. These can safely be ignored if your tests still pass, as they often indicate temporary or local environment issues rather than problems with Redis configuration.


Verifying the Data in Redis

Once you’ve run the test, you can verify the stored data using your Redis CLI:

127.0.0.1:6379> GET user
"Sam"

If you’re using a binary codec, you may not get readable output here — that’s expected since the data isn’t stored as plain text.


Summary

In this tutorial, we learned how Redis clients like Redisson handle serialization using codecs. Here’s what you should remember:

  1. Serialization determines how your data is stored and retrieved from Redis.
  2. Redisson provides multiple codecs like StringCodec, JsonJacksonCodec, MessagePackCodec, and more.
  3. Choosing the right codec depends on your use case:
    • For simple data, use StringCodec.
    • For JSON or complex Java objects, use JsonJacksonCodec.
    • For compact binary storage, consider ProtocolBuffersCodec or MessagePackCodec.
  4. You can verify serialization results using the Redis CLI to observe how data is actually stored.

In the next section, we’ll explore how to serialize and deserialize custom Java objects using different codecs and understand how these affect performance and readability.