<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Anti-Pattern on Manuel Bernhardt</title><link>https://manuel.bernhardt.io/tags/anti-pattern/</link><description>Recent content in Anti-Pattern on Manuel Bernhardt</description><generator>Hugo</generator><language>en</language><lastBuildDate>Sat, 18 Feb 2023 20:54:16 +0100</lastBuildDate><atom:link href="https://manuel.bernhardt.io/tags/anti-pattern/index.xml" rel="self" type="application/rss+xml"/><item><title>Akka anti-patterns: too many actors</title><link>https://manuel.bernhardt.io/2018/08/06/akka-anti-patterns-many-actors/</link><pubDate>Mon, 06 Aug 2018 06:01:22 +0200</pubDate><guid>https://manuel.bernhardt.io/2018/08/06/akka-anti-patterns-many-actors/</guid><description>&lt;p>&lt;em>Update (07.08.2018): clarified optimal vs. suboptimal use cases of having many actors at runtime (it could have been misunderstood that Akka isn&amp;rsquo;t meant to build systems with many actors - it entirely is - but it is questionable whether that&amp;rsquo;s always the best approach)&lt;/em>&lt;/p>
&lt;p>It occurred to me that I haven&amp;rsquo;t written yet about this very frequent anti-pattern. It is to be found in codebases written by developers who have just discovered the actor model.&lt;/p></description></item><item><title>Akka anti-patterns: Java serialization</title><link>https://manuel.bernhardt.io/2018/07/20/akka-anti-patterns-java-serialization/</link><pubDate>Fri, 20 Jul 2018 11:29:45 +0200</pubDate><guid>https://manuel.bernhardt.io/2018/07/20/akka-anti-patterns-java-serialization/</guid><description>&lt;p>&lt;a href="http://akka.io">Akka&lt;/a> makes use of serialization when messages leave the JVM boundaries. This can happen in mainly two scenarios: sending messages over the network when using Akka Cluster (&lt;a href="https://manuel.bernhardt.io/2017/06/08/akka-anti-patterns-using-remoting/">do not use Akka Remote directly&lt;/a>) or using Akka Persistence.&lt;/p>
&lt;p>Now here&amp;rsquo;s the catch: the default serialization technology configured in Akka is nothing but the infamous Java serialization, &lt;a href="https://developers.slashdot.org/story/18/05/26/0520227/oracle-calls-java-serialization-a-horrible-mistake-plans-to-dump-it">which Mark Reinhold called a &amp;ldquo;horrible mistake&amp;rdquo; and which Oracle plans to dump in the near future anyway&lt;/a>.&lt;/p></description></item><item><title>Akka anti-patterns: stateless actors</title><link>https://manuel.bernhardt.io/2018/05/30/akka-anti-patterns-stateless-actors/</link><pubDate>Wed, 30 May 2018 07:31:24 +0200</pubDate><guid>https://manuel.bernhardt.io/2018/05/30/akka-anti-patterns-stateless-actors/</guid><description>&lt;p>Actors are object-orientation done right (as opposed to say, objects in Java): their state is not visible from the outside and they communicate via messages. There&amp;rsquo;s no way to break encapsulation because you can&amp;rsquo;t peek into an actor&amp;rsquo;s state while it is running. That&amp;rsquo;s the entire point of actors: they provide you with the illusion of a safe space in which things are executed sequentially, one message after the other, allowing you to use mutable state inside of your actor without having to worry about race conditions (well, almost: &lt;a href="https://manuel.bernhardt.io/2016/08/02/akka-anti-patterns-shared-mutable-state/">don&amp;rsquo;t leak the state&lt;/a>).&lt;/p></description></item><item><title>Akka anti-patterns: overusing ActorSelection</title><link>https://manuel.bernhardt.io/2018/03/20/akka-anti-patterns-overusing-actorselection/</link><pubDate>Tue, 20 Mar 2018 09:27:15 +0100</pubDate><guid>https://manuel.bernhardt.io/2018/03/20/akka-anti-patterns-overusing-actorselection/</guid><description>&lt;p>&lt;a href="https://akka.io">Akka&amp;rsquo;s&lt;/a> &lt;a href="https://doc.akka.io/japi/akka/current/akka/actor/ActorSelection.html">ActorSelection&lt;/a> makes it possible to look up actors by logical path in the hierarchy:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-scala" data-lang="scala">&lt;span class="line">&lt;span class="cl">&lt;span class="k">val&lt;/span> &lt;span class="n">selection&lt;/span>&lt;span class="k">:&lt;/span> &lt;span class="kt">ActorSelection&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">context&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">actorSelection&lt;/span>&lt;span class="o">(&lt;/span>&lt;span class="s">&amp;#34;../processor/storage&amp;#34;&lt;/span>&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>This selection can then be used like &lt;code>ActorRef&lt;/code> in order to send messages to it using the &lt;code>tell&lt;/code> or &lt;code>ask&lt;/code> patterns:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-scala" data-lang="scala">&lt;span class="line">&lt;span class="cl">&lt;span class="n">selection&lt;/span> &lt;span class="o">!&lt;/span> &lt;span class="nc">Storage&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="nc">Store&lt;/span>&lt;span class="o">(&lt;/span>&lt;span class="s">&amp;#34;important words&amp;#34;&lt;/span>&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">val&lt;/span> &lt;span class="n">allWords&lt;/span>&lt;span class="k">:&lt;/span> &lt;span class="kt">Future&lt;/span>&lt;span class="o">[&lt;/span>&lt;span class="kt">Seq&lt;/span>&lt;span class="o">[&lt;/span>&lt;span class="kt">String&lt;/span>&lt;span class="o">]]&lt;/span> &lt;span class="k">=&lt;/span> &lt;span class="n">selection&lt;/span> &lt;span class="o">?&lt;/span> &lt;span class="nc">Storage&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="nc">RetrieveAll&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>&lt;code>ActorSelection&lt;/code> is therefore quite useful and allows more flexibility when designing an actor system given that trees can be built dynamically and queried dynamically at runtime. That being said, there are a few things that you should be aware of when using this mechanism.&lt;/p></description></item><item><title>Akka anti-patterns: naming your application components after Akka concepts</title><link>https://manuel.bernhardt.io/2017/08/26/akka-anti-patterns-naming-your-application-after-akka-concepts/</link><pubDate>Sat, 26 Aug 2017 06:00:47 +0200</pubDate><guid>https://manuel.bernhardt.io/2017/08/26/akka-anti-patterns-naming-your-application-after-akka-concepts/</guid><description>&lt;p>So you&amp;rsquo;ve just learned Akka and are tremendously excited. It&amp;rsquo;s got dispatchers, routers, supervisors - so many shiny things to play with, and in style (I mean, come on, a dispatcher sounds a lot nicer than, say, an &lt;code>AbstractSingletonProxyFactoryBean&lt;/code>).&lt;/p>
&lt;p>
&lt;figure style="text-align: center">
 &lt;img src="https://manuel.bernhardt.io/wp-content/d726ffbf9d0930295e3d98de53b93c53.jpg" class="pure-img" alt="">
&lt;/figure>

&lt;/p>
&lt;p>And so it happens that, perhaps even unknowingly to you (you&amp;rsquo;re busy configuring a &lt;a href="http://doc.akka.io/docs/akka/2.5.3/scala/routing.html">pool router&lt;/a>), your actors start getting names such as:&lt;/p>
&lt;ul>
&lt;li>&lt;code>Dispatcher&lt;/code>&lt;/li>
&lt;li>&lt;code>IncomingRouter&lt;/code>&lt;/li>
&lt;li>&lt;code>FailureSupervisor&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>I&amp;rsquo;ve observed this on three projects now, for two of which I delivered &lt;a href="https://manuel.bernhardt.io/fast-track-to-akka-with-java/">an introductory Akka training&lt;/a> to the team beforehand. I&amp;rsquo;d say that this anti-pattern is especially to be found in projects where people are just getting started with Akka.&lt;/p></description></item><item><title>Akka anti-patterns: trusting the network</title><link>https://manuel.bernhardt.io/2017/06/20/akka-anti-patterns-trusting-network/</link><pubDate>Tue, 20 Jun 2017 05:06:33 +0200</pubDate><guid>https://manuel.bernhardt.io/2017/06/20/akka-anti-patterns-trusting-network/</guid><description>&lt;p>One anti-pattern I&amp;rsquo;ve observed in networked Akka applications is a tendency to forget that it is, well, networked and to treat the network as a friendly place.&lt;/p>
&lt;p>
 &lt;figure style="text-align: center">
 &lt;img src="https://manuel.bernhardt.io/wp-content/trusting-the-network.jpg" class="pure-img" alt="">
 &lt;figcaption>Trusting the network to be reliable&lt;/figcaption>
 &lt;/figure>

&lt;/p>
&lt;p>How does that look like? Well, consider the following piece of code:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-java" data-lang="java">&lt;span class="line">&lt;span class="cl">&lt;span class="n">actorOnDifferentNode&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="na">tell&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">veryImportantMessage&lt;/span>&lt;span class="p">,&lt;/span>&lt;span class="w"> &lt;/span>&lt;span class="n">sender&lt;/span>&lt;span class="p">());&lt;/span>&lt;span class="w">
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>Akka&amp;rsquo;s default message sending mechanism is the fire-and-forget &lt;code>tell&lt;/code> method. And that works great - until it doesn&amp;rsquo;t. See, &lt;code>tell&lt;/code> has &lt;a href="http://doc.akka.io/docs/akka/current/java/general/message-delivery-reliability.html">at-most-once message delivery semantics&lt;/a>. This means that in theory (and in practice!) messages may not make it to their destination.&lt;/p></description></item><item><title>Akka anti-patterns: using remoting</title><link>https://manuel.bernhardt.io/2017/06/08/akka-anti-patterns-using-remoting/</link><pubDate>Thu, 08 Jun 2017 07:14:03 +0200</pubDate><guid>https://manuel.bernhardt.io/2017/06/08/akka-anti-patterns-using-remoting/</guid><description>&lt;p>
 &lt;figure style="text-align: center">
 &lt;img src="https://manuel.bernhardt.io/wp-content/lucy-waves.jpg" class="pure-img" alt="">
 &lt;figcaption>Senior developer using Akka remoting directly to build a special type of cluster&lt;/figcaption>
 &lt;/figure>

&lt;/p>
&lt;p>Whilst I have always successfully discouraged my clients from using &lt;a href="http://doc.akka.io/docs/akka/snapshot/scala/remoting.html">Akka Remoting&lt;/a> in their application, I often get questions regarding remoting while &lt;a href="https://manuel.bernhardt.io/talks/">talking&lt;/a> about anti-patterns at conferences and user groups.&lt;/p>
&lt;p>Don&amp;rsquo;t get me wrong. I love Akka remoting. Especially the new &lt;a href="https://github.com/real-logic/Aeron">Aeron&lt;/a>-based &lt;a href="http://doc.akka.io/docs/akka/snapshot/scala/remoting-artery.html">Artery&lt;/a> remoting version that is based on UDP rather than TCP. Whilst it maintains the same delivery guarantees, it is quite a bit faster than the TCP counterpart.&lt;/p></description></item><item><title>Akka anti-patterns: blocking</title><link>https://manuel.bernhardt.io/2017/05/15/akka-anti-patterns-blocking/</link><pubDate>Mon, 15 May 2017 07:59:20 +0200</pubDate><guid>https://manuel.bernhardt.io/2017/05/15/akka-anti-patterns-blocking/</guid><description>&lt;p>This is probably one of the most frequent (and dangerous) anti-patterns when it comes to working with &lt;a href="http://akka.io/">Akka&lt;/a>. Let&amp;rsquo;s look at an adequate description of the mindset you must be in in order to use it:&lt;/p>
&lt;p>So maybe you didn&amp;rsquo;t actually want to see the world burn. Maybe you thought that this one time it was okay to call blocking code inside of an actor. Little did you know that, seven months later, this decision would bring down the entire system minutes after its official launch and announcement in the media.&lt;/p></description></item><item><title>Akka anti-patterns: being out of touch with the hardware</title><link>https://manuel.bernhardt.io/2016/11/21/akka-anti-patterns-being-out-of-touch-with-the-hardware/</link><pubDate>Mon, 21 Nov 2016 17:02:31 +0100</pubDate><guid>https://manuel.bernhardt.io/2016/11/21/akka-anti-patterns-being-out-of-touch-with-the-hardware/</guid><description>&lt;p>Choosing Akka as a tool is often - if not always - driven by the need for good performance. Surely, the actor model itself is appealing as a means for organizing and reasoning about code, but this isn&amp;rsquo;t in itself a good reason enough to use the Akka toolkit. If all you are concerned about is a nice way to organize code and build modular applications you might as well pick the Spring Framework which has a very rich and clear component model and provides very good support for building software where performance isn&amp;rsquo;t one of the driving factors.&lt;/p></description></item><item><title>Akka anti-patterns: logging (the wrong way)</title><link>https://manuel.bernhardt.io/2016/08/31/akka-anti-patterns-logging-the-wrong-way/</link><pubDate>Wed, 31 Aug 2016 05:59:33 +0200</pubDate><guid>https://manuel.bernhardt.io/2016/08/31/akka-anti-patterns-logging-the-wrong-way/</guid><description>&lt;p>&lt;strong>Update&lt;/strong>: removed rogue &lt;code>toString&lt;/code> call in the second code example, since it was unnecessary (and harmful).&lt;/p>
&lt;p>
 &lt;figure style="text-align: center">
 &lt;img src="https://manuel.bernhardt.io/wp-content/2534404231_f65753fc3a_b.jpg" class="pure-img" alt="">
 &lt;figcaption>Proper logging&lt;/figcaption>
 &lt;/figure>

