48
votes

I was just comparing the performance of scala actors vs java threads.

I was amazed to see the difference, I observed that with my system I was able to spawn maximum ~2000 threads (live at a time) only But with the same system I was able to spawn ~500,000 actors of scala.

Both programs used around 81MB of Heap memory of JVM.

Can you explain How java thread are this much heavy weight than scala / akka actors? What is the key factor which made scala-actor this much light weight?

If I want to achieve best scalability, Should I go for actor based web server instead of java based traditional web/app server like JBoss or Tomcat?

Thanks.

2
As for your web server question, it depends on what you need. There are really scalable solutions wrt to high throughput, based on NIO like Netty, and they don't use actors. Do you need a full blown web server? high traffic services? High integration with known frameworks (jee, spring,...)? Actors in general, and Akka 2.1 in particular with support for clustering, or play! which is based on akka, are a "scalable" solution, too, but there's no silver bullet for all scenarios. It really depends on your needs.pagoda_5b

2 Answers

39
votes

Scala actors (including the Akka variety) use Java threads. There's no magic: more than a few thousand threads running simultaneously is a problem for most desktop machines.

The Actor model allows for awake-on-demand actors which do not occupy a thread unless they have work to do. Some problems can be modeled effectively as lots of sleeping agents waiting to get some work, who will do it relatively quickly and then go back to sleep. In that case, actors are a very efficient way to use Java threading to get your work done, especially if you have a library like Akka where performance has been a high priority.

The Akka docs explain the basics pretty well.

All reasonably scalable web servers have to solve this sort of problem one way or another; you probably ought not be basing your decision for web server primarily on whether actors are used under the hood, and regardless of what you use you can always add actors yourself.

19
votes

An Akka actor is not equivalent to a thread. It is more like a Callable that is executed on a threadpool.

When a message is dispatched to an actor, that actor is placed on a threadpool to process the message. When it is done, the pooled thread can be used to execute other actors.