After I compressed data by Snappy, and then stored it to Redis. But When I took it out of Redis, I found the data is different. I don't know why it is
2 Answers
Yes, Snappy compression algorithm can be used with Redis. In fact, you can use any compression algorithm. As far as Redis is concerned, it is just bytes. What redis calls as "Strings" is more traditionally called as "byte array" in programming languages.
If you are seeing different values, it is most likely a problem with the way you are reading values. Some redis client libraries try to decode the response as UTF-8, and that isn't what you want it to do. Look at your client library documentation, and use the method that lets you set and get values as byte arrays.

- 30,948
- 4
- 76
- 83
-
agree. you should clear mentioned either you want to use `UTF-8` or `UTF-16` – roottraveller Jul 23 '19 at 05:36
Yes is the answer. If your problem is on java. You can check this repo here, with what i had done for compression and serialization java-spring-redis-compression-snappy-kryo
java-spring-redis-compression-snappy-kryo
Here is a sample of a Java Spring Boot Redis Cluster Data configuration.
It is an implementation with Redis Cluster and Redis Cache Manager.
Snappy Compression
Kryo Serialization
Support ttl per cache key
Gradle configuration
- spring-data-redis
- lettuce-core
- snappy-java
- kryo
- commons-codec
Application Properties
data.rediscluster.nodes=redis1.foo.com:6379,redis2.foo.com:6379,redis3.foo.com:6379,redis4.foo.com:6379,redis5.foo.com:6379,redis6.foo.com:6379
data.rediscluster.ttl=2700
data.rediscluster.redirects=3
How to use it
RedisCacheConfiguration.defaultCacheConfig()
.disableKeyPrefix()
.disableCachingNullValues()
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(genericSnappyRedisSerializer()))
.entryTtl(Duration.ofSeconds(ttl));

- 49
- 2
- 9