-Hariharan Anantharaman
Architect,Cognizant Technology Solutions
Founder, Hobbyshopindia.com
Hariharan.anantharaman@gmail.com
 Get/Access the resource or Data once, use
multiple times
 Frequently used strategy to improve
performance
 Approach is language independent
 Architecture and Design decision.
 Multiple options available at multiple layers
JUG-Chennai 2
JUG-Chennai 3
 Serializable Java Objects
 Stored In-Memory or File based
 Key – Value Pairs
 Scale of the Application determines the
solution
JUG-Chennai 4
 Static Data
◦ State List
◦ Countries List
 Pseudo Static Data
◦ Catalog Data
◦ Exchange Rate
 Context Specific Data
◦ User Profile Data
◦ Session Specific Data
◦ Data From external Systems
Different Needs, Different
Solutions
JUG-Chennai 5
Determining Factors
• TTL (Time to Live)
• Frequency of Access
• Frequency of Update
• Deployment Architecture
• Scalability of the systems
• Object Size
• Heap Memory Availability
JUG-Chennai 6
 HashMaps
 Session Objects
 Localized On-Memory Cache
 Intra cluster Distributed Cache (Replicated Cache)
 Global Distributed Cache
 JSR 107 has been conceptualized and released on 18 March
2014 replicated cache
JUG-Chennai 7
Things to be Considered
• Clustered Environments
• Session Replication
• Distributed Servers
• Failover Scenarios
• Object Replication, Garbage Collection
• Effective Memory Utilization
 ICacheManager
 CaCheManagerImpl
 put(Key ,Value)
 get(Key )
 replace(Key ,Value)
 delete(Key )
JUG-Chennai 8
 Number of parallel sessions
 Available Heap memory
 Average Session Duration
 Number of Global objects to be stored in
Cache
 Number of transaction specific objects to be
stored in Cache
 Size of objects to be stored in Cache
JUG-Chennai 9
 Profiler Tool
 Java.lang.instrument.Instrumentation
Cache Population Strategies
 Eager Fetch
 Lazy Load
JUG-Chennai 10
Open Source/Free
 EhCache
 JCS
 JBossCache
 MemaCache
JUG-Chennai 11
Commercial/Properiatery Solutions
• TerraCotta BigMemorys
• IBM Dynacache(Shipped with Websphere)
• IBM Extreme Scale
 Maximum Memory
 Purge Strategy
 Reload
 Scheduled Cache Expiry
 Algorithm for removing item from
Cache(LRU,FIFO),MRU,Last Access Time etc
 Object size based cache Removal
JUG-Chennai 12
 High performance Distributed memory object caching system
 Memacache consists of two parts
◦ Memacache Server. Separate process running in same
machine as server or running in different machine
◦ Memacache Client. Used by Client applications. Langauage
specific client jars are available to work with Memacache
JUG-Chennai 13
JUG-Chennai 14
 Predominantly based on Memacache
 Available with all major cloud providers
 Provides easy scalability, recovery etc
JUG-Chennai 15
 Based Upon Memache or
Redis
 Cache clusters consists of
multiple cache nodes.
 Configured from AWS
mgmt console or tools
 Redis-
◦ A popular open-source
in-memory key-value
store that supports data
structures such as sorted
sets and lists.
◦ ElastiCache supports
Redis master / slave
replication which can be
used to achieve cross AZ
redundancy.
JUG-Chennai 16
 Provides Two Types of Cache
◦ In-Context Cache
 Available only for the duration of single HTTP Request
 Data is present in memory
◦ Memacache
 Default and Enabled for All applications
 Faster than datastore, slower than In-context cache
 By default, a nontransactional context caches all entities in
memcache.
 Memcache does not support transactions
 When DB is updated, it needs to be updated explicitly in
Memacache
JUG-Chennai 17

Caching In Java- Best Practises and Pitfalls

  • 1.
    -Hariharan Anantharaman Architect,Cognizant TechnologySolutions Founder, Hobbyshopindia.com Hariharan.anantharaman@gmail.com
  • 2.
     Get/Access theresource or Data once, use multiple times  Frequently used strategy to improve performance  Approach is language independent  Architecture and Design decision.  Multiple options available at multiple layers JUG-Chennai 2
  • 3.
  • 4.
     Serializable JavaObjects  Stored In-Memory or File based  Key – Value Pairs  Scale of the Application determines the solution JUG-Chennai 4
  • 5.
     Static Data ◦State List ◦ Countries List  Pseudo Static Data ◦ Catalog Data ◦ Exchange Rate  Context Specific Data ◦ User Profile Data ◦ Session Specific Data ◦ Data From external Systems Different Needs, Different Solutions JUG-Chennai 5 Determining Factors • TTL (Time to Live) • Frequency of Access • Frequency of Update • Deployment Architecture • Scalability of the systems • Object Size • Heap Memory Availability
  • 6.
  • 7.
     HashMaps  SessionObjects  Localized On-Memory Cache  Intra cluster Distributed Cache (Replicated Cache)  Global Distributed Cache  JSR 107 has been conceptualized and released on 18 March 2014 replicated cache JUG-Chennai 7 Things to be Considered • Clustered Environments • Session Replication • Distributed Servers • Failover Scenarios • Object Replication, Garbage Collection • Effective Memory Utilization
  • 8.
     ICacheManager  CaCheManagerImpl put(Key ,Value)  get(Key )  replace(Key ,Value)  delete(Key ) JUG-Chennai 8
  • 9.
     Number ofparallel sessions  Available Heap memory  Average Session Duration  Number of Global objects to be stored in Cache  Number of transaction specific objects to be stored in Cache  Size of objects to be stored in Cache JUG-Chennai 9
  • 10.
     Profiler Tool Java.lang.instrument.Instrumentation Cache Population Strategies  Eager Fetch  Lazy Load JUG-Chennai 10
  • 11.
    Open Source/Free  EhCache JCS  JBossCache  MemaCache JUG-Chennai 11 Commercial/Properiatery Solutions • TerraCotta BigMemorys • IBM Dynacache(Shipped with Websphere) • IBM Extreme Scale
  • 12.
     Maximum Memory Purge Strategy  Reload  Scheduled Cache Expiry  Algorithm for removing item from Cache(LRU,FIFO),MRU,Last Access Time etc  Object size based cache Removal JUG-Chennai 12
  • 13.
     High performanceDistributed memory object caching system  Memacache consists of two parts ◦ Memacache Server. Separate process running in same machine as server or running in different machine ◦ Memacache Client. Used by Client applications. Langauage specific client jars are available to work with Memacache JUG-Chennai 13
  • 14.
  • 15.
     Predominantly basedon Memacache  Available with all major cloud providers  Provides easy scalability, recovery etc JUG-Chennai 15
  • 16.
     Based UponMemache or Redis  Cache clusters consists of multiple cache nodes.  Configured from AWS mgmt console or tools  Redis- ◦ A popular open-source in-memory key-value store that supports data structures such as sorted sets and lists. ◦ ElastiCache supports Redis master / slave replication which can be used to achieve cross AZ redundancy. JUG-Chennai 16
  • 17.
     Provides TwoTypes of Cache ◦ In-Context Cache  Available only for the duration of single HTTP Request  Data is present in memory ◦ Memacache  Default and Enabled for All applications  Faster than datastore, slower than In-context cache  By default, a nontransactional context caches all entities in memcache.  Memcache does not support transactions  When DB is updated, it needs to be updated explicitly in Memacache JUG-Chennai 17