&lt;/p>
&lt;p>Debugging actor systems is no small feat, even when there is &lt;a href="http://scala-ide.org/docs/current-user-doc/features/async-debugger/index.html">IDE support for it&lt;/a>. In fact, debugging any asynchronous system for that matter is a rather complicated task. Which is why, especially during development, it is not entirely uncommon to rely on DEBUG level logging to get a sense of how messages flow between actors. Whilst doing so is fine, there are a few precautions that need to be taken when logging at DEBUG level (or any other level). Remember, logging ranks very high on the &lt;a href="https://vimeo.com/177215741">list of top 10 performance mistakes&lt;/a>.&lt;/p></description></item><item><title>Akka anti-patterns: too many actor systems</title><link>https://manuel.bernhardt.io/2016/08/23/akka-anti-patterns-too-many-actor-systems/</link><pubDate>Tue, 23 Aug 2016 07:25:10 +0200</pubDate><guid>https://manuel.bernhardt.io/2016/08/23/akka-anti-patterns-too-many-actor-systems/</guid><description>&lt;p>Admittedly I&amp;rsquo;ve seen this one in use only one time, but it was one time too many. For some reason I keep seeing clients come up with this during design discussions and reviews though, therefore it makes it into the list of &lt;a href="https://manuel.bernhardt.io/tags/anti-pattern/">Akka anti-patterns&lt;/a>.&lt;/p>
&lt;p>What I am talking about is this:&lt;/p>
&lt;p>
 &lt;figure style="text-align: center">
 &lt;img src="https://manuel.bernhardt.io/wp-content/actorsystems.png" class="pure-img" alt="">
 &lt;figcaption>Many ActorSystems on a single JVM, competing for resources&lt;/figcaption>
 &lt;/figure>

&lt;/p>
&lt;p>Reasons I hear for this design:&lt;/p>
&lt;ul>
&lt;li>isolation of concerns: each ActorSystem is separated, hence failure of one system does not affect the other&lt;/li>
&lt;li>custom classloading in the ActorSystem, dynamically loading libraries and isolating them between systems&lt;/li>
&lt;li>using multiple Akka versions without having to restart&lt;/li>
&lt;/ul>
&lt;p>And I am not talking about designs where there are 2 or 3 ActorSystems on the same JVM here, but possible hundreds if not thousands. Now, Akka itself &lt;a href="http://doc.akka.io/docs/akka/current/general/actor-systems.html#Configuration_Container">does not share any global state&lt;/a> so you can theoretically pull this off. But should you?&lt;/p></description></item><item><title>Akka anti-patterns: race conditions</title><link>https://manuel.bernhardt.io/2016/08/16/akka-anti-patterns-race-conditions/</link><pubDate>Tue, 16 Aug 2016 05:00:39 +0200</pubDate><guid>https://manuel.bernhardt.io/2016/08/16/akka-anti-patterns-race-conditions/</guid><description>&lt;p>
&lt;figure style="text-align: center">
 &lt;img src="https://manuel.bernhardt.io/wp-content/3169240519_49e8193ab4_o.jpg" class="pure-img" alt="">
