Introduction to Redis database and using it with Java

Introduction:

More speed is a dominant need in our life and IT field has no exception from that. Redis comes to meet the need of speed in the database domain.

So, what is Redis?

It is a NO-SQL Key-value database or as the official site defines it “Redis is an open source, in-memory Data Structure Store, used as a database, a caching layer or a message broker”.

Feature Explanation
In memory means that it remains in Ram and also can be persistent on hard disk by Snapshotting or AOF ”Append only file” techniques.
Data Structure store means that the keys stored in database can be hashes, lists, strings, sorted or unsorted sets.
Caching layer means it can be used as a cache by setting a limited time-to-live to the keys through “EXPIRE” command to improve the performance or just by setting a memory size limit through “maxmemory” command .
Message broker means that it can be used as Message Queue which passes, holds, and delivers messages across a application or services.

Story of Redis:

As Salvatore Sanfilippo creator of Redis said: "My problem was conceptually simple: my server was receiving a stream of page views from multiple websites using a small JavaScript tracker. I needed to store the latest n page views for every site and show them in real time to users connected to a web interface, while maintaining a small history. With a peak load of a few thousand page views per second, whatever my database schema was, and whatever trade-offs I was willing to pick, there was no way for my SQL store to handle the load with such poor hardware. My inability to upgrade the hardware for cost concerns coupled with the feeling that to handle a capped list of values shouldn’t have been so hard, after all, gave me the idea of creating a throwaway prototype of an in-memory data store that could handle lists as a native data type, with constant-time pop and push operations on both sides of the lists. To make a long story short, the concept worked, I rewrote the first prototype using the C language, added a fork-based persistence feature, and Redis was born"[1]

Installation of Redis:

You have two ways to install redis database server in your linux machine(Ubuntu).

  • Installing it from Package management (apt-get):

    1. Update package list via "$sudo apt-get update"
    2. Install Redis via  "$sudo apt-get install redis-server"
    3. Start Redis via "$redis-server"
    4. Start Redis cli via "$redis-cli" and then type “ping” the output could be “PONG”.
  •  Compiling it from the source code manually (recommendded):

    1. Install build essential “ sudo apt-get install build-essential.
    2. Download TCL (Tool Command Language) via “ sudo apt-get install tcl8.5”.
    3. Download latest redis stable version via“wget http://download.redis.io/releases/redis-stable.tar.gz”.
    4. Extract it via “tar xzf redis-stable.tar.gz” and change directory to extracted folder.
    5. Execute “make” command to compile the source code.
    6. Change directory to utils and execute “sudo ./install_server.sh” .
    7. After finishing installation steps, open terminal and execute “redis-cli” then type “ping” the output could be “PONG”.

Integration with Java:

Redis has clients nearly to every programming language and the most used client implemented in Java is Jedis.

Using Jedis:

In order to have Jedis in your application you can: 

  1. Download the Jedis jar file directly with its dependency Apache Commons pool 2 and add then to your project.
  2. Use Maven dependency by just adding this denepency in pom.xml file.
<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.7.2</version>
 <type>jar</type>
 </dependency>

Basic Example:

import redis.clients.jedis.Jedis;
 import redis.clients.jedis.JedisPool;
 import redis.clients.jedis.JedisPoolConfig;
 
 /** 
 * @author Taha Emara
 */
 
 public class TestJedis { 
    public static void main(String[] args) {
         JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");        
         Jedis jedis = pool.getResource();       
         jedis.set("Animal", "Cat");
         String animal = jedis.get("Animal");
         System.out.println(animal); // it prints Cat  
         pool.returnResource(jedis);  
         pool.destroy();    
    }
 }

References:

  1. Redis in Action (Book).

Subscribe to Our Mailing List



Share:

Subscribe


Blog Tags

Follow me

Facebook Page