How To Add Compression to Caching in Spring Boot
Tags: coding, programming, softwaredevelopment, softwareengineering
Published: 2024-01-19 21:13:55

RAM is one of the most expensive resources offered by cloud providers. So, storing all your cached data in in-memory caches comes with a price. This is why it is essential to implement techniques aimed at not wasting it.

Plus, this becomes practically unavoidable when your Spring Boot application and cache server coexist in the same machine, sharing the underlying resources. In fact, the less RAM the cache steals from the application, the better. Also, serialized Java objects are known to take up a lot of space. So, by caching them, your RAM may run out of space very easily.

This is where compression comes into play!

Let's see how to add compression to your caching system in [Spring Boot](https://spring.io/projects/spring-boot) in both Kotlin and Java.

## Compression and Caching

Normally, when dealing with caching in Spring Boot, data is serialized and then stored in the cache. When needed, data is searched, deserialized, and finally returned to the application in its original format, which is represented in this case by Java/Kotlin objects.

![Caching flow graph](https://writech.run/wp-content/uploads/2023/08/image-146.png)

Adding a compression layer means compressing data after serializing it and decompressing it before deserializing it, modifying the normal flow as follows:

![Compressed data caching flow graph](https://writech.run/wp-content/uploads/2023/08/image-147.png)

Compressing data reduces the size of your cache and gives you two options:

1. Reducing the RAM required by the cache server, saving you money.

3. Keeping the cache the same size but allowing you to save much more data.

Both options are great, but compression also introduces overhead. In particular, compression and decompression come with a cost in terms of time, and this may significantly reduce the performance benefit of caching. This represents a trade-off between RAM and CPU, and it is up to you to find out whether this approach fits your particular case or not.

## Implementing the Compression Logic

Keep in mind that the approach presented above can be adopted with any [cache provider](https://docs.spring.io/spring-boot/docs/1.3.0.M1/reference/html/boot-features-caching.html#_supported_cache_providers) supported by Spring Boot. Let's see how to implement it while using a [Redis](https://redis.io/) cache. This can easily be achieved by registering a custom-defined class inheriting from [`JdkSerializationRedisSerializer`](https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/serializer/JdkSerializationRedisSerializer.html) as the default Redis serializer.

First, you need to define a valid Redis serializer implementing the compression and decompression logic as explained in the graph above. You are going to see how to use [GZIP](https://en.wikipedia.org/wiki/Gzip), which is natively implemented in Java, but other compression approaches are possible. Plus, the [Commons IO](https://commons.apache.org/proper/commons-io/) library will be used to keep the decompression logic simple.

If you are a Gradle user, add this dependency to your project's build file:

```groovy
compile "commons-io:commons-io:2.9.0"
```

Otherwise, if you are a Maven user, add the following dependency to your project's build POM:

```xml
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.9.0</version>
</dependency>
```

Now, you have everything required to define a custom Redis serializer.

**Java**

```java
class RedisCacheGZIPSerializer extends JdkSerializationRedisSerializer {
    @Override
    public Object deserialize(
            byte[] bytes
    ) {
        return super.deserialize(decompress(bytes));
    }

    @Override
    public byte[] serialize(
            Object o
    ) {
        return compress(super.serialize(o));
    }

    private byte[] compress(
            byte[] data
    ) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        
        // compressing the input data using GZIP
        try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream)) {
            gzipOutputStream.write(data);
        } catch (IOException e) {
            throw new SerializationException(e.getMessage());
        }

        return byteArrayOutputStream.toByteArray();
    }

    private byte[] decompress(
            byte[] data
    ) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            // decompressing the input data using GZIP
            IOUtils.copy(new GZIPInputStream(new ByteArrayInputStream(data)), out);
        } catch (IOException e) {
            throw new SerializationException(e.getMessage());
        }

        return out.toByteArray();
    }
}
```

**Kotlin**

```kotlin
class RedisCacheGZIPSerializer : JdkSerializationRedisSerializer() {
    override fun deserialize(
        bytes: ByteArray?
    ) : Any {
        return super.deserialize(decompress(bytes))
    }

    override fun serialize(
        o: Any?
    ): ByteArray {
        return compress(super.serialize(o))
    }

    private fun compress(
        data: ByteArray
    ) : ByteArray {
        val byteArrayOutputStream = ByteArrayOutputStream()

        try {
            // compressing the input data using GZIP
            GZIPOutputStream(byteArrayOutputStream).use { gzipOutputStream -> gzipOutputStream.write(data) }
        } catch (e: IOException) {
            throw SerializationException(Constants.COMPRESSION_ERROR_MESSAGE, e)
        }

        return byteArrayOutputStream.toByteArray()
    }

    private fun decompress(
        data: ByteArray?
    ) : ByteArray {
        val out = ByteArrayOutputStream()

        try {
            // decompressing the input data using GZIP
            IOUtils.copy(GZIPInputStream(ByteArrayInputStream(data)), out)
        } catch (e: IOException) {
            throw SerializationException(Constants.DECOMPRESSION_ERROR_MESSAGE, e)
        }

        return out.toByteArray()
    }
}
```

Second, you need to declare the just-defined class as the default Redis value serializer. This can be done by registering a `RedisCacheConfiguration` [primary bean](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Primary.html) in a custom [`@Configuration`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html) annotated class implementing [`CachingConfigurerSupport`](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/cache/annotation/CachingConfigurerSupport.html), as follows:

**Java**

```java
@Configuration
class CacheConfig extends CachingConfigurerSupport {
    @Bean
    @Primary
    public RedisCacheConfiguration defaultCacheConfig() {
        RedisCacheGzipSerializer serializerGzip = new RedisCacheGzipSerializer();

        return RedisCacheConfiguration
                .defaultCacheConfig()
                .serializeValuesWith(SerializationPair.fromSerializer(serializerGzip));
    }
}
```

**Kotlin**

```kotlin
@Configuration
class CacheConfig : CachingConfigurerSupport() {
    @Bean
    @Primary
    fun defaultCacheConfig(
    ) : RedisCacheConfiguration? {
      // registering the custom Redis serializer
        val serializerGzip = RedisCacheGzipSerializer()

        return RedisCacheConfiguration
            .defaultCacheConfig()
            .serializeValuesWith(SerializationPair.fromSerializer(serializerGzip))
    }
}
```

Et voilà! Your Redis cache will now be freer than ever!

## Conclusion

Dealing with in-memory caches might significantly increase the cost of your architecture. This is exactly why you should adopt approaches aimed at reducing the space of your cached data. As we have just seen, adding compression logic to a caching system in your Spring Boot application is not complex, and it allows you to save space and money as a consequence.

Thanks for reading! I hope that you found this article helpful.
  
***
_The post "[How To Add Compression to Caching in Spring Boot](https://writech.run/blog/how-to-add-compression-to-caching-in-spring-boot-d4d21533167c/)" appeared first on [Writech](https://writech.run)._