Saturday, December 10, 2011

Memory footprint of java primitives

Memory foot print of Java primitives

Java Primitive Data Types

Data Type Description Size Default Value
boolean true or false 1-bit false
char Unicode Character 16-bit \u0000
byte Signed Integer 8-bit (byte) 0
short Signed Integer 16-bit (short) 0
int Signed Integer 32-bit 0
long Signed Integer 64-bit 0L
longfloat Real number 32-bit 0.0f
double Real number 64-bit 0.0d

Next -- Garbage Collection explained
Prev -- Querying memory usage of a java object




ALSO READ
Java development 2.0: Ultra-lightweight Java web services with Gretty
An actor framework for Java concurrency
10 things you didn't know about java performance monitoring
Java collection performance - Chart view
The Clean Coder

Gretty - example - web-service



import org.mbte.gretty.httpserver.* 

@GrabResolver(name='gretty', 
  root='http://groovypp.artifactoryonline.com/groovypp/libs-releases-local')
@Grab('org.mbte.groovypp:gretty:0.4.279') 

GrettyServer server = [] 
server.groovy = [ 
    localAddress: new InetSocketAddress("localhost", 8080), 
    defaultHandler: { 
        response.redirect "/" 
    }, 
    "/:name": {
        get {
            response.text = "Hello ${request.parameters['name']}"
        } 
    } 
] 
server.start()
What this program does -

I created a server listening on port 8080, then set up a simple root endpoint containing the parameter name. Any request to some other endpoint will be routed back to / via the defaultHandler. The handler basically sends the requesting client an HTTP 301 "moved permanently" code, with the location of /. All requests will receive a response (with the content-type set to text/plain) containing the string "Hello" along with the value of any parameter passed; for instance, /Andy would yield "Hello Andy."

required librabies -

Groovy 1.8
gretty 0.4.279 and its dependent librabies downloaded by Grape


Execute -

groovy server.groovy

Making content type text/html -


"/:name": {
 get {
  response.html = "Hello ${request.parameters['name']}"
 } 
} 
Using a template -
<html> <head>
  <title>Hello!</title>
 </head>
 <body>
  <p>${message}</p>
 </body>
</html>
Using the template in response -

"/:name" {  get {
   response.html = template("index.gpptl", 
     [message: "Hello ${request.parameters['name']}"])
  }
}
Accessing Mongo DB- use Morphia



@GrabResolver(name='morphia', root='http://morphia.googlecode.com/svn/mavenrepo/')
@Grab(group='com.google.code.morphia', artifactId='morphia', module="morphia", 
  version='0.99')

Anyone's guess that this is a pure restful service - So this brings us to the question on when to use a restful service vs a soap based service. A quick read REST vs SOAP service




Also Read
An actor framework for Java concurrency
Instrumentation - Querying the memory usage of a Java object
10 things you didn't know about java performance monitoring
The Clean Coder

Sample code - Kilim

A simple Calculator example -pretty naive but then demonstrates using Actor based model in java

In Kilim a thread must extend Task object and implement the execute method which should throw Pausable exception. Kilim weaver (read it as byte code enhancer) interprets classes containing operations throwing Pausable exception and weaves(enhances) it.

import java.math.RoundingMode;

import kilim.Mailbox;
import kilim.Pausable;
import kilim.Task;

public class Calculator extends Task{

 private Mailbox mailbox;

 public Calculator(Mailbox mailbox) {
  super();
  this.mailbox = mailbox;
 }

 @Override
 public void execute() throws Pausable, Exception {
  while (true) {   
   Calculation calc = mailbox.get(); // blocks
   if (calc.getAnswer() == null) {
    calc.setAnswer(calc.getDividend().divide(calc.getDivisor(), 8, 
      RoundingMode.HALF_UP));    
    System.out.println("Calculator determined answer");
    mailbox.putnb(calc);
   }
   Task.sleep(1000);
  }
 }
}


Thread2 - demonstrates sharing of data between Thread1 using a Mailbox

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Date;
import java.util.Random;

import kilim.Mailbox;
import kilim.Pausable;
import kilim.Task;

public class DeferredDivision extends Task {

 private Mailbox mailbox;

 public DeferredDivision(Mailbox mailbox) {
  super();
  this.mailbox = mailbox;
 }

 @Override
 public void execute() throws Pausable, Exception {
  Random numberGenerator = new Random(new Date().getTime());
  MathContext context = new MathContext(8);
  while (true) {
   System.out.println("I need to know the answer of something");
   mailbox.putnb(new Calculation(
     new BigDecimal(numberGenerator.nextDouble(), context), 
     new BigDecimal(numberGenerator.nextDouble(), context)));
   Task.sleep(1000);
   Calculation answer = mailbox.getnb(); // no block
   if (answer != null && answer.getAnswer() != null) {
    System.out.println("Answer is: " + answer.printAnswer());
   }
  }
 }
}



Using Ant invoke Kilim's weaver(byte code enhancer)

 
  
  
  
  
 



A simple test runner
import kilim.Mailbox;
import kilim.Task;

public class CalculationCooperation {
 public static void main(String[] args) {
  Mailbox sharedMailbox = new Mailbox();

  Task deferred = new DeferredDivision(sharedMailbox);
  Task calculator = new Calculator(sharedMailbox);

  deffered.start();
  calculator.start();

 }
}

Notice how the same Mailbox is shared between threads without a lock or synchronization

Your output will vary — actors are nondeterministic
[java] I need to know the answer of something
[java] Calculator determined answer
[java] Answer is: The answer of 0.36477377 divided by 0.96829189 is 0.37671881
[java] I need to know the answer of something
[java] Calculator determined answer
[java] Answer is: The answer of 0.40326269 divided by 0.38055487 is 1.05967029
[java] I need to know the answer of something
[java] Calculator determined answer
[java] Answer is: The answer of 0.16258913 divided by 0.91854403 is 0.17700744
[java] I need to know the answer of something
[java] Calculator determined answer
[java] Answer is: The answer of 0.77380722 divided by 0.49075363 is 1.57677330

Sequence Diagram -


Conclusion - As in Scala or Erlang , Actor model can be applied to java.



Also Read
Java development 2.0: Ultra-lightweight Java web services with Gretty
Instrumentation - Querying the memory usage of a Java object
10 things you didn't know about java performance monitoring
5 things you didn't know about java.util.concurrent
The Clean Coder

Friday, December 09, 2011

An actor framework for Java concurrency

Recently my curiosity towards learning something new , bumped me into a framework called Kilim. Ya that's right , not sure how many of you have heard about it.

Goal of this framework - to introduce Actor based concurrency to java ala. making the model similar to Erlang and Scala

Why do we need this switch from thread based model to Actor based model when it comes to concurrency - thread based model depends on using shared memory when it comes to communicating between threads. Actor based model takes a slightly different approach of using Mailboxes to communicate.

So whats the advantage ? - well when coded against Actor based model we don't need to worry about thread locks or use any kind of synchronization blocks. Actors are guaranteed to run on multi-core processors. Since there is no limitation or a dependency on shared memory actors can be scheduled on any core or a processor. Improves the overall performance and the scalability of system.

How to use Kilim? sample code - here








Also Read
Java development 2.0: Ultra-lightweight Java web services with Gretty
Instrumentation - Querying the memory usage of a Java object
10 things you didn't know about java performance monitoring
5 things you didn't know about java.util.concurrent
The Clean Coder

Thursday, December 08, 2011

Querying the memory usage of a Java object

Creating the instrumentation agent class - would work with Jdk 5 and above



The JVM will pass to our method an implementation of the Instrumentation interface, defined in java.lang.instrument. In turn, this interface defines the method getObjectSize(). So for example, if we want to measure the memory usage of an instance of SomeClass, our agent code would look as follows:
import java.lang.instrument.*;
import com.somepackage.SomeClass;

