Generally speaking I would leave database processing and the cache the database. I have done some measurements, and these show that processing records in a JVM is quite slow (filter, sort, distinct, group).
This is especially the case, if you are using higher level collections like ArrayList
in contrast to arrays (like Customer[]
).
I would not even worry with "remote" databases; in case of localhost databases just give the database more RAM for its cache or work memory, and it's hard to beat the database access planning/cache of a database with Java.
But you should follow the rule to query your database with a single SQL statement only. It doesn't really matter how complex that query is, but it's important that your request is done in a single query.
There is another adavantage: You have no cache to deal with explicitly. It's not hard to build caches in Java, but cache synchronization/invalidation can be difficult. So the codebase you have to maintain is much smaller.