&lt;/figure>

&lt;/p>
&lt;p>The actor model makes it possible to build highly-concurrent applications through the notion that actors obey the &lt;a href="http://doc.akka.io/docs/akka/snapshot/general/jmm.html#Actors_and_the_Java_Memory_Model">actor send rule and the actor subsequent processing rule&lt;/a>, hence creating a &lt;strong>single-threaded environment&lt;/strong> inside of an actor.&lt;/p>
&lt;p>That being said, it&amp;rsquo;s all an &lt;strong>illusion&lt;/strong>: as we have &lt;a href="https://manuel.bernhardt.io/2016/08/02/akka-anti-patterns-shared-mutable-state/">briefly talked about previously&lt;/a>, Akka&amp;rsquo;s dispatchers make sure that messages are being processed by actors and so the thread by which one message is processed may not be the same as the one by which the next message is going to be processed.&lt;/p></description></item><item><title>Akka anti-patterns: flat actor hierarchies or mixing business logic and failure handling</title><link>https://manuel.bernhardt.io/2016/08/09/akka-anti-patterns-flat-actor-hierarchies-or-mixing-business-logic-and-failure-handling/</link><pubDate>Tue, 09 Aug 2016 04:00:31 +0200</pubDate><guid>https://manuel.bernhardt.io/2016/08/09/akka-anti-patterns-flat-actor-hierarchies-or-mixing-business-logic-and-failure-handling/</guid><description>&lt;p>
 &lt;figure style="text-align: center">
 &lt;img src="https://manuel.bernhardt.io/wp-content/Portable-Network-Graphics-image-C1B055469582-1.png" class="pure-img" alt="">
 &lt;figcaption>Flat actor hierarchy. Sad actors being all flat.&lt;/figcaption>
 &lt;/figure>

&lt;/p>
&lt;p>One of the fundamental ideas built into Akka is the one of failure handling through parental &lt;em>supervision&lt;/em>.&lt;/p>
&lt;p>In other words this means agencing actors in such a way that parent actors that depend upon the correct execution of a child to perform their work are also responsible for deciding what to do when one of the child actor crashes.&lt;/p>
&lt;p>An exception thrown inside of an actor results in that actor crashing, prompting its parent to decide what to do, which it does using a &lt;a href="http://doc.akka.io/docs/akka/current/general/supervision.html">SupervisorStrategy&lt;/a>. You should not be writing defensive &lt;code>try...catch&lt;/code> blocks inside of an actor - except perhaps for a few rare cases, this too is an anti-pattern! Instead the idea is to &lt;a href="http://letitcrash.com">let it crash&lt;/a> and handle failures through another channel entirely.&lt;/p></description></item><item><title>Akka anti-patterns: shared mutable state</title><link>https://manuel.bernhardt.io/2016/08/02/akka-anti-patterns-shared-mutable-state/</link><pubDate>Tue, 02 Aug 2016 15:00:40 +0200</pubDate><guid>https://manuel.bernhardt.io/2016/08/02/akka-anti-patterns-shared-mutable-state/</guid><description>&lt;p>When I work with clients on designing actor systems there are a few anti-patterns that seem to make it into initial, non-reviewed designs no matter what. In this series of short articles I would like to cover a few of those.&lt;/p>
&lt;h1 id="anti-pattern-1-sharing-mutable-state-accross-actors">Anti-pattern #1: sharing mutable state accross actors&lt;/h1>
&lt;p>
&lt;figure style="text-align: center">
 &lt;img src="https://manuel.bernhardt.io/wp-content/one-does-not-state.jpg" class="pure-img" alt="">
&lt;/figure>

&lt;/p>
&lt;p>Even though the Akka documentation &lt;a href="http://doc.akka.io/docs/akka/current/general/actor-systems.html#Actor_Best_Practices">points this out&lt;/a> &lt;a href="http://doc.akka.io/docs/akka/current/general/jmm.html#Actors_and_shared_mutable_state">in various places&lt;/a>, one of the favourite anti-patterns I&amp;rsquo;ve seen is use is &lt;em>sharing mutable state accross actor boundaries&lt;/em>.&lt;/p></description></item></channel></rss>