Ben Shi

Blog Post

Building high performance Java application (Lessons Learnt)

Posted Under: notes
November 03, 2013

Whilst on training abroad last month, I had the opportunity of building an automated rates trading system in Java from scratch to compete with systems built by the other teams.

Surprisingly, this opportunity has turned out to be a great learning experience for me building a high performance application. My team did well in terms of performance and here are some of the lessons I learnt in the 2 weeks.

1. Monetary Calculations

  • Try to use long/double instead of BigDecimal for monetary calculation
  • Round result of multiplication/division calculation to the smallest currency unit
  • Don’t convert double to BigDecimal, convert using a string instead

2. Caching

3. Listeners

  • Keep listeners light
  • Hand events to another part of the system for processing

3. Multi-Threading

  • Thread creation is expensive
  • Use threadpools
  • Avoid synchronisation where possible
  • Have flow control to regulate the amount of threads
  • Fork-Join in 1.7 (or (link: text: jsr166) in 1.6)

4. Java Specifics

java -server
java -XX:+OptimizeStringConcat

5. Garbage Collection (GC)

6. Benchmark

  • Do it regularly (if possible, at the end of every sprint in agile)
  • Look for performance consistencies from historical benchmark
  • Use jvisualvm and visual GC plugin
  • Enable GC monitoring options
java -verbose:gc -Xloggc:<filename>

Ben Shi

I'm Ben Shi, a Sydney-based Full Stack Software Engineer specialising in micro-services and API development. This is a playground for me to document my learnings and experiences as a technologist.

You can find out a little bit more about me over here.

Contact: [email | twitter| linkedin]