July 28, 2010

Are we going back? (Client Server == AJAX )

One of the fundamental rule in designing software systems is always to divorce presentation logic, business logic and data access logic,
  • so that each tier can vary withstand modifications of other tiers
  • so that different teams can focus on separate layer and specialize in that
  • so that building complex systems can be easier etc etc

Many books have been written emphasizing the advantages of this pattern.
Coupling any of these layers would take us back to ancient Single tier or Client Server architectures.

Why am I telling all this?

One of the projects which I'm currently working on is a Web 2.0 application. This app helps users to do financial analysis by pulling large amounts of data  from the database and plotting charts. And there are no page refreshes and every request is a XHR request (nice :) ).
jQuery is the Ajax framework & jQPlot is the charting framework.

One of the interesting side effects that I'm observing building this Ajax Web 2.0 app is more and more "business logic" is getting into the front end javascript files(and btw the other side effect is - Ajax apps are not search engine friendly - not "crawl-able").

What could this mean:
  • sooner or later javascripts files grow larger and soon will end up in an un-manageable mess
  • anything running on the browser is kind of "open source" available to entire world - as these files get downloaded on the browser cache. Anybody with little effort can look at the business logic
  • bad user experience because of browser freeze - This is basically a counter effect. The primary reason why somebody would take ajax approach is to give a better user experience i.e. if the code runs in the browser then it it faster. But these days, an average user will have min 6 to 7 browser windows open (plus mail client (lotus or outlook), some office documents, pdf readers , IDEs etc) and if the browser starts taking your laptops cpu cycles looping some java-script code and rendering and re-drawing html --then a browser freeze is guaranteed (HTML5 promises to solve this)
Its kind of going backwards in time where all the presentation logic, business logic is getting mixed.
The browser for Ajax apps are kind of acting as a thick client, downloading data from the server (usually as JSON content) and manipulating most of it on the client side. The app server is just acting as a content provider.


Why this could be happening?

One word. Performance. Users are impatient and response > 3 secs is not acceptable.
The primary reason the business logic ends up on the client side is to limit the number of requests to the web server.
Network calls are very expensive (one to the web server and another from web server to the database server to get the actual content). So naturally developers would take easy way out, bypass these network calls, get chunks of data on the browser and manipulate it.

How to avoid this ?
  • Avoid putting business logic putting on the client side
  • Use java script wisely to manipulate the DOM
  • Use good server side caching solution
  • Tune your SQL queries
  • Design a good UI
  • Anticipate user clicks, fire the request at background and have the data cached on the server side
  • Keep clearing the server side cache
just my thoughts...

No comments: