- Deadlocks
- Bad scaling due to "over-synchronization" or contention on common resources
- Errors caused by invalid synchronization are timing dependent and hard to reproduce. It is common they appear under special conditions (load, hardware) in production.
- As a software project grows, it gets hard to safely modify the application. A programmer needs global awareness of an applications control flow and data access patterns in order to change the software without introducing errors.
- The subtleties of the Java Memory Model are not well known to most Java programmers. Even if JMM is known, incorrect programming isn't obvious and will fail rarely. It can be a nightmare to spot them from production logs of large applications.
Actors
Real Life Analogy: People cooperating by sending mails to each other.
An Actor is like an object instance executed by a single thread. Instead of direct calls to methods, messages are put into the Actors "mailbox" (~queue). The actor single threaded reads and processes messages from the queue sequentially (with exceptions).
Internal state is exposed/shared by passing messages (with copied state) to other Actors.
![]() |
CSP (Communicating Sequential Processes)
Real Life Analogy: People phoning each other.
Though a different terminology is used, CSP systems can be seen as a special actor system having bounded mailboxes (queues) of size 0.
So if one process (~Actor) wants to pass a message to another process, the caller is blocked until the receiver accepts the message. Alternatively to being blocked, a CSP-process can choose to do other things e.g. check for incoming messages (this introduces non determinism to the order of outgoing messages). A receiver cannot accept incoming messages if he is occupied with processing a prior one.
Disruptor
The disruptor data structure came up some years ago and was invented and pioneered by Martin Thompson, Mike Barker, and Dave Farley at LMAX exchange.
Real Life Analogy: Assembly line
The disruptor is a bounded queue (implemented by a ring buffer) where producers add to the head of the queue (if slots are available, else the producer is blocked).
Consumers access the queue at different points, so each consumer has its own read position (cursor) inside the queue. Sequencing is used to manage queue access and processing order.
Thread + locking of shared data
![]() |
contention |
Conclusion
While the CSP/Actor-Model realizes inter-thread communication by passing/copying thread-local data to some queue or process, the Disruptor keeps data in place and assures only one thread (consumer or producer) is owner of a data item (=queue slot) at a time.
This model seems to fit existing CPU, memory and VM architectures better resulting in high throughput and superior performance. It avoids high allocation rates and reduces the probability of cache misses. Additionally it implicitly balances processing speed of interdependent consumers without growing queues somewhere.
There is no silver bullet for concurrent programming, one still has to plan carefully and needs to know what's going on.
While I am writing this, I realize each of these patterns has its set of best-fit problem domains, so my initial intention using a single benchmark to compare performance of different concurrency models might be somewhat off :-).
The Benchmark Application
Update: Jason Koch implemented a custom executor performing significantly better than the stock JDK executors. See his blog post.
The benchmark approximates the value of PI concurrently. Therefore N jobs are created, and the result of each job must be added to finally get an approximation of PI. For maximum performance one would create one large job for each Core of the CPU used. With this setup all solutions roughly would perform the same as there is no significant concurrent access and scheduling overhead.
However if we compute PI by creating 1 million jobs, each computing a 0..100-loop slice of PI, we get an impression of:
- cost of accessing shared data when the result of each job is added to one single result value
- overhead created by the concurrency control mechanics (e.g. sending messages/scheduling Runnables to a thread pool, CAS, Locks, volatile r/w ..).
- 100 iterations per pi-slice-job, 1,000,000 jobs
- 1,000 iterations per pi-slice-job, 100,000 jobs
- AMD Opteron 6274 Dual Socket 2,2 Ghz. Each socket has 8 cores + 8 Hardware Threads (so overall 16 cores + 16 HW Threads)
- Intel XEON@3Ghz dual socket six core (12 cores + Hyperthreading turned off). (2011 release)
Stop the blabber, gimme results !
See bottom of this post for the benchmark source.
- Threads - somewhat naive thread based implementation of the Pi computation. Enough effort invested it is possible to match any of the other results of course. At the core of the VM, threads, locks (+atomic, volatile r/w + CAS) are the only concurrent primitives. However there is no point in creating an ad-hoc copy of the Disruptor or an Actor system in order to compare concurrency approaches.
- Akka - a popular Actor implementation on the VM. The benchmark has been reviewed and revised (especially the ActorSystem configuration can make a big difference) by the Akka crew. Threads are scheduled using Java 7's fork join pool. Actually the Pi computation is one of Akka's tutorial examples.
- Abstraktor - my experimental Actor/CSP implementation. It's using short bounded queues (so leans more to the CSP side) and avoids deadlocks by maintaining 2 queues per Actor (in and out). If the out-queue is blocked, it just reads from the in-queue.
I am using Nitsan Wakarts excellent MPSC queue implementation (check his blog or github jaq-in-a-box) and that's the major reason it shows kind of competitive performance+scaling.
I use this to get a rough baseline for comparision and experiment with different flavours of Actors/CSP. Probably the only thing one can do with it is to run the Pi bench ;-).
Update: The experimental version benchmarked here has been consolidated + improved. You can find it on https://github.com/RuedigerMoeller/kontraktor - Disruptor - my naive approach implementing the benchmark based on the Disruptor 3.2. It turned out that I used a not-yet-polished utility class, however I keep this benchmark just to illustrate how smallish differences in implementation may have big consequences.
- Disruptor2 - As Michael Barker would have implemented it (Thanks :-) ). Its actually more than twice as fast for the 1 million test as the closest runner up.
Well, it seems with 100k jobs of 1000 iterations, the benchmark is dominated by computation, not concurrency control. Therefore I retry with 1 million jobs each computing a 100 iteration slice.
Ok, we probably can see differences here :-)
AMD Opteron 6274 Dual Socket 2,2 Ghz (=16 real cores, 16 HW Threads)
Again the 1 million tiny-job variant spreads the difference amongst the approaches (and their implementation):
Note there is like 5% run to run jitter (GC and stuff), however that does not change the big picture.
Last but not least: Best results per CPU architecture per lib:
Discussion of Results
We see that scaling behaviour can be significantly different depending on the hardware platform used.
Akka loses a lot of CPU time doing GC and allocation. If I modify Abstraktor to use unbounded Concurrent Linked Queue (Akka uses those), it performs similar to Akka and builds up temporary queues >100k elements. This is an inherent issue of the Actor model. By leaning towards CSP (use very short bounded blocking queues with size <100), performance also suffers, as threads are blocked/spinning for input too often. However with a queue size of 5000 elements, things work out pretty well (introducing other problems like deadlocks in case of cyclic Actor graphs).
The Disruptor library is very well implemented, so a good part of the performance advantage could be attributed to the excellent quality of implementation.
Cite from the Disruptor documentation regarding queuing:
3.5 The Problems of QueuesCouldn't have said it better myself =).
[...] If an in-memory queue is allowed to be unbounded then for many classes of problem it can grow unchecked until it reaches the point of catastrophic failure by exhausting memory. This happens when producers outpace the consumers. Unbounded queues can be useful in systems where the producers are guaranteed not to outpace the consumers and memory is a precious resource, but there is always a risk if this assumption doesn’t hold and queue grows without limit. [...]
When in use, queues are typically always close to full or close to empty due to the differences in pace between consumers and producers. They very rarely operate in a balanced middle ground where the rate of production and consumption is evenly matched. [...]
Conclusion
Although the Disruptor worked best for this example, I think looking for "the concurrency model to go for" is wrong. If we look at the real world, we see all 4 patterns used dependent on use case.
So a broad concurrency library ideally would integrate the assembly-line pattern (~Disruptor), queued messaging (~Actors) and unqueued communication (~CSP).
Benchmark source
AKKA
Multi Threading
Abstraktor
Disruptor naive
Disruptor optimized
Rudiger, is hard to arrive to a conclusion. There are too many variables and misconceptions. But are we looking at the same thing. CSP and actors are both high level abstractions. And disruptor seems to me a very sophisticated queue implementation. Couldn't CSP and/or Actors be implemented with ring queues and memory barriers under the hood? Clearly platforms that rely too much on garbage collection may be at odds with the disruptor approach. But we could eventually see CSP and/or Actors catch up.
ReplyDeleteI don't think there is a general "best" model, it depends on use case.
ReplyDeleteCSP has inherent problems with blocking, if one wants to avoid that, one has to accept indeterministic order of processing, which is a no go for many applications.
Actors do not work well with any kind of bounded queue. As soon you limit queue size there is a deadlock scenario if your actor graph contains cycles.
However the disruptor is more than a "queue" implementation. While actors need a queue for each processing unit following "move data not code", disruptor does this vice-versa "move code, not data". The analogy of a "mailbox" system and an "assembly line" pretty good illustrates the difference of the approaches. There are queues involved, but still the approach is quite diferrent.
And what about software transactional memory?
ReplyDeleteThere are no serious pure-software attempts for benchmarking available. However there are attempts to support this in hardware (Intel). I am not deep enough into the details to judge/guess viability of this approach. I'd assume it performes well for low contention but not so good in high contention cases. Additionally its not clear how and when the JVM will make this hardware feature avaiable.
DeleteAt time of writing I wasn't even aware of STM :-)
Useful read for me. Apart from throughput comparison I am also interested in know which concurrency model did you find easy to use and which one gives best safety guarantees. I am yet to dig to into concurrency models to reason about correctness and safety. Could you throw some light based on your experience?
ReplyDeleteI think there is no "fits all" model.
DeleteI found the Disruptor quite usable to setup high speed event/request processing engines, however it requires a different style to represent business logic and proper planning.
As soon you hit some blocking API you'll need other ways to deal with concurrency.
Actors: From my experience its manageable if you use it very course grained, as soon you setup complex actor systems with cyclic messaging/dependencies accross many actors, you'll run into serious trouble once real load is put on (queue overflows or deadlocks with bounded actor queues). Its by far not the "silver bullet" some folks try to make it.
Try to think+design "reactive" e.g. a server component should be seen as a component processing a stream of incoming events single threaded (use NIO). If that is too slow, try to parallize sub-tasks, not the complete application flow. If that's still too slow, try partitioning. Avoid synchonous stuff (e.g. synchronous WebServices or remote calls, synchronous data base queries ). There are tricks to wrap blocking operations in order to avoid multithreading. Get used to queuing in order pass data from one processing unit to another.
Unfortunately many of the old API's (e.g. servlets, jdbc) still are hard to use for a reactive style of programming and encourage/require creation of many threads without gaining anything but complexity.
This comment has been removed by the author.
ReplyDeleteNice article, I think you have to consider reactive-streams as it is giving "asynchronous stream processing with non-blocking back pressure" which is meaning that it is reactive but consumer "subscriber" can control the pressure of the reactive calling, implementing dynamic push-pull way is giving benefits of reactive plus iterating, one of its implementations reactor using Disruptor implicitly.
ReplyDeleteI implemented remoted reactive streams here: https://github.com/RuedigerMoeller/kontraktor/tree/trunk/modules/reactive-streams
DeleteThough it might look "new" to many its in essence a simplified version of credit based flow control known from networking.
A shortfall of current spec is the non-adaptive nature of "request(n)" pull sizes [tcp is better here] which is not an issue in-memory, but an issue at network grade latency. As the "request(n)" message has some travel time (depending on physical network conditions) the publisher might run dry. Its not too bad if libraries allow for manual tweaking, but not all allow for adjustment of "pull" size which leads to some whacky throughput variation when streaming over network.
Also note that Reactive Streams require queuing so its way slower than pure Disruptor processing. However its much easier to built up large scale, adaptive complex event processing pipelines using flow controlled Reactive Streams
DeleteGreat article with clarity, Rüdiger! Just one question: how would you compare Asynchronous Programming to those paradigms? Thanks!
ReplyDelete
ReplyDeleteThanks for sharing this amazing blog
Java Online Course
Hi, very clear. What about the "design" fact that in CSP one entity sends a message to a channel and another entity reads it from a channel, while in Actors actors sends directly messages one to another, so i can pass around the channel, it looks like a different abstractions which can be useful or not depending on use case, but in general having a channel I send events to sounds more reasnoable to me than objects talking directly (in many cases they should not know one another but when you read actor code you see that they are aware one of another a bad design by the fact of ease of misuse).
ReplyDeletenice post...
ReplyDeleteabacus training classes in chennai
vedic maths training chennai
franchise opportunities
Abacus Classes in chennai
memory techniques
vedic maths
franchise opportunities
Devops is not a Tool.Devops Is a Practice, Methodology, Culture or process used in an Organization or Company for fast collaboration, integration and communication between Development and Operational Teams. In order to increase, automate the speed of productivity and delivery with reliability.
ReplyDeletepython training in bangalore
aws training in bangalore
artificial intelligence training in bangalore
data science training in bangalore
machine learning training in bangalore
hadoop training in bangalore
devops training in bangalore
corporate training companies
ReplyDeletecorporate training companies in mumbai
corporate training companies in pune
corporate training companies in delhi
corporate training companies in chennai
corporate training companies in hyderabad
corporate training companies in bangalore
Gaining Python certifications will validate your skills and advance your career.
ReplyDeletepython certification
Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work
ReplyDeleteDevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.
Good to learn about DevOps at this time.
devops training in chennai | devops training in chennai with placement | devops training in chennai omr | devops training in velachery | devops training in chennai tambaram | devops institutes in chennai | devops certification in chennai
Devops is not a Tool.Devops Is a Practice, Methodology, Culture or process used in an Organization or Company for fast collaboration, integration and communication between Development and Operational Teams. In order to increase, automate the speed of productivity and delivery with reliability.
ReplyDeletedigital marketing courses in Bangalore With placement
digital marketing training in Bangalore
seo training in Bangalore
Very Nice Information! Brainy helps children's to develop their creativity and social skills.
ReplyDeleteAt Brainy, There are many child brain development activities.
Also you can apply for educational franchise opportunity India.
lampungservice.com
ReplyDeleteserviceiphonebandarlampung.blogspot.com
youtubelampung.blogspot.com
bimbellampung.blogspot.com
bateraitanam.blogspot.com
Lampung
ReplyDeleteSamsung
youtube
youtube
lampung
kuota
Indonesia
lampung
lampung
youtube
Maniry anao foana ny fifaliana sy ny fahasambarana. Manantena ianao fa manana lahatsoratra tsara kokoa ianao.
ReplyDeletePhối chó bull pháp
Phối giống chó Corgi
Phối chó Pug
Phối giống chó alaska
Sharp
ReplyDeleteLampung
Metroyoutube
youtube
lampung
kuota
Indonesia
This is really a big and great source of information. We can all contribute and benefit from reading as well as gaining knowledge from this content just amazing
ReplyDeleteexperience Thanks for sharing such a nice information.
DedicatedHosting4u.com
Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
ReplyDeleteSql server dba online training
It is a very popular application which provides you to download videos, songs, mashups, remixes, trailers in your mobile , and off course in any quality. It contains many videos and of many types .
ReplyDeletelike funny videos, motivational videos, songs lyrics, trailer and many more.
Want to watch movie in slow connection,
Vidmate app,
Vidmate app to watch videos,
vidmate,
Vidmate Features,
Vidmate Download,
Multi sSource Downloader,
cm security antivirus,
leaked Movie Online,
Why do people prefer Vidmate App.
This is a nice Site to watch out for and we provided information on
ReplyDeletevidmate make sure you can check it out and keep on visiting our Site.
Download and install Vidmate App which is the best HD video downloader software available for Android. Get free latest HD movies, songs, and your favorite TV shows.
ReplyDeletequickbooks online pricing plans
ReplyDeleteI really enjoyed your blog Thanks for sharing such an informative post.
ReplyDeletehttps://myseokhazana.com/
Best Website Development service In Noida
Web Designer in Noida
Best Website Development service In Noida
Website Designing service In Noida
Best digital marketing service In Noida
Best digital marketing Company in Noida
Best SEO service In Noida
Best SEO Company in Noida
Software development Company in Noida
Web hosting Company in Noida
Best bulk emails Company in Noida
Best content writing Company in Noida
Best bulk sms Company in Noida
Bulk sms Company in Noida
Bulk sms service In Noida
This comment has been removed by the author.
ReplyDeleteDownload latest audio and video file fromvidmate
ReplyDeleteget free apps on 9apps
ReplyDeleteQuickbooks Accounting Software
ReplyDeleteAmerican eagle credit card login
ReplyDeleteDownload latest audio and video file fromvidmate
ReplyDeleteDownload latest audio and video file fromvidmate
ReplyDeletevidmate app
ReplyDeleteThanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts...
ReplyDeleteCloud Computing Online Training
I really like your blog. You make it interesting to read and entertaining at the same time. I cant wait to read more from you.
ReplyDeletesalesforce online Certification
vidmate
ReplyDeletevidmate
ReplyDeletevidmate
ReplyDeletevidmate
ReplyDelete
ReplyDeleteThanks you sharing information.
You can also visit on
How to think positive
Cure For Cowardice
Mudras
SOCIAL ANXIETY AND LOW SELF-ESTEEM
PUBLIC MEETING AND PRESENTATION
ReplyDeleteNuvigil smart drug is the trade name for Armodafinil smart drug and is the enantiopure compound of Modafinil. There are a number of health benefits that the use of Nuvigil smart drug has to offer to its users. So, you can buy Nuvigil online. The use of the smart drug is safe and has no health issues attached to its usage. In the year 2006, the FDA approved the use of generic Nuvigil smart drug to be used to treat a number of mental issues. Buy Nuvigil online as the use of Nuvigil smart drug has long effect when compared to other smart drugs. This is mainly because Armodafinil smart drug has a longer half-life than the other smart drugs. Therefore more and more people are depending on Nuvigil smart drug to carry on their daily routine smoothly.
Buy Nuvigil online
call adultxxx
ReplyDeletecall girl
xadult
Wikindly | Trending Female Celebrities Biographies
ReplyDeleteFemale Celebrities Biographies
Female Celebrities Biographies
Female Celebrities Biographies
Female Celebrities Biographies
Female Celebrities Biographies
Female Celebrities Biographies
Female Celebrities Biographies
Female Celebrities Biographies
Really i found this article more informative, thanks for sharing this article! Also Check here
ReplyDeleteDownload and install Vidmate App which is the best HD video downloader software available for Android. Get free latest HD movies, songs, and your favorite TV shows
Vidmate App Download
Vidmate apk for Android devices
Vidmate App
download Vidmate for Windows PC
download Vidmate for Windows PC Free
Vidmate Download for Windows 10
Download Vidmate for iOS
Download Vidmate for Blackberry
Vidmate For IOS and Blackberry OS
found this helpful
ReplyDeleteshowbox
showbox for pc