2. Everyone wants to be Facebook
• Few apps start with millions
of hits a day
• Scaling throws up many
more issues than just the
framework
Monday, 30 May 2011 2
8. Database performance
• Reduce the number of queries
– Use appropriate fetch mode
– Don’t fetch data you don’t need
• Tune your queries
– Add appropriate indexes
– Don’t be afraid to change the model
8
Monday, 30 May 2011 8
9. Fetch mode and indexing
class User {
String username
String passwordHash
Profile profile
static hasMany = [ roles: Role ]
static mapping = {
username index: 'username_idx'
profile fetch: 'join'
roles lazy: false
}
}
Monday, 30 May 2011 9
10. Database performance
• Caching
– Hibernate 2nd-level cache
– Distributed cache/data grid
• Terracotta
• GemFire
• Alternative data store!
10
Monday, 30 May 2011 10
11. Hibernate 2nd-level cache
•hibernate {
DomainClass.get()
– Works reliably and well
cache.use_second_level_cache = true
• Query cache
cache.use_query_cache = true
– Each query must be declared= cached
cache.provider_class as
– Changes to data clear the cache
'net.sf.ehcache.hibernate.EhCacheProvider'
} – Best for data that changes infrequently
Monday, 30 May 2011 11
12. Data grid
GemFire
GemFire Grails
DB GemFire
App
12
Monday, 30 May 2011 12
13. Business logic
• Caching
– Spring Cache Plugin for service methods
• Go asynchronous
– Ideal for long running tasks
– Spring Events, Executor
– Messaging: JMS, AMQP
13
Monday, 30 May 2011 13
14. Business logic
• Fall back to Java
– Limited applicability
– Numerically intensive tasks?
• Groovy++?
– One JAR
– @Typed on class or method
14
Monday, 30 May 2011 14
15. Business logic
class MyService {
@Typed(TypePolicy.MIXED)
void doSomething() {
expensiveMethod()
}
@Typed
void expensiveMethod() {
// Do something
}
}
15
Monday, 30 May 2011 15
16. Network and UI
• Reduce view processing
– Spring Cache plugin for views
• Reduce number of requests (latency)
– Expires HTTP header
– Bundle CSS & Javascript files
– Image spriting
• Reduce amount of data transferred to browser (bandwidth)
– Minify Javascript
– Compress data
• Plugins
– Performance UI, JAWR
– Resources et al.
Monday, 30 May 2011 16
17. Spring Cache with views
Sitemesh
Spring Cache
Render GSP Cached View
Monday, 30 May 2011 17
18. Spring Cache in code
import grails.plugin.springcache.annotations.*
class PostController {
@Cacheable("myCache")
def list = {
...
}
@CacheFlush("myCache")
def addPost = {
...
}
}
Monday, 30 May 2011 18
19. Compressing page content
• Container-dependent
– Tomcat has connector setting
– For Jetty, add GzipFilter to your web.xml
• Enable gzip compression in Tomcat plugin:
eventConfigureTomcat = { tomcat ->
tomcat.connector.setAttribute(
"compression", "on")
tomcat.connector.port = serverPort
}
Monday, 30 May 2011 19
20. Resources plugin
• Modularise static content
• Bundle files of the same type
• Caching Resources plugin
– Sets HTTP Expires header 1 year ahead
– Gives static files a unique name
• Zipped Resources plugin
– Gzips static content
– Can exclude files by extension
Monday, 30 May 2011 20
22. Rich UIs
• Browser does the work
• Minimal traffic
• GWT, Flex, etc.
• Data transfer
– JSON
– XML
– GWT-RPC
Monday, 30 May 2011 22
23. Summary
• Profile before you optimise!
• Several tools available for profiling
• Several plugins available for improving performance
• Many techniques apply to any web application
• Law of diminishing returns
– At some point, further performance improvements aren’t worth the
required effort
• Resources will be in Grails 1.4
Monday, 30 May 2011 23