<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ostinelli|net</title>
	<atom:link href="https://www.ostinelli.net/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.ostinelli.net</link>
	<description>Roberto Ostinelli&#039;s blog.</description>
	<lastBuildDate>Mon, 17 Apr 2023 16:16:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2.8</generator>
	<item>
		<title>Simple Procedural Walk for UE4</title>
		<link>https://www.ostinelli.net/simple-procedural-walk-for-ue4/</link>
					<comments>https://www.ostinelli.net/simple-procedural-walk-for-ue4/#respond</comments>
		
		<dc:creator><![CDATA[Roberto Ostinelli]]></dc:creator>
		<pubDate>Mon, 26 Apr 2021 08:13:00 +0000</pubDate>
				<category><![CDATA[UE4]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[procedural]]></category>
		<category><![CDATA[simple procedural walk]]></category>
		<guid isPermaLink="false">http://www.ostinelli.net/?p=1312</guid>

					<description><![CDATA[<p>In my spare time I love dedicating some energy to Unreal Engine 4. Today, I&#8217;m releasing my first UE4 plugin, Simple Procedural Walk. I&#8217;ve had an idea for a game for quite a while (on [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/simple-procedural-walk-for-ue4/">Simple Procedural Walk for UE4</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In my spare time I love dedicating some energy to Unreal Engine 4. Today, I&#8217;m releasing my first UE4 plugin, <a href="https://www.unrealengine.com/marketplace/simple-procedural-walk" target="_blank" rel="noreferrer noopener">Simple Procedural Walk</a>.</p>



<p>I&#8217;ve had an idea for a game for quite a while (on Steam, <a href="https://store.steampowered.com/app/860020/Subtype_Grounds/" target="_blank" rel="noreferrer noopener">Subtype Grounds</a>), which basically takes you in a cooperative journey to destroy the A.I. responsible for robots taking over the world. That&#8217;s another story, but while I was animating the killer robots for this game, I found out about how procedural animations can really help in making their movement realistic and interesting, so I decided to give that a try. But&#8230; What is procedural animation?</p>



<span id="more-1312"></span>



<p>&#8220;Standard&#8221; animations are generally the result of pre-saved animations, whether these are designed by hand with tools such as Blender, or recorded by using motion capture systems. These animations are then applied to your models and played back, bringing your characters to life.</p>



<p>&#8220;Procedural&#8221; animations, on the contrary, are not pre-made or pre-recorded: everything is computed in real time based on the character position and their environment. This has numerous advantages. For instance in a walking cycle, the feet of your character will perfectly adapt to the terrain, and they will not slide in unexpected ways. They can even move apart to avoid holes in the ground, or look for places were they can properly get a foothold. The body can be moved so that it gets the proper inclination, and so on.</p>



<p>These two techniques (pre-saved and procedural) are often combined, for instance a motion-captured human walking cycle might be coupled with procedural elements so that, for instance, the feet adapt to the terrain. This gives the advantage of a more natural, organic looking cycle, while supporting the character adaptation to their surroundings. However, pre-saved animations can only be modified up to a certain point while, on the other hand, procedural animations have their own limitations (for instance, it is difficult to achieve properly looking organic movements).</p>



<p>For my robots, procedural animation is a perfect fit, since it is particularly good looking for robotic or insectoid creatures.</p>



<p>Here&#8217;s Simple Procedural Walk trailer video:</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Simple Procedural Walk Trailer" width="1170" height="658" src="https://www.youtube.com/embed/JLZa2ZsE3SE?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>It is interesting to see how much control you can have on the movement itself with procedural animations.</p>



<p>For example, it is possible to considerably change the overall feel of the movement by having the character unplant its feet at different times. This video demonstrates how a single parameter in the plugin can change the look of a walk cycle:</p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Simple Procedural Walk: Progressive Legs Movement" width="1170" height="658" src="https://www.youtube.com/embed/Lm2GpmVtZLg?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>If you are interested, you may check Simple Procedural Walk out on the <a href="https://www.unrealengine.com/marketplace/simple-procedural-walk" target="_blank" rel="noreferrer noopener">Unreal Engine 4 marketplace</a>.</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/simple-procedural-walk-for-ue4/">Simple Procedural Walk for UE4</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ostinelli.net/simple-procedural-walk-for-ue4/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>A journey to Syn v2, a better Erlang &#038; Elixir Process Registry and Group manager</title>
		<link>https://www.ostinelli.net/a-journey-to-syn-v2/</link>
					<comments>https://www.ostinelli.net/a-journey-to-syn-v2/#comments</comments>
		
		<dc:creator><![CDATA[Roberto Ostinelli]]></dc:creator>
		<pubDate>Mon, 18 Nov 2019 14:44:09 +0000</pubDate>
				<category><![CDATA[Elixir]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[cluster]]></category>
		<category><![CDATA[distributed]]></category>
		<category><![CDATA[process registry]]></category>
		<guid isPermaLink="false">http://www.ostinelli.net/?p=1036</guid>

					<description><![CDATA[<p>For those who don&#8217;t know it, Syn (short for synonym) is a global Process Registry and Process Group manager for Erlang and Elixir that I&#8217;ve written a few years back. It has been around for [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/a-journey-to-syn-v2/">A journey to Syn v2, a better Erlang &#038; Elixir Process Registry and Group manager</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>For those who don&#8217;t know it, <a href="https://github.com/ostinelli/syn">Syn</a> (short for <em>synonym</em>) is a global Process Registry and Process Group manager for Erlang and Elixir that I&#8217;ve written a few years back. It has been around for some time now, and it has served me well on many occasions. Through the years, I&#8217;ve built up a series of considerations and ideas for improvement, and now the time has come to put all of these back into Syn.</p>



<p>Syn v2 has been rewritten from the grounds up, and I&#8217;d like to share the reasoning and the architectural choices behind this work. Since Syn is written in Erlang, the few portions of the code here below will show Erlang syntax. Don&#8217;t let that discourage you if you are an Elixir developer though, as the same principles apply.</p>



<span id="more-1036"></span>



<h2 class="wp-block-heading">The analysis</h2>



<h3 class="wp-block-heading">Things I wanted to keep</h3>



<p>I&#8217;ve always treated a Process Registry as a specialized subset of a Key / Value store. Yes, the Key is the process registered name and the Value is the <code>pid()</code>, but the important bit here is that a process inherently belongs to a node &#8211; because it <em>runs there</em>. This changes the game quite significantly.</p>



<p>Contrary to standard Key / Value stores where you <em>want</em> to keep all of your data when a node leaves or crashes, why would you want to keep the name reference to a process, if that process runs on the node that left, or worse, crashed (and the process with it)? If a node gets added, why would you want to handoff a running process&#8217; registration handling to this new node (so, <em>not</em> the node the process is running on)? In general, why would you want to decouple the <em>node</em> that handles the registered name of a process from the <em>process</em> itself?</p>



<p>If your answer is that in case of nodes leaving / crashing you need to keep the process&#8217; information, then what you need is a persistent data storage, not a Process Registry. If your answer is that you want to <em>spawn</em> and <em>load balance</em> processes across a cluster (i.e. workers, that also happen to end up registered by name), then maybe what you need is a Job Queue, not necessarily a Process Registry.</p>



<p>Now, if what you want from your Process Registry is to register <em>existing</em> processes, then maybe you can agree that a <em>process</em>&#8216; registration is strongly tied to the <em>node</em> the process runs on. If the node dies, then the process dies as well, and keeping its registration name around probably doesn&#8217;t make much sense. If you agree to this, then my suggestion is that in a distributed registry every node should be handling the registration of the processes that run on it.</p>



<p>In this scenario, we have ourselves a different paradigm from the ones of standard Key / Value stores. For instance, you do not need a Hash Ring to load balance the processes in your cluster and create replicas for fault tolerance of data: if a process or the node it runs on dies, you want to let its registration name go. You could still consider using a hash ring or consensus algorithms such as Raft to register a name, because you might experience race conditions for the same name being registered simultaneously on different nodes. However, Syn takes the approach of using a similar conflict resolution mechanism used to resolve net splits for these cases.</p>



<p>This is even more true in the context where Syn was born. In <a href="https://en.wikipedia.org/wiki/Internet_of_things">IoT</a> applications, you generally have a <em>process</em> that handles an external (TCP/UDP) <em>socket</em> to a physical <em>device</em>. It only makes sense that the process runs on the same node of the socket it handles, because in this scenario (maybe it&#8217;s just me) I cannot see the sense of having an external socket being managed by a process that runs on a different node:</p>



<ul><li>If the node the socket runs on dies, you most probably want the process that handles it to die as well.</li><li>Reciprocally, if the socket handler process dies, you&#8217;d loose all of its state and you&#8217;d probably would want to disconnect the device from the socket anyway.</li></ul>



<p>To sum it up, I wanted to keep Syn v1 paradigm which is: every node is the <em>authority</em> for registering the processes that run on it. Load balancing is not part of this Process Registry, and it rather has to do with whatever causes processes to be spawned in the first place (i.e. external sockets get created on a specific node based on TCP Load Balancing mechanisms). </p>



<p>Finally, I wanted also to keep a full registry replica on every node of the cluster, so that Syn is optimized for read-intensive operations rather than write-intensive ones. This also seems to make sense, since if you do want to register a process by a name it&#8217;s probably because you want it to live long  enough for it to have an alias and keep track of it system-wide.</p>



<h3 class="wp-block-heading">Things I wanted to improve</h3>



<h4 class="wp-block-heading">1. Dynamic node membership</h4>



<p>Contrary to what some forum users seem to think, Syn v1 does manage dynamic node addition pretty well. The only caveat is that Syn v1 needs to be initialized on a node <em>after</em> a node joins a cluster: your application needs to have the logic to connect to the other nodes, and only then issue a call to <code>syn:init/0</code> which initializes the mnesia tables on the node. This is because Syn v1 uses mnesia&#8217;s replication features, and mnesia enforces a specific instructions&#8217; order when creating / adding a node to existing replicated tables.</p>



<p>For instance, the call to <a href="http://erlang.org/doc/man/mnesia.html#change_config-2">mnesia:change_config/2</a> to configure <code>extra_db_nodes</code> needs to happen before the creation of a table (via <a href="http://erlang.org/doc/man/mnesia.html#create_table-2">mnesia:create_table/2</a>) or the addition of a node to existing replicated tables (via <a href="http://erlang.org/doc/man/mnesia.html#add_table_copy-3">mnesia:add_table_copy/3</a>). If you&#8217;re curious about this process, you can head to <a href="https://github.com/ostinelli/syn/blob/1.6.3/src/syn_backbone.erl#L43">syn_backbone.erl</a> where you can get the gist on how Syn v1 sets up dynamic replication via mnesia.</p>



<p>This <em>could</em> also potentially lead to a rare race condition, which BTW never happened to me in all of these years. If two nodes of a cluster were to be started during a net split so that they do not see each other on boot, they would initialize mnesia separately which would result in them having their own version of Syn tables, with their own fingerprint, that cannot be merged afterwards (due to mnesia internals).</p>



<p>Finally, while mnesia does support node addition, it does not as easily support node removal. The equivalent command to remove a table copy, <a href="http://erlang.org/doc/man/mnesia.html#del_table_copy-2">mnesia:del_table_copy/2</a>, has some other caveats (for example it requires mnesia to be stopped). It is certainly doable though, but the question here would be <em>when</em> to do it in an automated way. For instance, a node could get notified that another node left the cluster, but should it then remove the missing node&#8217;s table copy? To do that, it would first have to stop mnesia &#8211; and lose all of the ram data &#8211; and then, what if the other node was simply down due to a temporary network failure that caused a net split? For these reasons, node removal was never implemented in Syn v1. If a node left, the local mnesia tables would still have it included in <code>extra_db_nodes</code> that mnesia uses for replication, which is not a big deal.</p>



<p>Therefore, even if Syn v1 does support dynamic node addition, there are these caveats to keep in mind &#8211; and I can see why some users might have misinterpreted them.</p>



<h4 class="wp-block-heading">2. Net Splits</h4>



<p>Even though these events are rare, they do happen. Moreover, I felt that a mechanism that would solve those would also basically implement a fully working dynamic addition / removal of nodes.</p>



<p>Mnesia does not handle net splits very well. To give some support to this issue, Ulf Wiger experimented and created his <a href="https://github.com/uwiger/unsplit">unsplit</a> framework (he talks about it in <a href="http://erlang.org/pipermail/erlang-questions/2010-February/049213.html">this post</a> on the Erlang questions mailing list). However, I&#8217;ve had mixed results with the mechanism that he uses in there.</p>



<p>Basically, Ulf&#8217;s solution works by first subscribing to mnesia events. If mnesia triggers an <em>inconsistent database</em> event for a remote node (so it is running a partitioned network), the unsplit code will check whether <a href="https://github.com/uwiger/unsplit/blob/8ab6130e6c8ef167e82f6b25f875b2bb293a1345/src/unsplit_server.erl#L143">the remote node is already part of mnesia&#8217;s local running nodes</a>. If it is not, it will manually connect the local to the remote node using the undocumented <a href="https://github.com/uwiger/unsplit/blob/8ab6130e6c8ef167e82f6b25f875b2bb293a1345/src/unsplit_server.erl#L163">mnesia_monitor:connect_nodes/1</a> method; while doing so, it will inject some custom resolution code that performs dirty read &amp; write operations to the local and to the remote node&#8217;s mnesia tables. I took this mechanism and made a specialized and simplified version for Syn v1, and if you&#8217;re up for it you can check this implementation in <a href="https://github.com/ostinelli/syn/blob/1.6.3/src/syn_consistency.erl">syn_consistency.erl</a>.</p>



<p>While it works nicely in a 2 nodes cluster, I have unfortunately encountered an inconsistent behavior in bigger clusters. In a cluster of 3 nodes I would see the inconsistent database events triggered correctly by mnesia, but unfortunately mnesia would randomly consider the remote node that triggered the event as already part of its local running nodes. Thus, the resolution part of the code that would perform the read &amp; write operations and solve the split brain situation wouldn&#8217;t get a chance to run. My hunch is that mnesia is able to reconnect to parted nodes in a way that may happen before all of this mechanism gets the chance to be called, especially in partial net splits scenarios.  I&#8217;ve also tried forcing the resolution code to run without the checks, but I got the error <code>not_merged</code> as the result of the merge fun in <code>mnesia_controller:connect_nodes/1</code>, more precisely <a href="https://github.com/ostinelli/syn/blob/1.6.3/src/syn_consistency.erl#L230">here</a>.</p>



<p>I don&#8217;t know whether this incapacity of randomly solving net splits in bigger clusters results from using mnesia&#8217;s in a non-intended way by taking advantage of Ulf&#8217;s mechanism (it seems that Ulf wanted to <a href="https://github.com/uwiger/unsplit/issues/2">test it with more than 2 nodes</a> himself), or if I might have missed something in my implementation version. That said, handling net splits is not an immediate task especially if you&#8217;re using a library that is not meant to do so. The feeling is that mnesia replication mechanisms are not intended to handle them, and hacking your way through might have unexpected results.</p>



<h4 class="wp-block-heading">3. Behavior and customization</h4>



<p>Syn v1 allows to register a process with one name at a time, a behavior consistent with Erlang&#8217;s global module. However, I felt this as a limitation and I wanted to allow multi-aliases process registration.</p>



<p>Also, I wanted a more clean approach to support customization callbacks, i.e. a single callback module with its <code>syn_event_handler</code> behavior that would be triggered depending on a developer&#8217;s choices.</p>



<p>Finally, in case of registry conflicts during a net split resolution (i.e. when two processes have registered the same name on different nodes during the net split), Syn v1 decides which process to keep and which one to discard. I wanted to provide developers with the ability of defining their own conflict resolution method. The developers could, for instance, save vector clocks data into the meta data of each process and use those to choose which process to keep in case of conflict.</p>



<h2 class="wp-block-heading">The rewrite</h2>



<p>Given all of the above, the choices were simple and clear &#8211; and so the implementation.</p>



<h4 class="wp-block-heading">Generic Register / Unregister operations&#8217; flow</h4>



<ul><li>When a registration request comes in for a Name and Pid, this request is routed to the node that the Pid is running on. In most cases, a registration request will be done from a process itself, which means that communication stays on the same node.</li><li>Every registration request is treated by a single (gen server) process (the <em>registry process</em>) on every node. This registry process starts monitoring the newly registered process, and also guarantees that registration / unregistration requests are necessarily consistent &#8211; since there&#8217;s a per-node single registry process authority that sequentially treats them.</li><li>The registry process writes to a <s>local (not replicated) mnesia table</s> (<strong>Edit in v2.1</strong>) local ETS tables the Name and related Pid. This is an in-memory only table that gets created on application start and killed on application stop. <s>I still use mnesia only because of its secondary index feature, as I need to be able to search for table entries both by Name and by Pid</s>. </li><li>The registry process then sends the registration / unregistration information to all of the other nodes in the cluster. This is not done by sending a message to the registry processes of the other nodes, rather it issues a remote procedure call (RPC) that directly writes in the local ETS tables of the remote nodes. This allows the other registry processes to be free from all intra-nodes syncing operations. </li><li><strong>Edit in v2.0.1</strong> &#8212;></li><li>When a node receives a registration information from another node, it checks locally whether there&#8217;s a name conflict. If one is found, the node will try to gain a global lock for the conflicting name to run a specific portion of code that merges the conflicting data, since other registry processes might see this conflict as well.</li><li>The registry process that succeeds in gaining the lock will compare the received registration data and will resolve the conflict between itself and the node that sent the registration information.</li><li>Once done, the registry process will free the lock, and all the other nodes that eventually experienced the same name conflict will resolve the same conflict.</li></ul>



<h4 class="wp-block-heading">Nodes&#8217; addition</h4>



<ul><li>When a node joins a cluster, all the registry processes of the nodes in the cluster receive a NODE UP event for that node. Simultaneously, the joining node&#8217;s registry process receives a NODE UP event for <em>all</em> of the existing nodes in the cluster.</li><li>Every registry process that receives a NODE UP event for a remote node will try to gain a global lock to run a specific portion of code that deals with merging remote data.</li><li>The registry process that succeeds in gaining the lock will issue a RPC on the remote node and request the registry data for all of the processes that run on it, for which the remote node is the authority. It will then write this information on its local ETS tables. If this happens after a net split, some naming conflicts might happen at this moment, and a choice on which process to keep will be made depending on the specified logic. By default, Syn will keep the local process, kill the remote process and remove the latter from the ETS tables on the remote node (via a RPC).</li><li>Once done, the registry process will free the lock. It will not send its data, as it will be requested from the other registry processes when they get their turn to grab the lock.</li><li>The code for all of this at the time of writing can be seen <a href="https://github.com/ostinelli/syn/blob/2.0.0-rc.1/src/syn_registry.erl#L242">here</a>.</li></ul>



<h4 class="wp-block-heading">Nodes&#8217; removal</h4>



<ul><li>When a node leaves a cluster (voluntarily, because it crashed or because of a net split), all the registry processes of the nodes in the cluster receive a NODE DOWN event for that node.</li><li>The registry processes that receive this event proceed to remove from their local mnesia tables all of the processes that ran on the disconnected node.</li><li>This works well also in context of partial net splits (<code>A &lt;--&gt; B &lt;--&gt; C</code> where node A can see B, B can see A and C, and C can only see B), since every node keeps locally only the information of the nodes it can see.</li></ul>



<h2 class="wp-block-heading">Results</h2>



<p>These are the results of the rewrite.</p>



<ul><li>Addition &amp; removal of nodes in a cluster is done in a completely dynamic and transparent way, with no caveats.</li><li>Automated repairing from net splits has now been taken to a new level. You can check the <a href="https://github.com/ostinelli/syn/tree/2.0.0-rc.1/test">existing test suites</a> to see what is being covered.</li><li>Finally, there now is a single callback module with the <code>syn_event_handler</code> behavior, and the ability for the developer to use a custom function to resolve naming conflicts after net splits.</li></ul>



<p>There still might be corner cases that I haven&#8217;t considered in terms of consistency, if some arise I will do my best to tackle any of those in future improvements of Syn.</p>



<p>You can grab your copy of Syn v2 on <a href="https://hex.pm/packages/syn">Hex</a> or <a href="https://github.com/ostinelli/syn/">Github</a>. Happy registering!</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/a-journey-to-syn-v2/">A journey to Syn v2, a better Erlang &#038; Elixir Process Registry and Group manager</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ostinelli.net/a-journey-to-syn-v2/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>Modern Erlang for Beginners: my course</title>
		<link>https://www.ostinelli.net/modern-erlang-for-beginners/</link>
					<comments>https://www.ostinelli.net/modern-erlang-for-beginners/#comments</comments>
		
		<dc:creator><![CDATA[Roberto Ostinelli]]></dc:creator>
		<pubDate>Wed, 13 Mar 2019 20:14:00 +0000</pubDate>
				<category><![CDATA[Elixir]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[course]]></category>
		<category><![CDATA[udemy]]></category>
		<guid isPermaLink="false">http://www.ostinelli.net/?p=1300</guid>

					<description><![CDATA[<p>I remember when I first started learning Erlang, many years ago. There was the fundamental Programming Erlang book by the late Joe Armstrong, the official Erlang documentation, some various books but what really helped me [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/modern-erlang-for-beginners/">Modern Erlang for Beginners: my course</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>I remember when I first started learning Erlang, many years ago. There was the fundamental Programming Erlang book by the late Joe Armstrong, the official  Erlang documentation, some various books but what really helped me was a screencast from Kevin Smith sold on the Pragmatic Bookshelf&#8217;s website.</p>



<p>I am a visual learner, which means that it is much easier for me to learn with videos or by pairing with other people. So, after those years I decided that I wanted to add my little contribution to the Erlang courses that available out there.</p>



<p>I&#8217;m pleased to say that as per today my course is live on <a href="https://pragprog.com/" target="_blank" rel="noreferrer noopener">The Pragmatic Bookshelf</a>. I hope it will help out those who are seeking to enter this fantastic world, or those Elixir programmers who want to understand Erlang better.</p>



<p><em>EDIT (Apr. 17, 2023): my course was retired from PragProg on Apr. 21, 2020 and moved to Udemy, but is now retired from Udemy as well.</em></p>



<p></p>



<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe loading="lazy" title="Modern Erlang for Beginners" width="1170" height="658" src="https://www.youtube.com/embed/IwhbjqsizFk?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>
</div></figure>



<p></p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/modern-erlang-for-beginners/">Modern Erlang for Beginners: my course</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ostinelli.net/modern-erlang-for-beginners/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Setting up multiple databases in Rails: the definitive guide</title>
		<link>https://www.ostinelli.net/setting-multiple-databases-rails-definitive-guide/</link>
					<comments>https://www.ostinelli.net/setting-multiple-databases-rails-definitive-guide/#comments</comments>
		
		<dc:creator><![CDATA[Roberto Ostinelli]]></dc:creator>
		<pubDate>Wed, 02 Dec 2015 18:00:34 +0000</pubDate>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[heroku]]></category>
		<category><![CDATA[multiple databases]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[schema]]></category>
		<guid isPermaLink="false">http://www.ostinelli.net/?p=893</guid>

					<description><![CDATA[<p>There are different reasons why you might consider having multiple databases in your Ruby on Rails application. In my specific case scenario, I needed to store large quantities of data representing user behavior: clicks, pages visited, historical [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/setting-multiple-databases-rails-definitive-guide/">Setting up multiple databases in Rails: the definitive guide</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>There are different reasons why you might consider having multiple databases in your Ruby on Rails application. In my specific case scenario, I needed to store large quantities of data representing user behavior: clicks, pages visited, historical changes, and so on.</p>
<p>This kind of databases generally are not mission critical, and grow much faster (and larger) than most databases. Their requirements are often different: for instance, they need more storage space, are more tolerant in the face of hardware or software failures, and are write-intensive. For these reasons, sometimes it is interesting to separate them from your application&#8217;s primary database. Often, non-RDBMS databases are chosen for these kind of tasks, something which is however beyond the scope of this article.</p>
<p>I googled and read many different solutions, however I couldn&#8217;t find one that was able to fully cover how to:</p>
<ul>
<li>Have different and isolated migrations and schemas for every database.</li>
<li>Use rails generators to create new migrations for every database, independently.</li>
<li>Offer database-specific rake tasks for the most common database operations (i.e. like the ones available for the primary database).</li>
<li>Integrate with RSpec&#8217;s default <pre class="crayon-plain-tag">spec</pre> task.</li>
<li>Work with <a href="https://github.com/DatabaseCleaner/database_cleaner">Database Cleaner</a>.</li>
<li>Work on <a href="https://www.heroku.com/">Heroku</a>.</li>
</ul>
<p>This is my take on how to solve all of these &#8211; and have a fully working multiple database solution for your Rails application.</p>
<p><span id="more-893"></span></p>
<h2>Create the custom database files</h2>
<p>For the purpose of this tutorial, we&#8217;re going to set up a second database called <em>Stats</em>. To do so, we&#8217;re going to duplicate how Rails handles the primary database, and stick to conventions.</p>
<p>First of all, create the file <pre class="crayon-plain-tag">config/database_stats.yml</pre> and populate it as you do with the primary database&#8217;s config file. Your file will look something like this:</p><pre class="crayon-plain-tag">development:
  adapter: postgresql
  encoding: utf8
  host: localhost
  pool: 10
  database: myapp_stats_development
  username: postgres
  password:

test:
  adapter: postgresql
  encoding: utf8
  host: localhost
  pool: 10
  database: myapp_stats_test
  username: postgres
  password:

production:
  adapter: postgresql
  encoding: utf8
  url:  &lt;%= ENV["DATABASE_STATS_URL"] %&gt;
  pool: &lt;%= ENV["DB_POOL"] || 5 %&gt;</pre><p>Note that I&#8217;ve given specific names to the databases, trying to follow as closely as possible Rails&#8217; naming conventions. Also, I&#8217;ve set the database production url to an environment variable <pre class="crayon-plain-tag">DATABASE_STATS_URL</pre>. This will allow us to easily set this variable to a secondary database when deploying to Heroku.</p>
<p>We&#8217;re now going to create a directory that will hold the schema and all the migrations of the Stats database, so that it will have its own files clearly isolated from the primary database. We are basically going to duplicate Rails&#8217; primary database <pre class="crayon-plain-tag">db</pre> directory.</p>
<p>Create the directory <pre class="crayon-plain-tag">db_stats</pre> in the Rails root and ensure to copy the structure and files of the primary database <pre class="crayon-plain-tag">db</pre> directory within it. You will have something like:</p><pre class="crayon-plain-tag">-- db
   |-- migrate
   schema.rb
   seeds.rb
-- db_stats
   |-- migrate
   schema.rb
   seeds.rb</pre><p>The created files <pre class="crayon-plain-tag">schema.rb</pre> and <pre class="crayon-plain-tag">seeds.rb</pre>, together with the <pre class="crayon-plain-tag">migrate</pre> directory, should just be empty.</p>
<p>&nbsp;</p>
<h2>Add Rake tasks</h2>
<p>To handle the Stats database, and allow for its creation, migrations, schema dumping and other functionalities we&#8217;re going to need custom Rake tasks. These tasks will provide us with the same functionalities that Rails provides us for the primary database.</p>
<p>Create a new file <pre class="crayon-plain-tag">lib/tasks/db_stats.rake</pre>, and paste the following:</p><pre class="crayon-plain-tag">task spec: ["stats:db:test:prepare"]

namespace :stats do

  namespace :db do |ns|

    task :drop do
      Rake::Task["db:drop"].invoke
    end

    task :create do
      Rake::Task["db:create"].invoke
    end

    task :setup do
      Rake::Task["db:setup"].invoke
    end

    task :migrate do
      Rake::Task["db:migrate"].invoke
    end

    task :rollback do
      Rake::Task["db:rollback"].invoke
    end

    task :seed do
      Rake::Task["db:seed"].invoke
    end

    task :version do
      Rake::Task["db:version"].invoke
    end

    namespace :schema do
      task :load do
        Rake::Task["db:schema:load"].invoke
      end

      task :dump do
        Rake::Task["db:schema:dump"].invoke
      end
    end

    namespace :test do
      task :prepare do
        Rake::Task["db:test:prepare"].invoke
      end
    end

    # append and prepend proper tasks to all the tasks defined here above
    ns.tasks.each do |task|
      task.enhance ["stats:set_custom_config"] do
        Rake::Task["stats:revert_to_original_config"].invoke
      end
    end
  end

  task :set_custom_config do
    # save current vars
    @original_config = {
      env_schema: ENV['SCHEMA'],
      config: Rails.application.config.dup
    }

    # set config variables for custom database
    ENV['SCHEMA'] = "db_stats/schema.rb"
    Rails.application.config.paths['db'] = ["db_stats"]
    Rails.application.config.paths['db/migrate'] = ["db_stats/migrate"]
    Rails.application.config.paths['db/seeds'] = ["db_stats/seeds.rb"]
    Rails.application.config.paths['config/database'] = ["config/database_stats.yml"]
  end

  task :revert_to_original_config do
    # reset config variables to original values
    ENV['SCHEMA'] = @original_config[:env_schema]
    Rails.application.config = @original_config[:config]
  end
end</pre><p>This needs a little explanation: let&#8217;s break up this file in its main sections. First of all, we simply provide &#8220;proxies&#8221; to standard Rails database tasks, in a newly created Rake namespace <pre class="crayon-plain-tag">stats:db</pre>:</p><pre class="crayon-plain-tag">task :drop do
  Rake::Task["db:drop"].invoke
end

task :create do
  Rake::Task["db:create"].invoke
end

task :setup do
  Rake::Task["db:setup"].invoke
end

task :migrate do
  Rake::Task["db:migrate"].invoke
end

[...]</pre><p>Then, we loop all of these tasks, and ensure the task <pre class="crayon-plain-tag">stats:set_custom_config</pre>  is run <em>before</em> and the task <pre class="crayon-plain-tag">stats:revert_to_original_config</pre>  <em>after</em> every one of the &#8220;proxy&#8221; tasks:</p><pre class="crayon-plain-tag"># append and prepend proper tasks to all tasks defined in stats:db namespace
ns.tasks.each do |task|
  task.enhance ["stats:set_custom_config"] do
    Rake::Task["stats:revert_to_original_config"].invoke
  end
end</pre><p>We have to do this since, unfortunately, Rails support for multiple databases isn&#8217;t that great, hence we need to provide minor hacks to make everything work. For this reason we have to set specific environment and configuration variables to custom values which match our Stats database before we run the &#8220;proxy&#8221; tasks, and then ensure that the original values are set back once those tasks have been run. The following two tasks do just that:</p><pre class="crayon-plain-tag">task :set_custom_config do
  # save current vars
  @original_config = {
    env_schema: ENV['SCHEMA'],
    config: Rails.application.config.dup
  }

  # set config variables for custom database
  ENV['SCHEMA'] = "db_stats/schema.rb"
  Rails.application.config.paths['db'] = ["db_stats"]
  Rails.application.config.paths['db/migrate'] = ["db_stats/migrate"]
  Rails.application.config.paths['db/seeds'] = ["db_stats/seeds.rb"]
  Rails.application.config.paths['config/database'] = ["config/database_stats.yml"]
end

task :revert_to_original_config do
  # reset config variables to original values
  ENV['SCHEMA'] = @original_config[:env_schema]
  Rails.application.config = @original_config[:config]
end</pre><p>Notice how the lines 9-13 set values to the files and directories we have created in the previous steps.</p>
<p>Finally, if you&#8217;re using RSpec you can add one dependency to the <pre class="crayon-plain-tag">spec</pre> task, to ensure that the Stats database is automatically prepared when tests are run:</p><pre class="crayon-plain-tag">task spec: ["stats:db:test:prepare"]</pre><p>Once all of this is set up, we can create the Stats database and run its first migration:</p><pre class="crayon-plain-tag">$ rake stats:db:create
$ rake stats:db:migrate</pre><p>This will generate the Stats database schema file in <pre class="crayon-plain-tag">db_stats/schema.rb</pre>.</p>
<p>&nbsp;</p>
<h2>Add a custom generator</h2>
<p>Unfortunately, we cannot simply use Rails&#8217; generator <pre class="crayon-plain-tag">ActiveRecord::Generators::MigrationGenerator</pre> because it <a href="https://github.com/rails/rails/blob/v4.2.4/activerecord/lib/rails/generators/active_record/migration/migration_generator.rb#L11">hardcodes the parent directory of the migration</a> (notice the path hardcoded to the directory <pre class="crayon-plain-tag">db/migrate</pre> in line 4 here below):</p><pre class="crayon-plain-tag">def create_migration_file
  set_local_assigns!
  validate_file_name!
  migration_template @migration_template, "db/migrate/#{file_name}.rb"
end</pre><p>Therefore, we need to have a custom generator to create migrations for the Stats database. <span style="line-height: 1.5;">However, we can still inherit from it and monkey patch this specific function. Create the following generator in </span><pre class="crayon-plain-tag">lib/generators/stats_migration_generator.rb</pre><span style="line-height: 1.5;">:</span></p><pre class="crayon-plain-tag">require 'rails/generators/active_record/migration/migration_generator'

class StatsMigrationGenerator &lt; ActiveRecord::Generators::MigrationGenerator
  source_root File.join(File.dirname(ActiveRecord::Generators::MigrationGenerator.instance_method(:create_migration_file).source_location.first), "templates")

  def create_migration_file
    set_local_assigns!
    validate_file_name!
    migration_template @migration_template, "db_stats/migrate/#{file_name}.rb"
  end
end</pre><p>In line 9 we set the directory base to the Stats database directory. Also, in line 4 we initialize the templates directory and point it at the original one used by the generator we&#8217;re inheriting from.</p>
<p>With all of this in place, we can now generate migrations for the Stats database:</p><pre class="crayon-plain-tag">$ rails g stats_migration create_clicks
      create  db_stats/migrate/20151201191642_create_clicks.rb</pre><p>You&#8217;ll notice that the migration file gets created in the Stats database migrate directory <pre class="crayon-plain-tag">db_stats/migrate</pre>. You can edit this file and then run your migrations with the Rake task that we&#8217;ve set up in the previous steps, just as you normally would do with your primary database:</p><pre class="crayon-plain-tag">$ rake stats:db:migrate</pre><p>&nbsp;</p>
<h2>Finalize connection and models</h2>
<p>We&#8217;re almost done. Add a new initializer file <pre class="crayon-plain-tag">config/initializers/db_stats.rb</pre>  and paste the following:</p><pre class="crayon-plain-tag"># save stats database settings in global var
DB_STATS = YAML::load(ERB.new(File.read(Rails.root.join("config","database_stats.yml"))).result)[Rails.env]</pre><p>Notice that we reference the Stats database configuration file that we created in the first step here above. By doing this, we initialise a global variable DB_STATS that holds the current environment&#8217;s configuration of the Stats database.</p>
<p>Finally, we can set our models&#8217; connection to this configuration. For example, let&#8217;s say that we have a <pre class="crayon-plain-tag">Click</pre>  model that corresponds to the migration here above. All you have to do is add one extra line that specifies which connection to use:</p><pre class="crayon-plain-tag">class Click &lt; ActiveRecord::Base
  establish_connection DB_STATS

end</pre><p>It&#8217;s that easy. Your model will now use the database Stats.</p>
<p>If you have multiple models that need to connect to the Stats database, however, you will need to add an extra step. If you were to have another model establishing its own connection to the Stats database, it would have its own connection pool and you might risk getting out of available connections to your Stats database. Therefore, if you have multiple models it is recommended to inherit from a single model, so that all the models connecting to the Stats database will share the same connection pool.</p>
<p>To do so, create the base model that connects to the Stats database:</p><pre class="crayon-plain-tag">class StatsBase &lt; ActiveRecord::Base
  establish_connection DB_STATS
  self.abstract_class = true
end</pre><p>You can now inherit in all your models:</p><pre class="crayon-plain-tag">class Click &lt; StatsBase
end

class View &lt; StatsBase
end</pre><p>&nbsp;</p>
<h2>Heroku</h2>
<p>As already anticipated, the last step that you need to make this work on Heroku is to set the environment variable <pre class="crayon-plain-tag">DATABASE_STATS_URL</pre>  to the database you want to use as Stats. For example, if you created a second database called <pre class="crayon-plain-tag">HEROKU_POSTGRESQL_TEAL_URL</pre> all you have to do is to set this database&#8217;s value using the <a href="https://toolbelt.heroku.com/">Heroku toolbelt</a>:</p><pre class="crayon-plain-tag">$ heroku config:set DATABASE_STATS_URL=postgres://gsdfjrthjsnaew:gry6OJF6drDjththjkSDngldsf@ec2-116-22-114-221.compute-1.amazonaws.com:5432/hmsrthj24dfgks</pre><p>And you&#8217;re ready to go.</p>
<p>&nbsp;</p>
<h2>Bonus: DatabaseCleaner</h2>
<p>If you&#8217;re using the <a href="https://github.com/DatabaseCleaner/database_cleaner">DatabaseCleaner</a> gem, you can set it to clean the models that use the Stats database too. For example, your <pre class="crayon-plain-tag">spec/rails_helper.rb</pre> may look something like this:</p><pre class="crayon-plain-tag">ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.use_transactional_fixtures = false
  config.infer_spec_type_from_file_location!

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
    DatabaseCleaner[:active_record, { model: Click }].clean_with(:truncation)
  end

  config.before(:each) do |example|
    unit_test = ![:feature, :request].include?(example.metadata[:type])
    strategy = unit_test ? :transaction : :truncation

    DatabaseCleaner.strategy = strategy
    DatabaseCleaner[:active_record, { model: Click }].strategy = strategy

    DatabaseCleaner.start
    DatabaseCleaner[:active_record, { model: Click }].start
  end

  config.after(:each) do
    DatabaseCleaner.clean
    DatabaseCleaner[:active_record, { model: Click }].clean
  end
end</pre><p>According to DatabaseCleaner <a href="https://github.com/DatabaseCleaner/database_cleaner/blob/master/README.markdown">README</a>, it should be possible to set a <em>connection</em> option instead of the <em>model </em>one. Unfortunately, my attempts at this have been unsuccessful. If anyone knows how to do this and avoid specifying a DatabaseCleaner strategy for every model, please let me know.</p>
<p>&nbsp;</p>
<p>I hope you&#8217;ve enjoyed reading this, and that my ramblings can be helpful to someone going down this same path. As usual, any suggestions on how to improve any of this are warmly welcome.</p>
<p>Happy multiple db&#8217;ing! :)</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/setting-multiple-databases-rails-definitive-guide/">Setting up multiple databases in Rails: the definitive guide</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ostinelli.net/setting-multiple-databases-rails-definitive-guide/feed/</wfw:commentRss>
			<slash:comments>54</slash:comments>
		
		
			</item>
		<item>
		<title>An evaluation of Erlang global process registries: meet Syn</title>
		<link>https://www.ostinelli.net/an-evaluation-of-erlang-global-process-registries-meet-syn/</link>
					<comments>https://www.ostinelli.net/an-evaluation-of-erlang-global-process-registries-meet-syn/#comments</comments>
		
		<dc:creator><![CDATA[Roberto Ostinelli]]></dc:creator>
		<pubDate>Mon, 06 Jul 2015 09:44:43 +0000</pubDate>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[cpg]]></category>
		<category><![CDATA[distributed process registry]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[gproc]]></category>
		<category><![CDATA[internet of everything]]></category>
		<category><![CDATA[Internet Of Things]]></category>
		<category><![CDATA[ioe]]></category>
		<category><![CDATA[IoT]]></category>
		<category><![CDATA[pg2]]></category>
		<category><![CDATA[process registry]]></category>
		<category><![CDATA[registry]]></category>
		<category><![CDATA[syn]]></category>
		<guid isPermaLink="false">http://www.ostinelli.net/?p=795</guid>

					<description><![CDATA[<p>Due to my personal interests and history, I often find myself building applications in field of the Internet Of Things. Most of the times I end up using Erlang: it is based on the Actor&#8217;s Model and is an [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/an-evaluation-of-erlang-global-process-registries-meet-syn/">An evaluation of Erlang global process registries: meet Syn</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Due to my personal interests and history, I often find myself building applications in field of the <a href="https://en.wikipedia.org/wiki/Internet_of_Things">Internet Of Things</a>. Most of the times I end up using <a href="http://www.erlang.org/">Erlang</a>: it is based on the <a href="https://en.wikipedia.org/wiki/Actor_model">Actor&#8217;s Model</a> and is an ideological (and practical) perfect match to manage IoT interactions.</p>
<p>I recently built an application where devices can connect to, and interact with each other. Every device is identified via a unique ID (its serial number) and based on this ID the devices can send and receive messages. Nothing new here: it&#8217;s a standard messaging platform, which supports a custom protocol.</p>
<p>Due to the large amount of devices that I needed to support, this application runs on a cluster of Erlang nodes. Once a device connects to one of those nodes, the related TCP socket events are handled by a process running on that node. To send a message to a specific device, you send a message to the process that handles the devices&#8217;s TCP socket.</p>
<p>While building this application, I was early in the process faced with a very common problem: I needed a g<em>lobal process registry</em> that would allow me to globally register a process based on its serial number, so that messages can be sent from anywhere in the cluster. This registry would need to have the following main characteristics:</p>
<ul>
<li>Distributed.</li>
<li>Fast write speeds (&gt;10,000 / sec).</li>
<li>Handle naming conflict resolution.</li>
<li>Allow for adding/removal of nodes.</li>
</ul>
<p>Therefore I started to search for possible solutions (which included posting to the <a href="http://erlang.org/pipermail/erlang-questions/2015-February/083175.html">Erlang Questions mailing list</a>), and these came out as my options:</p>
<ul>
<li>Erlang&#8217;s <pre class="crayon-plain-tag">global</pre>  module.</li>
<li>Erlang&#8217;s <pre class="crayon-plain-tag">pg2</pre>  module.</li>
<li><a href="https://github.com/uwiger/gproc">Gproc</a>.</li>
<li><a href="https://github.com/okeuday/cpg">CloudI Process Groups</a>.</li>
<li>Roll out a custom solution.</li>
</ul>
<p><span id="more-795"></span></p>
<h2>The Stress Test</h2>
<p>I decided to evaluate every one of these solutions based on a variety of considerations. However, I also wanted to see how they would perform when submitted to some kind of a stress test. Therefore, I defined and wrote a simple one that:</p>
<ol>
<li><em>Launches</em> a certain number of processes per node (for example, 25,000 processes per node).</li>
<li><em>Registers</em> these processes (25,000 processes per node), each with a globally unique Key.</li>
<li>Waits for those Keys to be <em>propagated</em> to all the nodes.</li>
<li><em>Unregisters</em> all of these processes.</li>
<li>Waits for those Keys to be <em>removed</em> from all the nodes.</li>
<li><em>Re-registers</em> all of the processes, to check for unwanted effects of subsequent add/remove operations.</li>
<li>Again, waits for those Keys to be <em>propagated</em> to all the nodes.</li>
<li><em>Kills</em> all the processes (this time, without previously unregistering them).</li>
<li>Waits for those Keys to be removed from all the nodes (to check for <em>process monitoring</em>).</li>
</ol>
<p>The test measures how long each one of these steps takes.</p>
<p>The following is the code for this stress test. You can see that it defines a behaviour: this is to implement callback modules that match the different syntax used by the different libraries.</p><pre class="crayon-plain-tag">-module(process_registry_bench).

-export([start/3]).
-export([register/2, unregister/2]).
-export([register_on_node/2, unregister_on_node/2]).

-callback init() -&gt; term().
-callback register(Key :: string(), pid()) -&gt; term().
-callback unregister(Key :: string(), pid()) -&gt; term().
-callback retrieve(Key :: string()) -&gt; pid() | undefined.
-callback process_loop() -&gt; any().

-define(MAX_RETRIEVE_WAITING_TIME, 60000).


start(CallbackModule, ProcessesCount, Nodes) -&gt;
	%% connect
	connect_nodes(Nodes),

	%% callback init
	CallbackModule:init(),

	%% launch processes
	{UpperKey, PidInfos} = launch_processes(CallbackModule, ProcessesCount),

	%% benchmark: register
	{TimeReg, _} = timer:tc(?MODULE, register, [CallbackModule, PidInfos]),
	io:format("Registered processes in ~p sec, at a rate of ~p/sec~n", [
		TimeReg/1000000,
		ProcessesCount/TimeReg*1000000
	]),

	%% benchmark: registration propagation
	{RetrievedInMs1, RetrieveProcess1} = retrieve(pid, CallbackModule, UpperKey),
	io:format("Check that process with Key ~p was found: ~p in ~p ms~n", [
		UpperKey, RetrieveProcess1, RetrievedInMs1
	]),

	%% benchmark: unregister
	{TimeUnreg, _} = timer:tc(?MODULE, unregister, [CallbackModule, PidInfos]),
	io:format("Unregistered processes in ~p sec, at a rate of ~p/sec~n", [
		TimeUnreg/1000000,
		ProcessesCount/TimeUnreg*1000000
	]),

	%% benchmark: unregistration propagation
	{RetrievedInMs2, RetrieveProcess2} = retrieve(undefined, CallbackModule, UpperKey),
	io:format("Check that process with Key ~p was NOT found: ~p in ~p ms~n", [
		UpperKey, RetrieveProcess2, RetrievedInMs2
	]),

	%% benchmark: re-registering
	{TimeReg2, _} = timer:tc(?MODULE, register, [CallbackModule, PidInfos]),
	io:format("Re-registered processes in ~p sec, at a rate of ~p/sec~n", [
		TimeReg2/1000000,
		ProcessesCount/TimeReg2*1000000
	]),

	%% benchmark: re-registration propagation
	{RetrievedInMs3, RetrieveProcess3} = retrieve(pid, CallbackModule, UpperKey),
	io:format("Check that process with Key ~p was found: ~p in ~p ms~n", [
		UpperKey, RetrieveProcess3, RetrievedInMs3
	]),

	%% benchmark: monitoring
	io:format("Kill all processes~n", []),
	kill_processes(PidInfos),
	{RetrievedInMs4, RetrieveProcess4} = retrieve(undefined, CallbackModule, UpperKey),
	io:format("Check that process with Key ~p was NOT found: ~p in ~p ms~n", [
		UpperKey, RetrieveProcess4, RetrievedInMs4
	]).

connect_nodes(Nodes) -&gt;
    [true = net_kernel:connect_node(Node) || Node &lt;- Nodes].

launch_processes(CallbackModule, ProcessesCount) -&gt;
	%% return the processes info in format [{Node, [{Key, Pid}]}, ...]
	Nodes = [node() | nodes()],
	ProcessesPerNode = round(ProcessesCount / length(Nodes)),
	UpperKey = integer_to_list(ProcessesPerNode * length(Nodes)),
	F = fun(Node, Acc) -&gt;
		StartingKey = length(Acc) * ProcessesPerNode,
		Pids = launch_processes_on_node(CallbackModule, ProcessesPerNode, StartingKey, Node),
		[{Node, Pids} | Acc]
	end,
	{UpperKey, lists:foldl(F, [], Nodes)}.
launch_processes_on_node(CallbackModule, ProcessesPerNode, StartingKey, Node) -&gt;
	%% return the key and process in a list of format [{Key, Pid}, ...]
	Seq = [
		integer_to_list(Key)
		|| Key &lt;- lists:seq(StartingKey + 1, ProcessesPerNode + StartingKey)
	],
	[{Key, spawn(Node, CallbackModule, process_loop, [])} || Key &lt;- Seq].

register(CallbackModule, PidInfos) -&gt;
	%% register in parallel on all nodes
	F = fun({Node, NodePidInfos}, Acc) -&gt;
		RpcKey = rpc:async_call(Node, ?MODULE, register_on_node, [
			CallbackModule, NodePidInfos
		]),
		[{Node, RpcKey} | Acc]
	end,
	RpcKeys = lists:foldl(F, [], PidInfos),
	%% wait for registration to complete on all nodes
	FResult = fun({Node, RpcKey}) -&gt;
		Registered = rpc:yield(RpcKey),
		io:format("Registered ~p processes on node ~p~n", [Registered, Node])
	end,
	lists:foreach(FResult, RpcKeys).
register_on_node(CallbackModule, NodePidInfos) -&gt;
	F = fun({Key, Pid}) -&gt;
		CallbackModule:register(Key, Pid)
	end,
	lists:foreach(F, NodePidInfos),
	length(NodePidInfos).

retrieve(Expected, CallbackModule, Key) -&gt;
	StartTime = epoch_time_ms(),
	retrieve(Expected, CallbackModule, Key, StartTime).
retrieve(pid, CallbackModule, Key, StartTime) -&gt;
	%% wait for a pid to be returned
	case CallbackModule:retrieve(Key) of
		undefined -&gt;
			timer:sleep(50),
			case epoch_time_ms() &gt; StartTime + ?MAX_RETRIEVE_WAITING_TIME of
				true -&gt; {error, timeout_during_retrieve};
				false -&gt; retrieve(pid, CallbackModule, Key, StartTime)
			end;
		{error, Error} -&gt;
			{error, Error};
		Pid -&gt;
			RetrievedInMs = epoch_time_ms() - StartTime,
			{RetrievedInMs, Pid}
	end;
retrieve(undefined, CallbackModule, Key, StartTime) -&gt;
	%% wait for undefined to be returned
	case CallbackModule:retrieve(Key) of
		undefined -&gt;
			RetrievedInMs = epoch_time_ms() - StartTime,
			{RetrievedInMs, undefined};
		{error, Error} -&gt;
			{error, Error};
		_Pid -&gt;
			timer:sleep(50),
			case epoch_time_ms() &gt; StartTime + ?MAX_RETRIEVE_WAITING_TIME of
				true -&gt; {error, timeout_during_retrieve};
				false -&gt; retrieve(undefined, CallbackModule, Key, StartTime)
			end
	end.

unregister(CallbackModule, PidInfos) -&gt;
	%% unregister in parallel on all nodes
	F = fun({Node, NodePidInfos}, Acc) -&gt;
		RpcKey = rpc:async_call(Node, ?MODULE, unregister_on_node, [
		CallbackModule, NodePidInfos
		]),
		[{Node, RpcKey} | Acc]
	end,
	RpcKeys = lists:foldl(F, [], PidInfos),
	%% wait for unregistration to complete on all nodes
	FResult = fun({Node, RpcKey}) -&gt;
		Unregistered = rpc:yield(RpcKey),
		io:format("Unregistered ~p processes on node ~p~n", [Unregistered, Node])
	end,
	lists:foreach(FResult, RpcKeys).
unregister_on_node(CallbackModule, NodePidInfos) -&gt;
	F = fun({Key, Pid}) -&gt;
		CallbackModule:unregister(Key, Pid)
	end,
	lists:foreach(F, NodePidInfos),
	length(NodePidInfos).

kill_processes(PidInfos) -&gt;
	F = fun({_Node, NodePidInfos}) -&gt;
		[exit(Pid, kill) || {_Key, Pid} &lt;- NodePidInfos]
	end,
	lists:foreach(F, PidInfos).

epoch_time_ms() -&gt;
    {Mega, Sec, Micro} = os:timestamp(),
    (Mega * 1000000 + Sec) * 1000 + round(Micro / 1000).</pre><p>To run this stress test:</p><pre class="crayon-plain-tag">process_registry_bench:start(CallbackModule, ProcessCount, Nodes).</pre><p>For instance, to launch it with the callback module <pre class="crayon-plain-tag">global_bench</pre> for 100,000 processes running on a cluster of 4 nodes <pre class="crayon-plain-tag">['1@127.0.0.1', '2@127.0.0.1', '3@127.0.0.1', '4@127.0.0.1']</pre>:</p><pre class="crayon-plain-tag">process_registry_bench:start(global_bench, 100000, [
    '1@127.0.0.1',
    '2@127.0.0.1',
    '3@127.0.0.1',
    '4@127.0.0.1'
]).</pre><p>Running this test returns an output similar to:</p><pre class="crayon-plain-tag">Registered 25000 processes on node '1@127.0.0.1'
Registered 25000 processes on node '2@127.0.0.1'
Registered 25000 processes on node '3@127.0.0.1'
Registered 25000 processes on node '4@127.0.0.1'
Registered processes in 6.385835 sec, at a rate of 15659.659230155492/sec
Check that process with Key "100000" was found: &lt;6218.25065.0&gt; in 0 ms
Unregistered 25000 processes on node '1@127.0.0.1'
Unregistered 25000 processes on node '2@127.0.0.1'
Unregistered 25000 processes on node '3@127.0.0.1'
Unregistered 25000 processes on node '4@127.0.0.1'
Unregistered processes in 4.481706 sec, at a rate of 22312.93172733776/sec
Check that process with Key "100000" was NOT found: undefined in 0 ms
Registered 25000 processes on node '1@127.0.0.1'
Registered 25000 processes on node '2@127.0.0.1'
Registered 25000 processes on node '3@127.0.0.1'
Registered 25000 processes on node '4@127.0.0.1'
Re-registered processes in 4.943493 sec, at a rate of 20228.611631492146/sec
Check that process with Key "100000" was found: &lt;6218.25065.0&gt; in 0 ms
Kill all processes
Check that process with Key "100000" was NOT found: undefined in 0 ms
ok</pre><p></p>
<h2></h2>
<h2>The Process Registry Libraries</h2>
<p>The following are the considerations that I made for every solution.</p>
<h3>1. Erlang&#8217;s native global module</h3>
<h4>Considerations</h4>
<p>The Erlang global module has native functionalities to support a global process registry. I was not particularly attracted to it, because:</p>
<ul>
<li>I always think that this module should be used to identify application&#8217;s long-running services.</li>
<li>I didn&#8217;t know if millions of entries can be supported. This module wasn&#8217;t built with my use case in mind: as per my previous point, it is generally used to register long-running processes.</li>
<li>It has a locking mechanism to ensure that the registration is atomic. I felt this could become a serious bottleneck to the registration of processes.</li>
</ul>
<p>However, this is a native Erlang module, which also allows to define a resolve function to be used for conflict resolution (i.e. in case of race conditions, or during net splits, when a Key gets registered simultaneously on two different nodes). It is able to satisfy the distributed requirements out of the box, with no need for additional libraries.</p>
<h4>Stress Test</h4>
<p>I gave it a go at my stress test, with the following callback module:</p><pre class="crayon-plain-tag">-module(global_bench).
-behaviour(process_registry_bench).

-export([init/0]).
-export([register/2, unregister/2]).
-export([retrieve/1]).
-export([process_loop/0]).

init() -&gt;
	ok.

register(Key, Pid) -&gt;
	yes = global:register_name(Key, Pid).

unregister(Key, _Pid) -&gt;
	global:unregister_name(Key).

retrieve(Key) -&gt;
	global:whereis_name(Key).

process_loop() -&gt;
	receive
		_ -&gt; ok
	end.</pre><p>Note that <pre class="crayon-plain-tag">process_loop</pre> (which is the loop running in the processes) does nothing, except keeping the process alive.</p>
<p>The results of the stress test are:</p>
<p><div class="table-responsive"><table  style="width:100%; "  class="easy-table easy-table-default " border="0">
<thead>
<tr><th  style="text-align:left" ></th>
<th  style="text-align:right" >1 Node</th>
<th  style="text-align:right" >2 Nodes</th>
<th  style="text-align:right" >3 Nodes</th>
<th  style="text-align:right" >4 Nodes</th>
</tr>
</thead>
<tbody>
<tr><td  style="text-align:left" >Reg / second</td>
<td  style="text-align:right" >27,233</td>
<td  style="text-align:right" >2,673</td>
<td  style="text-align:right" >1,997</td>
<td  style="text-align:right" >1,579</td>
</tr>

<tr><td  style="text-align:left" >Retrieve registered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Unreg / second</td>
<td  style="text-align:right" >29,491</td>
<td  style="text-align:right" >2,908</td>
<td  style="text-align:right" >2,206</td>
<td  style="text-align:right" >1,596</td>
</tr>

<tr><td  style="text-align:left" >Retrieve unregistered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Re-Reg / second</td>
<td  style="text-align:right" >27,149</td>
<td  style="text-align:right" >2,993</td>
<td  style="text-align:right" >2,131</td>
<td  style="text-align:right" >2,542</td>
</tr>

<tr><td  style="text-align:left" >Retrieve re-registered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Retrieve Key of killed Pid (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" ><strong>timeout</strong></td>
<td  style="text-align:right" ><strong>timeout</strong></td>
<td  style="text-align:right" ><strong>timeout</strong></td>
</tr>
</tbody></table></div></p>
<h4>Conclusions</h4>
<ul>
<li>The locking mechanism heavily influences the decrease in performance that can be seen when adding nodes. With a cluster of 2+ nodes we already are under the spec of 10,000 registrations / second.</li>
<li>The monitoring of processes is slow. After having killed all the processes, in a cluster of 2+ nodes it takes more than 60 seconds to have <pre class="crayon-plain-tag">global:whereis_name/1</pre> return <pre class="crayon-plain-tag">undefined</pre> (this is what <em>timeout</em> means in the table here above). I had to decrease the number of processes to around 80,000 to have the stress test pass in a cluster of 4 nodes, and it would take around 55 seconds for a killed process&#8217; Key to be removed from the registry.</li>
</ul>
<p>For these reasons, it didn&#8217;t look like I could use this module.</p>
<p>&nbsp;</p>
<h3>2. Erlang&#8217;s native pg2 module</h3>
<h4>Considerations</h4>
<p>Erlang pg2 module has native functionalities to support a global process registry. I was not particularly attracted to it, because:</p>
<ul>
<li>This library handles Process Groups, which is very different from handling unique Registered Names. We can use it for our purpose though, by basically creating Groups with a single entry. These groups are named according to our Keys, and every Group has a single entry: the Pid that we are registering. This is kind of a trick, but it&#8217;s not a showstopper.</li>
<li>Having Process Groups basically means that conflict resolution isn&#8217;t covered. If two processes are registered on different nodes with the same Key (because of race conditions or during a net split) this will result in having a Process Group with two elements instead of one. Sometimes this is fine; however, I wanted to ensure that there would be a clearly identified single Pid per device in the whole system. Not a showstopper either, but a turn-off.</li>
<li>I didn&#8217;t know if millions of entries can be supported. This module wasn&#8217;t built with my use case in mind.</li>
<li>Here too, it has a locking mechanism to ensure that the registration is atomic which could become a bottleneck to the registration of processes.</li>
</ul>
<h4>Stress Test</h4>
<p>Here&#8217;s the callback module:</p><pre class="crayon-plain-tag">-module(pg2_bench).
-behaviour(process_registry_bench).

-export([init/0]).
-export([register/2, unregister/2]).
-export([retrieve/1]).
-export([process_loop/0]).

init() -&gt;
	ok.

register(Key, Pid) -&gt;
	ok = pg2:create(Key), %% create group
	ok = pg2:join(Key, Pid). %% add pid

unregister(Key, _Pid) -&gt;
	ok = pg2:delete(Key).

retrieve(Key) -&gt;
	case pg2:get_members(Key) of
		{error, {no_such_group, Key}} -&gt; undefined;
		[] -&gt; undefined;
		[Pid] -&gt; Pid
	end.

process_loop() -&gt;
	receive
		_ -&gt; ok
	end.</pre><p>The results of the stress test are:</p>
<p><div class="table-responsive"><table  style="width:100%; "  class="easy-table easy-table-default " border="0">
<thead>
<tr><th  style="text-align:left" ></th>
<th  style="text-align:right" >1 Node</th>
<th  style="text-align:right" >2 Nodes</th>
<th  style="text-align:right" >3 Nodes</th>
<th  style="text-align:right" >4 Nodes</th>
</tr>
</thead>
<tbody>
<tr><td  style="text-align:left" >Reg / second</td>
<td  style="text-align:right" >25,062</td>
<td  style="text-align:right" >3,823</td>
<td  style="text-align:right" >2,914</td>
<td  style="text-align:right" >1,862</td>
</tr>

<tr><td  style="text-align:left" >Retrieve registered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Unreg / second</td>
<td  style="text-align:right" >39,522</td>
<td  style="text-align:right" >6,903</td>
<td  style="text-align:right" >5,191</td>
<td  style="text-align:right" >3,425</td>
</tr>

<tr><td  style="text-align:left" >Retrieve unregistered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td ></td>
</tr>

<tr><td  style="text-align:left" >Re-Reg / second</td>
<td  style="text-align:right" >25,701</td>
<td  style="text-align:right" >3,794</td>
<td  style="text-align:right" >2,783</td>
<td  style="text-align:right" >1,817</td>
</tr>

<tr><td  style="text-align:left" >Retrieve re-registered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Retrieve Key of killed Pid (ms)</td>
<td  style="text-align:right" ><strong>timeout</strong></td>
<td  style="text-align:right" ><strong>timeout</td>
<td  style="text-align:right" ><strong>timeout</strong></td>
<td  style="text-align:right" ><strong>timeout</strong></strong></td>
</tr>
</tbody></table></div></p>
<h4>Conclusions</h4>
<ul>
<li>The locking mechanism heavily influences the decrease in performance that can be seen when adding nodes. With a cluster of 2+ nodes we already are under the spec of 10,000 registrations / second.</li>
<li>The monitoring of processes is slow. After having killed all the processes, even on a single nodes it takes more than 60 seconds to have <pre class="crayon-plain-tag">pg2:get_members/1</pre> return that the group no longer exits. I had to decrease the number of processes to around 45,000 to have the stress test pass in a cluster of 4 nodes, and it would take a little less than 60 seconds for a killed process&#8217; Key to be removed from the registry.</li>
</ul>
<p>For these reasons, it didn&#8217;t look like I could use this module.</p>
<p>&nbsp;</p>
<h3>3. Gproc</h3>
<h4>Considerations</h4>
<p><a href="https://github.com/uwiger/gproc">gproc</a> is a well-known process registry which is normally used for the additional features that it provides on top of Erlang&#8217;s native process dictionary (for instance, it is able to provide pub/sub patterns). It is a solid and well-supported library, and you can often see Ulf Wiger (one of the library&#8217;s authors) generously providing support for it.</p>
<p>However, there were some concerns I had:</p>
<ul>
<li>For the distributed part it relies on <pre class="crayon-plain-tag">gen_leader</pre>, on which I&#8217;ve heard too many horror stories (maybe that&#8217;s not a thing anymore). Ulf pointed me to a gproc <a href="https://github.com/uwiger/gproc/tree/uw-locks_leader">branch that uses locks_leader</a>, where he is mainly concentrating his efforts for gproc&#8217;s support for distributed operations.</li>
<li>I felt that the main purpose of this library is not to provide a <em>distributed</em> process registry as much as <em>extending</em> the existing Erlang registration mechanisms with some additional features. The README in gproc&#8217;s Github page clearly depicts it as being an &#8220;Extended process dictionary&#8221;; it just felt that the distributed part hasn&#8217;t been the primary focus in the development of this library.</li>
<li>I could not understand how conflict resolution is managed in a distributed environment.</li>
</ul>
<h4>Stress Test</h4>
<p>Here&#8217;s the callback module:</p><pre class="crayon-plain-tag">-module(gproc_bench).
-behaviour(process_registry_bench).

-export([init/0]).
-export([register/2, unregister/2]).
-export([retrieve/1]).
-export([process_loop/0]).

init() -&gt;
	%% start app on every node
	Nodes = [node() | nodes()],
	F = fun(Node) -&gt;
		rpc:call(Node, application, ensure_all_started, [gproc]),
		rpc:call(Node, gproc_dist, start_link, [Nodes])
	end,
	lists:foreach(F, Nodes).

register(Key, Pid) -&gt;
	Pid ! {self(), reg, Key},
	receive
		done -&gt; ok
	end.

unregister(Key, Pid) -&gt;
	Pid ! {self(), unreg, Key},
	receive
		done -&gt; ok
	end.

retrieve(Key) -&gt;
	case catch gproc:lookup_pid({n, g, Key}) of
		{'EXIT', _} -&gt; undefined;
		Pid -&gt; Pid
	end.

process_loop() -&gt;
	receive
		{Sender, reg, Key} -&gt;
			gproc:reg({n, g, Key}, ignored),
			Sender ! done,
			process_loop();
		{Sender, unreg, Key} -&gt;
			gproc:unreg({n, g, Key}),
			Sender ! done,
			process_loop()
	end.</pre><p>Note: in gproc, to ensure thread safety, a process can only set its own values. That&#8217;s why the <pre class="crayon-plain-tag">register/2</pre> and <pre class="crayon-plain-tag">unregister/2</pre> callbacks here above send messages to the processes, which then register or unregister themselves (see <pre class="crayon-plain-tag">process_loop</pre>). As you can see here above I&#8217;ve decided to provide a locking call for these functions (by using a <pre class="crayon-plain-tag">receive</pre> block), to emulate the locking calls that I&#8217;ve used in the other libraries.</p>
<p>The results of the stress test are:</p>
<p><div class="table-responsive"><table  style="width:100%; "  class="easy-table easy-table-default " border="0">
<thead>
<tr><th  style="text-align:left" ></th>
<th  style="text-align:right" >1 Node</th>
<th  style="text-align:right" >2 Nodes</th>
<th  style="text-align:right" >3 Nodes</th>
<th  style="text-align:right" >4 Nodes</th>
</tr>
</thead>
<tbody>
<tr><td  style="text-align:left" >Reg / second</td>
<td  style="text-align:right" >67,011</td>
<td  style="text-align:right" >19,111</td>
<td  style="text-align:right" >22,048</td>
<td  style="text-align:right" >15,659</td>
</tr>

<tr><td  style="text-align:left" >Retrieve registered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Unreg / second</td>
<td  style="text-align:right" >118,228</td>
<td  style="text-align:right" >22,845</td>
<td  style="text-align:right" >24,282</td>
<td  style="text-align:right" >22,312</td>
</tr>

<tr><td  style="text-align:left" >Retrieve unregistered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Re-Reg / second</td>
<td  style="text-align:right" >127,200</td>
<td  style="text-align:right" >22,115</td>
<td  style="text-align:right" >25,884</td>
<td  style="text-align:right" >20,228</td>
</tr>

<tr><td  style="text-align:left" >Retrieve re-registered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Retrieve Key of killed Pid (ms)</td>
<td  style="text-align:right" >178</td>
<td  style="text-align:right" >1,890</td>
<td  style="text-align:right" >7,584</td>
<td  style="text-align:right" >10,600</td>
</tr>
</tbody></table></div></p>
<h4>Conclusions</h4>
<ul>
<li>These are overall very good results.</li>
<li>I didn’t need to reduce the process count to make all of the test pass.</li>
<li>The monitoring of processes can be optimized. After having killed all the processes, on a cluster of 4 nodes it takes &gt;10 seconds for <pre class="crayon-plain-tag">gproc:lookup_pid/1</pre> to not find the Pid once a process has exited.</li>
<li>Unfortunately, I had some inconsistent results running this test in a cluster of 2+ nodes. Often, the test could not retrieve the registered Key (after the first registration round) in less than 60 second, and timed out.</li>
</ul>
<p>I was a little skeptical though on the inconsistency that I saw in the test results, which might be related to the <pre class="crayon-plain-tag">gen_leader</pre>  issues that I&#8217;ve occasionally heard about. The author&#8217;s choice to move towards <pre class="crayon-plain-tag">locks_leader</pre> might be a sign of this. Despite these thoughts, this looked like a good potential candidate.</p>
<p>&nbsp;</p>
<h3>4. CloudI Process Groups</h3>
<h4>Considerations</h4>
<p><a href="https://github.com/okeuday/cpg">cpg</a> is an actively maintained library, and his main author Michael Truog is often very available to discuss his choices and provide support. cpg deals with Process Groups and not unique Registered Names, therefore my concerns where similar to the ones I had with pg2:</p>
<ul>
<li>Handling Process Groups is very different from handling unique Registered Names. We can use the same trick used with pg2, i.e. creating Process Groups named with Key, with a single entry (the Pid).</li>
<li>Here too, having Process Groups basically means that conflict resolution isn&#8217;t covered. This made me a little uncomfortable because I wanted to ensure that there would be a clearly identified single Pid per device in the whole system.</li>
</ul>
<h4>Stress Test</h4>
<p>Here&#8217;s the callback module:</p><pre class="crayon-plain-tag">-module(cpg_bench).
-behaviour(process_registry_bench).

-export([init/0]).
-export([register/2, unregister/2]).
-export([retrieve/1]).
-export([process_loop/0]).

init() -&gt;
	%% start app on every node
	Nodes = [node() | nodes()],
	[rpc:call(Node, reltool_util, application_start, [cpg]) ||  Node &lt;- Nodes].

register(Key, Pid) -&gt;
	ok = cpg:join(Key, Pid).

unregister(_Key, Pid) -&gt;
	ok = cpg:leave(Pid).

retrieve(Key) -&gt;
	case catch cpg:get_members(Key) of
		{ok, Key, [Pid]} -&gt; Pid;
		{error, {no_such_group, Key}} -&gt; undefined;
		Error -&gt; {error, Error}
	end.

process_loop() -&gt;
	receive
		_ -&gt; ok
	end.</pre><p>The results of the stress test are:</p>
<p><div class="table-responsive"><table  style="width:100%; "  class="easy-table easy-table-default " border="0">
<thead>
<tr><th  style="text-align:left" ></th>
<th  style="text-align:right" >1 Node</th>
<th  style="text-align:right" >2 Nodes</th>
<th  style="text-align:right" >3 Nodes</th>
<th  style="text-align:right" >4 Nodes</th>
</tr>
</thead>
<tbody>
<tr><td  style="text-align:left" >Reg / second</td>
<td  style="text-align:right" >110,198</td>
<td  style="text-align:right" >42,680</td>
<td  style="text-align:right" >20,703</td>
<td  style="text-align:right" >8,488</td>
</tr>

<tr><td  style="text-align:left" >Retrieve registered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Unreg / second</td>
<td  style="text-align:right" >109,374</td>
<td  style="text-align:right" >32,264</td>
<td  style="text-align:right" >25,599</td>
<td  style="text-align:right" >15,128</td>
</tr>

<tr><td  style="text-align:left" >Retrieve unregistered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >1</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Re-Reg / second</td>
<td  style="text-align:right" >126,791</td>
<td  style="text-align:right" >30,862</td>
<td  style="text-align:right" >32,138</td>
<td  style="text-align:right" >20,791</td>
</tr>

<tr><td  style="text-align:left" >Retrieve re-registered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Retrieve Key of killed Pid (ms)</td>
<td  style="text-align:right" ><strong>error</strong></td>
<td  style="text-align:right" ><strong>error</strong></td>
<td  style="text-align:right" ><strong>error</strong></td>
<td  style="text-align:right" ><strong>error</strong></td>
</tr>
</tbody></table></div></p>
<h4>Conclusions</h4>
<ul>
<li>These are overall very good results.</li>
<li>I was surprised of the major drop in a cluster of 4 nodes. I run this test multiple times and it always returned similar results.</li>
<li>The monitoring of processes didn&#8217;t work appropriately. Even on a single node, the test experienced an internal timeout:</li>
</ul>
<p></p><pre class="crayon-plain-tag">{'EXIT',
 {timeout,
  {gen_server,
   call,
   [cpg_default_scope,
    {get_members,
     "100000"}]}}}</pre><p>I had to decrease the number of processes to around 25,000 to have the stress test pass in a cluster of 4 nodes. The monitoring issue didn&#8217;t make me feel particularly at ease, however this library did look like a potential candidate.</p>
<p>&nbsp;</p>
<h3>5. Custom Solution: Syn</h3>
<h4>Considerations</h4>
<p>Since it became clear that I could not use Erlang&#8217;s native <pre class="crayon-plain-tag">global</pre> or <pre class="crayon-plain-tag">pg2</pre> modules, and that the two other libraries I looked into were candidates but each one with their own little twerks, I decided to try a custom solution, which I called <pre class="crayon-plain-tag">syn</pre>  (short for <em>synonym</em>).</p>
<p>In any distributed system you are faced with a consistency challenge, which is often resolved by having one master arbiter performing all write operations (chosen with a mechanism of leader election), or through atomic transactions. As said here above, I needed a global process registry for an application of the IoT field. In this context, Keys used to identify a process are often the physical object&#8217;s unique identifier (for instance, its serial or mac address), and are therefore already defined and unique before hitting the system. The consistency challenge is less of a problem in this case, since the likelihood of concurrent incoming requests that would register processes with the same Key is extremely low and, in most cases, acceptable.</p>
<p>Therefore, Availability has been chosen over Consistency and Syn is <a href="http://en.wikipedia.org/wiki/Eventual_consistency">eventually consistent</a>.</p>
<p>Under the hood, Syn performs dirty reads and writes into a distributed in-memory Mnesia table, replicated across all the nodes of the cluster. This made me feel comfortable that I wouldn&#8217;t need to reinvent the replication mechanisms of Erlang&#8217;s native DB, however I needed a way to handle conflict resolution and net splits. For this reason, Syn can automatically manage conflict resolution by implementing a specialized and simplified version of the mechanisms used in Ulf Wiger&#8217;s <a href="https://github.com/uwiger/unsplit">unsplit</a> framework.</p>
<p>You can read more about Syn in its <a href="https://github.com/ostinelli/syn">github repo</a>.</p>
<h4>Stress Test</h4>
<p>Here&#8217;s the callback module:</p><pre class="crayon-plain-tag">-module(syn_bench).
-behaviour(process_registry_bench).

-export([init/0]).
-export([register/2, unregister/2]).
-export([retrieve/1]).
-export([process_loop/0]).

init() -&gt;
	%% start app on every node
	Nodes = [node() | nodes()],
	F = fun(Node) -&gt;
		rpc:call(Node, syn, start, []),
		rpc:call(Node, syn, init, [])
	end,
	lists:foreach(F, Nodes).

register(Key, Pid) -&gt;
	ok = syn:register(Key, Pid).

unregister(Key, _Pid) -&gt;
	ok = syn:unregister(Key).

retrieve(Key) -&gt;
	syn:find_by_key(Key).

process_loop() -&gt;
	receive
		_ -&gt; ok
	end.</pre><p>The results of the stress test are:<br />
<div class="table-responsive"><table  style="width:100%; "  class="easy-table easy-table-default " border="0">
<thead>
<tr><th  style="text-align:left" ></th>
<th  style="text-align:right" >1 Node</th>
<th  style="text-align:right" >2 Nodes</th>
<th  style="text-align:right" >3 Nodes</th>
<th  style="text-align:right" >4 Nodes</th>
</tr>
</thead>
<tbody>
<tr><td  style="text-align:left" >Reg / second</td>
<td  style="text-align:right" >106,324</td>
<td  style="text-align:right" >52,792</td>
<td  style="text-align:right" >60,958</td>
<td  style="text-align:right" >40,929</td>
</tr>

<tr><td  style="text-align:left" >Retrieve registered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >56</td>
</tr>

<tr><td  style="text-align:left" >Unreg / second</td>
<td  style="text-align:right" >105,506</td>
<td  style="text-align:right" >50,591</td>
<td  style="text-align:right" >67,042</td>
<td  style="text-align:right" >42,896</td>
</tr>

<tr><td  style="text-align:left" >Retrieve unregistered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Re-Reg / second</td>
<td  style="text-align:right" >106,424</td>
<td  style="text-align:right" >51,322</td>
<td  style="text-align:right" >77,258</td>
<td  style="text-align:right" >47,125</td>
</tr>

<tr><td  style="text-align:left" >Retrieve re-registered Key (ms)</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
<td  style="text-align:right" >0</td>
</tr>

<tr><td  style="text-align:left" >Retrieve Key of killed Pid (ms)</td>
<td  style="text-align:right" >719</td>
<td  style="text-align:right" >995</td>
<td  style="text-align:right" >1,577</td>
<td  style="text-align:right" >1,825</td>
</tr>
</tbody></table></div></p>
<h4>Conclusions</h4>
<ul>
<li>These are overall very good results. I&#8217;m not sure why Syn is performing better with 3 nodes than with 2 (and I&#8217;ve repeated this test more than once).</li>
<li>I didn&#8217;t need to reduce the process count to make all of the test pass.</li>
<li>The monitoring of processes worked appropriately.</li>
</ul>
<p>&nbsp;</p>
<h2>Final notes</h2>
<p>I want to stress out how comparisons and these tests are difficult to perform. Every library behaves differently, and it is hard (if not impossible) to define some kind of a common stress test to allow for a better understanding of their performance levels. I gave it a go, but looking at the above definition of my stress test for instance I ask myself: &#8220;Why did I set the process count to 100,000? I can see that most libraries behave fine with lower numbers&#8221;. Also, &#8220;What would happen if instead of registering processes sequentially in a single process per node, we had them register themselves simultaneously, therefore increasing the load on the registry?&#8221;. More importantly, &#8220;Does this test represent some kind of real life scenario?&#8221;.</p>
<p>This article wants to share my thoughts and how I ended up writing Syn. Sure, Syn performs well in the defined use case and stress test, but this does in no way mean that the other libraries here won&#8217;t perform way better in other stress tests and scenarios. I&#8217;d actually be glad to know that someone else is willing to take the time to evaluate these, and other, global process registries. They are a kind of holy grail; and let&#8217;s remember that anything distributed is never easy, nor given.</p>
<p>As a final note, I&#8217;d enjoy reading comments from the library authors or other Erlang enthusiasts. This is such a delicate matter that I&#8217;d love to have a healthy exchange of opinions, hopefully contributing to improving all of our experiences.</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/an-evaluation-of-erlang-global-process-registries-meet-syn/">An evaluation of Erlang global process registries: meet Syn</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ostinelli.net/an-evaluation-of-erlang-global-process-registries-meet-syn/feed/</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		
			</item>
		<item>
		<title>How to build a Rails API server: Optimizing the framework</title>
		<link>https://www.ostinelli.net/how-to-build-a-rails-api-server-optimizing-the-framework/</link>
					<comments>https://www.ostinelli.net/how-to-build-a-rails-api-server-optimizing-the-framework/#comments</comments>
		
		<dc:creator><![CDATA[Roberto Ostinelli]]></dc:creator>
		<pubDate>Tue, 23 Jun 2015 16:54:44 +0000</pubDate>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[api server]]></category>
		<category><![CDATA[json api server]]></category>
		<category><![CDATA[json-api]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[server]]></category>
		<guid isPermaLink="false">http://www.ostinelli.net/?p=644</guid>

					<description><![CDATA[<p>I have been developing Rails JSON API applications for quite some time now, and I&#8217;d like to share a few of my setups and discuss why I do things this way. I&#8217;m starting today a series [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/how-to-build-a-rails-api-server-optimizing-the-framework/">How to build a Rails API server: Optimizing the framework</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I have been developing Rails JSON API applications for quite some time now, and I&#8217;d like to share a few of my setups and discuss why I do things this way. I&#8217;m starting today a series of articles that will cover up pretty much the steps I take every time I bootstrap a new Rails JSON API application.</p>
<p>One of the first things I do is to ensure I&#8217;m optimizing Rails for speed. I basically optimize the framework itself, prior coding any specific application logic.</p>
<p>You may have heard before that &#8220;<a href="http://c2.com/cgi/wiki?PrematureOptimization">Premature optimization is the root of all evil</a>&#8220;. However, &#8220;Premature optimization is a phrase used to describe a situation where a programmer lets performance considerations affect the design of a piece of code&#8221;, which &#8220;can result in a design that is not as clean as it could have been or code that is incorrect, because the code is complicated by the optimization and the programmer is distracted by optimizing&#8221; (source: <a href="https://en.wikipedia.org/wiki/Program_optimization">WikiPedia</a>). This is not what we&#8217;re doing here: we&#8217;re just going to apply a few changes to Rails, and then basically forget about those and start coding in a framework that is optimized to serve our API.</p>
<p>Many of Rails functionalities are simply not needed when building an API server, and by stripping down Rails to a bare minimum we can actually achieve pretty significant performance increases.</p>
<p><span id="more-644"></span></p>
<h3>Greenfield Ruby On Rails</h3>
<p>Let&#8217;s first see what an empty project can achieve. I&#8217;m currently using Ruby 2.2.2 and Rails 4.2.1. Let&#8217;s create a new Rails application:</p><pre class="crayon-plain-tag">rails new api_greenfield -T</pre><p>Let&#8217;s add a production server. For the scope of this post, it&#8217;s not really important what we use, as long as it&#8217;s a server that we can use in production. We are going to benchmark the results we get after applying our changes to Rails, so the absolute values resulting from our benchmarks are not as important as the relative improvements that we see in speed.</p>
<p>We&#8217;re going to use <a href="http://puma.io/">Puma</a>, as it is now the <a href="https://devcenter.heroku.com/changelog-items/594">recommended Ruby webserver by Heroku</a> (and as I host most of my applications there, using it has become my default choice). Add it to the project Gemfile:</p><pre class="crayon-plain-tag">source 'https://rubygems.org'
ruby '2.2.2'

gem 'rails', '4.2.1'
gem 'sqlite3'

gem 'puma'</pre><p>Then <pre class="crayon-plain-tag">bundle install</pre>. Create a Puma configuration file <pre class="crayon-plain-tag">config/puma.rb</pre>  and set the following basic params:</p><pre class="crayon-plain-tag">workers 4
threads_count = 1
threads threads_count, threads_count

preload_app!

rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RAILS_ENV'] || 'development'

on_worker_boot do
  # Worker specific setup for Rails 4.1+
  # See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
  ActiveRecord::Base.establish_connection
end</pre><p>We now need to set up a simple response page that we will hit with our benchmarks. We&#8217;re going to create a controller and an action that responds with a JSON body to the entry point <pre class="crayon-plain-tag">/benchmarks/simple</pre>. To do so, let&#8217;s create <pre class="crayon-plain-tag">benchmarks_controller.rb</pre>:</p><pre class="crayon-plain-tag">class BenchmarksController &lt; ApplicationController

  def simple
    # example from http://json.org/example
    json = {
      glossary: {
        title: "example glossary",
        gloss_div: {
          title: "S",
          gloss_list: {
            gloss_entry: {
              id: "SGML",
              sort_as: "SGML",
              gloss_term: "Standard Generalized Markup Language",
              acronym: "SGML",
              abbrev: "ISO 8879:1986",
              gloss_def: {
                para: "A meta-markup language, used to create markup languages such as DocBook.",
                gloss_see_also: ["GML", "XML"]
              },
              gloss_see: "markup"
            }
          }
        }
      }
    }

    render json: json
  end
end</pre><p>Set the routes for this controller:</p><pre class="crayon-plain-tag">Rails.application.routes.draw do
  resources :benchmarks, only: :none do
    collection do
      get :simple
    end
  end
end</pre><p>Start Puma in production:</p><pre class="crayon-plain-tag">RAILS_ENV=production bundle exec puma -C config/puma.rb</pre><p>Verify that Rails responds with our JSON body at the chosen entry point:</p><pre class="crayon-plain-tag">$ curl -H "Content-type: application/json" http://127.0.0.1:3000/benchmarks/simple
{"glossary":{"title":"example glossary","gloss_div":{"title":"S","gloss_list":{"gloss_entry":{"id":"SGML","sort_as":"SGML","gloss_term":"Standard Generalized Markup Language","acronym":"SGML","abbrev":"ISO 8879:1986","gloss_def":{"para":"A meta-markup language, used to create markup languages such as DocBook.","gloss_see_also":["GML","XML"]},"gloss_see":"markup"}}}}}</pre><p>The server is up and ready. We can now benchmark our greenfield Rails application running with Puma. We will use the basic <a href="http://httpd.apache.org/docs/2.2/programs/ab.html">Apache Benchmark</a> tool to do so.</p><pre class="crayon-plain-tag">$ ab -c 5 -n 10000 -H "Content-type: application/json" http://127.0.0.1:3000/benchmarks/simple
This is ApacheBench, Version 2.3 &lt;$Revision: 1604373</pre><p></p><pre class="crayon-plain-tag"></pre><p></p>
<p></p><pre class="crayon-plain-tag">gt; Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) Completed 1000 requests Completed 2000 requests Completed 3000 requests Completed 4000 requests Completed 5000 requests Completed 6000 requests Completed 7000 requests Completed 8000 requests Completed 9000 requests Completed 10000 requests Finished 10000 requests Server Software: Server Hostname: 127.0.0.1 Server Port: 3000 Document Path: /benchmarks/simple Document Length: 369 bytes Concurrency Level: 5 Time taken for tests: 4.676 seconds Complete requests: 10000 Failed requests: 0 Total transferred: 6990000 bytes HTML transferred: 3690000 bytes Requests per second: 2138.53 [#/sec] (mean) Time per request: 2.338 [ms] (mean) Time per request: 0.468 [ms] (mean, across all concurrent requests) Transfer rate: 1459.79 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.0 0 0 Processing: 1 2 1.0 2 27 Waiting: 1 2 1.0 2 27 Total: 1 2 1.0 2 27 Percentage of the requests served within a certain time (ms) 50% 2 66% 2 75% 3 80% 3 90% 3 95% 4 98% 4 99% 5 100% 27 (longest request)</pre><p>This is actually not bad at all! A greenfield Rails project is able to sustain <em>2,138</em> req/sec. Obviously, this is without any application logic, nor database calls, but it is still a good starting point.</p>
<h3>The Rails API gem</h3>
<p>The <a href="https://github.com/rails-api/rails-api">Rails API gem</a> is &#8220;a subset of a normal Rails application, created for applications that don&#8217;t require all functionality that a complete Rails application provides. It is a bit more lightweight, and consequently a bit faster than a normal Rails application. The main example for its usage is in API applications only, where you usually don&#8217;t need the entire Rails middleware stack nor template generation&#8221;.  Note that <a href="http://wyeworks.com/blog/2015/4/20/rails-api-is-going-to-be-included-in-rails-5/">Rails API will be part of Rails 5</a>, but for now we still have to include the gem:</p><pre class="crayon-plain-tag">source 'https://rubygems.org'
ruby '2.2.2'

gem 'rails', '4.2.1'
gem 'rails-api'
gem 'sqlite3'

gem 'puma'</pre><p>Don&#8217;t forget to <pre class="crayon-plain-tag">bundle install</pre>. Then, change our <pre class="crayon-plain-tag">benchmarks_controller.rb</pre> to inherit from the Rails::API Action Controller:</p><pre class="crayon-plain-tag">class BenchmarksController &lt; ActionController::API</pre><p>Also, comment out in <pre class="crayon-plain-tag">application_controller.rb</pre> :</p><pre class="crayon-plain-tag"># protect_from_forgery with: :exception</pre><p>Let&#8217;s try a new benchmark (portions omitted):</p><pre class="crayon-plain-tag">$ ab -c 5 -n 10000 -H "Content-type: application/json" http://127.0.0.1:3000/benchmarks/simple

[...]

Concurrency Level:      5
Time taken for tests:   4.220 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      6990000 bytes
HTML transferred:       3690000 bytes
Requests per second:    2369.39 [#/sec] (mean)
Time per request:       2.110 [ms] (mean)
Time per request:       0.422 [ms] (mean, across all concurrent requests)
Transfer rate:          1617.39 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     1    2   0.9      2      27
Waiting:        1    2   0.9      2      27
Total:          1    2   0.9      2      27

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      3
  90%      3
  95%      3
  98%      4
  99%      4
 100%     27 (longest request)</pre><p>We now see a response rate of <em>2,369</em> req/sec, which is an increase in performance of ~11% over greenfield Rails. This is a modest improvement, but an improvement nonetheless.</p>
<h3>OJ</h3>
<p>Rails&#8217; default JSON serializer isn&#8217;t the fastest out there, so let&#8217;s swap it for <a href="https://github.com/ohler55/oj">Oj</a>:</p><pre class="crayon-plain-tag">source 'https://rubygems.org'
ruby '2.2.2'

gem 'rails', '4.2.1'
gem 'rails-api'
gem 'sqlite3'

gem 'puma'

gem 'oj'
gem 'oj_mimic_json'</pre><p>Let&#8217;s run the benchmark with Oj (portions omitted):</p><pre class="crayon-plain-tag">$ ab -c 5 -n 10000 -H "Content-type: application/json" http://127.0.0.1:3000/benchmarks/simple

[...]

Concurrency Level:      5
Time taken for tests:   4.040 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      6990000 bytes
HTML transferred:       3690000 bytes
Requests per second:    2475.34 [#/sec] (mean)
Time per request:       2.020 [ms] (mean)
Time per request:       0.404 [ms] (mean, across all concurrent requests)
Transfer rate:          1689.71 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0      27
Processing:     1    2   0.8      2      29
Waiting:        1    2   0.8      1      29
Total:          1    2   0.9      2      29
WARNING: The median and mean for the waiting time are not within a normal deviation
        These results are probably not that reliable.

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      3
  90%      3
  95%      3
  98%      3
  99%      4
 100%     29 (longest request)</pre><p>We can see a small improvement here, which is practically irrelevant (~4%) as we hit <em>2,475</em> req/sec. The switch to Oj is going to be more relevant the bigger the JSON objects to serialize are, but at this stage it doesn&#8217;t hurt to keep Oj in here.</p>
<h3>ActionController::Metal</h3>
<p>It is now time to give the final boost, by:</p>
<ul>
<li>Removing unnecessary railties.</li>
<li>Using Rails&#8217; <em>ActionController::Metal</em> instead of the base controllers that our <em>BenchmarkController</em> has inherited from until now.</li>
</ul>
<p>First, remove unnecessary imports from <pre class="crayon-plain-tag">application.rb</pre> (your mileage may vary &#8211; this is my standard setup and I&#8217;ve rarely needed anything else):</p><pre class="crayon-plain-tag"># require "active_model/railtie"
# require "active_job/railtie"
require "active_record/railtie"
# require "action_controller/railtie"
require "action_mailer/railtie"
# require "action_view/railtie"
# require "sprockets/railtie"</pre><p>Second (and this is what is really going to make a difference), we&#8217;re going to create a new controller that all of our API controllers are going to inherit from. Let&#8217;s create our base <pre class="crayon-plain-tag">api_controller.rb</pre> :</p><pre class="crayon-plain-tag">class ApiController &lt; ActionController::Metal
  abstract!

  include AbstractController::Callbacks
  include ActionController::RackDelegation
  include ActionController::StrongParameters

  private

  def render(options={})
    self.status = options[:status] || 200
    self.content_type = 'application/json'
    body = Oj.dump(options[:json], mode: :compat)
    self.headers['Content-Length'] = body.bytesize.to_s
    self.response_body = body
  end

  ActiveSupport.run_load_hooks(:action_controller, self)
end</pre><p>As you can see, in this controller we define our custom render method. By default, I&#8217;ve already included the three modules that I basically use everywhere:</p>
<ul>
<li><em>AbstractController::Callbacks</em> which allows you to set callbacks such as <pre class="crayon-plain-tag">before_action</pre>  in your controllers.</li>
<li><em>ActionController::RackDelegation</em> which is needed to set the <pre class="crayon-plain-tag">response_body</pre>  (called in the <pre class="crayon-plain-tag">render</pre>  method).</li>
<li><em>ActionController::StrongParameters</em> which allows you to use Strong Params in your controllers.</li>
</ul>
<p>Other modules that you might want to include here are, for instance:</p>
<ul>
<li><em>ActionController::HttpAuthentication::Token::ControllerMethods</em> to use the <pre class="crayon-plain-tag">authenticate_with_http_token</pre>  helper method if you are going to use token authentication in your API.</li>
<li><em>ActionController::HttpAuthentication::Basic::ControllerMethods</em> to use the <pre class="crayon-plain-tag">authenticate_with_http_basic</pre>  helper method if you are going to use basic authentication in your API.</li>
</ul>
<p>Now for our benchmarks, let&#8217;s ensure that <pre class="crayon-plain-tag">benchmarks_controller.rb</pre>  inherits from our newly created controller:</p><pre class="crayon-plain-tag">class BenchmarksController &lt; ApiController</pre><p>Here are the results of the benchmark that includes all of above changes (portions omitted):</p><pre class="crayon-plain-tag">$ ab -c 5 -n 10000 -H "Content-type: application/json" http://127.0.0.1:3000/benchmarks/simple

[...]

Concurrency Level:      5
Time taken for tests:   2.377 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      7200000 bytes
HTML transferred:       3690000 bytes
Requests per second:    4206.19 [#/sec] (mean)
Time per request:       1.189 [ms] (mean)
Time per request:       0.238 [ms] (mean, across all concurrent requests)
Transfer rate:          2957.48 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     1    1   0.4      1       5
Waiting:        0    1   0.4      1       5
Total:          1    1   0.4      1       5

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      2
  95%      2
  98%      2
  99%      2
 100%      5 (longest request)</pre><p>This time the impact is notable, as we hit <em>4,206</em> req/sec.</p>
<h3>Final Touch</h3>
<p>With our latest <em>ApiController</em>, we are not using the controller that the Rails API gem exposes to us. Therefore, let&#8217;s remove the gem:</p><pre class="crayon-plain-tag">source 'https://rubygems.org'
ruby '2.2.2'

gem 'rails', '4.2.1'
# gem 'rails-api'
gem 'sqlite3'

gem 'puma'

gem 'oj'
gem 'oj_mimic_json'</pre><p>However, the Rails API gem did other interesting things under the hood, such as disabling some unnecessary Rails middleware. Since we removed it, we now need to do so ourselves. Add to <pre class="crayon-plain-tag">application.rb</pre>:</p><pre class="crayon-plain-tag">module ApiGreenfield
  class Application &lt; Rails::Application

    [...]

    # remove unnecessary middleware
    config.middleware.delete Rack::Sendfile
    config.middleware.delete Rack::MethodOverride
    config.middleware.delete ActionDispatch::Cookies
    config.middleware.delete ActionDispatch::Session::CookieStore
    config.middleware.delete ActionDispatch::Flash
  end
end</pre><p>Running the benchmark returns the previous results, so we can safely say we don&#8217;t need the Rails API gem anymore.</p>
<h3>Conclusions</h3>
<p>We have started with a greenfield Rails project, and have gradually applied changes to improve the speed performance of a simple benchmarked application:</p>
<p><div class="table-responsive"><table  style="width:100%; "  class="easy-table easy-table-default " border="0">
<thead>
<tr><th >Version</th>
<th >Req/sec</th>
<th >Increase</th>
</tr>
</thead>
<tbody>
<tr><td >Greenfield Rails</td>
<td >2,138</td>
<td >-</td>
</tr>

<tr><td >+ Rails API Gem</td>
<td >2,369</td>
<td >+11%</td>
</tr>

<tr><td >+ Rails API Gem + Oj</td>
<td >2,475</td>
<td >+15%</td>
</tr>

<tr><td >+ Oj + ActionController::Metal + Custom middleware</td>
<td >4,206</td>
<td ><strong>+97%</strong></td>
</tr>
</tbody></table></div></p>
<p>Overall, we experienced an increase from <em>2,138</em> to <em>4,206</em> req/sec, which is <em>doubling the initial performance</em> of a greenfield Rails application.</p>
<p>For additional boosts, you may consider caching techniques (such as partial JSON caching), which are application dependent and are therefore out of scope here.</p>
<p>Happy API&#8217;ing!</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/how-to-build-a-rails-api-server-optimizing-the-framework/">How to build a Rails API server: Optimizing the framework</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ostinelli.net/how-to-build-a-rails-api-server-optimizing-the-framework/feed/</wfw:commentRss>
			<slash:comments>12</slash:comments>
		
		
			</item>
		<item>
		<title>GIN: a JSON-API framework in Lua</title>
		<link>https://www.ostinelli.net/gin-a-json-api-framework-in-lua/</link>
					<comments>https://www.ostinelli.net/gin-a-json-api-framework-in-lua/#comments</comments>
		
		<dc:creator><![CDATA[Roberto Ostinelli]]></dc:creator>
		<pubDate>Thu, 18 Jun 2015 12:03:53 +0000</pubDate>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[gin]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[json api server]]></category>
		<category><![CDATA[json-api]]></category>
		<category><![CDATA[lua]]></category>
		<category><![CDATA[server]]></category>
		<guid isPermaLink="false">http://www.ostinelli.net/?p=529</guid>

					<description><![CDATA[<p>GIN is a fast, low-latency, low-memory footprint, web JSON-API framework with Test Driven Development helpers and patterns. It is a framework that I open sourced some time ago, as an experiment.  It all started when I heard so [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/gin-a-json-api-framework-in-lua/">GIN: a JSON-API framework in Lua</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>GIN is a<span class="s1"> fast, low-latency, low-memory footprint, web JSON-API framework with Test Driven Development helpers and patterns. It is a framework that I open sourced some time ago, as an experiment. </span></p>
<p>It all started when I heard so many good things about <a href="http://www.lua.org/">Lua</a> that I wanted to see it in action and find a project where I could unleash its power. Being an API fan, it came natural for me to build a JSON-API server framework. And GIN was born.</p>
<p>GIN is currently in its early stage, but it already enables fast development, TDD and ease of maintenance. It is helpful when you need an extra-boost in performance and scalability, since it is entirely written in <a href="http://www.lua.org/">Lua</a> and it runs embedded in a packaged version of nginx called <a href="http://openresty.org/">OpenResty</a>. For those not familiar with Lua, don&#8217;t let that scare you away: Lua is really easy to use, very fast and simple to get started with.</p>
<h3>Controllers</h3>
<p>The syntax of a controller is extremely simple. For instance, a simple controller that returns an application&#8217;s version information looks like:</p><pre class="crayon-plain-tag">local InfoController = {}

function InfoController:root()
    return 200, { id = 'my_api', description = 'An API server powered by GIN.' }
end

return InfoController</pre><p>The return statement specifies:</p>
<ul>
<li>The HTTP code 200</li>
<li>The body of the response, which gets encoded by GIN into JSON as:</li>
</ul>
<p></p><pre class="crayon-plain-tag">{
  "id": "my_api",
  "description": "An API server powered by GIN."
}</pre><p>Most of standard JSON-API paradigms are already embedded in GIN.</p>
<p><span id="more-529"></span></p>
<h3>Versioning</h3>
<p>For instance, Versioning is extremely simple. Major versioning is baked into GIN. Based on directory naming conventions, the controllers that correspond to the versioning specified in the header will be called by GIN automatically. Gin uses the HTTP header <em>Accept</em> to version your API. The header has the format:</p><pre class="crayon-plain-tag">application/vnd.{application-name}.v{version}+json</pre><p>So for instance, if your application name (as defined in the file <em>./config/application.lua</em>) is <em>demo</em>, a client trying to access the version <em>1</em> of your API will have to send the header:</p><pre class="crayon-plain-tag">Accept: application/vnd.demo.v1+json</pre><p>Gin only accepts requests that provide JSON in the body, hence the <em>+json</em> portion of the Accept header.</p>
<h3>Routing</h3>
<p>Routing is very easy, too. For instance, this is an example of a routing file that handles multiple versions:</p><pre class="crayon-plain-tag">local routes = require 'gin.core.routes'

-- define version 1
local v1 = routes.version(1)

-- define routes
v1:GET("/users", { controller = "users", action = "index" })
v1:POST("/users", { controller = "users", action = "create" })

-- define version 2
local v2 = routes.version(2)

-- define routes
v2:GET("/users", { controller = "users", action = "index" })
v2:POST("/users", { controller = "users", action = "create" })
v2:GET("/users/:id", { controller = "users", action = "show" })

return routes</pre><p></p>
<h3>Errors</h3>
<p>Moreover, Errors are defined globally at application level. This allows to keep track of error numbering and descriptions in a single file. For example, an error file is defined as:</p><pre class="crayon-plain-tag">local Errors = {
    [1000] = { status = 400, message = "Invalid request.", headers = { ["X-Header"] = "header" } },
}
return Errors</pre><p>The global variable Errors contains the entries for all possible errors of your application. Every item in this table defines an error, where:</p>
<ul>
<li>The 1000 key is the error number.</li>
<li><em>status</em> (required): is the HTTP status code.</li>
<li><em>message</em> (required): is the error description.</li>
<li><em>headers</em> (optional): are the additional headers to be returned in the response.</li>
</ul>
<p>When this is defined, then raising these errors from a controller is as simple as calling:</p><pre class="crayon-plain-tag">self:raise_error(1000)</pre><p>As per this example, this will return a HTTP status 400, with the specified headers and the JSON body:</p><pre class="crayon-plain-tag">{
  "code": 1000,
  "message": "Invalid request."
}</pre><p></p>
<h3>Testing</h3>
<p>However, this wouldn&#8217;t be a complete framework if it didn&#8217;t provide an easy way to test your application. The main testing framework embedded in GIN is <a href="http://olivinelabs.com/busted/">Busted</a>, which has an <a href="http://rspec.info/">RSpec</a>-like syntax. If you ever used <a href="http://pivotal.github.io/jasmine/">Jasmine</a> you&#8217;ll feel right at home. In addition, Gin provides test helpers for you to use.</p>
<p>Controller Tests in Gin are actually integration tests. An instance of OpenResty nginx will be started and a real request will be issued to a test server. The main Gin test helper you&#8217;ll need to test Controllers is the method <em>hit</em>, that will perform the integration test for you.</p>
<p>Here&#8217;s an example of a controller test (file located in <em>./spec/controllers/1/info_controller_spec.lua</em>), that is a valid test for the <em>InfoController</em> shown here above:</p><pre class="crayon-plain-tag">require 'spec.spec_helper'

describe("InfoController", function()
    describe("#root", function()
        it("responds with application info", function()
            local response = hit({
                method = 'GET',
                path = "/"
            })

            assert.are.same(200, response.status)
            assert.are.same({ id = 'my_api', description = 'An API server powered by GIN.' }, response.body)
        end)
    end)
end)</pre><p></p>
<h3>Others</h3>
<p>GIN also has models and support for multiple databases, and includes a migration engine. Furthermore, it is packed with a lot of other features, such as:</p>
<ul>
<li>An API Console (which allows you to try out your API from a web interface).</li>
<li>Support for multiple environments.</li>
<li>A client console.</li>
</ul>
<h3>Alright, I want to see it!</h3>
<p>GIN main website is <a href="http://gin.io">gin.io</a>, where you&#8217;ll find full documentation, and even a <a href="http://gin.io/tutorial.html">full tutorial</a> on how to Test Drive your Development with GIN. Full code is available on <a href="https://github.com/ostinelli/gin">Github</a>.</p>
<p>Would this be worthy more than a simple experiment?</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/gin-a-json-api-framework-in-lua/">GIN: a JSON-API framework in Lua</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ostinelli.net/gin-a-json-api-framework-in-lua/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>A comparison between Misultin, Mochiweb, Cowboy, NodeJS and Tornadoweb</title>
		<link>https://www.ostinelli.net/a-comparison-between-misultin-mochiweb-cowboy-nodejs-and-tornadoweb/</link>
					<comments>https://www.ostinelli.net/a-comparison-between-misultin-mochiweb-cowboy-nodejs-and-tornadoweb/#comments</comments>
		
		<dc:creator><![CDATA[Roberto Ostinelli]]></dc:creator>
		<pubDate>Mon, 09 May 2011 03:19:08 +0000</pubDate>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[cowboy]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[http servers]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[michiweb]]></category>
		<category><![CDATA[misultin]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[tornadoweb]]></category>
		<guid isPermaLink="false">http://www.ostinelli.net/?p=411</guid>

					<description><![CDATA[<p>As some of you already know, I&#8217;m the author of Misultin, an Erlang HTTP lightweight server library. I&#8217;m interested in HTTP servers, I spend quite some time trying them out and am always interested in [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/a-comparison-between-misultin-mochiweb-cowboy-nodejs-and-tornadoweb/">A comparison between Misultin, Mochiweb, Cowboy, NodeJS and Tornadoweb</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>As some of you already know, I&#8217;m the author of Misultin, an Erlang HTTP lightweight server library. I&#8217;m interested in HTTP servers, I spend quite some time trying them out and am always interested in comparing them from different perspectives.</p>
<p>Today I wanted to try the same benchmark against various HTTP server libraries:</p>
<ul>
<li><a href="https://github.com/ostinelli/misultin">Misultin</a> (Erlang)</li>
<li><a href="https://github.com/mochi/mochiweb">Mochiweb</a> (Erlang)</li>
<li><a href="https://github.com/extend/cowboy">Cowboy</a> (Erlang)</li>
<li><a href="http://nodejs.org/">NodeJS</a> (V8)</li>
<li><a href="http://www.tornadoweb.org/">Tornadoweb</a> (Python)</li>
</ul>
<p>I&#8217;ve chosen these libraries because they are the ones which currently interest me the most. Misultin, obviously since I wrote it; Mochiweb, since it&#8217;s a very solid library widely used in production (afaik it has been used or is still used to empower the Facebook Chat, amongst other things); Cowboy, a newly born lib whose programmer is very active in the Erlang community; NodeJS, since bringing javascript to the backend has opened up a new whole world of possibilities (code reusable in frontend, ease of access to various programmers,&#8230;); and finally, Tornadoweb, since Python still remains one of my favourites languages out there, and Tornadoweb has been excelling in loads of benchmarks and in production, empowering FriendFeed.</p>
<p><span id="more-411"></span></p>
<p>Two main ideas are behind this benchmark. First, I did not want to do a &#8220;Hello World&#8221; kind of test: we have static servers such as <a href="http://nginx.org/">Nginx</a> that wonderfully perform in such tasks. This benchmark needed to address dynamic servers. Second, I wanted sockets to get periodically closed down, since having all the load on a few sockets scarcely correspond to real life situations.</p>
<p>For the latter reason, I decided to use a <a href="http://gom-jabbar.org/articles/2009/02/04/httperf-and-file-descriptors">patched version</a> of <a href="http://www.hpl.hp.com/research/linux/httperf/">HttPerf</a>. It&#8217;s a widely known and used benchmark tool from HP, which basically tries to send a desired number of requests out to a server and reports how many of these actually got replied, and how many errors were experienced in the process (together with a variety of other pieces of information). A great thing about HttPerf is that you can set a parameter, called <em>&#8211;num-calls</em>, which sets the amount of calls per session (i.e. socket connection) before the socket gets closed by the client. The command issued in these tests was:</p><pre class="crayon-plain-tag">httperf --timeout=5 --client=0/1 --server= --port=8080 --uri=/?value=benchmarks --rate= --send-buffer=4096
        --recv-buffer=16384 --num-conns=5000 --num-calls=10</pre><p>The value of rate has been set incrementally between 100 and 1,200. Since the number of requests/sec = <em>rate</em> * <em>num-calls</em>, the tests were conducted for a desired number of responses/sec incrementing from 1,000 to 12,000. The total number of requests = <em>num-conns</em> * <em>rate</em>, which has therefore been a fixed value of 50,000 along every test iteration.</p>
<p>The test basically asks servers to:</p>
<ul>
<li>check if a GET variable is set</li>
<li>if the variable is not set, reply with an XML stating the error</li>
<li>if the variable is set, echo it inside an XML</li>
</ul>
<p>Therefore, what is being tested is:</p>
<ul>
<li>headers parsing</li>
<li>querystring parsing</li>
<li>string concatenation</li>
<li>sockets implementation</li>
</ul>
<p>The server is a virtualized up-to-date Ubuntu 10.04 LTS with 2 CPU and 1.5GB of RAM. Its <em>/etc/sysctl.conf</em> file has been tuned with these parameters:</p><pre class="crayon-plain-tag"># Maximum TCP Receive Window
net.core.rmem_max = 33554432
# Maximum TCP Send Window
net.core.wmem_max = 33554432
# others
net.ipv4.tcp_rmem = 4096 16384 33554432
net.ipv4.tcp_wmem = 4096 16384 33554432
net.ipv4.tcp_syncookies = 1
# this gives the kernel more memory for tcp which you need with many (100k+) open socket connections
net.ipv4.tcp_mem = 786432 1048576 26777216
net.ipv4.tcp_max_tw_buckets = 360000
net.core.netdev_max_backlog = 2500
vm.min_free_kbytes = 65536
vm.swappiness = 0
net.ipv4.ip_local_port_range = 1024 65535
net.core.somaxconn = 65535</pre><p>The <em>/etc/security/limits.conf</em> file has been tuned so that ulimit -n is set to 65535 for both hard and soft limits.</p>
<p>Here is the code for the different servers.</p>
<p><strong>Misultin</strong></p><pre class="crayon-plain-tag">-module(misultin_bench).
-export([start/1, stop/0, handle_http/1]).

start(Port) -&gt;
    misultin:start_link([{port, Port}, {loop, fun(Req) -&gt; handle_http(Req) end}]).

stop() -&gt;
    misultin:stop().

handle_http(Req) -&gt;
    % get value parameter
    Args = Req:parse_qs(),
    Value = misultin_utility:get_key_value("value", Args),
    case Value of
        undefined -&gt;
            Req:ok([{"Content-Type", "text/xml"}], ["no value specified"]);
        _ -&gt;
            Req:ok([{"Content-Type", "text/xml"}], ["", Value, ""])
    end.</pre><p><strong>Mochiweb</strong></p><pre class="crayon-plain-tag">-module(mochi_bench).
-export([start/1, stop/0, handle_http/1]).

start(Port) -&gt;
    mochiweb_http:start([{port, Port}, {loop, fun(Req) -&gt; handle_http(Req) end}]).

stop() -&gt;
    mochiweb_http:stop().

handle_http(Req) -&gt;
    % get value parameter
    Args = Req:parse_qs(),
    Value = misultin_utility:get_key_value("value", Args),
    case Value of
        undefined -&gt;
            Req:respond({200, [{"Content-Type", "text/xml"}], ["no value specified"]});
        _ -&gt;
            Req:respond({200, [{"Content-Type", "text/xml"}], ["", Value, ""]})
    end.</pre><p><em>Note: i&#8217;m using misultin_utility:get_key_value/2 function inside this code since proplists:get_value/2 is much slower.</em></p>
<p><strong>Cowboy</strong></p><pre class="crayon-plain-tag">-module(cowboy_bench).
-export([start/1, stop/0]).

start(Port) -&gt;
    application:start(cowboy),
    Dispatch = [
        %% {Host, list({Path, Handler, Opts})}
        {'_', [{'_', cowboy_bench_handler, []}]}
    ],
    %% Name, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
    cowboy:start_listener(http, 100,
        cowboy_tcp_transport, [{port, Port}],
        cowboy_http_protocol, [{dispatch, Dispatch}]
    ).

stop() -&gt;
    application:stop(cowboy).</pre><p></p><pre class="crayon-plain-tag">-module(cowboy_bench_handler).
-behaviour(cowboy_http_handler).
-export([init/3, handle/2, terminate/2]).

init({tcp, http}, Req, _Opts) -&gt;
    {ok, Req, undefined_state}.

handle(Req, State) -&gt;
    {ok, Req2} = case cowboy_http_req:qs_val(&lt;&lt;"value"&gt;&gt;, Req) of
        {undefined, _} -&gt;
            cowboy_http_req:reply(200, [{&lt;&lt;"Content-Type"&gt;&gt;, &lt;&lt;"text/xml"&gt;&gt;}], &lt;&lt;"no value specified"&gt;&gt;, Req);
        {Value, _} -&gt;
            cowboy_http_req:reply(200, [{&lt;&lt;"Content-Type"&gt;&gt;, &lt;&lt;"text/xml"&gt;&gt;}], ["", Value, ""], Req)
    end,
    {ok, Req2, State}.

terminate(_Req, _State) -&gt;
    ok.</pre><p><strong>NodeJS</strong></p><pre class="crayon-plain-tag">var http = require('http'), url = require('url');
http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type":"text/xml"});
    var urlObj = url.parse(request.url, true);
    var value = urlObj.query["value"];
    if (value == ''){
        response.end("no value specified");
    } else {
        response.end("" + value + "");
    }
}).listen(8080);</pre><p><strong>Tornadoweb</strong></p><pre class="crayon-plain-tag">import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        value = self.get_argument('value', '')
        self.set_header('Content-Type', 'text/xml')
        if value == '':
            self.write("no value specified")
        else:
            self.write("" + value + "")

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8080)
    tornado.ioloop.IOLoop.instance().start()</pre><p>I took this code and run it against:</p>
<ul>
<li>Misultin 0.7.1 (Erlang R14B02)</li>
<li>Mochiweb 1.5.2 (Erlang R14B02)</li>
<li>Cowboy master 420f5ba (Erlang R14B02)</li>
<li>NodeJS 0.4.7</li>
<li>Tornadoweb 1.2.1 (Python 2.6.5)</li>
</ul>
<p>All the libraries have been run with the standard settings. Erlang was launched with Kernel Polling enabled, and with SMP disabled so that a single CPU was used by all the libraries.</p>
<p><strong>Test results</strong></p>
<p>The raw printout of HttPerf results that I got can be downloaded from <a href="http://www.ostinelli.net/wp-content/uploads/2011/05/results.zip">here</a>.</p>
<p><img decoding="async" loading="lazy" class="aligncenter wp-image-384 size-full" title="desired_vs_real" src="http://www.ostinelli.net/wp-content/uploads/2011/05/desired_vs_real.jpg" alt="" width="650" height="529" srcset="https://www.ostinelli.net/wp-content/uploads/2011/05/desired_vs_real.jpg 650w, https://www.ostinelli.net/wp-content/uploads/2011/05/desired_vs_real-300x244.jpg 300w" sizes="(max-width: 650px) 100vw, 650px" /></p>
<p><img decoding="async" loading="lazy" class="aligncenter wp-image-385 size-full" title="errors" src="http://www.ostinelli.net/wp-content/uploads/2011/05/errors.jpg" alt="" width="650" height="542" srcset="https://www.ostinelli.net/wp-content/uploads/2011/05/errors.jpg 650w, https://www.ostinelli.net/wp-content/uploads/2011/05/errors-300x250.jpg 300w" sizes="(max-width: 650px) 100vw, 650px" /></p>
<p><img decoding="async" loading="lazy" class="aligncenter wp-image-394 size-full" title="response_times_log" src="http://www.ostinelli.net/wp-content/uploads/2011/05/response_times_log.jpg" alt="" width="650" height="545" /></p>
<p style="text-align: center;"><em>Note: the above graph has a logarithmic Y scale.</em></p>
<p><img decoding="async" loading="lazy" class="aligncenter wp-image-386 size-full" title="response_count" src="http://www.ostinelli.net/wp-content/uploads/2011/05/response_count.jpg" alt="" width="650" height="577" srcset="https://www.ostinelli.net/wp-content/uploads/2011/05/response_count.jpg 650w, https://www.ostinelli.net/wp-content/uploads/2011/05/response_count-300x266.jpg 300w" sizes="(max-width: 650px) 100vw, 650px" /></p>
<p>According to this, we see that <strong>Tornadoweb</strong> tops at around 1,500 responses/seconds, <strong>NodeJS</strong> at 3,000, <strong>Mochiweb</strong> at 4,850, <strong>Cowboy</strong> at 8,600 and <strong>Misultin</strong> at 9,700. While Misultin and Cowboy experience very little or no error at all, the other servers seem to funnel under the load. Please note that &#8220;Errors&#8221; are timeout errors (over 5 seconds without a reply). Total responses and response times speak for themselves.</p>
<p>I have to say that I&#8217;m surprised on these results, to the point I&#8217;d like to have feedback on code and methodology, with alternate tests that can be performed. Any input is welcome, and I&#8217;m available to update this post and correct eventual errors I&#8217;ve made, as an ongoing discussion with whomever wants to contribute.</p>
<p>However, please do refrain from flame wars which are not welcomed here. I have published this post exactly because I was surprised on the results I got.</p>
<p>What is your opinion on all this?</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p><strong>UPDATE (May 16th, 2011)</strong></p>
<p>Due to the success of these benchmarks I want to stress an important point when you read any of these (including mines).</p>
<p>Benchmarks often are misleading interpreted as &#8220;the higher you are on a graph, the best that <em>*lib-of-the-moment-name-here*</em> is at doing everything&#8221;. This is absolutely the wrongest way to look at those. I cannot stress this point enough.</p>
<p>&#8216;Fast&#8217; is only 1 of the &#8216;n&#8217; features you desire from a webserver library: you definitely want to consider stability, features, ease of maintenance, low standard deviation, code usability, community, developments speed, and many other factors whenever choosing the best suited library for your own application. There is no such thing as generic benchmarks. These ones are related to a very specific situation: fast application computational times, loads of connections, and small data transfer.</p>
<p>Therefore, please use this with a grain of salt and do not jump to generic conclusions regarding any of the cited libraries, which as I&#8217;ve clearly stated in the beginning of my post I all find interesting and valuable. And I still am very open in being criticized for the described methodology or other things I might have missed.</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/a-comparison-between-misultin-mochiweb-cowboy-nodejs-and-tornadoweb/">A comparison between Misultin, Mochiweb, Cowboy, NodeJS and Tornadoweb</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ostinelli.net/a-comparison-between-misultin-mochiweb-cowboy-nodejs-and-tornadoweb/feed/</wfw:commentRss>
			<slash:comments>61</slash:comments>
		
		
			</item>
		<item>
		<title>Misultin: erlang and websockets</title>
		<link>https://www.ostinelli.net/misultin-erlang-and-websockets/</link>
					<comments>https://www.ostinelli.net/misultin-erlang-and-websockets/#comments</comments>
		
		<dc:creator><![CDATA[Roberto Ostinelli]]></dc:creator>
		<pubDate>Wed, 20 Jan 2010 15:33:12 +0000</pubDate>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[comet]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[misultin]]></category>
		<category><![CDATA[websocket]]></category>
		<category><![CDATA[websockets]]></category>
		<guid isPermaLink="false">http://www.ostinelli.net/?p=331</guid>

					<description><![CDATA[<p>Inspired by Joe Armstrong&#8217;s post, I&#8217;ve recently added websocket support to misultin v0.4, my Erlang library for building fast lightweight HTTP servers. Basically, websockets allow a two-way asynchronous communication between browser and servers, filling the [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/misultin-erlang-and-websockets/">Misultin: erlang and websockets</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Inspired by Joe Armstrong&#8217;s <a href="http://armstrongonsoftware.blogspot.com/2009/12/comet-is-dead-long-live-websockets.html">post</a>, I&#8217;ve recently added <a href="http://dev.w3.org/html5/websockets/">websocket</a> support to <a href="http://code.google.com/p/misultin/">misultin</a> v0.4, my Erlang library for building fast lightweight HTTP servers.</p>
<p>Basically, websockets allow a two-way asynchronous communication between browser and servers, filling the gap that some technologies such as ajax and comet have tried to fulfill in these recent years. If you want to try this out yourself, you will first need to grab a browser which implements websockets, such as <a href="http://www.google.com/chrome/">Google Chrome</a>.</p>
<p><span id="more-331"></span></p>
<p>The typical html page with javascript code to use websockets is as follows:</p><pre class="crayon-plain-tag">&lt;html&gt;
   &lt;head&gt;
     &lt;script type="text/javascript"&gt;
         function addStatus(text){
            var date = new Date();
            document.getElementById('status').innerHTML = document.getElementById('status').innerHTML
               + date + ": " + text + "&lt;br&gt;";
         }
         function ready(){
            if ("WebSocket" in window) {
               // browser supports websockets
               var ws = new WebSocket("ws://localhost:8080/service");
               ws.onopen = function() {
                  // websocket is connected
                  addStatus("websocket connected!");
                  // send hello data to server.
                  ws.send("hello server!");
                  addStatus("sent message to server: 'hello server'!");
               };
               ws.onmessage = function (evt) {
                  var receivedMsg = evt.data;
                  addStatus("server sent the following: '" + receivedMsg + "'");
               };
               ws.onclose = function() {
                  // websocket was closed
                  addStatus("websocket was closed");
               };
            } else {
               // browser does not support websockets
               addStatus("sorry, your browser does not support websockets.");
            }
         }
      &lt;/script&gt;
   &lt;/head&gt;
   &lt;body onload="ready();"&gt;
      &lt;div id="status"&gt;&lt;/div&gt;
   &lt;/body&gt;
&lt;/html&gt;</pre><p>&nbsp;</p>
<p>Here’s the code to use misultin to handle the requests of this script:</p><pre class="crayon-plain-tag">-module(misultin_websocket_example).
-export([start/1, stop/0]).

% start misultin http server
start(Port) -&gt;
    misultin:start_link([{port, Port}, {loop, fun(Req) -&gt; handle_http(Req, Port) end},
    {ws_loop, fun(Ws) -&gt; handle_websocket(Ws) end}]).

% stop misultin
stop() -&gt;
    misultin:stop().

% callback on request received
handle_http(Req, Port) -&gt;    
    % output
    Req:ok([]).

% callback on received websockets data
handle_websocket(Ws) -&gt;
    receive
        {browser, Data} -&gt;
            Ws:send(["received '", Data, "'"]),
            handle_websocket(Ws);
        _Ignore -&gt;
            handle_websocket(Ws)
    after 5000 -&gt;
        Ws:send("pushing!"),
        handle_websocket(Ws)
    end.</pre><p>handle_websocket/1 is spawned by misultin to handle the connected websockets. Data coming from a browser will be sent to this process and will have the message format {browser, Data}, where Data is a string(). If you need to send data to the browser, you may do so by using the parametrized function Ws:send(Data), Data being a string() or an iolist().</p>
<p>Compile and run the example here above with misultin_websocket_example:start(8080). Then, open up your Chrome (or other websocket compliant browser) and point it to an .html file containing the above code.</p>
<p>You should normally see this being gradually printed on your browser:</p><pre class="crayon-plain-tag">Wed Jan 20 2010 15:18:52 GMT+0100 (CET): websocket connected!
Wed Jan 20 2010 15:18:52 GMT+0100 (CET): sent message to server: 'hello server'!
Wed Jan 20 2010 15:18:52 GMT+0100 (CET): server sent the following: 'received 'hello server!''
Wed Jan 20 2010 15:18:57 GMT+0100 (CET): server sent the following: 'pushing!'
Wed Jan 20 2010 15:19:02 GMT+0100 (CET): server sent the following: 'pushing!'
Wed Jan 20 2010 15:19:07 GMT+0100 (CET): server sent the following: 'pushing!'</pre><p>In normal environments you may consider serving the .html page from misultin directly. You may do so with the following and complete misultin module:</p><pre class="crayon-plain-tag">-module(misultin_websocket_example).
-export([start/1, stop/0]).

% start misultin http server
start(Port) -&gt;
   misultin:start_link([{port, Port}, {loop, fun(Req) -&gt; handle_http(Req, Port) end}, {ws_loop, fun(Ws) -&gt; handle_websocket(Ws) end}]).

% stop misultin
stop() -&gt;
   misultin:stop().

% callback on request received
handle_http(Req, Port) -&gt;
   % output
   Req:ok([{"Content-Type", "text/html"}],
   ["
   &lt;html&gt;
      &lt;head&gt;
         &lt;script type=\"text/javascript\"&gt;
            function addStatus(text){
               var date = new Date();
               document.getElementById('status').innerHTML = document.getElementById('status').innerHTML
                  + date + \": \" + text + \"&lt;br&gt;\";
            }
            function ready(){
               if (\"WebSocket\" in window) {
                  // browser supports websockets
                  var ws = new WebSocket(\"ws://localhost:", integer_to_list(Port) ,"/service\");
                  ws.onopen = function() {
                     // websocket is connected
                     addStatus(\"websocket connected!\");
                     // send hello data to server.
                     ws.send(\"hello server!\");
                     addStatus(\"sent message to server: 'hello server'!\");
                  };
                  ws.onmessage = function (evt) {
                     var receivedMsg = evt.data;
                     addStatus(\"server sent the following: '\" + receivedMsg + \"'\");
                  };
                  ws.onclose = function() {
                     // websocket was closed
                     addStatus(\"websocket was closed\");
                  };
               } else {
                  // browser does not support websockets
                  addStatus(\"sorry, your browser does not support websockets.\");
               }
            }
         &lt;/script&gt;
      &lt;/head&gt;
      &lt;body onload=\"ready();\"&gt;
         &lt;div id=\"status\"&gt;&lt;/div&gt;
      &lt;/body&gt;
   &lt;/html&gt;"]).

% callback on received websockets data
handle_websocket(Ws) -&gt;
   receive
      {browser, Data} -&gt;
         Ws:send(["received '", Data, "'"]),
         handle_websocket(Ws);
      _Ignore -&gt;
         handle_websocket(Ws)
   after 5000 -&gt;
      Ws:send("pushing!"),
      handle_websocket(Ws)
   end.</pre><p>Please note that the <a href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-68">Websocket Protocol</a> still is draft. use with caution.</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/misultin-erlang-and-websockets/">Misultin: erlang and websockets</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ostinelli.net/misultin-erlang-and-websockets/feed/</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		
			</item>
		<item>
		<title>Misultin library</title>
		<link>https://www.ostinelli.net/misultin/</link>
					<comments>https://www.ostinelli.net/misultin/#respond</comments>
		
		<dc:creator><![CDATA[Roberto Ostinelli]]></dc:creator>
		<pubDate>Mon, 27 Jul 2009 12:11:47 +0000</pubDate>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[lightweight http server]]></category>
		<category><![CDATA[misultin]]></category>
		<guid isPermaLink="false">http://www.ostinelli.net/?p=323</guid>

					<description><![CDATA[<p>Today I&#8217;ve released Misultin (pronounced mee-sul-teen), an Erlang library for building fast lightweight HTTP servers. The first benchmarks are quite satisfying, even though there still is work to do. Here is the simple code for [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/misultin/">Misultin library</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Today I&#8217;ve released Misultin (pronounced mee-sul-teen), an Erlang library for building fast lightweight HTTP servers. The first <a href="http://code.google.com/p/misultin/wiki/Benchmarks">benchmarks</a> are quite satisfying, even though there still is work to do.</p>
<p>Here is the simple code for Misultin&#8217;s Hello World.</p><pre class="crayon-plain-tag">-module(misultin_hello_world).
-vsn('0.1').
-export([start/1, stop/0, handle_http/1]).

% start misultin http server
start(Port) -&gt;
    misultin:start_link([{port, Port}, {loop, fun(Req) -&gt; handle_http(Req) end}]).

% stop misultin
stop() -&gt;
    misultin:stop().

% callback on request received
handle_http(Req) -&gt;
    Req:ok("Hello World.").</pre><p><span id="more-323"></span></p>
<p>Here&#8217;s the code to echo a GET variable in a XML form.</p><pre class="crayon-plain-tag">-module(misultin_get_variable).
-vsn('0.1').
-export([start/1, stop/0, handle_http/1]).

% start misultin http server
start(Port) -&gt;
    misultin:start_link([{port, Port}, {loop, fun(Req) -&gt; handle_http(Req) end}]).

% stop misultin
stop() -&gt;
    misultin:stop().

% callback on request received
handle_http(Req) -&gt;
    % get params
    Args = Req:parse_qs(),
    Value = proplists:get_value("value", Args),
    case Value of
        undefined -&gt;
            Req:ok([{"Content-Type", "text/xml"}], "no value specified");
        _ -&gt;
            Req:ok([{"Content-Type", "text/xml"}], "~s", [Value])
    end.</pre><p>Available also are <a href="http://code.google.com/p/misultin/wiki/ExamplesPage">additional code examples</a>, and the <a href="http://code.google.com/p/misultin/wiki/Exports">full list of exports</a>.</p>
<p>You may find Misultin, released under the <a href="http://www.opensource.org/licenses/bsd-license.php">New BSD License</a>, on its <a href="http://code.google.com/p/misultin">project page on Google code</a>.</p>
<p>The post <a rel="nofollow" href="https://www.ostinelli.net/misultin/">Misultin library</a> appeared first on <a rel="nofollow" href="https://www.ostinelli.net">ostinelli|net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.ostinelli.net/misultin/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
