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

Cache night at the Singapore Spring User Group

Michael Isvy
February 06, 2014

Cache night at the Singapore Spring User Group

3 presentations:
- Michael Isvy on Spring's Caching support
- Sergiu Bodiu on Redis
- Wang Yi on SqlFire

Michael Isvy

February 06, 2014
Tweet

More Decks by Michael Isvy

Other Decks in Programming

Transcript

  1. Agenda ▪ Michael Isvy – Spring Caching support ▪ Sergiu

    Bodiu – Redis ▪ Wang Yi – SqlFire
  2. About Caching ▪ What is a cache? • In this

    context: a key-value store = Map ▪ Where do we use this caching? • Any method that always returns the same result for the same argument(s) • This method could do anything • Calculate data on the fly • Execute a database query • Request data via RMI, JMS, a web-service ... • A unique key must be generated from the arguments • That's the cache key
  3. Caching Support ▪ Available since Spring 3.1 • Transparently applies

    caching to Spring beans (AOP) • Define one or more caches in Spring configuration • Mark methods cacheable • Indicate caching key(s) • Name of cache to use (multiple caches supported) • Use annotations or XML @Cacheable public Country[] loadAllCountries() { … } Spring Application In-memory cache Spring 3.1
  4. Caching with @Cacheable ▪ @Cacheable marks a method for caching

    • its result is stored in a cache • subsequent invocations (with the same arguments) • fetch data from cache using key, method not executed ▪ @Cacheable attributes • value: name of cache to use • key: the key for each cached data-item • Uses SpEL and argument(s) of method @Cacheable(value="books", key="#refId.toUpperCase()") public Book findBook(String refId) {...}
  5. Caching via Annotations @Cacheable(value="books", key="#title" condition="#title.length < 32") public Book

    findBook(String title, boolean checkWarehouse); @Cacheable(value="books", key="#author.name") public Book findBook2(Author author, boolean checkWarehouse); @Cacheable(value="books", key="T(example.KeyGen).hash(#author)") public Book findBook3(Author author, boolean checkWarehouse); @CacheEvict(value="books") public Book loadBooks(); @Configuration @EnableCaching public class MyConfig { @Bean public class BookService bookService() { } @Configuration @EnableCaching public class MyConfig { @Bean public class BookService bookService() { } clear cache before method invoked use object property Only cache if condition true custom key generator Use 'books' cache
  6. Setup 'JDK' Cache Manager ▪ Must specify a cache-manager •

    Some provided, or write your own • See org.springframework.cache package. <bean id="cacheManager" class="o.s.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="o.s.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="authors" /> <bean class="o.s.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="books" /> </set> </property> </bean> <bean id="cacheManager" class="o.s.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="o.s.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="authors" /> <bean class="o.s.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="books" /> </set> </property> </bean> Concurrent Map Cache
  7. Third-Party Cache Managers ▪ EhCache example <bean id="cacheManager" class="...EhCacheCacheManager" p:cache-manager-ref="ehcache"

    /> <bean id="ehcache" class="o.s.cache.ehcache.EhCacheCacheManagerFactoryBean" p:config-location="ehcache.xml" /> <bean id="cacheManager" class="...EhCacheCacheManager" p:cache-manager-ref="ehcache" /> <bean id="ehcache" class="o.s.cache.ehcache.EhCacheCacheManagerFactoryBean" p:config-location="ehcache.xml" /> EHcache
  8. 1 Singapore Spring User Group 6th February 2014 Sergiu Bodiu

    https://www.linkedin.com/in/sergiubodiu
  9. 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
  10. 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
  11. 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
  12. 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...
  13. 7 Redis Strings 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
  14. 8 Redis Hashes 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 lasname sanfilipo (integer) 1 > HGET user:1 firstname "salvatore" > HGETALL user:1 1) "firstname" 2) "salvatore" 3) "lasname" 4) "sanfilipo"
  15. 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.
  16. 10 Redis is feature rich 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)
  17. 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.
  18. 12 Configure RedisTemplate.... <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory " p:use-pool="true"/> <!-- redis

    template definition --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnFactory"/>
  19. public class Example { // inject the actual template @Autowired

    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()); } }
  20. 1 © Copyright 2013 Pivotal. All rights reserved. 1 ©

    Copyright 2013 Pivotal. All rights reserved. NewSQL: Pivotal SQLFire WangYi April 2013
  21. 2 © Copyright 2013 Pivotal. All rights reserved. Fast Data

    :Modern Approach to Data Mgmt §  Virtual data ‘fabric’ manages data in-memory using low cost x-86 machines for elastic scalability §  Deploy locally or as high speed data backbone across global regions and private/ public clouds §  Use as operational data store or to complement existing RDBMS File Systems Databases Mainframes / other GemFire/SQLFire “Distributed caching platforms (DCPs) have proved to be key enablers for mainframe and DBMS offloading, performance and scalability boosting in Web commerce applications, high-performance publish and subscribe, cloud and software as a service (SaaS), and other scenarios.” – Gartner Emerging Technology Analysis: Distributed Caching Platforms Enable Innovative Scenarios; November 2, 2010
  22. 3 © Copyright 2013 Pivotal. All rights reserved. Enabling Elastic

    Scale for the Data Tier Load Balancer Web and App Servers GemFire/SQLFire Application Tier Middleware Tier Database Tier
  23. 5 © Copyright 2013 Pivotal. All rights reserved. SQLFire § 

    RDBMS components §  JDBC driver §  Query engine §  Network server §  Reliable data distribution §  Replication and partitioning §  Caching framework §  Parallel “data- aware” application behavior routing
  24. 6 © Copyright 2013 Pivotal. All rights reserved. 6 ©

    Copyright 2013 Pivotal. All rights reserved. Demo
  25. 8 © Copyright 2013 Pivotal. All rights reserved. Colocation Order

    Idw Customer Id1 Order Idx Customer Id2 Order Idy Customer Id3 Order Idz Customer Idn ProductA ProductB ProductC ProductA ProductB ProductC Master/Reference Data Replicated Active Data Partitioned & Colocated
  26. 9 © Copyright 2013 Pivotal. All rights reserved. Colocation Order

    Idw Customer Id1 Order Idx Customer Id2 Order Idy Customer Id3 Order Idz Customer Idn ProductA ProductB ProductC ProductA ProductB ProductC Master/Reference Data Replicated Active Data Partitioned & Colocated
  27. 10 © Copyright 2013 Pivotal. All rights reserved. Colocation Master/Reference

    Data Replicated Active Data Partitioned & Colocated CustomerId FirstName LastName State 0952-039-2352 John Tillman CA 0938-345-2196 Francine Sparks OR OrderId CustomerId Status TotalPrice 8762885932 0952-039-2352 Shipped $788.90 1435996288 0938-345-2196 Invoiced $308.95 ProductId SKU UnitPrice UnitQty AB75W67 8906367723857 $165.67 12 HRT8974 4599846779843 $78.89 1 NM9877G 4865776928749 $308.95 1 CUSTOMERS ORDERS PRODUCTS OrderLineId OrderId ProductId QtyOrdered 7803986049363 8762885932 HRT8974 10 8803299688492 1435996288 NM9877G 1 ORDERLINES CustomerId FirstName LastName State 8432-523-6857 Robert Beckman NY 3965-789-4536 Paula Sinclari OK OrderId CustomerId Status TotalPrice 1453526642 3965-789-4536 Backordered $617.90 6584586674 08432-523-6857 Fulfilled $479.01 ProductId SKU UnitPrice UnitQty AB75W67 8906367723857 $165.67 12 HRT8974 4599846779843 $78.89 1 NM9877G 4865776928749 $308.95 1 CUSTOMERS ORDERS PRODUCTS OrderLineId OrderId ProductId QtyOrdered 3453445364545 6584586674 AB75W67 3 8567556743456 1453526642 NM9877G 2 ORDERLINES
  28. 11 © Copyright 2013 Pivotal. All rights reserved. Colocated Transaction

    (Objects) Order Idw Customer Id1 Order Idx Customer Id2 Order Idy Customer Id3 Order Idz Customer Idn ProductA ProductB ProductC ProductA ProductB ProductC Transaction Boundary
  29. 12 © Copyright 2013 Pivotal. All rights reserved. Colocated Query

    (Objects) Order Idw Customer Id1 Order Idx Customer Id2 Order Idy Customer Id3 Order Idz Customer Idn ProductA ProductA ProductB ProductC Query Boundary ProductC ProductB
  30. 13 © Copyright 2013 Pivotal. All rights reserved. Colocated Transaction

    (Relational) CustomerId FirstName LastName State 0952-039-2352 John Tillman CA 0938-345-2196 Francine Sparks OR OrderId CustomerId Status TotalPrice 8762885932 0952-039-2352 Shipped $788.90 1435996288 0938-345-2196 Invoiced $308.95 ProductId SKU UnitPrice UnitQty AB75W67 8906367723857 $165.67 12 HRT8974 4599846779843 $78.89 1 NM9877G 4865776928749 $308.95 1 CUSTOMERS ORDERS PRODUCTS OrderLineId OrderId ProductId QtyOrdered 7803986049363 8762885932 HRT8974 10 8803299688492 1435996288 NM9877G 1 ORDERLINES CustomerId FirstName LastName State 8432-523-6857 Robert Beckman NY 3965-789-4536 Paula Sinclari OK OrderId CustomerId Status TotalPrice 1453526642 3965-789-4536 Backordered $617.90 6584586674 08432-523-6857 Fulfilled $479.01 ProductId SKU UnitPrice UnitQty AB75W67 8906367723857 $165.67 12 HRT8974 4599846779843 $78.89 1 NM9877G 4865776928749 $308.95 1 CUSTOMERS ORDERS PRODUCTS OrderLineId OrderId ProductId QtyOrdered 3453445364545 6584586674 AB75W67 3 8567556743456 1453526642 NM9877G 2 ORDERLINES Transaction Boundary
  31. 14 © Copyright 2013 Pivotal. All rights reserved. Colocated Query

    (Relational) CustomerId FirstName LastName State 0952-039-2352 John Tillman CA 0938-345-2196 Francine Sparks OR OrderId CustomerId Status TotalPrice 8762885932 0952-039-2352 Shipped $788.90 1435996288 0938-345-2196 Invoiced $308.95 ProductId SKU UnitPrice UnitQty AB75W67 8906367723857 $165.67 12 HRT8974 4599846779843 $78.89 1 NM9877G 4865776928749 $308.95 1 CUSTOMERS ORDERS PRODUCTS OrderLineId OrderId ProductId QtyOrdered 7803986049363 8762885932 HRT8974 10 8803299688492 1435996288 NM9877G 1 ORDERLINES CustomerId FirstName LastName State 8432-523-6857 Robert Beckman NY 3965-789-4536 Paula Sinclari OK OrderId CustomerId Status TotalPrice 1453526642 3965-789-4536 Backordered $617.90 6584586674 08432-523-6857 Fulfilled $479.01 ProductId SKU UnitPrice UnitQty AB75W67 8906367723857 $165.67 12 HRT8974 4599846779843 $78.89 1 NM9877G 4865776928749 $308.95 1 CUSTOMERS ORDERS PRODUCTS OrderLineId OrderId ProductId QtyOrdered 3453445364545 6584586674 AB75W67 3 8567556743456 1453526642 NM9877G 2 ORDERLINES