Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Redis Overview

sergiubodiu
February 06, 2014

Redis Overview

Presenting Redis and Spring Data Redis (key-value store).
Redis is an open source, BSD-licensed, key-value data store that also comes with a messaging system. The server is freely available at http://redis.io/download.
Spring Data provides easy configuration and access to Redis from Spring applications. It offers both low-level and high-level abstractions for interacting with the store, freeing the user from infrastructural concerns.
Presented at Singapore Spring User Group 6th February.

sergiubodiu

February 06, 2014
Tweet

More Decks by sergiubodiu

Other Decks in Technology

Transcript

  1. 6th February 2 What is a REDIS REmote DIctionary Server

    REDIS is an in-memory Key-value data-structure server “Advanced” key value store database Memcached on steroids redis 127.0.0.1:6379 > PUBLISH
  2. 6th February 3 What is a REDIS Perhaps more interesting,

    we use Redis as a caching layer for the entire network. Kyle recently mentioned the specs of the machine in passing. Prior to the NY move our Redis instance was in a virtual machine on a (lightly loaded) web tier machine. In our (admittedly limited) experience, Redis is so fast that the slowest part of a cache lookup is the time spent reading and writing bytes to the network. This is not surprising, really, if you think about it. http://meta.stackoverflow.com/questions/69164/does-stack-overflow-use-caching-and-if-so-how/69172#69172
  3. 6th February 4 What is a REDIS Redis powers their

    main feed, activity feed, sessions system, and other services, it runs on several Quadruple Extra-Large Memory instances. Occasionally shard across instances. Redis runs in a master-replica setup. Replicas constantly save to disk. EBS snapshots backup the DB dumps. Dumping on the DB on the master was too taxing. http://highscalability.com/blog/2012/4/9/the-instagram-architecture-facebook-bought-for-a-cool-billio.html
  4. 6th February 6 SMEMBERS redis:types Strings SET, SETBIT, SETEX, SETNX,

    SETRANGE, GETSET, GET, INCR, DECR, APPEND, INCRBY.... Hashes HSET, HSETNX, HKEYS, HLEN, HVALS, HDEL, HEXISTS, HGET, HGETALL, HINCRBY... Lists LINSERT, LLEN, LPOP, LPUSH, LPUSHX, LRANGE, LREM, LSET, RPOP, RPUSH, RPUSHX ... Sets & Sorted Sets SADD, SPOP, SREM, SMEMBERS, SDIFF, ZADD, ZCOUNT, ZRANGE, ZRANK, ZREM, ZSCORE...
  5. 6th February 7 Strings in Redis Set key to hold

    the string value. If key already holds a value, it is overwritten, regardless of its type. Get the value of key. If the key does not exist the special value nil is returned. An error is returned if the value stored at key is not a string, If the values of your keys are integers, then you can use DECR or DECRBY to decrement and INCR or INCRBY to increment them. These operations are useful in scenarios in which you want to maintain a count of something – say , the number of hits for a web page redis> SET hello "world" OK > GET hello "world" redis> INCR visits (integer) 1 > INCR visits (integer) 2 > DECR visits (integer) 1 > INCRBY visits 41 (integer) 42
  6. 6th February 8 Hashes in Redis Hashes allow you to

    store a key- value pair against a hash. This option can be useful in scenarios in which you want to save an object with several properties, as in this example: redis> HSET user:1 firstname salvatore (integer) 1 > HSET user:1 lastname sanfilipo (integer) 1 > HGET user:1 firstname "salvatore" > HGETALL user:1 1) "firstname" 2) "salvatore" 3) "lastname" 4) "sanfilipo"
  7. 6th February 9 More Redis commands > SET pet:1 '{name:

    "Leo", bdate: "2010-09-07", type: "cat"}' > SET pet:2 '{name: "Basil", bdate: "2012-08-06", type: "hamster"}' > LPUSH visits pet:1 > LPUSH visits pet:2 > LPUSH visits pet:1 # Read > LRANGE visits 0 -1 > MGET pet:1 pet:2 1) "{name: "Leo", bdate: "2010-09-07", type: "cat"}" 2) "{name: "Basil", bdate: "2012-08-06", type: "hamster"}" Returns the values of all specified keys. For every key that does not hold a string value or does not exist, the special value nil is returned. Because of this, the operation never fails.
  8. 6th February 10 Advantages of Redis RDB Point-in-time snapshot AOF

    Append-only file Replication Master/Slave #redis.conf 127.0.0.1 6379 Single Threaded/ single Core utiliztion Shared Memory, Mqs, Semaphores, PubSub Data structures Strings, Lists, Hashes, Sets, Sorted sets. Caching, Expiration, Eviction Policy EXPIRE key seconds Set a key's time to live in seconds EXPIREAT key timestamp Set the expiration for a key as a UNIX timestamp Redis commands give time complexity in big-O notation O(1) < O(log(n)) < O(n) < O(n^2)
  9. 6th February 11 Spring Data Redis <dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId>

    <version>1.1.1.RELEASE</version> </dependency> </dependencies> Spring Data Redis, part of the larger Spring Data family, provides easy configuration and access to Redis from Spring applications. It offers both low-level and high-level abstractions for interacting with the store, freeing the user from infrastructural concerns.
  10. 6th February 12 Configure RedisTemplate.... <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisC onnectionFactory" p:use-pool="true"/>

    <!-- redis template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory"/>
  11. 6th February 13 Class Example { @Autowired // inject the

    actual template private RedisTemplate<String, String> template; // inject the template as ListOperations // can also inject as Value, Set, ZSet, and HashOperations @Resource(name="redisTemplate") private ListOperations<String, String> listOps; public void addLink(String userId, URL url) { listOps.leftPush(userId, url.toExternalForm()); // or use template directly redisTemplate.boundListOps(userId).leftPush(url.toExternalForm()); }