0
REDIS
For Developer
3rd WEEK
SATURDAY, JULY 06, 2013 – SUNDAY, JULY 07, 2013
GEEK ACADEMY 2013
INSTRUCTOR TEAM
TAVEE SOMKIAT THAWATCHAI
Tavee (Vee) Khunbida Somkiat (Pui) Puisungnuen Thawatchai (Boy) Jongsuwanpaisan
S...
WHAT IS REDIS ?
o REmote DIrectory Server
o By Salvatore Sanfilippo (@antirez) working at VMWare
o March 2009
o redis.io
WHAT IS REDIS ?
o REmote DIrectory Server
o NOSQL => Key-Value
o Data Structure Server
NOSQL ( Not Only SQL )
o Key-Value Store
o Document Database
o Column Store
o Graph Database
http://www.10gen.com/nosql
NOSQL ( Not Only SQL )
o Key-Value Store
o Redis, Riak, Memcached, Tokyo Cabinet, LevelDB
o Document Database
o MongoDB, C...
WHY WE USE REDIS ?
o Very Fast ( In memory )
o Few Dependencies
o Single Thread
o Lot of client available
o Java
o Ruby
o ...
FEATURES
o Values Type
o String
o List
o Set
o Hash
o Sorted Set
o Persistence
o Memory
o Snapshot
o Append Only Log ( AOL...
FEATURES
o Replication
o Master-Slave
o Pub-Sub
o Clustering ( Beta version )
WHO USE REDIS ?
http://redis.io/topics/whos-using-redis
REDIS IN PRACTICE
o View Count
o Presence
o Activity Feed
o Suggestion
o Caching
o Tracking
o Logging
o ….
DATA STRUCTURE
GEEK ACADEMY 2013
List
o List of String
o Sorted by insertion order
o Max length of List = 232 -1
o Big O
o Add operation = O(1)
o Access op...
Set
o Unordered collections of String
o Not repeat members
o Max length of List = 232 -1
o Operation => Union, Intersectio...
Sorted Set
o Like Set BUT …
o Ordered collections of String by Score
o Big O
o Add operation = O(log(N))
o Access operatio...
Hash
o Mapping Key-Value
o Max length of List = 232 -1
o Big O
o Add operation = O(N)
o Access operation = O(1)
o Delete =...
Summary
INSTALLATION FOR
DEV
GEEK ACADEMY 2013
FOR WINDOWS
o Download at
o https://github.com/dmajkic/redis/downloads
o Commands
o redis-server.exe
o redis-cli.exe
o red...
FOR UNIX
$ wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz
$ tar xzf redis-2.6.14.tar.gz
$ cd redis-2.6.14
$ ma...
FOR MAC
o Up to you
INSTALLATION FOR
DEVOPS
GEEK ACADEMY 2013
VAGRANT + PUPPET
o OS = Ubuntu
o Configure repository of Redis
o Install with apt-get
Configure repository
Install and Starting Redis Service
Install on Virtual Machine
$vagrant up
$vagrant reload
$vagrant provision
BASIC USAGE
GEEK ACADEMY 2013
LET’S DEMO
o redis-cli
o http://try.redis.io/
o http://redis.io/commands
DEMO 1
o PING
o INFO
o KEYS
o EXISTS
o DEL
o FLUSHDB
DEMO 2 :: Working with String
o SET
o GET
o GETRANGE
o APPEND
DEMO 3 :: Counter
o INCR
o INCRBY
o DECR
o DECRBY
DEMO 4 :: Working with List
o LPUSH
o RPUSH
o LRANGE
o LTRIM
o LLEN
DEMO 5 :: Working with Set
o SADD
o SMEMBERS
o SINTER
o SPOP
o SRANDMEMBER
o SUNIONSTORE
DEMO 6 :: Working with Sorted Set
o ZADD
o ZRANGE
o ZRANGEBYSCORE
o ZRANK
o ZINCRBY
o ZREVRANGE
DEMO 7 :: Working with Hash
o HSET
o HGET
o HGETALL
o HDEL
o HEXISTS
o HLEN
o HMSET
o HMGET
REDIS#VIEW COUNT
GEEK ACADEMY 2013
View count with Redis
REDIS#PRESENCE
GEEK ACADEMY 2013
Presence with Redis
o Presence = Who’s Online ?
o What’s Data Structure ?
Presence with Redis
Online
Users Friends
Data Structure ?
o List
o Set
o Sorted Set
Implementation ?
o Set
o Define Key
o Online user
o Key = user:online:<time>
o Value = <user id>
o Friends
o Key = user:fr...
Implementation ?
o Online Users
o sadd user:online:1 1
o sadd user:online:1 2
o sadd user:online:1 3
o sadd user:online:2 ...
Implementation ?
o Friends by User
o sadd user:friend:1 2
o sadd user:friend:1 3
Implementation ?
o Find Friends are online in last 2 minutes
o sunionstore online_users user:online:1 user:online:2
o sint...
Presence with Redis
REDIS#SUGGESTION
GEEK ACADEMY 2013
Keyword Suggestion with Redis
DBRMS Solution
o SELECT KEYWORD
o FROM SOME_TABLE
o WHERE LOWER(KEYWORD) LIKE “redis%”
Redis Solution
o What is Data Structure ?
o List
o Set
o Sorted Set
Implementation ?
o Sorted Set
o Example => redis
o r
o re
o red
o redi
o redis
Specified word
o Example => redis
o r
o re
o red
o red*
o redi
o redis
o redis*
Algorithm
o Called N-gram
http://en.wikipedia.org/wiki/N-gram
http://books.google.com/ngrams/
Add data to Redis Sorted Set
o zadd my_data 0 r
o zadd my_data 0 re
o zadd my_data 0 red
o zadd my_data 0 red*
o zadd my_d...
List of data in Sorted Set
o zrange my_data 0 -1
o zrank my_data r
o zrank my_data re
How to apply in application ?
o Let’s discussion
REDIS#CACHING
GEEK ACADEMY 2013
Caching
o Denormalization Data
o Generalization Cache with LRU, TTL
Redis :: Memory policy
o Volatile-LRU
o Volatile-TTL
o Volatile-Random
o AllKeys-LRU
o AllKeys-Random
o NoEviction
WORKING WITH JAVA
GEEK ACADEMY 2013
Redis Client Library
o Jedis
o JRedis
o JDBC-Redis
o RJC
o Redis-protocol
o Lettuce
http://redis.io/clients
For Traditional Developer
o Goto web
o Search and Download
o Copy to build path of project !!!!
o Not for geek ….
For Geek
o Use Maven or Gradle
Jedis
o https://github.com/xetorthio/jedis
o Maven Dependency
<dependency>
<groupId>redis.clients</groupId>
<artifactId>je...
Jedis :: Easy to use
Jedis jedis = new Jedis("localhost");
jedis.set("foo", "bar");
String value = jedis.get("foo");
WORKING WITH
SPRING FRAMEWORK
GEEK ACADEMY 2013
Let’s go
o We use Spring Data with Redis
o http://www.springsource.org/spring-data/redis
Working with Maven
<repository>
<id>spring-release</id>
<name>Spring Maven RELEASE Repository</name>
<url>http://maven.spr...
Working with Annotation
@Configuration
public class RedisConfig {
@Bean
JedisConnectionFactory jedisConnectionFactory() {
...
Using RedisTemplate in Repository
@Repository
public class LoggingDaoImpl implements LoggingDao {
private final static Str...
See more
o https://github.com/up1/geeky_week3_demo
o https://github.com/up1/Spring-Framework-
Demo/tree/master/SpringRedis
Question ?
GEEK ACADEMY 2013
THANK YOU FOR
YOUR TIME
GEEK ACADEMY 2013
Upcoming SlideShare
Loading in...5
×

Geek Academy Week 3 :: Redis for developer

1,871

Published on

Geek Academy Week 3 :: Redis for developer
- Redis
- With Java Library => Jedis
- With Spring Data :: Redis

Published in: Technology

Transcript of "Geek Academy Week 3 :: Redis for developer"

  1. 1. REDIS For Developer 3rd WEEK SATURDAY, JULY 06, 2013 – SUNDAY, JULY 07, 2013 GEEK ACADEMY 2013
  2. 2. INSTRUCTOR TEAM TAVEE SOMKIAT THAWATCHAI Tavee (Vee) Khunbida Somkiat (Pui) Puisungnuen Thawatchai (Boy) Jongsuwanpaisan Siam Chamnan Kit tavee@sprint3r.com Siam Chamnan Kit somkiat@sprint3r.com Siam Chamnan Kit thawatchai@sprint3r.com
  3. 3. WHAT IS REDIS ? o REmote DIrectory Server o By Salvatore Sanfilippo (@antirez) working at VMWare o March 2009 o redis.io
  4. 4. WHAT IS REDIS ? o REmote DIrectory Server o NOSQL => Key-Value o Data Structure Server
  5. 5. NOSQL ( Not Only SQL ) o Key-Value Store o Document Database o Column Store o Graph Database http://www.10gen.com/nosql
  6. 6. NOSQL ( Not Only SQL ) o Key-Value Store o Redis, Riak, Memcached, Tokyo Cabinet, LevelDB o Document Database o MongoDB, CouchDB, RethinkDB o Column Family Store o Cassandra, Hadoop, HBase o Graph Database o Neo4J, HyperGraphDB, InfoGrid
  7. 7. WHY WE USE REDIS ? o Very Fast ( In memory ) o Few Dependencies o Single Thread o Lot of client available o Java o Ruby o PHP o Python o .Net
  8. 8. FEATURES o Values Type o String o List o Set o Hash o Sorted Set o Persistence o Memory o Snapshot o Append Only Log ( AOL )
  9. 9. FEATURES o Replication o Master-Slave o Pub-Sub o Clustering ( Beta version )
  10. 10. WHO USE REDIS ? http://redis.io/topics/whos-using-redis
  11. 11. REDIS IN PRACTICE o View Count o Presence o Activity Feed o Suggestion o Caching o Tracking o Logging o ….
  12. 12. DATA STRUCTURE GEEK ACADEMY 2013
  13. 13. List o List of String o Sorted by insertion order o Max length of List = 232 -1 o Big O o Add operation = O(1) o Access operation = O(S+N) o Update = O(1) o Delete = O(1)
  14. 14. Set o Unordered collections of String o Not repeat members o Max length of List = 232 -1 o Operation => Union, Intersection, Different o Big O o Add operation = O(N) o Access operation = O(N) o Update = O(1) o Delete = O(1) o Exists = O(1)
  15. 15. Sorted Set o Like Set BUT … o Ordered collections of String by Score o Big O o Add operation = O(log(N)) o Access operation = O(log(N)+M) o Delete = O(M*log(N))
  16. 16. Hash o Mapping Key-Value o Max length of List = 232 -1 o Big O o Add operation = O(N) o Access operation = O(1) o Delete = O(1)
  17. 17. Summary
  18. 18. INSTALLATION FOR DEV GEEK ACADEMY 2013
  19. 19. FOR WINDOWS o Download at o https://github.com/dmajkic/redis/downloads o Commands o redis-server.exe o redis-cli.exe o redis-benchmark
  20. 20. FOR UNIX $ wget http://redis.googlecode.com/files/redis-2.6.14.tar.gz $ tar xzf redis-2.6.14.tar.gz $ cd redis-2.6.14 $ make Starting server $ src/redis-server Interact with Redis $ src/redis-cli
  21. 21. FOR MAC o Up to you
  22. 22. INSTALLATION FOR DEVOPS GEEK ACADEMY 2013
  23. 23. VAGRANT + PUPPET o OS = Ubuntu o Configure repository of Redis o Install with apt-get
  24. 24. Configure repository
  25. 25. Install and Starting Redis Service
  26. 26. Install on Virtual Machine $vagrant up $vagrant reload $vagrant provision
  27. 27. BASIC USAGE GEEK ACADEMY 2013
  28. 28. LET’S DEMO o redis-cli o http://try.redis.io/ o http://redis.io/commands
  29. 29. DEMO 1 o PING o INFO o KEYS o EXISTS o DEL o FLUSHDB
  30. 30. DEMO 2 :: Working with String o SET o GET o GETRANGE o APPEND
  31. 31. DEMO 3 :: Counter o INCR o INCRBY o DECR o DECRBY
  32. 32. DEMO 4 :: Working with List o LPUSH o RPUSH o LRANGE o LTRIM o LLEN
  33. 33. DEMO 5 :: Working with Set o SADD o SMEMBERS o SINTER o SPOP o SRANDMEMBER o SUNIONSTORE
  34. 34. DEMO 6 :: Working with Sorted Set o ZADD o ZRANGE o ZRANGEBYSCORE o ZRANK o ZINCRBY o ZREVRANGE
  35. 35. DEMO 7 :: Working with Hash o HSET o HGET o HGETALL o HDEL o HEXISTS o HLEN o HMSET o HMGET
  36. 36. REDIS#VIEW COUNT GEEK ACADEMY 2013
  37. 37. View count with Redis
  38. 38. REDIS#PRESENCE GEEK ACADEMY 2013
  39. 39. Presence with Redis o Presence = Who’s Online ? o What’s Data Structure ?
  40. 40. Presence with Redis Online Users Friends
  41. 41. Data Structure ? o List o Set o Sorted Set
  42. 42. Implementation ? o Set o Define Key o Online user o Key = user:online:<time> o Value = <user id> o Friends o Key = user:friend:<user id> o Value = <user id>
  43. 43. Implementation ? o Online Users o sadd user:online:1 1 o sadd user:online:1 2 o sadd user:online:1 3 o sadd user:online:2 3 o sadd user:online:2 4
  44. 44. Implementation ? o Friends by User o sadd user:friend:1 2 o sadd user:friend:1 3
  45. 45. Implementation ? o Find Friends are online in last 2 minutes o sunionstore online_users user:online:1 user:online:2 o sinter online_users user:friend:1
  46. 46. Presence with Redis
  47. 47. REDIS#SUGGESTION GEEK ACADEMY 2013
  48. 48. Keyword Suggestion with Redis
  49. 49. DBRMS Solution o SELECT KEYWORD o FROM SOME_TABLE o WHERE LOWER(KEYWORD) LIKE “redis%”
  50. 50. Redis Solution o What is Data Structure ? o List o Set o Sorted Set
  51. 51. Implementation ? o Sorted Set o Example => redis o r o re o red o redi o redis
  52. 52. Specified word o Example => redis o r o re o red o red* o redi o redis o redis*
  53. 53. Algorithm o Called N-gram http://en.wikipedia.org/wiki/N-gram http://books.google.com/ngrams/
  54. 54. Add data to Redis Sorted Set o zadd my_data 0 r o zadd my_data 0 re o zadd my_data 0 red o zadd my_data 0 red* o zadd my_data 0 redi o zadd my_data 0 redis o zadd my_data 0 redis*
  55. 55. List of data in Sorted Set o zrange my_data 0 -1 o zrank my_data r o zrank my_data re
  56. 56. How to apply in application ? o Let’s discussion
  57. 57. REDIS#CACHING GEEK ACADEMY 2013
  58. 58. Caching o Denormalization Data o Generalization Cache with LRU, TTL
  59. 59. Redis :: Memory policy o Volatile-LRU o Volatile-TTL o Volatile-Random o AllKeys-LRU o AllKeys-Random o NoEviction
  60. 60. WORKING WITH JAVA GEEK ACADEMY 2013
  61. 61. Redis Client Library o Jedis o JRedis o JDBC-Redis o RJC o Redis-protocol o Lettuce http://redis.io/clients
  62. 62. For Traditional Developer o Goto web o Search and Download o Copy to build path of project !!!! o Not for geek ….
  63. 63. For Geek o Use Maven or Gradle
  64. 64. Jedis o https://github.com/xetorthio/jedis o Maven Dependency <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.0.0</version> <type>jar</type> <scope>compile</scope> </dependency>
  65. 65. Jedis :: Easy to use Jedis jedis = new Jedis("localhost"); jedis.set("foo", "bar"); String value = jedis.get("foo");
  66. 66. WORKING WITH SPRING FRAMEWORK GEEK ACADEMY 2013
  67. 67. Let’s go o We use Spring Data with Redis o http://www.springsource.org/spring-data/redis
  68. 68. Working with Maven <repository> <id>spring-release</id> <name>Spring Maven RELEASE Repository</name> <url>http://maven.springframework.org/release</url> </repository> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.5.RELEASE</version> </dependency>
  69. 69. Working with Annotation @Configuration public class RedisConfig { @Bean JedisConnectionFactory jedisConnectionFactory() { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); jedisConnectionFactory.setHostName(redisHost); jedisConnectionFactory.setPort(redisPort); return jedisConnectionFactory; } @Bean RedisTemplate<String, String> redisTemplate() { final RedisTemplate<String, String> template = new RedisTemplate<String, String>(); template.setConnectionFactory(jedisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericToStringSerializer<Object>(Object.class)); template.setValueSerializer(new GenericToStringSerializer<Object>(Object.class)); return template; } }
  70. 70. Using RedisTemplate in Repository @Repository public class LoggingDaoImpl implements LoggingDao { private final static String LOGGING_KEY = "logging"; @Autowired RedisTemplate<String, String> redisTemplate; public void addData(String key, String value) { redisTemplate.opsForList().rightPush(LOGGING_KEY, key); redisTemplate.opsForValue().set(key, value); System.out.println( LOGGING_KEY ); System.out.println( key ); } }
  71. 71. See more o https://github.com/up1/geeky_week3_demo o https://github.com/up1/Spring-Framework- Demo/tree/master/SpringRedis
  72. 72. Question ?
  73. 73. GEEK ACADEMY 2013
  74. 74. THANK YOU FOR YOUR TIME GEEK ACADEMY 2013
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×