on May 25th, 2012Scaling Grails – a checklist for tuning your grails application
I have been using Grails for almost a year now and have fallen in love with it. If you are looking for a good rapid application development framework, and are comfortable with Java, then Grails is definitely something you should check out. Given that it has the patronage of the Spring community (now VMWare) , I can surely bet on Grails to become even better and deliver greater value to developers. If you are familiar to any of the RAD frameworks, you know they are extremely good for prototyping and turning ideas into code. They are not however things that don’t scale well instantly, primarily because they are designed with convenience in mind. This is called the convenience – functionality trade off. This is imminent irrespective of the RAD framework you are using. But there are some simple ways in which a Grails application can be tuned to perform better irrespective of your set up. I will go one step further and call this a checklist that every Grails developer must go through prior to deploying a Grails application. I take most of my points from the Tuning a Grails Application Screencast by Peter Ledbrook, which I have linked to at the bottom of my post. If you are a Grails developer, this video is a must watch. So here is the checklist:
- GORM caching – Turn on hibernate’s second level cache which caches entity fetches. For static functions like permission/access checkers , Listing cities from the database etc this speeds up GORM operations significantly.
- One of the facets of GORM is to eagerly fetch related entities. Use Lazy fetches whenever possible by defining the fetch type for an association. Also, for large associations of type M:N, do not use the hasMany and belongsTo way of associating. Use a custom defined mapping instead to stop GORM from ensuring validity of the association for each DML operation. Burt Beckwith talks about it here in his Advanced GORM talk at SpringOne.
- Query Caching : You can cache all dynamic finders and criteria queries by adding the cache:true parameter to each function. There are multiple types of caches, like read-only and read-write etc, ensure you use the right kind of cache.
- If you need to profile your application to see what functions (user defined & Grails defined) are the ones hampering performance, use the Spring Insights module with your Tomcat or use the Insights module in STS. You can track how every request is serviced, see and debug problem areas, view everything from the call stack to the SQL query generated and profile your Grails application.
- Use the resources plugin (now bundled into Grails 2.x.x), this automatically web optimizes your javascript and CSS files. It also allows you to defines modules that can bunch up different js/css files together for optimizing delivery times of the page render. There is a video on how to get a perfect yslow score using the resources plugin, be sure to check that out.
- The good part about running on the JVM is that you can always fall back to Java. To increase the execution speed of a method, you can use the @Typed annotation and write static typed code to increase performance.
- Continuing my point about benefiting from the JVM, enable page compression in Tomcat for faster page responses. Other vendors have their own Page caching and compression mechanisms, be sure to enable them.
- Use Spring Caching (depends on ehcache) for controllers and services methods which get used very often.
- Tune your application server’s memory settings. A simple look up will bring you hundreds of pages on how to achieve this for the container of your choice. Adding a little more heap space and memory to an app server can do wonders.
When you find the time, do watch Peter Ledbrook’s video on tuning a grails application



