Basically, Redis is a key-value store. A key-value is usually a database where a key (string) and
Basically, Redis is a key-value store . A key-value is usually a database where a key (string) and a value (string) are saved, although they can save more complicated structures. However, it is generally used for saving dictionaries.
Redis… Where to use it?
Redis is very fast. It is very common to use it as a cache for information that is frequently accessed and where the response time has to be incredibly fast (we are talking milliseconds). It can be easily implemented as a cache since an expiration time can be set up for the keys (TTL).
It is also used to implement APIs, where quick responses are sought.
How it works?
Redis’ speed works through memory, and can optionally be configured to save, in a specific amount of time, information/data in hard disk persist data in the disk.
There are two ways of persisting data:
• RDB (database snapshots)
Save a picture from the database at a given time (it can be configured).
• AOF (Append-only file)
Save all the log operations in a file, with which the entire database can be recreated.
Both methods can be combined.
Set-up
Linux
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
Windows
https://github.com/MSOpenTech/redis/releases
Who uses Redis
MercadoLibre, Twitter, Github, Weibo, Pinterest, Snapchat, Craigslist, Digg, StackOverflow,Flickr.
Clients
- redis-cli (command line)
- jedis (client for Java)
Try it out without setting-it-up
http://try.redis.io/
Example
Suppose that we want to count the number of clicks that a system of Ads (similar to Google´s AdWords) receives from each user (customer) and for each ad. The keys can be written in the following style:
customerId:123:adId:666 1001
From redis-cli (command line client)
Set-up a value
> set customerId:123:adId:666 1001
Obtain it
> get customerId:123:adId:666
> “1001”
Increase it
> incr customerId:123:adId:666
> (integer) 1002
Basic commands
set
Ej.: set aKey aValue
get
Ej.: get aKey
keys
Ej.: keys “*” (shows all keys)
info (shows general information of the database)
Java examples
/**
* Shows on console:
*
* hola: sabor
* keys: [hola, chau] *
*/
public class Main {
private static Jedis jedis;
public static void main(String[] args) {
try {
jedis = new Jedis(“localhost”);
jedis.set(“hola”, “sabor”);
jedis.set(“chau”, “100”);
String value = jedis.get(“hola”);
System.out.println(“hola: ” + value);
System.out.println(“keys: ” + jedis.keys(“*”));
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
}
}