J2EE Design Strategies That Boost Performance : Page 2
Your J2EE application is up and running, but you've got problems. The response time is slow, the batch-processing throughput is woeful, and the users are complaining. Learn how the application of a few key design strategies can alleviate your performance problems.
by Lara D'Abreo
Dec 15, 2003
Page 2 of 3
Performance Patterns and Techniques
You can scale your system to satisfy an increased load by using vertical and horizontal scaling techniques (software and hardware), but not always. Sometimes you can't use these techniques because either the budget is tight, time is limited, or your system has inherent architectural limitations. In lieu of adding more hardware, distributing your code, or adding more servers, you can use the following server-side strategies to improve the performance of a single J2EE/EJB application.
Database I/O Optimization
Poor DBMS performance can arise from a mismatch between the data-model, entity-bean design, and its usage. If your most frequent user request joins 10 different tables and returns five entities of which you use only one, then this is a design flaw. Align your DBMS access to support your usage patterns. Write a query to return only the one piece of data that you need.
Search queries that use finders can take a long time to run. Not only do they execute the finder query, but they also make subsequent calls to the DBMS to load entity beans. It's more efficient to call the DBMS with one bulk query than it is to issue a series of smaller requests.
Consolidate your search queries into a single JDBC call using a DataAccessObject (DAO) or FastLaneReader (Marinescu/J2EE Blueprints).
If your query results are large, however, you may encounter problems when you try loading it all into memory in one hit. If the client paginates the results, consider restricting the number of rows returned to a total closer to your page size. You can always fetch more rows on demand.
Use built-in database features that may help reduce query times, such as stored procedures, indexes, views, and table caches.
Within a given user transaction, the same data may be requested multiple times. Try to reduce the number of redundant reads. Either pass the information around as method parameters or consider caching it on the session or thread context. Data that changes infrequently, such as meta data or configuration data, is good for caching. You can load the cache once at startup or on demand.
Place your server caches strategically inside your facades to make them transparent to the caller.
Long-lived transactions can lead to connection timeouts, sharp memory increases, and DBMS lock contention. Your session beans control transactions. Break up extremely long-lived transactions into multiple shorter, more-reliable chunks.
Chaining together many short-lived transactions can also be slow. Widen your transaction boundaries. Increase throughput and efficiency by manipulating the length of your transaction so it performs many operations at once rather than one at a time.
Establish general-purpose session bean controllers to control your transactions. These can be independent of your business logic. Use your beans to adjust the number of records processed in one transaction so that you get optimum throughput for your request.
Batch processes written with EJBs can run very slowly because developers often build single-threaded batch jobs without considering transactions. A process that executes inside one very long-lived transaction (that could take hours or days) is fragile. If something goes wrong, the entire job gets rolled back and you have to start again from scratch. If your job executes as many small transactions chained together inside a loop, then your job will be more reliable. The throughput will be low, however.
The container controls server-side threads. Application code cannot create its own threads inside the EJB container, so it cannot take advantage of threading directly. Typically, a container-managed thread pool dispatches and services incoming server requests. This model works well for session-oriented usage. The container load balances across competing requests.
However, for batch processes, the goal is to achieve high throughput so the job finishes as quickly as possible. Parallel processing can aid this. Spawn threads outside the container and divvy up the load into small units of work that are capable of being executed concurrently. Use threads to spread the load. Don't spawn more threads than the server can handle. Keep the number of client threads to fewer than the containers thread pool limit.
Aggressive threading can reveal locking issues and non-thread-safe code inside the server. Structure your components so that they are thread-safe and can work concurrently, avoiding DBMS contention. Divide your units of work up so that they operate over different data sets.
How real-time are your requirements? Not every component has to respond in real-time. If your on-line response time is slow, reduce the amount of work performed during the request and defer expensive or non-essential functions to later. You can store work temporarily in holding tables or execute it asynchronously using JMS.
Components that don't have strict real-time requirements, such as data-extraction programs, collation and sorting routines, file import/export, etc., can be performed off-line as batch processes.
Every bean you write comes with built-in overhead. Many beans add up over the lifetime of a request. When a request is made to a session bean, the container has to locate and allocate resources (threads, objects, and connections), transfer context information, and serialize parameterswhich takes time.
Not every business component has to be a bean. Write your core components as plain old Java objects (POJOs). Design your core business logic and domain model independently of your session beans. That way, your core components are mobile, flexible, and reusable.
Think of your session beans in terms of deployment not development. Put your session beans in to manage your remote access points and control your transactions. Wrap them around your existing core business components.
Structure your session beans as general-purpose transaction controllers with extensible command-like remote interfaces. Delegate the business logic to your core components. Use session beans and remote interfaces sparingly to minimize request overheads and streamline your code.