public class MyAgent {
  public static void premain(String args, Instrumentation inst) {
    SomeClass obj = new SomeClass();
    long size = inst.getObjectSize(obj);
    System.out.println("Bytes used by object: " + size);
  }
}
Package the agent into a jar -

create manifest.txt with
Premain-Class: mypackage.MyAgent

execute this to create a jar -

jar -cmf manifest.txt agent.jar mypackage/MyAgent.class

Run the application with the agent -

java -javaagent:agent.jar -cp . com.mypackage.Main

Accessing the Instrumentation object from within our application -

public class MyAgent {
  private static volatile Instrumentation globalInstr;
  public static void premain(String args, Instrumentation inst) {
    globalInstr = inst;
  }
  public static long getObjectSize(Object obj) {
    if (globalInstr == null)
      throw new IllegalStateException("Agent not initted");
    return globalInstr.getObjectSize(obj);
  }
}

Now, provided the agent is included in the JVM command line parameters as above, then from anywhere in our application we can call MyAgent.getObjectSize() to query the memory size of an object created by our Java application

Note that the getObjectSize() method does not include the memory used by other objects referenced by the object passed in.


Next -- Memory footprint of java datatypes




Also Read
Java development 2.0: Ultra-lightweight Java web services with Gretty
An actor framework for Java concurrency
Java Garbage Collection explained
Java collection performance - Chart view
The Clean Coder

Hello world with gretty



Gretty is a simple web framework for both building web servers and clients. Built on top of netty, it supports NIO style http server, asynchronous http client. It also supports both websocket server and client.

It's designed to be light weight and run as a standalone embedded solution.

It's written in Groovy++. But you can use it with pure Groovy, Scala or even Java.

Do bear with me for not being a fancy writer -

Sample code demonstrating a quick web-service implementation



Also Read
An actor framework for Java concurrency
Instrumentation - Querying the memory usage of a Java object
10 things you didn't know about java performance monitoring
The Clean Coder

Thursday, November 24, 2011

Tortoise svn for linux

A very useful tortoise svn replacement for developers using Linux but just cant live without a graphical svn client

http://www.krishnashasankar.com/2008/11/tortoise-svn-in-linux-ubuntu-alternatives-here/




Wednesday, October 19, 2011

Spring MVC and Spring WS in the same app - how to's

Hosting Spring MVC controller and Spring-WS service in the same web application -

    This is fundamentally useful when developing an application which has its own view and additionally needs to host a bunch of services to its clients.  So how do we do this ?

1. web.xml of the web app needs to have 2 set of dispatcher servlets configured

    <servlet>
        <servlet-name>mvcControllerServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>soapServiceServlet</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>


Map these servlets to different URL's

   <servlet-mapping>
        <servlet-name>soapServiceServlet</servlet-name>
        <url-pattern>/appname/service/update</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>soapServiceServlet</servlet-name>
        <url-pattern>*.wsdl</url-pattern>
    </servlet-mapping>   
    <servlet-mapping>
        <servlet-name>mvcControllerServlet</servlet-name>
        <url-pattern>/app-name/mvc/*</url-pattern>
    </servlet-mapping>


2. Define 2 spring context XML's mvcControllerServlet-servlet.xml and soapServiceServlet-servlet.xml
   
     mvcControllerServlet-servlet.xml - Define all the mvc controllers in this one

    <!-- URL mapping definitions -->
    <bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
         <property name="mappings">
             <props>
                 <prop key="**/info">InfoController</prop>
              
            </props>
        </property>
    </bean>

   
    <bean id="InfoController" class="com.test.web.InfoController" >
        <property name="successView">
            <value>mvc/success</value>
        </property>
       </bean>




    soapServiceServlet-servlet.xml - Define messageReceiver etc in this one to facilitate spring SOAP message handling

    <bean id="messageReceiver" class="org.springframework.ws.soap.server.SoapMessageDispatcher"/>  

    <bean id="payloadMapping" class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
        <property name="endpointMap">
            <map>
            <entry key="{http://www.testapp.com/service}UpdateRequest" value-ref="endpoint" />
            </map>
        </property>
    </bean>


Service implementation goes into the endpoint class and mvc implementation can be written in InfoController.

Simple and neat.



 

Monday, October 17, 2011

CHART - Java collection framework perfromance





Also Read
The Clean Coder

Wednesday, October 05, 2011

How to get C like performance in Java


  • The JVM does implicit bounds checking on array access and updates. This has a small overhead - you can unsafely eliminate this (and open yourself to buffer overflows and other problems) using the the Unsafe class or direct buffers.
  • Use memory-minimized collections to reduce memory usage.
  • You can use Direct memory to store data how you wish (this is what BigMemory uses).
  • Use blocking IO in NIO (which is the default for a Channel) - don't use Selectors unless you need them.
  • Most systems can handle 1K-10K threads efficiently. Scalability beyond 10K users/server doesn't buy you anything in the real world since the server resources will be consumed servicing 10k concurrent users.
  • -XX:+UseCompressedStrings use byte[] instead of char[] for strings which don't need 16-bit characters - this saves memory but is 5%-10% slower.
  • To reduce string space usage, you can use your own Text type which wraps a byte[], or get your text data from ByteBuffer, CharBuffer or use Unsafe or -XX:+UseCompressedStrings.
  • To start the JVM faster, load fewer libraries.
  • Use primitives instead of primitive wrapper objects.

Thursday, September 29, 2011

6 Ways Not to Scale that Will Make You Hip, Popular and Loved By VCs

Tips from - High Scalability

  • Recently "hot" and newer tools typically have not been tested for scalability, and are risky to use in where you need scalability.
  • You should be resource monitoring, performance testing, monitoring traffic, load testing, and doing tuning analysis using statistics and mathematical modelling.
  • Design and implement your applications to use parallel programming from the ground up.
  • Locks, bottlenecks forcing single-thread execution, wider than needed scope of variables and memory are all recipies for reduced scalability.
  • Avoid: frequently-updated single-row tables; single master queues that controls everything; and blocking threads.
  • Cache the majority of remote calls; avoid remote comms wherever possible.
  • Analyse again and again for single points of failure, and endeavour to eliminate them.



Tuesday, September 27, 2011

50 Strategies For Creating A Successful Web 2.0 Product - 41/50

41. Understand your business model and use it to drive your product design. Too many Web 2.0 applications hope
that they will create large amounts of traffic and will then find someone interested in acquiring them. Alternatively, some
products charge too much up front and prevent themselves from reaching critical mass. While over-thinking your exit strategy
or trying to determine your ultimate business model before you do anything isn't good either, too many startups don't sit down
and do the rigorous thinking around how to make their business a successful one in the nearer term. Take a look at Andrew
Chen's How To Create a Profitable Freemium Startup for a good example of the framework on how to do some of the business
model planning. Taking into account the current economic downturn and making sure you're addressing how you offering can
help people and businesses in the current business climate will also help right now.

42. Embrace emergent development methods. While a great many of the Web's best products had a strong product
designer with a clear vision that truly understood his or her industry, the other half of the equation that often gets short shrift
is the quality of emergent design through open development. This captures the innatec r owds o urc ing aspects of ecosystembased
products, specifically those that have well-defined points of connectedness with external development inputs and 3rd
party additions. Any Web application has some emergent development if it takes development inputs or extensibility with via
3rd party plug-ins, widgets, open APIs, open source contributions, and so on. The development (and a good bit of the design)
of the product then "emerges" as a function of multiple inputs. Though there is still some top-down control, in essence, the
product becomes more than the sum total of its raw inputs. Products like Drupal and Facebook are good examples of this,
with thousands of plug-ins or 3rd party apps that have been added to them by other developers.

