Saturday 26 January 2013








Redis is an open-source, networked, in-memory, key-value data store with optional durability. It is written in ANSI C. The development of Redis is sponsored by VMware.

Persistence

Redis typically holds the whole dataset in RAM. Persistence is reached in two different ways: One is called snapshotting, and is a semi-persistent durability mode where the dataset is asynchronously transferred from memory to disk from time to time, written in RDB dump format.

Data Model

In its outer layer, the Redis data model is a dictionary where keys are mapped to values. One of the main differences between Redis and other structured storage systems is that values are not limited to strings. In addition to strings, the following abstract data types are supported:

  • Lists of strings
  • Sets of strings (collections of non-repeating unsorted elements)
  • Sorted sets of strings (collections of non-repeating elements ordered by a floating-point number called score)
  • Hashes where keys and values are strings
The type of a value determines what operations (called commands) are available for the value itself. Redis supports high level atomic server side operations like intersection, union, and difference between sets and sorting of lists, sets and sorted sets.

Replication

Redis supports master-slave replication. Data from any Redis server can replicate to any number of slaves. A slave may be a master to another slave. This allows Redis to implement a single-rooted replication tree. Redis slaves are writable, permitting intentional and unintentional inconsistency between instances. The Publish/Subscribe feature is fully implemented, so a client of a slave may SUBSCRIBE to a channel and receive a full feed of messages PUBLISHed to the master, anywhere up the replication tree. Replication is useful for read (but not write) scalability or data redundancy.

Performance

When the durability of data is not needed, the in-memory nature of Redis allows it to perform extremely well compared to database systems that write every change to disk before considering a transaction committed. There is no notable speed difference between write and read operations.

Architecture



Redis Vs. Memcached

 



Who is using Redis?


Language Bindings


Pros

1) Redis can be used for storing data that are being used frequently (like user's session, database credentials, Top N MRU user's details)

2) We can also configure the user's session to persist for a specific time period.

3) Faster Get/Set in redis.

4) Data is being persistent in-memory as well as stored on disk.

5) We can create a mechanism that first check the data in Redis and if redis does not contain the data then check in MySql DB.

6) Fast ~1,00,000 queries per second.

Cons

1) Data is larger than memory

2) Need ACID property requirements.

3) Big Data can't be implemented easily.




Configuring Redis


Step 1) Install Redis:
    - Get Zip File from (http://redis.googlecode.com/files/redis-2.6.7.tar.gz)
    - Unzip File and open terminal
    $ make

Step 2) Running Redis:
- To run Redis with the default configuration just type:
    $ cd src
    $ ./redis-server
   
- If you want to provide your redis.conf, you have to run it using an additional
 parameter (the path of the configuration file):
    $ cd src
    $ ./redis-server /path/to/redis.conf

- It is possible to alter the Redis configuration passing parameters directly
 as options using the command line. Examples:

    $ ./redis-server --port 9999 --slaveof 127.0.0.1 6379
    $ ./redis-server /etc/redis/6379.conf --loglevel debug

Step 3) Playing with Redis
    a) Using Terminal
- You can use redis-cli to play with Redis. Start a redis-server instance,
      then in another terminal try the following:

        $ cd src
        $ ./redis-cli
    $ redis> ping
        PONG
        $ redis> set foo bar
    OK
    $ redis> get foo
    "bar"
    $ redis> incr mycounter
    (integer) 1
    $ redis> incr mycounter
    (integer) 2

    You can find the list of all the available commands here:

b) Using JEdis (Redis Java Client)
    - Add “commons-pool-1.6.jar” and “jedis-2.1.0.jar” in Your classpath
    - Following Code demonstrates the Get/Set Values:

        Jedis jedis = new Jedis("localhost");
          jedis.set("foo", "bar");
           String value = jedis.get("foo");
           System.out.println("Value: " + value);
              System.out.println(jedis.keys("*")); // lists all keys available in Redis DB

    - To Store User Defined Objects in Redis:
i) You can either use Johm (http://code.google.com/p/johm/)
ii) Using Model Class that implements Serializable interface:

public class UsersDetails implements Serializable {
private String userId ;
private String authToken ;
// Getter and Setter Methods
}

public class RedisManager {
   private Jedis jedis;
   public RedisManager() {
       jedis = new Jedis("localhost");
   }
   public static void main(String[] args) {
       RedisManager redisManager = new RedisManager();
       
       UsersDetails userDetails = new UsersDetails();
       userDetails.setAuthToken("asdasd");
       userDetails.setUserId("101");
       redisManager.setObjectValue("101", userDetails);
       System.out.println(redisManager.getAllKeys());
   }
   public void setObjectValue(String key, Object value) {
       jedis.set(key.getBytes(), toBytes(value));
   }
   public Object getObjectValue(String key) {
       return fromBytes(jedis.get(key.getBytes()));
   }
   public Object getAllKeys() {
       return jedis.keys("*");
   }
   public Object fromBytes(byte key[]) {
       Object obj = null;
       if(key!=null)
       {
           try {
               ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(key));
               obj = ois.readObject();
               ois.close();
           } catch (ClassNotFoundException e) {
               e.printStackTrace();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       return obj;
   }
   public byte[] toBytes(Object object) {
       ByteArrayOutputStream baos;
       ObjectOutputStream oos;
       try {
           baos = new ByteArrayOutputStream();
           oos = new ObjectOutputStream(baos);
           oos.writeObject(object);
           oos.close();
           return baos.toByteArray();
       } catch (IOException e) {
           e.printStackTrace();
       }
       return null;
   }
}

1 comment:

Find me on Facebook! Follow me on Twitter!