J2EE: Best Practices for Application Development and Achieving High-Volume Throughput Michael S Pallos, MBA Session: 3567, 4:30 pm August 11, 2003
Agenda Architecture Overview WebSphere Application Server scalability A proven design pattern Servlets and Java Server Pages Sessions Memory usage
Agenda (continued) Enterprise Java Beans Connecting to legacy systems and databases Connection Pooling Prepared Statements Session Database Java Database Connector (JDBC) Systems Management
Architectural Overview WebSphere Application Server 3.5 & 4.x 5.x
WAS Architecture (3.5 & 4.x) GUI Client PC Web Browser HTTP(S) WAS Admin Console Java Admin Console http://localhost:9090/admin HTTP(S) HTTP Server Plug-in plugin-cfg.xml IIOP Embedded HTTP Server Servlet JSP Web Container Admin Server EJB EJB Container Application Server - JVM Application Database Administration Repository CMD WebSphere Control Program (WSCP) XMLConfig Admin Server - JVM Node (machine)
WAS Architecture (5.x) Client PC HttpServer1 AppServerX Web Browser HTTP(S) HTTP Server Application Instance (JVM) Servlet Container Plug-in plugin-cfg.xml HTTP(S) Embedded HTTP Server Servlet JSP Application Database WAS Administrative Console Standard J2EE 1.3 Web App wsadmin Scripting command line Tool (BSF & JMX based) EJB EJB Container XML Configuration Files
Application Server Clones Server Groups Templates Application server and contents Changes to group propagate to all clones Not JVMs and Not Application Servers Clones Exact Copy configured/functional application server Managed by parent server group Vertical Clones Additional running copy of Application Server on same node Horizontal Clone Additional running copy of Application Server on different node
Vertical Cloning Web Browser Client HTTP(S) Servlets JSPs Web Container EJBs EJB Container Application Server #1 Clone A - JVM Application Database HTTP Server Plug-in plugin-cfg.xml Machine #1 Servlets JSPs Web Container EJBs EJB Container Application Server #1 Clone B - JVM Admin Server - JVM Node (machine #2) Administration Repository
Vertical Cloning Strengths Improved throughput on large machines Easy to maintain and support Weaknesses Limited available resources on single machine Threats Single point of failure Memory/CPU overhead if have too many clones Opportunities Can be combined with other topologies
Horizontal Cloning Web Browser Client HTTP(S) Servlets JSPs Web Container EJBs Application Server #1 Clone A - JVM Admin Server - JVM Node (machine #2) EJB Container Application Database HTTP Server Plug-in plugin-cfg.xml machine #1 Servlets JSPs Web Container EJBs EJB Container Application Server #1 Clone A - JVM Admin Server - JVM Node (machine #3) Administration Repository
Horizontal Cloning Strengths Distribution of load among machines Failover support if one machine goes down More machines provide more CPU power Weaknesses More difficult to setup and maintain More $$ for hardware and software Threats Requires session persistency Increased network traffic between machines Opportunities Can increase number of machines as needed, highly scalable
Design Pattern Model View Controller
Design Pattern MVC Model responsible for the data or business process (persistence/business logic) View responsible for the user s display (CUI/GUI) Controller is responsible for the flow of the application (traffic cop)
Model-View-Controller View Controller Model HTML Request Servlet Beans Cookie Submit Forward DB HTML Response JSP Beans
Servlets & Java Server Pages (JSP)
Servlets & JSP Small, thin Servlets 50 < Lines of code Avoid single-threaded Avoid System.Out.Println() Avoid string concatenation Use Java StringBuffer Avoid Serialization Synchronize statements are costly
Synchronize statements 60 HTTP Requests/Sec (Hundreds) 50 40 30 20 10 0 www.developer.ibm.com No synchronization Synchronization
Sessions
Sessions Keep session objects small < 2k Designing session data Life of Session Persistent session as dedicated data source Single row persistence Multi row persistence w/ a large field Keep large object graphs out
Sessions (continued) Do not create HttpSession objects by default on JSPs Even though it is part of J2EE spec Use <%@page session= false %> Writing your own JDBC calls
Data Source & Backend Systems Data Source Connection Pooling Prepare Statements Session Database Java Database Connector (JDBC)
Data Source Set up data-source once Object initialization phase Servlet.init(), ejbactive() Release to connection pool
Data Source Caching HTTP Requests/Sec (Hundreds) 80 70 60 50 40 30 20 10 www.developer.ibm.com 0 Cached Datasource Non-cached Datasource
Connection Pooling Obtain and close the connection in the same method JNDI lookups are expensive Do not declare connections as static objects Do not close connections in the finalize method If you open a connection, close the connection Do not manage data access in Container Managed Persistence (CMP) beans
Connection Pooling HTTP Requests/Sec (Hundreds) www.developer.ibm.com 80 70 60 50 40 30 20 10 0 Conn. Pooling No Conn. Pooling
Prepared Statements Statement Compiled prior to execution Prepared Statement Separates the compilation process and adding substitution variables Use this one! Callable Statement Removes SQL entirely by making a Stored Procedure Call
How is SQL effecting CPU?
Session Persistence Enable session persistence Reduce session size Release HttpSession when finished Choose persistence options Avoid creating HttpSession by JSP default <%@page session= false %> Tune the cache size
Session Persistence Cache Exceeded (1000 Default) Yes Overflow Configured (Default) Yes Add Until Heap is Exceeded No Cache! No Persistence No Input Waits Until Sessions Expire In-memory cache overflow algorithm. Yes Persist!
Session Persistence (continued) Tune the cache size Add additional application server clones Tune the session timeout interval Tune multi-row persistence management Simplified multi-row session representation. Session Web Property Small Value Big Value ID Application AB12345 CandleDemo CandleDemo.First.Name Michael AB12345 CandleDemo CandleDemo.last.Name Pallos AB12345 CandleDemo CandleDemo.String A huge string
JDBC Choose your driver wisely JDBC API levels supported JDBC 1.0 is the default JDBC 2.0 is required by WAS Select a Type 2 JDBC driver When incorporating distributed transactions and cataloged databases, Type 2 JDBC driver offers the best performance.
DB Connections
Memory Usage
Memory Usage Allocation Avoid dynamic allocation Hidden object creation Streams Print writer Garbage collections Pool and reuse objects Servlet guideline < 50 lines of code Health checks Watch for memory usage growth Small page size Small message size
Enterprise Java Beans (EJB)
Enterprise Java Beans One size DOES NOT fit all Ensure you need distributed objects Alternative to Entity beans Tactical Non-Scalable Access Entity beans from Session beans Triple performance as opposed to accessing directly from client Reduces the number of remote method calls Single transaction context
Accessing Entity Beans 30 HTTP Requests/Sec (Hundreds) 25 20 15 10 www.developer.ibm.com 5 0 Access from Client Code Access via Session Bean
Enterprise Java Beans Read-Only getter methods / Access Intent Reduce Tx Level TRANSACTION_READ_UNCOMMITTED TRANSACTION_READ_COMMITTED TRANSACTION_REPEATABLE_READ TRANSACTION_SERIALIZABLE Cache EJB Home Reduce JNDI calls
EJB Caching HTTP Requests/Sec (Hundreds) www.developer.ibm.com 70 60 50 40 30 20 10 0 Client Code Home Caching Session Bean
Thread Pools Create thread pool funnel Minimize the size of thread pools Web Browser Client Request for Static Pages & Listener Threads Pool Servlet/JSP Thread Pool Database Connection Pool Dynamic Pages Queue Queue 100 20 10 Static Pages Dynamic Pages Servlet Threads Requesting DB Connections Application Database HTTP Server & plug-in Application Server Web Container EJB Container Data Source
Monitoring Tools What to look for. Analyst Recommendations.
Monitoring Tools Customer (End-User) Experience End-To-End (More than just J2EE environment) Latency Time WebSphere Application Server Instrumentation (class loader hooks) Workflow Analysis IBM API (PMI) Application Server Log SMF 120 Records (mainframe)
Monitoring Tools (continued) Legacy Systems CICS IMS Database Connection Pools DBMS
WebSphere Application Server Source:
Summary Distributed Applications are Complex A proven design pattern Servlets and Java Server Pages (JSP) Sessions Connecting to legacy systems and databases Connection Pooling Prepared Statements Session Database Java Database Connector
Summary Distributed Applications are Complex Memory usage Enterprise Java Beans Solid Monitoring Tool Hire Experience (consultants) Follow road to success Architecture Development Testing Deployment (Migration) Monitoring Best Practices
Questions E-mail: Michael_Pallos@candle.com
Thank you for your participation WebSphere Advisor, Nov/Dec 2002 Best Practices: Application Performance http://advisor.com/doc/10050 WebSphere Advisor, April 2003 5 Java Performance Best Practices http://advisor.com WebSphere Developers Journal, May/June 2003 Best Practices: WAS / DBMS Persistence http://www.candle.com (Link from home page, Aug 03) Webinar expoq Dailey Best Practices: J2EE http://www.ebizq.net/expoq/events/event22.html
A note from Legal This presentation represents customer's use of Candle's products. Because each customer's systems, uses and needs are different, Candle does not guarantee comparable results. Candle does not warrant the accuracy of information provided by customers or Candle's business partners. If a product mentioned herein is not yet generally available to Candle's commercial customers, Candle has no obligation to develop the product, to commercially release it, or to incorporate any features or functionalities of the product into any future commercially released product. Other product names and terms in this document may be trademarks of their respective companies. -- Candle Corporation Legal Department