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.
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. |
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]
You have two ways to install redis database server in your linux machine(Ubuntu).
Redis has clients nearly to every programming language and the most used client implemented in Java is Jedis.
In order to have Jedis in your application you can:
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> <type>jar</type> </dependency>
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(); } }