This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Best Practices, web apps and database queries  RSS feed

 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been struggling with a concept, and part of the struggle is that I am having difficulty defining my problem! It is in relation to web applications where users log in and there is a database backend.

I've been googling "architecture web applications", "developing web applications", "designing web applications", "best practices [INSERT TERM HERE]" and variations on the above, and finding many interesting articles and resources, but am still just as hazy.

I am looking for "best practices" at a more granular level than MVC/MVC2 architecture.

So - for the sake of exploring my question and trying to give you something of an idea of what I am missing here:

We have a hypothetical application where users log in and do searches in a database. At the same time, there is quite a bit of information about the user in the database as well - preferences, saved searches, account information, maybe subscriptions, history, and if it is a social site, probably some sort of messages or something.

Various pages and parts of the application are going to display user-related information, most likely lots of user related information which isn't going to change throughout the visit, or some which will, but not with every new click.

Most of the code I have seen makes repeated calls to the database. Every time a page is refreshed, it makes a request to the database for information that probably didn't change.



What should one do about this semi-persistent data? Should one pull out the relevant information, plunk it into a session variable, and then only update the session and database if something changes? Caching of some sort?

What are best practices when dealing with querying a database?

What subject am I even inquiring about? Is it design, architecture, something completely different?

Katrina
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sound to me like you are mostly worrying about performance here? If so, we could move this thread to our Performance forum...

In general, hitting the database each time for information that doesn't change sounds wasteful. On the other hand, it might be that it is so fast that it's not a problem at all (which will also depend on the physical connection to the database, for example).

Your best bet is to start by designing your software as if it wasn't a problem - but care about a well decoupled design, because that will allow you to later optimize performance when it proves to be necessary.

Once you have proven that the naive approach leads to a performance problem, the first thing you need to do is to find the bottleneck - most probably by using some kind of profiling tool. When you've identified the bottleneck, we can start to discuss possible solutions - caching might actually be a good candidate, if your doubt proves to manifest in reality.

Does that help?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats true. We have exactly similar suggestion from Joshua Bloch in his Effective Java.

"Premature optimization is the root of all evil" - Donald E. Knuth

It wouldn't be a great idea to suggest anything at this stage. But for your database particular thing you can look for some cache stuff.
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, folks! I'll post in the optimization forum when I get far enough along that I see a bottleneck.
 
Chris Adkin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Katrina,

I think someone else has already mentioned this, but it depends on what sort of data is retrieved from the database, if this is standing / configuration data and the numbers of logical reads this results in is a significant total of the total number of logical reads that your app performs, then you most definitely need to look at caching this data. If the data is so large that it cannot be cached in the heap without causing serialization or garbage collection collection issues you might want to look at products specifically designed for this sort of job such as Coherence or Gigaspace application fabric etc. Although you should not design or tune for performance goals that do no exist, a general rule of thumb is that accessing a database from any language damages performance, therefore you should make your queries as efficient as possible in terms of execution plans, in stead of going out to the database ten times to get data, see if you can do this using one query, batch queries together using the JDBCs batching features and consider database stored procedures.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!