Geek Academy Week 3 :: Redis for developer
Upcoming SlideShare
Loading in...5
×
 

Geek Academy Week 3 :: Redis for developer

on

  • 1,595 views

Geek Academy Week 3 :: Redis for developer

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

Statistics

Views

Total Views
1,595
Views on SlideShare
1,595
Embed Views
0

Actions

Likes
12
Downloads
42
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Geek Academy Week 3 :: Redis for developer Geek Academy Week 3 :: Redis for developer Presentation Transcript

  • 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 Siam Chamnan Kit tavee@sprint3r.com Siam Chamnan Kit somkiat@sprint3r.com Siam Chamnan Kit thawatchai@sprint3r.com
  • 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, CouchDB, RethinkDB o Column Family Store o Cassandra, Hadoop, HBase o Graph Database o Neo4J, HyperGraphDB, InfoGrid
  • 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
  • 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 operation = O(S+N) o Update = O(1) o Delete = O(1)
  • 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)
  • 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))
  • 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)
  • 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 redis-benchmark
  • 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
  • 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:friend:<user id> o Value = <user id>
  • 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
  • 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 sinter online_users user:friend:1
  • 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_data 0 redi o zadd my_data 0 redis o zadd my_data 0 redis*
  • 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>jedis</artifactId> <version>2.0.0</version> <type>jar</type> <scope>compile</scope> </dependency>
  • 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.springframework.org/release</url> </repository> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.5.RELEASE</version> </dependency>
  • 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; } }
  • 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 ); } }
  • 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