Caching Across Enterprise Application Tiers Reza Rahman Expert Group Member, Java EE 6 and EJB 3.1 Resin EJB 3.1 Lite Container Developer Author, EJB 3.

Slides:



Advertisements
Similar presentations
HTTP and Apache Roy T. Fielding eBuilt, Inc. The Apache Software Foundation
Advertisements

CS193H: High Performance Web Sites Lecture 17: Rule 14 – Make Ajax Cacheable Steve Souders Google
11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Enterprise Java and Data Services Designing for Broadly Available Grid Data Access Services.
Welcome to Middleware Joseph Amrithraj
J2EE Architecture Overview
Database System Concepts and Architecture
Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
JLab Lattice Portal – Data Grid Web Service Ying Chen, Chip Watson Thomas Jefferson National Accelerator Facility.
Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
PACS – 06/21/14 1 Cache? What is caching? A way to increase the average rate of a process by preferentially using a copy of data in a faster, closer, probably.
Apache Struts Technology
TCP/IP Protocol Suite 1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 22 World Wide Web and HTTP.
ISE 390 Dynamic Web Development Java EE Web Applications.
An architecture for webb applications, J2EE
JBoss Seam: Contextual Components Jason Bechtel
What’s a Web Cache? Why do people use them? Web cache location Web cache purpose There are two main reasons that Web cache are used:  to reduce latency.
2/9/2004 Web and HTTP February 9, /9/2004 Assignments Due – Reading and Warmup Work on Message of the Day.
PL-IV- Group A HTTP Request & Response Header
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Word Wide Cache Distributed Caching for the Distributed Enterprise.
Eclipse and Oracle | © 2008 Oracle; made available under the EPL v1.0 Building Applications with Eclipse Frameworks and Tools for Oracle Shaun Smith EclipseLink,
Chapter 4: Core Web Technologies
Ing. Ignacio Roqueta ARTech GeneXus and J2EE.
CP476 Internet Computing Lecture 5 : HTTP, WWW and URL 1 Lecture 5. WWW, HTTP and URL Objective: to review the concepts of WWW to understand how HTTP works.
TCP/IP Protocol Suite 1 Chapter 22 Upon completion you will be able to: World Wide Web: HTTP Understand the components of a browser and a server Understand.
Java Omar Rana University of South Asia. Course Overview JAVA  C/C++ and JAVA Comparison  OOP in JAVA  Exception Handling  Streams  Graphics User.
CS 493/693: Distributed Systems Programming V. “Juggy” Jagannathan CSEE, West Virginia University March 21, 2005.
JBoss Cache. Cache A place to temporarily store data that is expensive or difficult to compute or retrieve. Caches should be fast to access. May or may.
Enterprise Java Bean Matt. 2 J2EE 3 J2EE Overview.
Enterprise JavaBeans. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports application development.
JBoss Seam Presented by Andy Nguyen Truc Pham. What is JBoss Seam? Created by Gavin King Created by Gavin King A lightweight framework for Java EE 5.0.
J2EE Structure & Definitions Catie Welsh CSE 432
Oracle Application Express Security. © 2009 Oracle Corporation Authentication Out-of-the-Box Pre-Configured Schemes LDAP Directory credentials Oracle.
Source: Peter Eeles, Kelli Houston, and Wojtek Kozaczynsky, Building J2EE Applicationa with the Rational Unified Process, Addison Wesley, 2003 Prepared.
Introduction to Web Dimitar Nenchev Ivan Nakov
A Presentation to Oracle OpenWorld Blistering Web Applications with Oracle TimesTen In Memory Option.
Middleware for FIs Apeego House 4B, Tardeo Rd. Mumbai Tel: Fax:
3-Tier Web Application Architecture. Simple Log-in public String button1_action() { // TODO: Process the button click action. Return value is a navigation.
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
NA-MIC National Alliance for Medical Image Computing UCSD: Engineering Core 2 Portal and Grid Infrastructure.
1-1 HTTP request message GET /somedir/page.html HTTP/1.1 Host: User-agent: Mozilla/4.0 Connection: close Accept-language:fr request.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Assignment of JAVA id : BSSE-F10-M-10-JAVA1 Overview of J2EE/Session 2/Slide 1 of 38.
27.1 Chapter 27 WWW and HTTP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Introduction to EJB. What is an EJB ?  An enterprise java bean is a server-side component that encapsulates the business logic of an application. By.
Java Servlets and Java Server Pages
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Overview of the Spring Framework Introducing.
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
EJB Enterprise Java Beans JAVA Enterprise Edition
1 Chapter 22 World Wide Web (HTTP) Chapter 22 World Wide Web (HTTP) Mi-Jung Choi Dept. of Computer Science and Engineering
Web Cache. What is Cache? Cache is the storing of data temporarily to improve performance. Cache exist in a variety of areas such as your CPU, Hard Disk.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
HTTP request message: general format
EJB (Enterprise Java Beans)
Server Concepts Dr. Charles W. Kann.
Web Software Model CS 4640 Programming Languages for Web Applications
Client / Session Identification Cookies
Web Caching? Web Caching:.
Introduction to J2EE Architecture
Internet Applications
ISE 390 Dynamic Web Development
Komponentbasert utvikling Den sanne objektorientering
EE 122: HyperText Transfer Protocol (HTTP)
Enterprise Java Beans.
Presentation transcript:

Caching Across Enterprise Application Tiers Reza Rahman Expert Group Member, Java EE 6 and EJB 3.1 Resin EJB 3.1 Lite Container Developer Author, EJB 3 in Action

Caching Caching is an essential tool for scaling web applications The basic concept is to avoid disk access, I/O, CPU power and network traffic – primarily to the backend relational database in modern web applications With memory becoming a commodity, it makes more and more sense to make intelligent use of caches – the real bottleneck is synchronization across the network Across enterprise application tiers, caching comes in many forms, some obvious and others not so obvious

Enterprise Application Tiers Layering is an essential way of managing complexity for non-trivial, team based (often distributed team based in a post-outsourcing world) enterprise applications Traditional layered architectures (a la J2EE 1.4 design patters) and domain driven design segment enterprise applications into roughly fours tiers Even a minimalist approach (a ala Seam) utilizes specialist APIs per logical tier Each of the tiers has caching of its own flavor

Enterprise Application Tiers

Web (HTTP) Tier Web (HTTP) tier caching is the easiest, most non- invasive and a very effective way of enhancing application performance You should consider caching static content such as straight HTML, images, PDF files, CSS and JavaScript You should also consider caching dynamic content that changes only infrequently The HTTP protocol provides robust support for caching

HTTP Caching Header Example HTTP/ OK Date: Fri, 30 Oct :19:41 GMT Server: Apache/1.3.3 (Unix) Cache-Control: max-age=3600, must-revalidate Expires: Fri, 30 Oct :19:41 GMT Last-Modified: Mon, 29 Jun :28:12 GMT ETag: "3e fbbc" Content-Length: 1040 Content-Type: text/html

HTTP HeaderDescription Last-modifiedThe time the content was last changed ETagUnique identifier for a given version of content ExpiresTime when content expires Cache-ControlExplicit instructions for caching: public (definitely cache), private (browser cache only), no-cache (dont cache), max-age=n (set expiration), s-maxage=n (set expiration for proxy caches only), must-revalidate (no short-cuts for validation), proxy-revalidate (no short- cuts for validation for proxies only) HTTP Caching Headers

Web Caching in the Data Center

Apache Web Server Caching ### activate mod_expires ExpiresActive On ### Expire.gif's 1 month from when they're accessed ExpiresByType image/gif A ### Expire everything else 1 day from when it's last modified ### (this uses the Alternative syntax) ExpiresDefault "modification plus 1 day" ### Apply a Cache-Control header to index.html Header append Cache-Control "public, must-revalidate"

Resin Proxy Caching <cache entries="16384 disk-size="2G" memory-size="256M"/>

Programmatic Cache Control <% long now = System.currentTimeMillis(); response.setDateHeader("Expires", now ); %> Count:

Presentation Tier Rich application state maintenance natural caching mechanism - avoids needless database interaction (a la old school CGI/PERL/PHP or some action-oriented web frameworks) Correct granularity (scope) is critical for efficient memory use Servlets/JSP, JSF, CDI (or Spring) provide roughly expanding granularity and abstraction Activation/passivation critical for proper memory management Most application servers automatically cluster state

APIScopes Servlet/JSP Application (Servlet Context), session, request, Flash (more scopes such as named conversations and window scope available through CDI plug-ins) SpringSingleton, global session, session, request, prototype (Spring Web Flow provides some expanded scopes such as conversations) Web Tier Scopes

Saving State in Servets request.setAttribute(request-key", request-value"); HttpSession session = request.getSession(false); if (session != null) { session.setAttribute("session-key", "session-value"); } getServletContext().setAttribute("application-key", "application-value"); response.addCookie( new Cookie("cookie-key", "cookie-value"));

Saving State in Servets request.setAttribute(request-key", request-value"); HttpSession session = request.getSession(false); if (session != null) { session.setAttribute("session-key", "session-value"); } getServletContext().setAttribute("application-key", "application-value"); response.addCookie( new Cookie("cookie-key", "cookie-value"));

Login Username: <h:inputText id="username" value="#{credentials.username}"/> Password: <h:inputSecret id="password" value="#{credentials.password}"/> <h:commandButton value="Login action="#{login.login}"/> JSF Page with CDI

public class Credentials implements Serializable { private String username; private String password;... Getters and setters... } CDI JSF Model

public class Login implements Serializable private Credentials private UserService userService; private User user; public String login() { user = userService.getUser(credentials.getUsername());... Do a password check here... return @LoggedIn public User getCurrentUser() { return user; } CDI JSF Event Handler

public class AccountManager implements private User private public Account getCurrentAccount() { return accountService.getAccount( user.getUsername()); } CDI Web Tier Producer

From Bank Account Bank Name: <h:inputText id="fromBank" value="#{transfer.fromBank}"/> Account Number: <h:inputText id="fromAccount value="#{transfer.fromAccount}"/> <h:commandButton value="Start action="#{transfer.enterToBank}"/> JSF Workflow (Step 1)

To Bank Account From Bank: <h:outputText id="fromBank" value="#{transfer.fromBank}"/> From Account: <h:outputText id="fromAccount value="#{transfer.fromAccount}"/> Bank Name: Account Number: <h:inputText id="toAccount" value="#{transfer.toAccount}"/> JSF Workflow (Step 2)

Transfer Amount From Bank: From Account: <h:outputText id="fromAccount value="#{transfer.fromAccount}"/> From Bank: To Account: Amount: JSF Workflow (Step 3)

public class Transfer implements Serializable private transient AccountService private Conversation conversation; private String fromBank; private String fromAccount; private String toBank; private String toAccount; private double amount;... Getters and setters... public String enterToBank() { conversation.begin(); return "enter_to_bank.xhtml"; } public String transfer() { accountService.transfer(toBank, toAccount, fromBank, fromAccount, amount); conversation.end(); return "account.xhtml"; } CDI Conversations

Server State Behind the Scenes

Activation/Passivation

Application Tier CDIs (or Springs) scoping, context and state management capabilities can be used in the application tier as well EJB stateless beans/MDB offer pooling EJB stateful beans offer better support for activation/passivation as well as persistence caches EJB singleton beans offer better support for concurrency Terracotta for Spring provides EJB-like clustering Well talk about direct distributed caching tools later

@MessageDriven(activationConfig = propertyName="destinationName", propertyValue="jms/OrderBillingQueue")}) public class OrderBillingMDB implements MessageListener {... public void onMessage(Message message) { try { ObjectMessage objectMessage = (ObjectMessage) message; Order order = (Order) objectMessage.getObject(); try { bill(order); notifyBillingSuccess(order); order.setStatus(OrderStatus.COMPLETE); } catch (BillingException be) { notifyBillingFailure(be, order); order.setStatus(OrderStatus.BILLING_FAILED); } finally { update(order); } } catch (Exception e) { e.printStackTrace(); }... } Message-Driven Bean

Message-Driven Bean Pooling

@Stateful public class BidderAccountCreatorBean implements BidderAccountCreator private DataSource dataSource; private public void openConnection() {... connection = public void cleanup() {... connection.close();... } EJB Stateful Bean

public class SharedDataBean private EntityManager entityManager; private Data private void init() { data = entityManager.find(Data.class, 1); private void destroy() { entityManager.merge(data); public void setData(Data data) { this.data = data; public Data getData() { return data; } EJB Singleton Bean

@Stateful public class ShoppingCartBean implements ShoppingCart private EntityManager entityManager; private Order order;... public void createOrder() { order = new Order();... entityManager.persist(order); } public void addToCart(Item item) { order.addItem(item); }... } Extended Persistence Context

Domain/Infrastructure Tier The persistence tier is an ideal point for caching since caching can be seen as a parallel concern to relational database centric persistence – particularly with ORM JPA 2 supports both transactional (1 st level) and shared (2 nd level) caching Persistence tier caching is well suited for domain objects with high read/write ratio Second level caching is largely an ORM adapter over existing distributed caches

JPA 2 Caching

<persistence xmlns=" xmlns:xsi=" xsi:schemaLocation=" version="2.0">... ENABLE_SELECTIVE... <property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider"/> <property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true"/> JPA Cache Configuration

@Entity = = "CATEGORIES") public class Category implements private Long id; private = "PARENT_ID") private Category = "parent") private Set subcategories;... } JPA Cache Configuration

public interface Cache { public boolean contains(Class clazz, Object key); public void evict(Class clazz, Object key); public void evict(Class clazz); public void evictAll(); } JPA Cache Control API

Resource (Database) Tier Database/resource connection pooling essential caching technique for any serious production application Most application servers will also provide prepared statement caching as part of the connection pool Tune both connection pool size and prepared statement cache size You can also do statement caching on the database end – talk to your DBA

Distributed Caches Distributed caches are a key infrastructure piece for any caching or clustering solution They can be used directly in the application or persistence tier where they are needed (a relative rarity) It is a crowded field with many open source and commercial options Tools come in a variety of clustering, replication, transactionality, robustness and administrative features (cost and support are all over the map too)

@Inject Cache cache;... cache.put(key, value);... String data = (String) cache.get("my-data"); JCache (JSR-107) API Example

public class DefaultBidDao implements BidDao private DataSource public Bid getBid(Long id) {... public void updateBid(Bid bid) {... }... } Caching as an Aspect

Java Distributed Caches Coherence Terracotta GigaSpaces Infinispan/JBossCache EHCache JCS SwarmCache OSCache

Caching an important optimization technique for vast majority of applications Each enterprise application tier is rife with opportunities for caching Some caching techniques are obvious and natural, others require more careful analysis Some are transparent while others are somewhat invasive Variety of options to match application needs Summary

References Caching in HTTP, Apache Caching Module, Resin Proxy Cache, cache.xtp cache.xtp Oracle Coherence, ce/overview/index.html ce/overview/index.html

References Terracotta, GigaSpaces, Infinispan/JBossCache, EHCache, JCS, SwarmCache, OSCache,