Caching In Java- Best Practises and Pitfalls

17
Caching -Hariharan Anantharaman Architect,Cognizant Technology Solutions Founder, Hobbyshopindia.com [email protected]

description

This presentation mentions about key concepts of Java side caching and things to consider. It also mentions about popular tools and caching in AWS and Google App Engine.

Transcript of Caching In Java- Best Practises and Pitfalls

Page 1: Caching In Java- Best Practises and Pitfalls

Caching-Hariharan Anantharaman

Architect,Cognizant Technology SolutionsFounder, Hobbyshopindia.com

[email protected]

Page 2: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 2

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

Need for Speed-Caching

Page 3: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 3

Regular N-Tier JEE Architecture

Page 4: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 4

Serializable Java Objects Stored In-Memory or File based Key – Value Pairs Scale of the Application determines the

solution

Caching in Java – Basics

Page 5: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 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

Use Cases

Determining Factors• TTL (Time to Live)• Frequency of Access• Frequency of Update• Deployment Architecture• Scalability of the systems• Object Size• Heap Memory Availability

Page 6: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 6

Cache Solution Overview

Page 7: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 7

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

Different Approaches

Things to be Considered• Clustered Environments• Session Replication• Distributed Servers• Failover Scenarios• Object Replication, Garbage Collection• Effective Memory Utilization

Page 8: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 8

ICacheManager CaCheManagerImpl put(Key ,Value) get(Key ) replace(Key ,Value) delete(Key )

General Implementation Approach

Page 9: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 9

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

Server Memory Cache

Page 10: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 10

Profiler Tool Java.lang.instrument.Instrumentation

Cache Population Strategies Eager Fetch Lazy Load

Finding Object Size

Page 11: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 11

Open Source/Free EhCache JCS JBossCache MemaCache

Popular Solutions

Commercial/Properiatery Solutions• TerraCotta BigMemorys• IBM Dynacache(Shipped with Websphere)• IBM Extreme Scale

Page 12: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 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

Critical Cache Configuration

Page 13: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 13

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

Memacache

Page 14: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 14

IBM Extreme Scale

Page 15: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 15

Predominantly based on Memacache Available with all major cloud providers Provides easy scalability, recovery etc

Caching in Cloud

Page 16: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 16

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.

Amazon Elastic Cache

Page 17: Caching In Java- Best Practises and Pitfalls

JUG-Chennai 17

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

Google App Engine