Slide 1

Slide 1 text

1 Singapore Spring User Group 6th February 2014 Sergiu Bodiu https://www.linkedin.com/in/sergiubodiu mailto:[email protected]

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

6th February 5 http://www.infoq.com/presentations/Real-Time-Delivery-Twitter

Slide 6

Slide 6 text

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...

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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"

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

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)

Slide 11

Slide 11 text

6th February 11 Spring Data Redis org.springframework.data spring-data-redis 1.1.1.RELEASE 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.

Slide 12

Slide 12 text

6th February 12 Configure RedisTemplate....

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

6th February 14 Redis> KEYS *Questions*