43. It's all about usability, usability, and usability. I've mentioned usability before in this list but I want to make it a
first class citizen. Nothing will be a more imposing barrier to adoption that people not understanding how your product
works. Almost nothing on this list will work until the usability of your application is a given. And hands down the most
common mistake I see are Web developers creating user experiences in isolation. If you're not funded to have a usability lab
(and you probably should be, at some level), then you need to grab every friend and family member you have to watch how
they use your application for the first time. Do this again for every release that makes user experience changes. You will
change a surprising number of assumptions and hear feedback that you desperately need to hear before you invest any more in
a new user experience approach. This now true even if you're developing enterprise applications for the Web.

44. Security isn't an afterthought It's a sad fact that far too much of a successful startup's time will be spent on security
issues. Once you are popular, you will be the target of every so-called script kiddie with a grudge or with the desire to get at
your customer data, etc. Software vulnerability are numerous and the surface area of modern Web apps large. You not only
have your own user experience but also your API, widgets, semantic Web connections, social networking applications, and
other points of attack. Put aside time and budget for regular vulnerability assessments. You can't afford a public data spill or
exploit due to a security hole that will compromise your user's data, or you may well find yourself with a lot of departing
customers.Web 2.0 applications also need unique types of security systems, from rate limiters to prevent valuable usergenerated
data from being systematically scraped from the site (this is vital to "maintaining control of unique and hard-to-recreate
datasets") to monitoring software that will screen for objectionable or copyrighted contributions.

45. Stress test regularly and before releases. It's a well known saying in the scalability business that your next
bottleneck is hiding just behind your last high water mark. Before your launch, data volumes and loads that work fine in the
lab should be tested to expected production volumes before launch. The Web 2.0 industry is rife with examples of companies
that went down the first time they got a good traffic spike. That's the very worst time to fail, since it's your best chance of
getting a strong initial network effect and may forever reduce your ultimate success. Know your volume limits and ceilings
with each release and prepare for the worst.

46. Backup and disaster recovery, know your plan. This is another unglamorous but essential aspect for any online
product. How often are backups being made of all your data? Are the backups tested? Are they kept offsite? If you don't know
the answers, the chances that you'll survive a major event is not high.

47. Good Web products understand that there is more than the Web. Do you have a desktop widget for Vista or the
Mac? Might you benefit from offering an Adobe AIR client version of your application? How about integration and
representation in vitual worlds and games? How about linkages to RFID or GPS sensors? Startups thinking outside the box
might even create their own hardware device if it makes sense (see Chumby and the iPhone/iPod for examples). If one thing
that is certain is that the next generation of successful Web startups will only partially resemble what we see today. Successful
new online products will take advantage of "software above the level of a single device" and deliver compelling combinations of
elements into entirely new products that are as useful and innovative as they are unexpected. A great Web 2.0 product often
has a traditional Web application as only part of its overall design, see the Doritos Crash the Superbowl campaign for just one
small example of this.

48. Look for emerging areas on the edge of the Web. These are the spaces that have plenty of room for new players
and new ideas, where network effects aren't overly established and marketshare is for the taking. What spaces are these? The
Semantic Web seems to be coming back with all new approaches (I continue to be amazed at how much appears about this
topic onhtt p: // d e l ic io us . c om / popu la r/ web3 . 0 these days.) Open platform virtual worlds such as Second Life were hot a few
years ago and may be again. Mobile Web applications are extremely hot today but slated to get over crowded this year as
everyone plans a mobile application for phone platforms. What is coming after this? That is less clear but those that are
watching closely will benefit the most.

49. Plan to evolve over time, for a long time. The Web never sits still. Users change, competitors improve, what's
possible continues to expand as new capabilities emerge in the software and hardware landscape. In the Perpetual Beta era,
products are never really done. Never forget that, continue to push yourself, or be relegated to a niche in history.

50. Continually improve yourself and your Web 2.0 strategies. While process improvement is one of those lip-service
topics that most people will at least admit to aspire to, few have the time and resources to carry it out on a regular basis. But
without that introspection on our previous experience we wouldn't have many of the "aha" moments that drove forward our
industry at various points in term. Without explicit attempts at improvement, we might not have developed the ideas that
became object-oriented languages, search engine marketing, Web 2.0, or even the Internet itself. This list itself is about that
very process and encapsulates a lot of what we've learned in the last 4 years. Consequently, if you're not sitting down and
making your own list from your own experiences, you're much more likely to repeat past history, never mind raising the bar.
Like I'm often fond of saving; civilization progresses when we make something that was formerly hard to do and make it easy
to do. Take the time, capture your lessons learned, and improve your strategies.
What else is missing here? Please contribute your own 2.0 strategies in comments below:
Prev

50 Strategies For Creating A Successful Web 2.0 Product - 31/40

31. Understand and apply Web-Oriented Architecture (WOA). The Web has a certain way that it works best and
understanding how HTTP works at a deep level is vital for getting the most out of the unique power that the Internet has to
offer. But HTTP is just the beginning of this way of thinking about the Web and how to use its intrinsic power to be successful
with with it. This includes knowing why and how link structure, network effects, SEO, API ecosystems, mashups, and other
aspects of the Web are key to making your application flourish. It's important to note that your internal application
architecture is likely not fundamentally Web-oriented itself (because most software development platforms are not Weboriented)
and you'll have to be diligent in enabling a WOA model in your Web-facing product design. The bottom line: Non-
Web-oriented products tend not to fare very well by failing to take advantage of the very things that have made the Web itself
so successful.

32. Online products that build upon enterprise systems should use open SOA principles. Large companies
building their first 2.0 products will often use existing IT systems and infrastructure that already have the data and
functionality they need. Although they will often decouple and cache them for scalability and performance, the connectedness
itself is best done using the principles of SOA. That doesn't necessarily mean traditional SOA products and standards,
although it could, often using more Web-oriented methods works better. What does this really mean? Stay away from
proprietary integration methods and use the most open models you can find, understanding that the back-end of most online
products will be consumed by more than just your front-end (see API discussion above for a fuller exploration).
33. Strategically use feeds and syndication to enable deep content distribution. This is another way to use Jakob's
Law to increase unintended uses and consumption of an application from other sites and ecosystems. Feeds enable many
beneficial use cases such as near real-time perception of fresh data in your application from across the Web in feed readers,

syndication sites, aggregators, and elsewhere. Like many other techniques here, knee-jerk use of feeds won't drive much
additional usage and adoption, but carefully designing feeds to achieve objectives like driving new customers back to the
application directly from the feed can make a big difference. Failing to offer useful feeds is one of the easiest ways to miss out
on business opportunities while giving your competitors an edge.
34. Build on the shoulders of giants; don't recreate what you can source from elsewhere. Today's Internet

application usually require too much functionality to be cost-effectively built by a single effort. Typically, an application will
actually source dozens of components and external functionality from 3rd parties. This could be off-the-shelf libraries or it
could be the live use of another site's API, the latter which has become one of the most interesting new business models in the
Web 2.0 era. The general rule of thumb: Unless it's a strategic capability of your application, try hard to source it from
elsewhere before you build it; 3rd parties sources are already more hardened, robust, less expensive, and lower defect than any
initial code could that you could produce. Get used to doing a rapid build vs. buy evaluation for each major component of your
application.

35. Register the user as soon as possible. One of the most valuable aspects of your onine product will be the registered
user base. Make sure you application gives them a good reason to register and that the process is as painless as possible. Each
additional step or input field will increase abandonment of the process and you can always ask for more information later.
Consider makingO pe nI D the default login, with your local user database a 2nd tier, to make the process even easier and more
comfortable for the user.

36. Explicitly enable your users to co-develop the product. I call this concept Product Development 2.0 and it's one of
the most potent ways to create a market-leading product by engaging the full capabilities of the network. The richest source of
creative input you will have is your audience of passionate, engaged users. This can be enabled via simple feedback forms,
harvested from surveys and online community forums, via services such asGe tSa tis f ac t io n, or as the ingredients to mashups
and user generated software. As you'll see below, you can even open the code base or provide a plug-in approach/open APIs to
allow motivated users and 3rd parties to contribute working functionality. Whichever of these you do, you'll find that the
innovation and direction to be key to making your product the richest and most robust it can be. A significant percentage of
the top online products in the world take advantage of this key 2.0 technique.

37. Provide the legal and collaborative foundations for others to build on your data and platform. A good place
to start is to license as much of your product as you can via Creative Commons or another licensing model that is less
restrictive and more open than copyright or patents. Unfortunately, this is something for which 20th century business models
around law, legal precedent, and traditional product design are ill-equipped to support and you'll have to look at what other
market leaders are doing with IP licensing that is working. Giving others explicit permission up-front to repurpose and reuse
your data and functionality in theirs can be essential to drive market share and success. Another good method is to let your
users license their data as well and Flickr is famous for doing this. It's important to understand that this is now the Some
Right Reserved era, not the All Rights Reserved era. So openly license what your have for others to use; the general rule of
thumb is that the more you give away, the more you'll get back, as long as you have a means of exercising control. This is why
open APIs have become as popular as they have, since they are essentially "IP-as-a-service" and poorly behaving
partner/licensees can be dealt with quickly and easily.

38. Design your product to build a strong network effect. The concept of the network effect is something I've covered
here extensively before and it's one of the most important items in this list. At their most basic, Web 2.0 applications are
successful because they explicitly leverage network effects successfully. This is the underlying reason why most of the leading
Internet companies got so big, so fast. Measuring network effects and driving them remains one of the most poorly
understood yet critical aspects of competing successfully online. The short version: It's extremely hard to fight an established
network effect (particularly because research has shown them to be highly exponential). Instead, find a class of data or a blue
ocean market segment for your product and its data to serve.

39. Know your Web 2.0 design patterns and business models. The fundamental principles of Web 2.0 were all
identifid and collected together for a good reason. Each principle is something that must be considered carefully in the design
of your product given how they can magnify your network effect. Your development team must understand them and know
why they're important, especially what outcomes they will drive in your product and business. It's the same withE nt erp r is e
2.0 products: There is another, related set of design principles (which I've summarized as FLATNESSES) that makes them
successful as well. And as with everything on this list, you don't apply 2.0 principles reflexively; they need to be intelligently
used for good reason.

40. Integrate a coherent social experience into your product. Social systems tend to have a much more pronounced
network effect (Reed's Law) than non-social systems. Though no site should be social without a good reason, it turns out that
most applications will benefit from having a social experience. What does this mean in practice? In general, social
applications let users perceive what other users are doing and actively encourage them to interact, work together, and drive
participation through social encouragement and competition. There is a lot of art to the design of the social architecture of an
online product, but there is also an increasing amount of science. Again, you can look at what successful sites are doing with
their social interaction but good places to start are with user profiles, friends lists, activity streams, status messages,s oc ial
media such as blogs and microsharing, and it goes up from there. Understand how Facebook Connect and other open social
network efforts such as OpenSocial can help you expand your social experience.
Next

50 Strategies For Creating A Successful Web 2.0 Product - 21/30

21. The link is the fundamental unit of thought on the Web, therefore richly link-enable your applications.

Links are what make the Web so special and fundamentally makes it work. Ensuring your application is URL addressable in a
granular way, especially if you have a rich user experience, is vital to participate successfully on the Web. The Web's link
ecosystem is enormously powerful and is needed for bookmarking, link sharing/propagation, advertising, makes SEO work,
drives your page rank, and much more. Your overall URL structure should be thought out and clean, look toFl ic k r and
del.cio.us for good examples.

22. Create an online user community for your product and nurture it. Online communities are ways to engage
passionate users to provide feedback, support, promotion, evangelism and countless other useful outcomes. While this is
usually standard fare now with online products, too many companies don't start this early enough or give it enough resources
despite the benefits it confers in terms of customer support, user feedback, and free marketing, to name just three benefits.
Investing in online community approaches is ultimately one of the least expensive aspects of your product, no matter the
upfront cost. Hire a good community manager and set them to work.

23. Offer a up-to-date, clean, compelling application design. Attractive applications inherently attract new customers
to try them and is a pre-requisite to good usability and user experience. Visual and navigational unattractiveness and
complexity is also the enemy of product adoption. Finally, using the latest designs and modes provides visual cues that
conveys that the product is timely and informed. A good place to start to make sure you're using the latest user experience
ideas and trends is Smashing Magazine's 2009 Web Design survey.
24. Load-time and responsiveness matter, measure and optimize for them on a regular basis. This is not a

glamorous aspect of Web applications but it's a fundamental that is impossible to ignore. Every second longer a key operation
like main page load or a major feature interaction takes, the more likely a customer is to consider finding a faster product. On
the Web, time is literally money and building high speed user experiences is essential. Rich Internet Application technologies
such as Ajax and Flash, albeit used wisely, can help make an application seem as fast as the most responsive desktop
application. Using content distribution networks and regional hosting centers.

25. User experience should follow a "complexity gradient." Novice users will require a simple interface but will want
an application's capabilities to become more sophisticated over time as they become more skilled in using it. Offering more
advanced features that are available when a user is ready but are hidden until they are allows a product to grow with the user
and keeps them engaged instead of looking for a more advanced alternative.

26. Monetize every page view. There is no excuse for not making sure every page is driving bottom-line results for your
online business. Some people will disagree with this recommendation and advertising can often seem overly commercial early
in a product's life. However, though a Web application should never look like a billboard, simple approaches like one line
sponsorships or even public service messages are good ideas to maximize the business value of the product and there are other
innovation approaches as well.

27. Users' data belongs to them, not you. This is a very hard strategy for some to accept and you might be able to get
away with bending this rule for a while, that is, until some of your users want to move their data elsewhere. Data can be a
short-term lock-in strategy, but long-term user loyalty comes from treating them fairly and avoiding a 'Roach Motel' approach
to user data ("they can check-in their data, but they can't check out.") Using your application should be a reversible process
and users should have control of their data. SeeD at aPor t ab il it y.or g for examples of how to get started with this.
28. Go to the user, don't only make them come to you. The aforementioned APIs and widgets help with this but are
not sufficient. The drive strong user adoption, you have to be everywhere else on the Web that you can be. This can mean
everything from the usual advertising, PR, and press outreach but it also means creating Facebook applications,Ope nSoc i a l
gadgets, and enabling use fromm as hups. These methods can often be more powerful than all the traditional ways combined.

29.SEO is as important as ever, so design for it. One of the most important stream of new users will be people coming
in from search engines looking for exactly what you have. This stream is free and quite large if you are ensuring your data is
URL addressable and can be found via search engine Web crawlers. Your information architecture should be deeply SEOfriendly
and highly granular.

30. Know thy popular Web standards and use them. From a consumer or creator standpoint, the data you will
exchange with everyone else will be in some format or another. And the usefulness of that data or protocol will be in inverse
proportion to how well-known and accepted the standard is. This generally means using CSS, Javascript, XHTML, HTTP,
ATOM, RSS, XML, JSON, and so on. Following open standards enables the maximum amount of choice, flexibility, time-tomarket,
access to talent pools, and many other benefits over time to both you and your customers.
Next

50 Strategies For Creating A Successful Web 2.0 Product -11/20

11. Variability in the productivity amongst programmers and development platforms each varies by an order
of magnitude. Combined together and your choice of programming talent and software development platforms can result in

a 100x overall effect on product development productivity. This means that some teams can ship product in as little as 3
months and some projects won't ship ever, at least not without truly prohibitive time and resource requirements. While there
are a great many inputs to an Internet startup that will help or hinder it (take a look at Paul Graham's great 18 Mistakes That
Kill Startups for a good list), these are two of the most central and variable: Who is developing the product and what

development platform they are using. Joel Spolsky's write-up on programmer productivity remains one of the best at understanding this issue. It usually turns out that paying a bit more for the right developer can often mean tremendous output gains. One the other side of the coin, choosing a development platform not designed for creating modern Web
applications is another decision that can sap your team of productivity while they spend months retrofitting it for the features
they'll need to make it work properly in today's Internet world.

12. Plan for testing to be a larger part of software development process than non-Web applications. Cross
browser testing, usability, and performance/load testing are much bigger issues with Web applications than many non-Web
applications. Having to do thorough testing in a half-dozen to a dozen browser types can be an unexpected tax on the time
and cost of creating a Web product. Doing adequate load testing is another item that often waits until the end, the very worst
time to find where the bottlenecks in your architecture are. Plan to test more than usual. Insist on automated unit and
integration tests that build up over time and run without having to pay developers or testers to do it manually.

13. Move beyond traditional application hosting. Single Web-server hosting models are not going to suffice for your 2.0
applications. Reliability, availability, and scalability are essential and must be designed into your run-time architecture and
supported by your hosting environment. Solutions like3 T era, Amazon's Elastic Compute Cloud, and Google's App Engine are
three compelling, yet very different solutions to the hosting problem. Either way, grid and cloud approaches to hosting will
help you meet your growth and scalability requirements while managing your costs.

14. Have an open source strategy. This has two important aspects. One, developing and hosting a product built with
open source software (the ubiquitiousLA MP stack) is almost always much less expensive than using commercial software and
is what most online products use. There are certainly commercial licenses that have fair terms for online services, but almost
none of them will match the cost of free. This is one reason why you won't find Windows or Oracle embedded in very many
Web 2.0 services. Two, you'll have to decide whether to open source or commercial open source your product. This has
entirely to do with what your product does and how it does it, but an increasing number of Web 2.0 hosted products are
releasing their offerings as open source to appeal to customers, particularly if they are business customers. Done right, open
sourcing can negate arguments about the size of your company while enlisting many 3rd party developers to help enrich and
make your product better.

15. Consider mobile users as important as your regular browser customers. Mobile devices will ultimately form
the majority of your user base as the capability and adoption of smartphones, Internet tablets, laptops, and netbooks ushers in
mobile Web use as the dominant model. Having an application strategy as well as well-supported applications for the iPhone,
Android, and RIM platforms is essential for most Web products these days. By the time you get to market, mobile will be even
more important than it is now. Infoworld confirmed today, in fact, that wireless enterprise development will be one of 2009's
bright spots.

16. Search is the new navigation, make it easy to use in your application. You have 5-10 seconds for a new user to
find what they want from your site or application. Existing users want to directly access what they need without going through
layers of menu items and links. Search is the fastest way to provide random access navigation. Therefore, offer search across
data, community, and help at a minimum. A search box must be on the main page and indeed, every page of the modern Web
application.

17. Whenever users can provide data to your product, enable them. Harnessing collective intelligence is the most
central high-level principle of Web 2.0 applications. To be a major online competitor, getting your millions of users to build a
valuable data set around the clock is the key to success. Many product designers look at this too narrowly and usually at a
small set of data. Keep a broad view of this and look for innovative ways to get information from explicit contributions to the
database of intentions can form your architecture of participation.
18. Offer an open API so that your Web application can be extended by partners around the world. I'vec ov e red
this topic many times in the past and if you do it right, your biggest customers will soon become 3rd party Web applications
building upon your data and functionality. Critically, offering an API converts your online product into an open platform with
an ecosystem of 3rd party partners. This is just one of many ways to realize Jakob's law, as is the next item.
19. Make sure your product can be spread around the Web by users, provide widgets, badges, and gadgets. If
your application has any success at all, your users will want to take it with them and use your features elsewhere. This is often
low-effort but can drive enormous growth and adoption; think about YouTube's badge.

20. Create features to make the product distribute virally. The potency of this is similar to widgets above and
everything from simple e-mail friend invites to importing contact lists and social graphs from other Web apps are critical ways
to ensure that a user can bring the people they want into the application to drive more value for them and you
Next


50 Strategies For Creating A Successful Web 2.0 Product - 1/10

1. Start with a simple problem. All of the most successful online services start with a simple premise and execute on it
well with great focus. This could be Google with it's command-line search engine, Flickr with photo sharing, Digg with user
generated news. State your problem simply: "I make it easier to do X". Focus on solving it elegantly and simply, only add
features carefully. Over time, complexity will become the enemy of both your product design and your software architecture,
so start with as much focus as you can muster.

2. Create prototypes as early as possible. Get your idea into a working piece of software as quickly as possible. The
longer you take to go through one entire cycle, the more unknown work you have ahead of you. Not producing software also
means that you are not getting better and better at turning the work of your team into the most important measurable output:
Functioning software. Throughout the life of your product, turning your ideas into software as quickly and inexpensively as
possible will be one of the most important activities to get right.

3. Get people on the network to work with the product prototype rapidly and often. The online world today is
fundamentally people-centric. If your product isn't about them and how it makes their lives better, your product really doesn't
matter. And if they're not using your Web application as soon as possible, you just don't know if you are building the right
product. Constant, direct feedback from real people is the most important input to our product design after your idea. Don't
wait months for this to happen; get a beta out to the world, achieve marketplace contact in weeks, or at most a few months,
and watch carefully what happens. This approach is sometimes called Web 2.0 Development .

4. Release early and release often. Don't get caught up in the massive release cycle approach, no matter how appealing it
may be. Large releases let you push off work tomorrow that should be done today. It also creates too much change at once
and often has too many dependencies, further driving an increase in the size of the release. Small releases almost always work
better, are easier to manage, but can require a bit more operations overhead. Done right, your online product will iterate
smoothly as well as improve faster and more regularly than your competitors. Some online products, notably Flickr, have been
on record as saying they make new releases to production up to several times a day. This is a development velocity that many
new startups have trouble appreciating or don't know how to enable. Agile software development processes are a good model
to start with and and these and even more extreme methods have worked well in the Web 2.0 community for years.

5. Manage your software development and operations to real numbers that matter. One often unappreciated
issue with software is its fundamentally intangible nature. Combine that with human nature, which is to manage to what you
can see, and you can have a real problem. There is a reason why software development has such a variable nature in terms of
time, budget, and resources. Make sure you have as many real numbers as possible to manage to: Who is making how many
commits a week to the source repository, how many registered users are there on a daily basis, what does the user analytics
look like, which product features are being used most/least this month, what are the top 5 complaints of customers, and so
on. All of these are important key performance indicators that far too many startups don't manage and respond to as closely
as they should.
6. Gather usage data from your users and input it back into product design as often as possible. Watch what

your users do live with your product, what they click on, what do they try to do with it, what they don't use, and so on. You will
be surprised; they will do things you never expected, have trouble with features that seem easy to you, and not understand
parts of your product that seemed obvious. Gather this data often and feed it back into your usability and information
architecture processes. Some Web applications teams do this almost daily, others look at click stream analytics once a quarter,
and some don't it at all. Guess who is shaping their product faster and in the right direction?

7. Put off irreversible architecture and product design decisions as long as possible. Get in the habit of asking
"How difficult will it be to change our mind about this later?" Choosing a programming language, Web framework, relational
database design, or a software interface tend to be one-way decisions that are hard to undo. Picking a visual design, logo,
layout, or analytics tool generally is not. Consequently, while certain major decisions must be made up front, be vigilant for
seemingly innocuous decisions that will be difficult to reverse. Not all of these will be a big deal, but it's all too often a surprise
to many people when they discover their architecture isn't malleable in the places that they want it to be. Reduce unpleasant
surprises by always asking this question.

8. Choose the technologies later and think carefully about what your product will do first. First, make sure your
ideas will work on the Web. I've seen too many startups with ideas that will work in software but not on the Web. Second,
Web technologies often have surprising limits, Ajax can't do video or audio, Flash is hard to get to work with SEO for example.
Choosing a technology too early will constrain what is possible later on. That being said, you have to choose as rapidly as you
can within this constraint since you need to build prototypes and the initial product as soon as you are able.

9. When you do select technologies, consider current skill sets and staff availability. New, trendy technologies
can have major benefits including higher levels of productivity and compelling new capabilities, but it also means it'll be
harder to find people who are competent with them. Having staff learn new technology on the job can be painful, expensive,
and risky. Older technologies are in a similar boat; you can find people that know them but they'll most likely not want to
work with them. This means the middle of the road is often the best place to be when it comes to selecting technology, though
you all-too-often won't have a choice depending on what your staff already knows or because of the pre-requisites of specific
technologies that you have to use.

10. Balance programmer productivity with operational costs. Programming time is the most expensive part of
product creation up front while operations is after you launch. Productivity-oriented platforms such as Ruby on Rails are very
popular in the Web community to drive down the cost of product development but can have significant run-time penalties

later when you are supporting millions of users. I've previously discussed the issues and motivations around moving to newer
programming languages and platforms designed for the modern Web, and I encourage you to read it. Productivity-oriented
platforms tend to require more operational resources during run-time, and unlike traditional software products, the majority
of the cost of operations falls upon the startup. Be aware of the cost and scale of the trade-offs since every dollar you save on
the development productivity side translates into a run-time cost forever after on the operations side.Next

Saturday, September 24, 2011

The Clean Coder

Before even getting into the book, it is good to know the style of Robert Martin, affectionately known as "Uncle Bob" to many people. Bob is a former preacher who comes at life — and topics he teaches — with a no-holds-bar approach. So when he approaches topics such as "Professionalism" and the software industry, I come expecting passionate discussion and serious assertions. The Clean Coder is no exception.

The book starts off with an overview of the Challenger space shuttle disaster. As a native Floridian who could see shuttle launches from my house (and, in fact, saw the Challenger explode just as it crested the trees from where we lived) this really resonated with me. The accident was a result of engineers saying no, but management overriding the decision. With this introduction, Bob makes it quite clear that when we choose not to stand up for that which we believe, it can have dire consequences.

We then dive right in, starting with the topic of Professionalism. The assertion is made that the key to professionalism is responsibility — "You can't take pride and honor is something you can't be held accountable for". But how do we take and achieve responsibility? Chapter one lays out two ways. To start, it looks at the Hippocratic Oath, specifically the rule of "First, Do No Harm". The book maps this to software by saying to do no harm to function or structure, ensure that QA doesn't find anything, know that your software works, and have automated QA. In fact, when I work with teams, I teach them that if your testing "phase" finds bugs, it's a problem with your process that needs to be addressed immediately, so the concept of ensuing that QA doesn't find anything is a great concept to bring out.

Then we move on to Work Ethic — specifically around knowing your field. This means continuous learning, practice (through things like Katas and Dojos), collaboration, mentoring, identifying with your employer/customer, and practicing humility. To help with that, Chapters 2 and 3 talk specifically about saying "No" and "Yes". When we say no, and when we want to say no, we should mean it. Saying, "We'll try" means that you, or your team, isn't already giving it their best, and that through some extraordinary effort you'll pull it off. Say no and stick to it. But, when you say Yes, mean it. People are counting on you to be truthful with them.

Chapters 4, 5, and 6 begin to talk about the specific practices of coding. Chapter 4 talks about the coding process itself. One of the hardest statements the book makes here is to stay out of "the zone" when coding. Bob asserts that you lose parts of the big picture when you go down to that level. While I may struggle with that assertion, I do agree with his next statement that debugging time is expensive, so you should avoid having to do debugger-driven development whenever possible. He finishes the chapter with examples of pacing yourself (walking away, taking a shower) and how to deal with being late on your projects (remembering that hope is not a plan, and being clear about the impact of overtime) along with a reminder that it is good to both give and receive help, whether it be small questions or mentoring others.

Chapters 5 and 6 cover Test-Driven Development and Practicing. The long and short is that TDD is becoming a wide-spread adopted practice, in that you don't get as many funny looks from people when you mention TDD as you once did. And that coding at work doesn't equal practicing your tools and techniques — instead you should set aside specific time to become better through coding exercises, reading and researching other areas (languages, tools, approaches), and attending events and conferences.

Chapters 7 and 8 cover testing practices. In Chapter 7 the book looks at Acceptance Tests and the cycle of writing them — specifically at what point the customer is involved (hint: continuously) and how to ensure they stay involved. Chapter 8 goes to more of the unit testing level, and defines some strategies and models for looking at unit testing, including an interesting "Test Automation Pyramid"

Now that we've covered the developer herself, coding and testing, the book moves on to discussing time. Chapter 9 covers Time Management strategies — staying out of "bogs" and "blind alleys", using techniques like the "Pomodoro" technique to create focus, and the law of two-feet — if you are in a meeting and aren't getting value out of it, you should feel free to (respectively) leave, or otherwise modify the meeting to get value from it.

Chapter 10 covers several different methods of estimation. In the teams I work with, estimation is perhaps one of the hardest things — not because estimating can be hard (which it can be) but because either they are held so tightly to the estimates that they are afraid to make them, or, worse, they are told what the estimates are going to be. The book really only skims the surface here, covering several techniques from Planning Poker, to PERT, to "Flying Fingers", but gives a decent overview of how to do those techniques.

Rounding out the discussions of time comes Chapter 11 and talking about Pressure. The key of this chapter is that because you have committed to your principles, practices and disciplines, you should be able to stay calm under pressure. I can certainly say from experience that the worst experiences in my career are when people weren't able to stay calm, and the way the book is laid out, if you are following the practices outlines so far, you should be able to be the voice of reason and calmness.

The last three chapters cover teams and collaboration. Chapter 12 talks about important practices such as shared code ownership, pairing, and respect for other team members. Chapter 13 covers teams and the importance of having teams that gel together. The book finishes with Chapter 14 and discussions of the importance of apprenticeship, mentorship and craftsmanship.

 I think that some people may be turned off by the hard line around "professionalism". Sometimes you do need to say no, and I think it is good to have encouragement from a book to do that. But sometimes things are more complex, and I think that you would have a harder time looking to this particular book for help with the edge cases.

In conclusion, I think this is a book which provides worthwhile information and an interesting look at how people are looking at software development as a profession. If you read between some of the hard lines made, there are some great nuggets to be gleaned from the book for software developers of any level.

You can purchase by clicking the Amazon link on the right banner

Also Read
An actor framework for Java concurrency
Instrumentation - Querying the memory usage of a Java object
10 things you didn't know about java performance monitoring
Ultra-lightweight Java web services with Gretty

Wednesday, September 21, 2011

My 2 cents on Hibernate

 Few hibernate experiences -

1. Implementing hashcode and equals on hibernate entities -
    Remember hibernate returns a set of entities when you have a one to many or a many to many relationships.
Accoring to Set rules - no two objects can be equal and hence it becomes all the more important to define the right equals implementation for your hibernate POJO's
Rule to thumb - equals should return true if the primary keys of the entities match.

2. Hibernate has dual caching layer -
    First level cache is stored at the hibernate SessionFactory level and the second level cache could be(if you choose to) a third party caching library.
Remember - enable second level cache only if your application has a high read to write ratio.

3. Hibernate transaction management -
    I am assuming in most cases you would use JTA. Hibernate sessions are stored on the transaction and hence if you use threadlocal to create and manage hibernate session ensure you clear the thread local once the session usage is over. Without this done explicity there is every chance that the session would not be garbage collected unless the transaction context is. Generally you would run into this issue if a transaction for some reason cannot be commited or rolledback and runs into a timeout.

Monday, September 19, 2011

GCompris -

GCompris is an educational software suite comprising of numerous activities for children aged 2 to 10. Some of the activities are game orientated, but nonetheless still educational.

We are working on a port to Android. Obviously this involves a lot of work, volunteers are always welcome. Please drop an email to me "mshamanth@hotmail.com" if you would like to help out

Sunday, January 16, 2011

Caching Scenarios

Caching Scenarios

Caching is a quick and easy way to save roundtrips between your application and where you store your data. However, it’s not as easy as just snapping a map into your application – to really leverage a cache, you not only have to understand where a cache can be used, but how it can affect what your application does and how your architecture is used.

Definition of caching

Caching is the use of an intermediate and temporary data store to preserve computation or data retrieval time. It sounds simple, but it's worth taking the time to consider cache strategies and approaches. (What’s more, if you’re really going to leverage the technology and tools, it’s worth wondering if you can get rid of the ‘intermediate’ and ‘temporary’ label there.)

Definitions

Some definitions are in order to make discussion easier.

Data. Data, in this context, is any in-memory representation of an object that represents a collection of data, or the result of a computation.

Data can have a lifetime, after which the data is considered stale and inappropriate for use.

Cache. A cache is the temporary datastore. Normally it's a region of memory, although this memory region can be persisted in any number of ways.

Datasource. A datasource is any source for data - it can be a process that performs calculations (in which case the result is often referred to as having been “memoized”) or a datastore like a database.

Participant. A participant system is any client of a cache, distributed or otherwise. In the case of a distributed cache, a participant is able to contribute local resources to the general cache.

Writethrough/Readthrough. Readthrough and writethrough refer to the ability of a cache to pass updates to the cache to the datasource, or automatically query the datasource for results. This is crucial for the ability of a cache to act as a system of record.

System of Record. A system of record is the authoritative repository for data. It will usually be a system's primary database, but some systems are able to use other systems of record efficiently. Few cache systems are able to serve as good systems of record, although some good systems of record can act as caches.

Transaction. A transaction is an operation in which state changes (deletes, updates, insertions) can be discarded or committed. An alternate definition is a cohesive operation, but this is a definition from application requirements and doesn’t have any type of technical definition in this context.

Scenarios

There are four basic strategies for caching, with two of them being fairly rare. They are: local caching, distributed caching, system of record with local storage, and event handling. Of the four of them, the first is by far the most common, the second is growing in deployments, the third is very rare but shouldn't be, and the last is almost entirely inappropriate for caching approaches - but is used nonetheless.

That doesn't mean that the distribution of caching strategies is appropriate. The plain caching approach is workable, but if it's used for the most common case - intermediate data storage - it usually means your data takes too long to retrieve. The second most common case is for memoization - storage of intermediate and calculated results - and local caching is generally fine for this, although distributed caching is better because it prevents different participating processors from having to calculate the same intermediate results.

Plain Caching

Plain caching and distributed caching are very different in terms of configuration, but otherwise very similar in terms of how they're used. Typically, the cache is represented to the participating application as a map (a key/value store) of some kind, where a data item uses a primary key of some kind to access the data.

The difficulties come in two areas: determining the data key and querying.

If the data represents a row in a database table, for example, the key can be very easy to determine: it's the table's primary key. However, if the data represents more than a simple row, things can get more complicated: the key might be a SQL query used to retrieve the data, or some other form of composite data.

This carries with it its own difficulties, because there might be more than one set of data for a given primary key. This leads into the other difficult area: queries.

Usually caches don't expose views of their data outside of the primary key. This means that if you can't reconstruct the key, that data is lost. This ... may be suboptimal.

Application: hibernate

Hibernate has two levels of caching. The first is caching held by a specific transaction, such that an object accessed repeatedly in the context of a single transaction is not fetched (or updated) over and over again.

The second level cache is held by the session factory, and is where plain caching (or distributed caching) comes into play. There are different levels of caching available here, from read-only caches, to fully transactional updating caches. The performance goes down as you traverse from read-only to read-write, to nonstrict-read-write, to transactional, but the update safety improves as a tradeoff for performance.

The second-level caches can be distributed, but distribution of a cache has drastic effects - because in most cases, synchronization takes time. Imagine an item stored in the cache on participant 1, then updated; it's then accessed from participant 2 and updated. There's a synchronization point that needs to be addressed between those nodes before updates can be properly applied.

Distributed Caching

Distributed caching is used when a cache is represented on more than one participant at once. Different cache providers provide different services for distributed caching.

Topologies

There are many topologies involved in distributed mechanisms (not just cache!) - the two basic starting points are hub-and-spoke and mesh topologies. Most other topologies can be considered as variants or mixtures of these two, although there are of course exceptions.

Hub and Spoke

The hub and spoke topology has the concept of a central server, and many clients (participants, using our definitions) connect to the central server. The main server is the cache's "system of record" although it may not be the application's system of record (and often isn't). The network bandwidth available to the central server is a limiting factor here.

One strength of this topology is that network management is usually very simple, and that only one system has to be optimized for data access: the central server.

Usually, systems with the hub and spoke topology have a backup live on the network in the case of failure; therefore, client systems are each connected to two servers (one primary, one backup).

Mesh

Mesh topologies are topologies in which each participating node is connected to every other participating node. There's usually not a central server in this topology, although there might be a coordinating process of some kind.

Each participant is a peer to the other, so there's no dominant server, nor is there an authoritative place for data to live. Network limitations are per-participant.

This is a very, very common topology for clustered environments. Participants tend to prefer data cached locally, which speeds things up; synchronization, however, can be costly depending on the transaction strategy.

One downside to this approach is that clients tend to need to have more resources allocated to them, to have enough room left over to serve cache data to other peers as well as having room to hold results locally that have been fetched from other peers.

Application: Memoization

One application for which distributed transaction shine is in memoization. We've mentioned memoization a few times, but it's worth defining: memoization is the turning the results of a function into something to be remembered.

Consider the strategy for calculating the sine of a value. One strategy that used to be very common (and may still be, although I'm not sure) was the storage of coarse sines - every quarter-radian, for example. Then, when a sine was requested, the calculation would look to see if it had previously calculated the result, and if not, would perform the expensive calculation... and save the result of the calculation in an internal table.

This is memoization. Typical uses for memoization in a caching application might be the calculation of slices of datasets, preserving the slices for later retrieval. Since calculating the slices can be expensive (depending on the size of the data set, of course), it's often worthwhile using a distributed cache to spend the calculation time once and then factor in only the time necessary to transfer the data across the network to other participants.

System of Record

A cache is not normally considered a system of record. The typical data access involving a cache looks something like this:

• Calculate a cache key.

• If cache key exists in the cache: return the data from the cache.

• Fetch the data from the system of record.

• Store the data in the cache.

• Return the data.

A typical write access would look like this:

• Calculate a cache key for the data to be written

• Write the data in the cache with the cache key

• Write the data to the system of record.

This works well, but means that data access is... complicated. Every write ends up going to the network multiple times (for the distributed cache and for the system of record) and synchronization is manual.

There are cache capabilities, however, that provide for write-through and read-through. With these capabilities, one is able to request a key from the cache, and if the cache does not have the value, it will itself go to the “official” system of record and request data; likewise, when data is written to the cache, the cache will synchronize to the database itself without programmer participation.

Naturally, there are concerns. It's usually of fairly minor impact if data is read twice from the backend system of record at the “same time” - but can be fatal if data is written to the database more than once. Therefore, transaction support is mandatory.

The transactions imply that the write-behind is synchronous. (If it's not, do not use it in any kind of transactional system.) The key at this point is to determine how synchronous the events are, and where the synchronous events take place.

Application: Gigaspaces

GigaSpaces XAP is a commercial product from GigaSpaces Technologies. It's more than a simple cache; it's a data grid and application platform (as we'll see more of in the next section). The network structure is a combination of mesh and hub-and-spoke; participants connect to a network of managing applications, which inform the participants who their peers are for failover and high availability. A participant is designated by the management system as a primary or backup; primaries synchronize to their backups and no other systems. (There are exceptions to this, naturally.)

In GigaSpaces XAP, write-behind is supported as a configuration element, normally supported by Hibernate (i.e., writing to a relational database) but offering a customizable externalization interface.

When a data item in the data grid is updated, the write is synchronized between the primary and its backup, and if the writebehind is configured, the update is also written through to a mirroring service. The mirroring service then consumes the writes and sends them to the backend datastore. Due to the nature of the mirror synchronization, you have to have a true and catastrophic failure of the data grid in order to have any problems.

Beyond Caching

The limitations of caching, up to the point of considering a cache a system of record, lie in the concept that data is stored in more than one place at any given time. This leads you to data synchronization issues, which cost you in time and complexity.

Considering a cache as a system of record can help solve some of this complexity, but most cache products’ support for systems of record are cruelly limited, to say the least (with writethrough and readthrough being generally bolted on.)

With that said, there are scenarios beyond caching to consider, arenas in which a “simple cache” can bloom into a full-bore application architecture: messaging, event handling, and distributed processing.

Messaging/Event Handling

An event handling, or messaging, system is a system in which events are delivered to waiting listeners. It’s really that simple: an event, or an object, is constructed with some information about what happened (which can be as simple as “EventHappened” or as complex as an insurance application with full details of the application, including the state of the application.)

A process somewhere is looking for that event, either constantly or regularly (i.e., always listening, or polling the event system at intervals), and upon receipt of the event, removes the event from the message system and does something to it (which often includes writing another event back in the system for further processing by something else.)

In many ways, event handling is a core concept of object-oriented programming, serving as the model for Objective C’s object paradigm, for example, and also serving the same role for Erlang and Smalltalk.

A message system, however, has the ability to queue operations, such that expensive resources (CPU time, database connections, whatever) can be managed by the number of consumers of messages; if your system can only handle one message at a time, you would simply use one consumer of messages, and messages could build up to be handled as the system was able.

A cache normally cannot serve as a message system, except very crudely at best. However, some cache-like systems (including GigaSpaces XAP) provide notification services and, in the best cases, complex and powerful message selectors to provide a full message bus.

GigaSpaces XAP not only provides a full message selection API, but multiple programming interfaces to send and receive messages, including cache-oriented interfaces.

Event handling systems refer to transactions in two contexts: one is the process transaction, an operation’s state in which changes can be committed or discarded. (This is the definition offered in the definitions section of this document.) The other definition is a requirements-defined unit of work, which can itself contain multiple transactions of both types.

It’s critical that event handlers be transactional from the process’ standpoint: otherwise an event handler that fails for any reason will cause the event to be lost entirely.

Messages should also have lifetimes, such that a message is discarded if it’s not consumed within a specific amount of time. This is usually available in cache-like systems, because cache entries normally have lifetimes associated with them already.

Another aspect for event handlers to be considered is whether event order is preserved. GigaSpaces XAP, of course, can support all of the requirements for an event handling system, and adds distributed processing and data locality as well.

Distributed Processing

Distributed processing refers to the capability of related tasks to occur simultaneously or on participants with available resources, a crucial aspect of message handling systems.

Imagine a system that needs to create PDFs on a regular basis. PDF creation is expensive (in time and memory); a system that needs to create many PDFs would easily be overloaded. With a messaging system, the number of simultaneous PDF creation processes can be limited per system participant. Correspondingly, if desired, machines with low system load could “pick up” the PDF generation tasks, which prevents the more burdened systems from being overloaded.

Distribution of tasks relies heavily on how far-reaching state changes are. A “law” called “Amdahl’s Law” refers to the sequential nature of tasks; if tasks can be broken away from serial processing, then those tasks can be executed in the amount of time it takes to process the slowest of them.

In other words, if you have a task “D” composed of tasks “A,” “B,” and “C,” which take three, four, and five seconds respectively, in a sequential process D would take twelve seconds to execute. However, if A, B, and C don’t rely on each others’ data – C doesn’t use A’s results, for example – then D can feasibly execute in five seconds, the amount of time it takes to run the longest subtask.

If C does rely on A’s data, but B does not, then D will take eight seconds to run (B would be able to finish in the time it takes to run A+C.) Amdahl’s Law refers to the potential for time savings by parallelizing tasks that don’t rely on other tasks.

There are also other variants: one is Map/Reduce, which refers to an algorithm that breaks a request into many small portions, easily handled by participant systems (the “map” phase), with the results finally collated into a cohesive result (the “reduce” phase.)

Map/Reduce is well-known in the distributed industry, but there’s an aspect that sometimes gets overlooked: locality of data.

Data can be routed, as it is in a hash, such that related data lives on a given participant. Node A might get all data for Florida, for example, while node B might get all data for New York. Therefore, tasks limited to Florida would get handled only by node A, as node B would have no local data to operate on. (That isn’t to say that node B couldn’t request this data, but this means extra resource consumption.)

This has huge implications for Map/Reduce.

Imagine a system whose purpose it is to contain… marbles of four colors: red, green, blue, and white. Counting the marbles of each color would traditionally involve iterating through all of the marbles (assuming you lose none), incrementing a count for color as each marble is processed. Even assuming the triviality of this task, it can be made quicker and simpler.

Consider: with multiple participant nodes (there’s no point discussing distributed processing with only a single node), you’d have marble colors assigned to each node. With four nodes, each node would have its own marble color; node A would have only red marbles, node B would have only green marbles, and so forth.

Counting marbles, then, becomes a task of merely counting however many marbles a given node has, then collating the results. Node A would count its marbles (with no consideration for color, as all it has are red marbles), and provide a result; nodes B through D would do the exact same thing. This is the Map phase.

The reduce phase would look at the four results (node A: n marbles, node B: p marbles, etc.) and build a list of marble colors and counts out of those results. This is the reduction phase.

The key here is that A, since it contains all of the red marbles, has direct access to its data, such that it’s merely iterating over its internal indexes of marbles – a surprisingly fast operation (not much slower than counting the elements in an array.)

Even if a node has more than one color of marble, it can be amazingly fast – since it’s still using its internal indexes instead of acquiring data from a filesystem or database.

Distributed systems also rely on transactions, as well as supporting systems of record; note that an external system of record (like a relational database) can cripple your execution time, since accessing the external database has to factor in to your runtime. Transactions (and transaction visibility, where a record might not be available to a task until another transaction is complete) will add to the execution time; a distributed system that also serves as a system of record lowers transaction speeds to near-heap access times, yielding massive benefits.