Skip to main content

Introduces Redis in Java with redis-collections

Posted by otaviojava on August 1, 2014 at 10:45 PM PDT

Redis is a NOSQL database written in C. The Remote Dictionary Server is a key-value data base whose the storage is in memory, then the write and read will fastest way, but which difference between Redis and Cache? What does happen when the database fall down? Will we lost the all informations?
The main goal of this article is talk about the Redis and show an open source project, the redis-collection.

Analyzing cache tools such memcache and infinispan, also has this behavior with key-value, the first question that a Java developers does is: Which difference about Redis and cache?. To start, the serialization of the values, while the caches write in binary way, with Kryo or, in Redis everything is text or String. The problem in binary serialization is the impossibility of change the file manually or using another language. When Kryo or are used if change the object structure, you cannot retrieve the information with this structure, however write and read is faster than using text. There is a reason to it, caches are made to be faster but also temporary. Redis is a database and can storage this information, doing backup in hard driver, this way, if Redis fall down it can retrieves without lost all informations. Another dissemblance, in Redis there are another structures inside:

  • String key and value
  • List: are simply lists of strings, sorted by insertion order. It is possible to add elements to a Redis (similar in Java with java.util.List).
  • Set: are an unordered collection of Strings constants with list you cannot put repetitive String (similar in Java with java.util.Set).
  • Sorted Set: are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score,
  • Hashes: are maps between string fields and string values, so they are the perfect data type to represent objects (simillar in Java with Map).

  • Beyond there are more two, Bit arrays and HyperLogs, but will not mentioned in this article.

    After explain divergences between Redis and cache, next step will install the Redis that is very easy:

    1. Do the Download here:
    2. Uncompress and then compile the Redis: ->
      tar zxvf redis-version.x.tar.gz ->
      cd redis-version.x ->
    3. Last step is run redis: cd src -> ./redis-server

    Done, Redis is running. To do some test, just execute the native client, just go to REDIS_HOME/src, next, run ./redis-cli.

    Feel free to run any commands, to know the commands in redis:

    After Redis ran and tested. Will used some data structures on Redis, some popular structures in Java: java.util.List, java.util.Set, java.util.Queue e java.util.Map, and more three: key-value as cache, counter and the last one to ranking.

    You can see the API here:

    public interface keyValueRedisStructure {

    T get(String key);

    void set(String key, T bean);

    List multiplesGet(Iterable keys);

    void delete(String key);

    public interface CountStructure {

    T get();

    T increment();

    T increment(T count);

    T decrement();

    T decrement(T count);

    void delete();

    void expires(int ttlSeconds);

    void persist();

    public interface ListStructure extends Expirable {

    List get(String key);

    void delete(String key);

    public interface MapStructure extends Expirable{

    Map get(String key);

    void delete(String key);


    public interface QueueStructure extends Expirable {

    Queue get(String key);

    void delete(String key);


    public interface RankingStructure extends Expirable {

    ScoresPoint create(String key);

    void delete(String key);

    The idea of Expirable interface is define on key, the lifecycle, or time to expiry, of the objects in seconds, to remove this lifecycle just use the persist method. Remembering the Redis is in memory, but periodically the data are saved and all information are represented with String. In these structures was chose to serialize and deserialize the objects JSON, because is