1

Use an atomic integer as an intance evariable within service classes that are injected into controllers. Scaling is limited by memory required for each thread to run operation, and how many requests are processed per second (cpu speend and time to perform operations on atomic integer?)

@Service
public MyService{

AtomciInteger count = new AtomicIntger(0);

 public int add() {
   return count.incrementAndGet();
 }

    // accessed via ajax loop (and controller), if value changes update display
  public int getCount() {
    return count.get();
  }
}

The service class will access via controller, the controller will be available as restful webserivce, accessed via ajax calls. How may siumulateous users/requests could it handle ?

The only alternative I can think of is to use a hibernate entity and store the integer value as a field. Letting hibernate handle the threading issues where a sessionfactory will be injected into dao, and the dao injected into service classes – scaling issues ? My spring classes are all singletons.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311

1 Answers1

2

AtomicIntger is as fast as it can be. Reading it (volatile field) is close to reading an ordinary int. Updating uses CAS operations with simple while loop, this can't be done faster and thread-safe. You are using Spring (and probably few layers of AOP aspects, Spring MVC controllers (with plenty of reflection) and you call this via HTTP protocol. Incrementing and accessing one int is the least of your problems.

The only benefit of using Hibernate (JDBC for that matter) is scalability across multiple servers. However instead of Hibernate use plain JDBC and database sequence - much faster, but slower by two orders of magnitude compared to Atomic* classes. Consider to get both: moderate speed and simplicity.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • This is storing state in a service class, something that should be generally avoided - but for such a simple case it is appropriate ? For a typical large scale website (and a more complex problem), persisting to a database/cache is the best as it allows a cluster of server/tomcat instances to operate? – NimChimpsky Jun 23 '12 at 12:35
  • @NimChimpsky: stateful service class isn't a problem as long as you have only one server. If you want to scale out and and have a cluster of servers, accessing the same table/sequence from multiple servers (I guess quite often) will become a bottleneck. Actually having sequential, consistent and reliable counter in distributed environment is a problem on its own, see: http://stackoverflow.com/questions/10017338 – Tomasz Nurkiewicz Jun 23 '12 at 12:43
  • Using an atmoic integer the major bottleneck for max number of requests (aka threads, or users) would be processor speed ? Each thread could access the method concurrently on the stack, so RAM should not be a problem ? Obviously for such a small problem other factors would come in ... but I am trying to understand the underlying issues. – NimChimpsky Jun 23 '12 at 13:08