<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Emil Kirschner on Medium]]></title>
        <description><![CDATA[Stories by Emil Kirschner on Medium]]></description>
        <link>https://medium.com/@entzik?source=rss-5e7b331e1e59------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9PPoFKMZ_B2jq1jP5MTNwQ.png</url>
            <title>Stories by Emil Kirschner on Medium</title>
            <link>https://medium.com/@entzik?source=rss-5e7b331e1e59------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 08:30:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@entzik/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Gemini CLI: The long run]]></title>
            <link>https://entzik.medium.com/gemini-cli-the-long-run-4926143646f0?source=rss-5e7b331e1e59------2</link>
            <guid isPermaLink="false">https://medium.com/p/4926143646f0</guid>
            <category><![CDATA[vibe-coding]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[claude-code]]></category>
            <category><![CDATA[gemini-cli]]></category>
            <dc:creator><![CDATA[Emil Kirschner]]></dc:creator>
            <pubDate>Wed, 11 Mar 2026 22:50:33 GMT</pubDate>
            <atom:updated>2026-03-24T13:45:54.902Z</atom:updated>
            <content:encoded><![CDATA[<p>In my previous post, “<a href="https://medium.com/@entzik/gemini-cli-a-holiday-stress-test-5baf3e03309f">Gemini CLI: A Holiday Stress Test</a>”, I took gemini-cli to a test. The challenge was to write a modern, versatile diagraming app for software engineers and architects, in rust &amp; web assembly, without writing a single line of code manually. Today, about 60 days and $200 later, did gemini-cli live up to the challenge? Well, mostly…</p><p>Good news is, I managed to build a versatile diagram framework, with support for various shape types and smart relationship management, and a fully functional flowchart editor on top of it.</p><p>You can play with it here: <a href="https://skitze-editor.pages.dev/">https://skitze-editor.pages.dev/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*103YI9D942z9yvLftXgfsQ.png" /></figure><p>How did I get here?</p><h3>The workflow</h3><p>I use the <a href="http://gemini.md/">GEMINI.md</a> file at the root of the project to store instructions to automatically commit to git with a meaningful message after each prompt, without asking. This results in many granular commits, but it is important because it allows me to easily roll back at any stable point in the past if the coding agent gets it wrong or dies on me. My commit history looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZoHIJXmmQ_r4WhxgPscwig.png" /></figure><p>I use multiple gemini CLI files in different folders in the project to enforce architecture and coding guidelines — I have this set up for the diagram engine written in Rust and for the host application written in Typescript / Angular.</p><h3>Prompting</h3><p>This is probably no news to seasoned vibe-coders, but the results you get are as good as your prompts. Often when I come up with a larger prompt, I give it to one of gemini, claude or chat-gpt (the web apps) and ask it to optimize it for coding agent consumption. Feeding this pre-processed prompt generally yields better results than using my original prompt.</p><p>For larger and complex features, I ask gemini-cli to elaborate a plan and save it to an MD file. I review the plan carefully and iterate on it until it looks good. Then I ask gemini-cli to execute the plan.</p><h3>The Good</h3><p>The good news is gemini-cli was able to handle 98% of the prompts I’ve thrown at it. It is able o successfully juggle between Angular / Typescript and Rust code and the code is globally of good quality.</p><h3>The not so good</h3><p>Tooling support seems inferior to claude-code’s. I tried to use Context7 and SerenaMCP with gemini-cli, but without much success. These are tools which work extremely well when used with claude-code. With gemini-cli, the story is different.</p><p>I have to specifically instruct gemini-cli to use Context7 to fetch up to date library documentation, even though I have a paragraph in the root <a href="http://gemini.md/">GEMINI.md</a> file that instructs it to do so without asking.</p><p>Trying to use SerenaMCP with gemini-cli proved to be a disaster. I noticed a dramatic drop in performance and accuracy and frequent bad decisions. After removing SerenaMCP connection, it all went back to normal.</p><p>On two occasions gemini-cli was just not able to successfully find a solution to the prompt I fed it. These were not easy problems to solve.</p><p>The first one is about implementing a mouse gesture for defining relationship waypoints on the diagram.</p><p>The second was one about refining the smart relationship arrangement and rendering algorithm for relationships between a diamond shape and a rectangular shapes, when the diamond corner on one end of the relationship projects on an edge segment of the rectangular shape at the other end.</p><p>I restarted from scratch five or six times, rephrased and refined my prompts through other LLMs, nothing did it. I eventually brought in claude-code with Opus 4.6, gave my original prompt and obtained a perfect result in one iteration.</p><p>I also used claude-code to identify divergences from architecture rules and guidelines stated in various <a href="http://gemini.md/">GEMINI.MD</a> files.</p><h3>The Ugly</h3><p>Apart from the 2.5-pro model availability problems, the most nerve-wrecking defect in gemini-cli is its habit to get stuck in infinite loops.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QvhoPhdWehaX7vFRhHxydQ.png" /></figure><p>You never know how it hits you:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X4GdH816s21wrD24M2eJOg.png" /></figure><p>It can happen in the middle of a task or at the beginning or at the end… in 50% of the cases you can get away with this prompt:</p><p>“<em>You got stuck in a loop while working on the previous task. Examine the state of the changes you made on his branch and continue from where you left off</em>”.</p><p>But in 50% of the cases it will either provide a bad result or it will get stuck again, and the only solution is to roll back to the latest stable git commit.</p><p>If you can live with a couple of these a day, you should be good.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4926143646f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gemini CLI: A Holiday Stress Test]]></title>
            <link>https://entzik.medium.com/gemini-cli-a-holiday-stress-test-5baf3e03309f?source=rss-5e7b331e1e59------2</link>
            <guid isPermaLink="false">https://medium.com/p/5baf3e03309f</guid>
            <category><![CDATA[claude]]></category>
            <category><![CDATA[gemini-cli]]></category>
            <category><![CDATA[vibe-coding]]></category>
            <category><![CDATA[claude-code]]></category>
            <category><![CDATA[gemini]]></category>
            <dc:creator><![CDATA[Emil Kirschner]]></dc:creator>
            <pubDate>Tue, 06 Jan 2026 20:19:38 GMT</pubDate>
            <atom:updated>2026-03-24T13:46:53.355Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Comparing Google&#39;s Gemini CLI Against Anthropic&#39;s Claude Code for AI-Assisted Development</em></p><p>I took Google&#39;s Gemini CLI coding agent for a spin over the Christmas break to see how it stacks up against Anthropic&#39;s Claude Code, which is currently considered the gold standard among coding agents.</p><p>Those who know me have heard my frequent critiques of diagram editors&#39; quality and usability. As a software architect, I live and breathe diagrams. Nowadays, draw.io is quite capable, but it remains too general-purpose. It lacks the structure and system design features that architects need. Years ago, I built my own software architect&#39;s diagram editor using Java and JavaFX, but never had time to finish it. For a few years now, I&#39;ve been eager to reboot that personal project as a web application written with WebAssembly and Rust, but startup life doesn&#39;t allow for such indulgences.</p><p>So when Christmas break arrived, I decided to take Gemini CLI for a spin and see if it could help me build my dream tool through AI-assisted development. My goal: don&#39;t write a single line of code.</p><h3>Initial Setup</h3><p>The first thing any experienced AI-assisted developer thinks about when starting a new project is how to make the coding agent adhere to the stack, apply relevant best practices, remain mindful of security, write tests, and most importantly, set up a scalable codebase.</p><h3>MCP Server Support</h3><p>Both Claude Code and Gemini CLI support MCP servers. I used Context7 with Gemini CLI, and it works nicely.</p><h3>Project Memory and Context</h3><p>Gemini CLI supports GEMINI.md files in subfolders to hold instructions specific to that directory. At the time of this writing, Gemini CLI does not support Claude Code-style skills; however, the latest versions appear to have experimental skills support, so it seems to be catching up.</p><p>Where Claude Code has plugins, Gemini CLI has extensions. The Gemini team maintains a curated extensions catalog. Gemini extensions can define custom commands and, soon, skills.</p><h3>Agent Performance</h3><h4>Context Window Size</h4><p>Gemini CLI uses a 1-million-token context window. Claude Code uses a 200,000-token context window by default, though under certain conditions it can expand to 1 million tokens—any requests exceeding 200,000 tokens are charged at premium rates. This gives Gemini CLI a significant advantage when performing analysis on large codebases, enabling more comprehensive analysis at lower cost.</p><h4>Models</h4><p>Where Claude Code uses one model at a time, Gemini CLI starts up in &quot;auto&quot; mode. In auto mode, it evaluates the complexity of each task and routes it—at the time of this writing—to either Gemini 2.5 Pro, Gemini 2.5 Flash, or Gemini 2.5 Flash Lite.</p><p>It is also possible to select a particular model and use it exclusively, although auto mode works well and is more economical, as many prompts are routed to the Flash and Flash Lite models. Gemini 3 Pro is available if experimental features are enabled.</p><h4>Model Performance and Availability</h4><p>Gemini CLI performs very well in auto mode. It successfully set up a modern toolchain for developing a Rust/WebAssembly-based web application, established a scalable codebase structure, and understood the architecture I instructed it to implement.</p><h4>Auto Mode</h4><p>When in auto mode and not affected by model availability issues, I could not find a task it couldn&#39;t handle. Having used Claude Code extensively in my professional life, I can&#39;t identify anything Claude Code would have done better.</p><h4>Gemini 2.5 Pro</h4><p>As suggested above, Gemini 2.5 Pro is very capable—on par, from my perspective, with current Claude models—but it suffers from availability problems.</p><p>Between December 18 and December 31, I was constantly encountering capacity issues despite being on a paid plan. I double-checked: this is not a quota problem; it&#39;s a compute shortage on Google&#39;s side. The only workaround is to manually switch to a lesser model such as Gemini 2.5 Flash.</p><pre> {<br>    &quot;error&quot;:{&quot;message&quot;:&quot;{  <br>    &quot;error&quot;: {   <br>      &quot;code&quot;: 503,    <br>      &quot;message&quot;: &quot;The model is overloaded. Please try again later.&quot;,    <br>      &quot;status&quot;: &quot;UNAVAILABLE&quot;  <br>    }<br>  },<br>  &quot;code&quot;:503,<br>  &quot;status&quot;:&quot;Service Unavailable&quot;<br>  }<br>}</pre><p><strong>These availability issues make Gemini CLI unsuitable for production use in its current state. </strong>Hopefully this is a transitional issue; I&#39;ll report back after further testing.</p><p>Apart from availability, I encountered only minor issues with 2.5 Pro:</p><p><strong>Occasional regressions: </strong>The situation improved significantly after I instructed it to add a note in GEMINI.md to execute tests after every change and ensure they all pass.</p><p><strong>Mermaid.js diagram issues: </strong>I asked it to generate a Markdown file documenting the architecture with embedded Mermaid.js diagrams. One sequence diagram simply would not render, and despite providing the exact syntax error, it could not fix it after several iterations. Copying the Mermaid.js code into the Claude web app and describing the problem was enough for Claude Opus 4.5 to fix the issue in one iteration.</p><h4>Gemini 2.5 Flash</h4><p>Where 2.5 Pro seems on par with current Claude models, 2.5 Flash simply is not.</p><p>Tasks take significantly longer to complete with 2.5 Flash than with 2.5 Pro, and latency increases exponentially with task complexity. Beyond the problematic latency, I also encountered the following issues:</p><p><strong>Infinite loops: </strong>Fortunately, it has a loop detection feature that stops after a few iterations, preventing budget exhaustion. Multiple times it got stuck in a loop mid-task. When this happens, the codebase remains in an inconsistent state, and you need to roll back to the last stable point and start again.</p><p><strong>Hallucinations: </strong>Particularly regarding task completion status. Multiple times I asked it to fix a bug, and it did so successfully. Later, while working on unrelated new tasks, it kept believing the bug was unfixed and repeatedly attempted to fix it again—even after I explicitly confirmed the bug was resolved. Each time I had to terminate the request and start over.</p><p><strong>Illogical behavior: </strong>Multiple times, while working on new features, it removed previously implemented features from the codebase. The new feature was in no way conflicting with the removed feature. When questioned, it simply re-added the removed feature.</p><p><strong>Conclusion: </strong>Gemini 2.5 Flash is not suitable for professional AI-assisted development due to the issues above. If you are on the free plan with a tight budget, you can try it, but don’t expect it to solve complex development tasks the way Gemini 2.5 Pro or Claude models would.</p><p>Fortunately, if you don’t encounter 2.5 Pro capacity issues, you can stay in auto mode, and the agent will only route to 2.5 Flash requests it is capable of handling.</p><h4>Gemini 3 Pro</h4><p>Gemini CLI has experimental support for Gemini 3 Pro, which is impressively capable. You can use it a reasonable amount before exhausting your quota. It’s a truly powerful model that impressed me on several requests — fast and precise. I don’t think Claude’s Opus 4.5 would have performed significantly better.</p><h3><strong>Conclusion</strong></h3><p>Gemini CLI is a promising tool for AI-assisted development with several advantages, including its large context window and intelligent auto-routing between models. However, significant availability issues with Gemini 2.5 Pro and reliability problems with 2.5 Flash currently limit its viability for professional use.</p><p>For engineering managers considering adoption: I recommend waiting until Google resolves the capacity constraints. For personal projects or exploratory work where some downtime is acceptable, Gemini CLI in auto mode offers a compelling, cost-effective alternative to Claude Code.</p><p>To be continued <a href="https://medium.com/@entzik/gemini-cli-the-long-run-4926143646f0">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5baf3e03309f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JSON vs Protocol Buffers — a performance comparison]]></title>
            <link>https://entzik.medium.com/json-vs-protocol-buffers-4771762663ea?source=rss-5e7b331e1e59------2</link>
            <guid isPermaLink="false">https://medium.com/p/4771762663ea</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[protobuf]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[json]]></category>
            <category><![CDATA[grpc]]></category>
            <dc:creator><![CDATA[Emil Kirschner]]></dc:creator>
            <pubDate>Fri, 28 Apr 2023 09:23:16 GMT</pubDate>
            <atom:updated>2023-04-28T09:23:59.783Z</atom:updated>
            <content:encoded><![CDATA[<p>JSON and Protobuf are two different formats used for data interchange. JSON, which stands for JavaScript Object Notation, is widely used and is the default format for implementing RESTful style APIs.</p><p>Protobuf, or Protocol Buffers, is less common and is mainly used with the gRPC framework. This article will compare the two formats based on payload size and encoding/decoding performance, and explore whether the payload’s nature affects these factors.</p><h3>Methodology</h3><p>We will perform a test to compare the serialization of identical data structures in JSON using the Jackson library and Protobuf. The payloads we will be testing will have different payload types, one with a string-heavy payload and another with a number-heavy payload. We will also vary the length of the field names.</p><p>To conduct the test, we will use the JMH micro benchmark harness framework in Java. The JMH protocol will perform multiple JVM warm-up runs before running the actual benchmark, and will correct the measurements for any statistical anomalies.</p><p>The serialized payload will consist of an array of 100 objects, each containing five fields of either strings or numbers. The data structures of the payloads we’ll be using for our test are displayed below.</p><h4>Number heavy payloads</h4><pre>public class ShortNamesNumbers {<br>    double sn1;<br>    double sn2;<br>    double sn3;<br>    double sn4;<br>    double sn5;<br><br>    public ShortNamesNumbers() {<br>    }<br><br>    public ShortNamesNumbers(double sn1, double sn2, double sn3, double sn4, double sn5) {<br>        this.sn1 = sn1;<br>        this.sn2 = sn2;<br>        this.sn3 = sn3;<br>        this.sn4 = sn4;<br>        this.sn5 = sn5;<br>    }<br><br>    // + all the getters and setters<br>}<br><br>public class LongNamesNumbers {<br>    double longName1;<br>    double longName2;<br>    double longName3;<br>    double longName4;<br>    double longName5;<br><br>    public LongNamesNumbers() {<br>    }<br><br>    public LongNamesNumbers(double longName1, double longName2, double longName3, double longName4, double longName5) {<br>        this.longName1 = longName1;<br>        this.longName2 = longName2;<br>        this.longName3 = longName3;<br>        this.longName4 = longName4;<br>        this.longName5 = longName5;<br>    }<br><br>    // + all the getters and setters<br>}</pre><pre>message ShortNamesNumbers {<br>  double sn1 = 1;<br>  double sn2 = 2;<br>  double sn3 = 3;<br>  double sn4 = 4;<br>  double sn5 = 5;<br>}<br><br>message ShortNamesNumbersList {<br>  repeated ShortNamesNumbers elements = 1;<br>}<br><br>message LongNamesNumbers {<br>  double longName1 = 1;<br>  double longName2 = 2;<br>  double longName3 = 3;<br>  double longName4 = 4;<br>  double longName5 = 5;<br>}<br><br>message LongNamesNumbersList {<br>  repeated LongNamesNumbers elements = 1;<br>}</pre><h4>String heavy payloads</h4><pre>public class ShortNamesStrings {<br>    String sn1;<br>    String sn2;<br>    String sn3;<br>    String sn4;<br>    String sn5;<br><br>    public ShortNamesStrings() {<br>    }<br><br>    public ShortNamesStrings(String sn1, String sn2, String sn3, String sn4, String sn5) {<br>        this.sn1 = sn1;<br>        this.sn2 = sn2;<br>        this.sn3 = sn3;<br>        this.sn4 = sn4;<br>        this.sn5 = sn5;<br>    }<br><br>    // + all the getters and setters<br>}<br><br>public class LongNamesStrings {<br>    String longName1;<br>    String longName2;<br>    String longName3;<br>    String longName4;<br>    String longName5;<br><br>    public LongNamesStrings() {<br>    }<br><br>    public LongNamesStrings(String longName1, String longName2, String longName3, String longName4, String longName5) {<br>        this.longName1 = longName1;<br>        this.longName2 = longName2;<br>        this.longName3 = longName3;<br>        this.longName4 = longName4;<br>        this.longName5 = longName5;<br>    }<br><br>    // + all the getters and setters<br>}</pre><pre>message ShortNamesStrings {<br>  string sn1 = 1;<br>  string sn2 = 2;<br>  string sn3 = 3;<br>  string sn4 = 4;<br>  string sn5 = 5;<br>}<br><br>message ShortNamesStringsList {<br>  repeated ShortNamesStrings elements = 1;<br>}<br><br>message ShortNamesStrings {<br>  string sn1 = 1;<br>  string sn2 = 2;<br>  string sn3 = 3;<br>  string sn4 = 4;<br>  string sn5 = 5;<br>}<br><br>message ShortNamesStringsList {<br>  repeated ShortNamesStrings elements = 1;<br>}</pre><h4>Protocol</h4><p>To ensure that we measure only serialization and deserialization, we constructed the payloads in “State” classes, and we used a Blackhole instance to consume the serialization/deserialization results so that the JVM does not drop the execution of code that produces a result which is not used. The payloads are serialized and deserialized to and from memory buffers to avoid involving I/O operations. This is important because interacting with I/O devices may introduce unpredictable latency and skew test results.</p><p>We also took steps to eliminate other potential sources of entropy during the testing. We stopped all unnecessary processes running on the computer and configured power management to prevent CPU throttling or screen saver activation during the benchmarks.</p><p>To reduce the statistical margin of error in the results, we configured JMH with 10 warmup iterations and 20 run iterations for each experiment.</p><p>The source code for this experiment is available on GitHub at <a href="https://github.com/entzik/json-proto-jmh-benchmark">https://github.com/entzik/json-proto-jmh-benchmark</a>.</p><h3>Results</h3><p>The raw JMH run report shows Protocol Buffers bellow is significantly faster than JSON:</p><pre>Benchmark                                                  Mode  Cnt       Score       Error  Units<br>CodecBenchmark.benchmarkJsonDecodeLongNamesNumbers        thrpt   20   16957.714 ±    35.877  ops/s<br>CodecBenchmark.benchmarkJsonDecodeLongNamesStrings        thrpt   20   31819.991 ±    22.943  ops/s<br>CodecBenchmark.benchmarkJsonDecodeShortNamesNumbers       thrpt   20   17277.170 ±    35.430  ops/s<br>CodecBenchmark.benchmarkJsonDecodeShortNamesStrings       thrpt   20   32346.328 ±   130.468  ops/s<br>CodecBenchmark.benchmarkJsonEncodeLongNamesNumbers        thrpt   20   23763.468 ±    40.468  ops/s<br>CodecBenchmark.benchmarkJsonEncodeLongNamesStrings        thrpt   20   68919.743 ±   277.151  ops/s<br>CodecBenchmark.benchmarkJsonEncodeShortNamesNumbers       thrpt   20   23987.768 ±    19.126  ops/s<br>CodecBenchmark.benchmarkJsonEncodeShortNamesStrings       thrpt   20   69156.867 ±   140.704  ops/s<br>CodecBenchmark.benchmarkProtoDecodeLongNamesNumbers       thrpt   20  639524.868 ±   752.181  ops/s<br>CodecBenchmark.benchmarkProtoDecodeLongNamesStrings       thrpt   20  726800.119 ± 16395.402  ops/s<br>CodecBenchmark.benchmarkProtoDecodeShortNamesNumbers      thrpt   20  640153.820 ±   655.052  ops/s<br>CodecBenchmark.benchmarkProtoDecodeShortNamesStrings      thrpt   20  730835.859 ± 20201.826  ops/s<br>CodecBenchmark.benchmarkProtoEncodeLongNamesNumbers       thrpt   20  817332.157 ±   413.905  ops/s<br>CodecBenchmark.benchmarkProtoEncodeLongNamesStrings       thrpt   20  180108.769 ±   997.701  ops/s<br>CodecBenchmark.benchmarkProtoEncodeShortNamesNumbers      thrpt   20  819602.488 ±   694.377  ops/s<br>CodecBenchmark.benchmarkProtoEncodeShortNamesStrings      thrpt   20  185876.296 ±   918.987  ops/s</pre><p>The difference can be more easily visualized in the charts bellow.</p><h4>Encoding</h4><p>Let’s look at payload encoding first. <strong>Protocol Buffers is ~2.7 times faster than JSON when encoding string heavy payloads</strong> but it really shines at encoding numerical values: <strong>Protobuf is ~34 times faster than JSON when encoding numbers heavy payloads</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1013/1*Km3gs67OphaLCg6QAOGfIA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*K5tZEC854Oj1al6lRXSF1A.png" /></figure><p>The difference in performance between JSON and Protobuf can be easily explained. For numbers, where the difference is significant, JSON has to convert each number into a string, which is a highly inefficient operation in terms of CPU cycles. Protobuf, on the other hand, simply copies the bytes that represent the number as they are, which is a much more efficient operation. On big-endian architectures, there is a small exception where the numbers need to be converted to little-endian format used by Protobuf, but this is not a major issue because it only involves changing the byte order.</p><p>For strings, the difference is less significant because both JSON and Protobuf copy the strings as they are. However, the performance difference can be explained by the fact that JSON has to repeat the field name in front of every value, which is more verbose and uses more CPU cycles.</p><p>We noticed that the difference in throughput between having long or short field names in JSON was not significant, and the difference in Protobuf was negligible. This was expected because Protobuf does not include field names in the payload, but instead uses numerical tags specified in the schema.</p><h4>Decoding</h4><p>No let’s have a look at decoding performance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qEvOEnttRJAgdSmsKJ5N9A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ZVy24nV-OLf3YXj5p7aHQ.png" /></figure><p><strong>Protocol Buffers is ~23 times faster than JSON when decoding string heavy payloads.</strong> Again, Protobuf shines when decoding numerical values: <strong>Protobuf is ~38 times faster than JSON when encoding decoding numbers heavy payloads</strong>.</p><p>The performance difference between JSON and Protobuf for number heavy payloads is due to the fact that JSON requires the numbers to be parsed, which is a very resource-intensive operation, while Protobuf just copies the bytes around.</p><p>For string heavy payloads, the performance difference is less pronounced, but Protobuf is still significantly faster than JSON. Although strings are copied as they are, JSON requires a fair amount of parsing for both field names and values.</p><p>The difference in throughput between long and short field names is negligible for decoding.</p><h3>Payload Size</h3><p>As anticipated, the size of JSON payloads is larger compared to equivalent Protobuf payloads. This is mainly due to the fact that JSON is a text-based, verbose format, while Protobuf is a binary and concise format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qr-17Zlf5zJ3sGtkb1_TtA.png" /><figcaption>serialized payload size, in bytes</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vFgiqZiBXfmN51h495ejWg.png" /><figcaption>serialized payload size, in bytes</figcaption></figure><p>We notice that the size of JSON payloads varies significantly with the length of fields names. This is expected, since field names are repeated in the JSON content.</p><h3>Conclusions</h3><p>After analyzing the test results, it’s evident that Protobuf is superior to JSON for machine-to-machine communication in every aspect. It outperforms JSON in terms of time and space complexity for both serialization and deserialization, and also provides type safety and greater expressiveness.</p><p>However, JSON has its advantages as a human-friendly format. Its text-based nature enables easy editing of payloads by humans, and it has native support in browser tooling.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4771762663ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Doodle In DAML — Part 2]]></title>
            <link>https://entzik.medium.com/a-doodle-in-daml-part-2-910614d94c62?source=rss-5e7b331e1e59------2</link>
            <guid isPermaLink="false">https://medium.com/p/910614d94c62</guid>
            <category><![CDATA[dlt]]></category>
            <category><![CDATA[daml]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Emil Kirschner]]></dc:creator>
            <pubDate>Wed, 11 Nov 2020 22:28:19 GMT</pubDate>
            <atom:updated>2020-11-12T08:15:54.996Z</atom:updated>
            <content:encoded><![CDATA[<h3><a href="https://medium.com/@entzik/a-doodle-in-daml-part-1-d2ef18bbf7e8?source=your_stories_page-------------------------------------">A Doodle In DAML — Part </a>2</h3><p>In the <a href="https://medium.com/@entzik/a-doodle-in-daml-part-1-d2ef18bbf7e8">previous post</a> we started to look at how to implement a doodle using smart contracts written in DAML. The last code snippet we looked at was the IssueInvites choice, which is used to send out invites to each of the voters. There’s one more detail about the code in that choice that I’d like to point out.</p><h3>Divulged Contracts</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c31509e9bafce72d0bea79f4dbce4c4/href">https://medium.com/media/4c31509e9bafce72d0bea79f4dbce4c4/href</a></iframe><p>Remember we mentioned that <strong><em>IssueInvites</em></strong> is a <strong><em>preconsuming</em></strong> choice. Exercising it will archive the current version of the contract before executing the choice code. Let’s see why this is important.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*d4KI2sHvxvs6lS0Eq6jOMA.png" /></figure><p>If the choice were not <strong><em>preconsuming</em></strong>, the version of the Doodle contract on which you exercise the choice would still exist when the contracts are created.</p><p>As a result of that, as shown on the screenshot on the left, every invite contract is going to be <a href="https://docs.daml.com/concepts/ledger-model/ledger-privacy.html"><strong><em>divulged</em></strong></a> to every voter.</p><blockquote>The fact that the invite contract is <strong><em>divulged</em></strong> means that parties which are neither signatories or observers of the <strong><em>VotingInvite</em></strong> contract, will see a copy of it once, when the contract is created. That’s because the invite contract was created as a consequence of exercising the <strong><em>IssueInvites</em></strong> choice on the <strong><em>Doodle</em></strong> contract, on which the voters are observers.</blockquote><blockquote>It should be noted that the parties to which a contract is divulged will only see that contract once when it is created. Any further attempt by these parties to fetch or exercise choices on the divulged contract will fail.</blockquote><p>Divulging the invites is not necessarily something we want to do, as “<strong><em>Voter1</em></strong>” has no business getting their hands on an invite meant for “<strong><em>Voter 2”</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*fGHtZp-pk88sBuEzlrRgXw.png" /></figure><p>In order to fix this problem we make the choice “<strong><em>preconsuming</em></strong>”, and the result can be seen in the screenshot on the left.</p><p>This is what is happening: the current version of the contract is archived before the choice is executed, and that means that there are no voters around to witness the creation of the invite contracts, and as such, the invite contracts are not <a href="https://docs.daml.com/concepts/ledger-model/ledger-privacy.html">divulged</a> to unwanted parties. Each voter only sees their own invite because they are an observer thereof.</p><h3>The Doodle Voting Invite</h3><p>Now that we’ve seen how the doodle organizer issues voting invites, let’s have a look at what those invites look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9423390c56c11fc68278bdc2cff20e26/href">https://medium.com/media/9423390c56c11fc68278bdc2cff20e26/href</a></iframe><p>The DoodleInvite template holds the doodle name, the organizer and the recipient voter.</p><p>Invite contracts are owned by the <strong><em>organizer</em></strong> because they decide who can vote in the doodle. This way, the organizer get the opportunity to withdraw an invite by archiving the invite contract.</p><p>The key of the invite contract is a triplet, made of the <strong><em>organizer</em></strong>, the <strong><em>voter</em></strong> and the <strong><em>doodle name</em></strong>. The organizer will therefore not be able to invite a voter more than once on the same doodle.</p><p>The <strong><em>voter</em></strong> is an observer on the invite contract. This will allow the voter party will be notified about the contract being created and, most importantly, act as a controller when exercising the “<strong><em>Vote”</em></strong> choice.</p><p>Let’s look into more detail at the “<strong><em>Vote”</em></strong> choice:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/633c84f6d7f62d00419d37fab9dfc502/href">https://medium.com/media/633c84f6d7f62d00419d37fab9dfc502/href</a></iframe><p>The choice takes just one parameter, and that’s the option we want to cast a vote for, and returns a Doodle updated with the vote.</p><p>You’ll also notice that the choice is “<strong><em>nonconsuming”</em></strong>, which means that the invite contract will not be archived after exercising this choice. That’s because a voter is allowed to use the invite to vote for more than one option, and so we need the invite contract around after casting the first vote.</p><p>First thing, the voter needs to verify that the doodle actually exists, so in line 6 we are looking for a Doodle contract signed by the <strong><em>organizer</em></strong> and having the name specified on the invite. This will return an optional wrapping a contract ID.</p><blockquote>DAML Optionals work just like <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Optional.html">Java Optionals</a>. Like in Java, an optional can either wrap something or be empty. But remember DAML is functional, not object oriented, so instead of calling methods on the optional itself to extract its value, you’ll use functions from the <a href="https://docs.daml.com/daml/stdlib/DA-Optional.html">DA.Optional</a> module.</blockquote><p>In line 7 we attempt to extract the contract ID from the optional using the <a href="https://docs.daml.com/daml/stdlib/DA-Optional.html#function-da-optional-fromsome-58212">fromSome</a> function and use it to fetch the contract payload. If the contract we are looking for does not exist, this is where it will fail.</p><blockquote>You may be wondering why in lines 6 and 7 we are using the “<strong>←</strong>” operator for assignment. That is because the “<strong><em>lookupByKey”</em></strong> and “<strong><em>fetch”</em></strong> instructions are not executed on the participant node, but somewhere on the ledger. “<strong>a←b</strong>” means “execute the instruction b somewhere on the ledger, then retrieve the result on the machine where this code is running and assign it to variable a”.</blockquote><p>On line 8, we are checking that the organizer on the invite matches the organizer on the doodle contract. This way a voter will not be able to impersonate the organizer on a doodle by issuing fake invites.</p><p>Ultimately, on line 9, we exercise the <strong><em>CastVote</em></strong> choice on <strong><em>doodle</em></strong> by specifying the <strong><em>voter</em></strong>, the <strong><em>option</em></strong> the voter is voting for and the ID of the <strong><em>invite</em></strong> contract itself. Passing the invite contract ID is necessary so that the doodle’s <strong><em>CastVote</em></strong> choice can ensure that the voter casting the vote was actually issued an invite to do so.</p><blockquote>Fun fact about DAML contracts: remember how in various programing languages an object can reference itself using either the “<strong><em>this”</em></strong> (Java, C++) or “<strong><em>self”</em></strong> (Python, Objective-c) keywords ? Well, DAML has both, but they don’t mean the same thing:</blockquote><blockquote>“<strong><em>this</em></strong>” is a contract ID, very much like in Java or C++ where “<strong><em>this”</em></strong> is a pointer to an object. The analogy is even more clear if we think about the a <strong><em>ContractId</em></strong> as a pointer that points to a contract stored somewhere on the ledger, just like a java or C++ pointer points to an object somewhere in memory.</blockquote><blockquote>“<strong><em>self</em></strong>” can be used to reference the contract payload itself. It is literally the equivalent of “<strong><em>*this</em></strong>” in C++.</blockquote><h3>The Doodle’s CastVote Choice</h3><p>To quickly recap: in order for a voter to effectively vote, they need to receive a <strong><em>DoodleInvite</em></strong> contract and exercise the <strong><em>Vote</em></strong> choice on it. The <strong><em>Vote</em></strong> choice will then look up he <strong><em>Doodle</em></strong> contract by name and organizer, and exercise <strong><em>CastVote</em></strong> on it.</p><p>So let’s see what this <strong><em>CastVote</em></strong> choice looks like.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5857f4316ea20ead84924f3b95b7df55/href">https://medium.com/media/5857f4316ea20ead84924f3b95b7df55/href</a></iframe><h4>CastVote Parameters</h4><p><strong><em>CastVote</em></strong> takes three parameters:</p><ul><li>the voter</li><li>the option the voter wants to cast a vote for</li><li>the ID of the <strong><em>DoodleInvite</em></strong> the voter has received</li></ul><p>It is obvious why we need to know who the voter is and what he intends to vote for in order to cast the vote. The invite contract ID however, is a little trick meant to prevent fraud.</p><h4>Making sure the vote is legitimate</h4><p>In line 8 we are fetching the invite contract. If we succeed, it will guarantee that the invite provided by the user actually exists. Once we get hold of the invite payload, we can go on and assert a certain number of conditions to make sure the vote is legitimate:</p><ul><li>in line 9 we ensure that the invite the voter has received was issued for the doodle the voter is trying to cast a vote on</li><li>in line 10, we check that the party exercising the <strong><em>CastVote</em></strong> choice is the one who received the invite. This check is defending against malicious parties hijacking the <strong><em>Doodle</em></strong> and voting on behalf of somebody they are not. Only if the controller party is able to produce an invite in their name is the vote allowed to go through.</li><li>in line 11 we make sure the invite provided by the voter was issued by the Doodle’s organizer. This defends against a situation where a voter with a valid invite for doodle A would try to use that to vote on a <strong><em>Doodle</em></strong> with the same name, but created by a different organizer.</li><li>in line 12 we check that the <strong><em>Doodle</em></strong> is open for voting</li><li>and finally in lines 13 and 14, we check that the controller is actually a registered voter and that the option they’re trying to cast a vote for is a valid one for this <strong><em>Doodle</em></strong>.</li></ul><p>If all checks go trough it means we are confident enough that the vote is legitimate and the <strong>Doodle</strong> is ready to receive it.</p><h4><strong><em>Using TextMap to keep count of the votes</em></strong></h4><p>Let’s zoom into the code that’s actually casting the vote.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/90634aa366a4efd8070407773e27a4c9/href">https://medium.com/media/90634aa366a4efd8070407773e27a4c9/href</a></iframe><p>There are a few syntax elements here that we did not see before. First, in line 4 we start a “<strong><em>let</em></strong>” block. A “<strong><em>let”</em></strong> block lets you define values (variables) and bind them in the scope of the following code. All expressions in the let block are evaluated locally on the participant node.</p><p>So let’s have a closer look at what’s going on in there.</p><p><strong>In line 5</strong> we retrieve the current state of votes for the option we are about to cast a vote for.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d40fe05b5563d026d8849d4f9a5a18c/href">https://medium.com/media/5d40fe05b5563d026d8849d4f9a5a18c/href</a></iframe><p>The goal here is to retrieve the VotingSlot that holds the votes for the specified option. To do so, we call <a href="https://docs.daml.com/daml/stdlib/DA-TextMap.html#function-da-textmap-lookup-99332"><em>DA.TextMap.lookup</em></a><em> </em>on the <a href="https://docs.daml.com/daml/stdlib/Prelude.html#type-da-internal-lf-textmap-34396"><strong>votes</strong></a> <a href="https://docs.daml.com/daml/stdlib/Prelude.html#type-da-internal-lf-textmap-34396">TextMap</a>, as you can see on the rightmost side of the expression.</p><p><a href="https://docs.daml.com/daml/stdlib/DA-TextMap.html#function-da-textmap-lookup-99332"><em>DA.TextMap.lookup</em>,</a> however, returns an Optional of type VotingSdlot<em> .</em> To extract the actual VotingSlot from it, we use the <a href="https://docs.daml.com/daml/stdlib/DA-Optional.html#function-da-optional-fromoptional-40876">DA.Optional.fromOptional</a> function.</p><p>fromOptional takes a fallback value and an optional. The fallback value will be returned if the optional — in our case, returned by <a href="https://docs.daml.com/daml/stdlib/DA-TextMap.html#function-da-textmap-lookup-99332"><em>DA.TextMap.lookup</em></a>— is empty. This can be the case if the <strong><em>votes</em></strong> map does not contain the option in its key set and that happens if nobody has voted for that option yet. The fallback value is a VotingSlot with a votes count of zero and an empty voters array — because if the fallback value is returned, it means that nobody has voted for that option yet.</p><p><strong><em>In line 6</em></strong> we update the votes TextMap with a new VotingSlot that reflects the new vote:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88b92dcf0bea3d35bbcff60edea385d4/href">https://medium.com/media/88b92dcf0bea3d35bbcff60edea385d4/href</a></iframe><p>Here we use <a href="https://docs.daml.com/daml/stdlib/DA-TextMap.html#function-da-textmap-insert-2213">DA.TextMap.insert</a> to insert an updated VotingSlot into the votes TextMap.</p><p><a href="https://docs.daml.com/daml/stdlib/DA-TextMap.html#function-da-textmap-insert-2213">DA.TextMap.insert</a> takes three parameters: the key, the value associated with the key, the map in which the key / value pair will be inserted.</p><p>If the TextMap does not already contain an entry for the specified key, one will be created. If one exists, the value previously associated with the key will be replaced with the new one.</p><p>The value we insert is the updated VotingSlot:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6606c7fef2f98ee8cb0bc8f88ec8c95d/href">https://medium.com/media/6606c7fef2f98ee8cb0bc8f88ec8c95d/href</a></iframe><p>The new VotingSlot is created with an incremented <strong><em>VotesCiount</em></strong> and an updated <strong><em>voters</em></strong> list.</p><p><strong><em>In line 7 — 8</em></strong> we create an updated version of the Doodle with a new <strong><em>votes</em></strong> map, but not before we check that the user has not already voted for the same option:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/062d33fd5b75ecbde0a58ef6356ec0e5/href">https://medium.com/media/062d33fd5b75ecbde0a58ef6356ec0e5/href</a></iframe><p>This should be read as: “create a new contract, identical to this one except for the votes argument, which should be replaced with updatedVotes”. Since the choice is not marked as “<strong><em>nonconsuming</em></strong>”, the contract will archive itself.</p><p>This wraps up the actual smart contract functionality for our Doodle. In the next iteration, we’ll be looking at how to properly and exhaustively test it.</p><p><strong><em>Proofreading credits</em></strong>: Peter Schubert</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=910614d94c62" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Doodle In DAML — Part 1]]></title>
            <link>https://entzik.medium.com/a-doodle-in-daml-part-1-d2ef18bbf7e8?source=rss-5e7b331e1e59------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2ef18bbf7e8</guid>
            <category><![CDATA[dlt]]></category>
            <category><![CDATA[daml]]></category>
            <category><![CDATA[digital-assets-management]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Emil Kirschner]]></dc:creator>
            <pubDate>Mon, 19 Oct 2020 21:02:37 GMT</pubDate>
            <atom:updated>2020-10-20T06:28:21.495Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Doodle In DAML — Part 1</h3><p>In my previous post, <a href="https://medium.com/@entzik/daml-smart-contracts-appetizer-2542e590bcf3">DAML Smart Contracts Appetizer</a>, I introduced the DAML smart contracts programming language and runtime and explained why I was so enthusiastic about it. I would now like to get into a slightly more complex example: <a href="https://github.com/entzik/daml-examples/blob/master/doodle/daml/Com/Thekirschners/Daml/Doodle.daml">a Doodle written in DAML</a>.</p><p>If you looked up DAML code on the internet and if your previous programming experience is with imperative, object oriented programming languages such as Java or C++, the <a href="https://www.haskell.org/">Haskell</a> style, purely functional DAML syntax may not come naturally for you. To make it easier to understand, and as we go through the DAML Doodle, we’ll draw parallels with equivalent imperative language constructs.</p><h3>What’s a Doodle?</h3><p>A Doodle is an ad-hock voting system where an organizer invites people to vote on on a set of options. Each voter can vote exactly once for each option. Votes are visible, so all participants know who voted for what.</p><p>Our DAML doodle must be easy to use and secure:</p><ul><li>A user — hereafter referred to as the <strong><em>organizer</em></strong>— must be able to create a Doodle with a description, voting <strong><em>options</em></strong> and an initial set of <strong><em>voters</em></strong>. After creation, the Doodle will be in a “set-up” mode, that is, not <strong><em>open</em></strong> for voting.</li><li>The <strong><em>organizer</em></strong> must be able to add / remove <strong><em>voters</em></strong></li><li>The <strong><em>organizer</em></strong> must be able to open the Doodle for voting and issue invites to <strong><em>voters</em></strong>.</li><li>Each <strong><em>voter</em></strong> must be able to vote exactly once for one or more <strong><em>options</em></strong></li><li>Each <strong><em>voter</em></strong> must be able to observe the doodle</li><li>A <strong><em>voter</em></strong> should not be able to vote on behalf of another <strong><em>voter</em></strong></li></ul><h3>The DAML Doodle</h3><p>Since we are doing this in DAML, the doodle will of course be modeled as a DAML template, which, when instantiated, will be signed into a DAML contract by the organizer.</p><h4>VotingSlot — DAML Data Structures</h4><p>Before looking at the template, we are going to define a data structure that the template will use to collect votes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be995b2c1d2833279bc604bfab050f26/href">https://medium.com/media/be995b2c1d2833279bc604bfab050f26/href</a></iframe><p>VotingSlot is used to count the votes for an option: it contains an integer that represents the vote count, and a list of parties containing the voters who have voted for that option.</p><p>The <strong><em>data</em></strong> keyword is used to define a pure data structure. This is very similar to a <a href="https://www.tutorialspoint.com/cprogramming/c_structures.htm">C/C++ struct</a>, a <a href="https://openjdk.java.net/jeps/359">Java record</a> or a <a href="https://docs.scala-lang.org/tour/case-classes.html">Scala case class</a>, but strictly immutable.</p><p>Coming from other languages, you may be tempted to call the “voted” field an <strong><em>array</em></strong>, most likely because it’s surrounded by square brackets. In DAML vocabulary that’s not an <strong><em>array</em></strong>, it’s a <strong><em>list</em></strong> and as every data structure in DAML, it’s also immutable.</p><p>In line 5 we specify that our data structure derives the “<strong><em>Eq”</em></strong> and “<strong><em>Show”</em></strong> interfaces. This is the equivalent of implementing the “<a href="https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)"><strong><em>equals</em></strong></a>” and “<a href="https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()"><strong><em>toString</em></strong></a>” methods on a <strong><em>Java</em></strong> class.</p><h4>Doodle Template Declaration</h4><p>To declare the Doodle template we use the “<strong><em>template</em></strong>” keyword as in the code fragment below.</p><p>A DAML <strong><em>template</em></strong> is the equivalent of a <strong><em>class</em></strong> in object oriented languages such as Java, Scala or C++. And while a <strong><em>class</em></strong> is instantiated into an <strong><em>object</em></strong>, a <strong><em>DAML template</em></strong> is instantiated into a <strong><em>contract</em></strong>.</p><p>As opposed to a Java , Scala or C++ instance objects, DAML contracts are immutable. This means that once created, a contract cannot be changed. Instead, it can be archived and a new version of itself can be created.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/21e997430c798b99d0ed733e710276da/href">https://medium.com/media/21e997430c798b99d0ed733e710276da/href</a></iframe><h4>Template Parameters</h4><p>If you are an imperative object oriented programmer, you should think about a <strong><em>template</em></strong> and its parameters as a class with a single constructor that takes all those parameters.</p><p>Let’s have a quick look at those parameters and their respective data types:</p><ul><li><strong><em>name </em></strong>—of type <strong><em>Text</em></strong>, which in <strong><em>Java</em></strong> and <strong><em>Scala</em></strong> would map to a <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html"><strong><em>java.lang.String</em></strong></a> and in <strong><em>C++</em></strong> it would be an <a href="http://www.cplusplus.com/reference/string/string/"><strong><em>std::string</em></strong></a>.</li><li><strong><em>organizer </em></strong>— of type <strong><em>Party</em></strong>. A <strong><em>Party</em></strong> is a native DAML type representing an entity that interacts with the ledger. It can be a natural person or an organization.</li><li><strong><em>voters </em></strong>— the list (not array) of <strong><em>type Party </em></strong>representing parties the organizer has invited to vote.</li><li><strong>options</strong> —a list of type <strong><em>String</em></strong> representing options voters can vote for</li><li><strong><em>votes </em></strong>—of type <strong><em>TextMap</em></strong>. This is a key-value map where the key is a Text and the value is of the specified type — in our case the <strong><em>VotingSlot</em></strong> data structure. In Java this would be a <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Map.html"><strong><em>java.util.Map</em></strong></a> and in C++ a <a href="https://en.cppreference.com/w/cpp/container/map"><strong><em>std:map</em></strong></a>.</li><li><strong>open </strong>— a boolean that controls the state of the doodle. When set to false, the organizer can still add or remove voters and options, voting is not possible yet. When set to true, voting is possible, but the organizer will not be able to change voter and options any more.</li></ul><h4>Signatories, Observers and Constraints</h4><p>The code snippet below shows how to define contract signatories, observers and integrity constraints.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a0a88ccae806c44e5b65fb342ed2116/href">https://medium.com/media/5a0a88ccae806c44e5b65fb342ed2116/href</a></iframe><h4>Signatory</h4><p>In line 2 we specify that the <strong><em>organizer</em></strong> party is a contract <strong><em>signatory</em></strong>. The signatory is the party that must agree to the creation of the contract. By agreeing to the creation of the contract the signatory also agrees to all the consequences of contract choices being exercised on the contract by itself or entitled third parties.</p><h4>Observers</h4><p>In line 3, we specify that all the parties in the <strong><em>voters</em></strong> list are <strong><em>observers</em></strong> of the contract. You should keep in mind that all signatories are also observers. Observers are allowed to see the content of the contract.</p><h4>Integrity</h4><p>In line 4, it gets more interesting: we don’t want a voter to be able to register twice, and we also don’t want duplicate options.</p><p>To enforce this rule, we use the “<strong><em>ensure</em></strong>” keyword followed by a boolean expression.</p><p>We use the “<a href="https://docs.daml.com/daml/stdlib/DA-List.html#function-da-list-unique-61019"><strong><em>unique</em></strong></a>” function to check if a list only contains distinct elements. “<strong><em>unique</em></strong>” is a function in the DA.List module. You should think of <strong><em>DAML modules</em></strong> as of <strong><em>java packages</em></strong> or <strong><em>C++ namespaces</em></strong>. After calling “<strong><em>unique</em></strong>” on both the <strong><em>voters</em></strong> and the <strong><em>options</em></strong> array, we combine the result using the “<strong><em>&amp;&amp;</em></strong>” logical operator.</p><blockquote>As a side note, you probably have noticed that in DAML, when invoking a function, parameters are not places between braces. They are just added after the function name.</blockquote><h4>The Key</h4><p>In lines 13 and 14 we’re setting up a contract key. This is a business key, as opposed to the contract’s ID which is a technical primary key.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/976dcc4081092789bc2fdb22db183c6f/href">https://medium.com/media/976dcc4081092789bc2fdb22db183c6f/href</a></iframe><p>A key is composed either of a signatory party or a tuple containing at least one signatory party and any number of other template parameters. One or more of the signatories that compose the key must be designated as “<strong>maintainer</strong>”. Maintainers own the key.</p><p>A key value is guaranteed to be unique per template.</p><p>In our Doodle template, the key is a tuple where the first element is a party (the organizer, who’s also the maintainer) and the second is a text (the name of the doodle). This will allow entitled parties to look up Doodle contracts if they know the organizer and the Doodle’s name.</p><blockquote>You’ll notice that in order to specify the maintainer we compute it from the key using the “<strong><em>fst key</em></strong>” expression. This may seem odd from an object oriented programming point of view where “<strong><em>fst</em></strong>” would be a behavioral feature of the tuple and you’d say something like “<strong><em>key.fst</em></strong>”. DAML however is a <strong><em>functional</em></strong> language, therefore <strong><em>fst</em></strong> is a function and you invoke it by passing the tuple as a parameter.</blockquote><h3>Choices, Contract Archival and Creation</h3><p>The organizer needs to be able to manage their doodle by adding and removing voters before issuing voting invites. Let’s have a look at the code below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75659aa5d5ea98fa3cc4c163ae6bfe7f/href">https://medium.com/media/75659aa5d5ea98fa3cc4c163ae6bfe7f/href</a></iframe><h4>Declaring a Choice</h4><p>Operations such as adding new voters, removing voters and opening the Doodle up for voting are achieved by exercising choices on the contract.</p><p>DAML contract choices are the equivalent a of public method or function in a Java, Scala or C++ class. Their declaration however is more verbose than what you’re used to from other programming languages.</p><p>To define a choice, you start with the “<strong><em>choice”</em></strong> keyword, followed by the name of the choice, a colon and the return data type.</p><p>If the choice requires parameters you’ll need to use the “<strong><em>with</em></strong>” keyword, followed by the parameters list, just like we’ve done for declaring the template’s parameters.</p><p>Up until now it’s the same as declaring a method in a class in other languages, except a bit more verbose. But it’s about to get more interesting: we can specify who is allowed to exercise the choice.</p><p>To do that, you need to use the “<strong><em>controller</em></strong>” keyword followed by a comma separated list of parties. These parties can be either template parameters or choice parameters.</p><p>Exercising the choice is only allowed if the party who initiated the transaction is one of the named controllers.</p><h4>Contract Archival</h4><p>We mentioned immutable. This means that in order to make changes to the contract, we need to archive the old version and create a new one.</p><p>Unless annotated with the “<strong><em>nonconsuming</em></strong>” keyword, choices will archive the current version of the contract. In order to create the new, updated version of the contract, we need to use the “create” keyword, as shown in lines 7 and 15 of the code snippet above.</p><p>When it comes to archiving contracts, choices can be “<strong><em>postconsuming”</em></strong>, “<strong><em>preconsuming”</em></strong> and “<strong><em>nonconsuming”</em></strong>.</p><p>Post-consuming choices will archive the current version of the contract after the choice execution has completed.</p><p>Pre-consuming choices will archive the current version of the contract before the choice execution has completed.</p><p>Non-consuming choices will not archive the current version of the contract</p><p>The difference between pre-consuming and post-consuming is subtle and consists merely in the set of parties which witness the creation of contracts created by exercising the choice.</p><h4>Creating a New Contract Version</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d355c91a3954e3d07a920a120394965a/href">https://medium.com/media/d355c91a3954e3d07a920a120394965a/href</a></iframe><p>The semantics of the <strong><em>create</em></strong> instruction is quite clever and compact. Instead of creating the new version of the contract by specifying the template name and repeating all it parameters, we say “<strong><em>create this with my_parameter=new_value</em></strong>”. This literally means “create a new contract, identical to this one, but set “<strong><em>my_parameter”</em></strong> to “<strong><em>new_value</em></strong>”</p><h3>Working With Lists</h3><p>Working with lists is going to look very familiar if you’re a <strong><em>Scala</em></strong> person. In DAML, list processing is implemented by functions in the <strong><em>DA.List</em></strong> and <strong><em>DA.Traversable</em></strong> modules.</p><h4>Adding an Element to a List</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aeb0b51d04bd4b90c92059cda0c5047a/href">https://medium.com/media/aeb0b51d04bd4b90c92059cda0c5047a/href</a></iframe><p>To add an element to the list you use the double colon operator as in the code fragment on the left. Remember, in DAML pretty much everything is immutable, and so are lists. So here, just like in Scala, you are not adding an element to the list, you are creating a new list by concatenating “<strong><em>voter</em></strong>” to the original “<strong><em>voters</em></strong>” list.</p><h4>Removing an Element from a List</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4784879999dc664377bfbac0f39cf4d1/href">https://medium.com/media/4784879999dc664377bfbac0f39cf4d1/href</a></iframe><p>To remove an element from a list, you can use the “<strong><em>DA.List.delete</em></strong>” function. Here again, you aren’t actually removing the element from the list, you are creating a new list with all the elements from the original one, in the same order, but without the one you want to remove.</p><h4>Transforming a List</h4><p>Remember from the Doodle specifications, in order to open it up for voting we need to issue an invite for each registered <strong><em>voter</em></strong>. This practically means that we need to issue an invite contract for each element in the voters list.</p><p>Functions to iterate over a list and process its elements are found in the <strong><em>DA.Traversable</em></strong> module.</p><p>Let’s have a look at how this works:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c31509e9bafce72d0bea79f4dbce4c4/href">https://medium.com/media/4c31509e9bafce72d0bea79f4dbce4c4/href</a></iframe><p>First thing to notice is that <strong><em>IssueInvites</em></strong> is a “<strong><em>preconsuming”</em></strong> choice. Exercising it will archive the current version of the contract before executing the choice code.</p><p>To transform each element of the list into a contract, we’re using the <strong><em>DA.Traversable.mapA</em></strong> function which takes two arguments:</p><ul><li>first — lines 6–7 in the code fragment above — a lambda that consumes a list element and creates a DoodleInvite contract out of it. <br>Creating a contract is quite self explanatory: use the “<strong><em>create</em></strong>” keyword followed by the template name and the list of named parameters.</li><li>second, the list the function will iterate over.</li></ul><p>mapA also collects all transformed elements in a list, but we are not interested in that for this use case.</p><p>To wrap up this choice we create a new version of the Doodle contract and set the open flag to true to prevent the voters and options lists from being changed again.</p><h3>To Be Continued</h3><p>This is it for now, hope it was interesting. In the next iteration of this series we’ll look at how each voter can exercise their right to vote using the voting invite contracts and how votes are counted in the Doodle.</p><p>Proofreading credits: Peter Schubert</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2ef18bbf7e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[DAML Smart Contracts Appetizer]]></title>
            <link>https://entzik.medium.com/daml-smart-contracts-appetizer-2542e590bcf3?source=rss-5e7b331e1e59------2</link>
            <guid isPermaLink="false">https://medium.com/p/2542e590bcf3</guid>
            <category><![CDATA[daml]]></category>
            <category><![CDATA[dlt]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[smart-contracts]]></category>
            <dc:creator><![CDATA[Emil Kirschner]]></dc:creator>
            <pubDate>Mon, 07 Sep 2020 20:22:42 GMT</pubDate>
            <atom:updated>2020-09-30T19:16:40.774Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>I am very enthusiastic about <a href="https://daml.com/">DAML</a>, and for good reason. DAML has a lot going for it:</p><ul><li>It’s a strongly typed, <a href="https://en.wikipedia.org/wiki/Functional_programming">functional programming</a> language, based on <a href="https://www.haskell.org/">Haskell</a></li><li>The language and its developer tools are open source</li><li>It comes with a smart contract test framework</li><li>DAML has private transactions out of the box, controlled using a declarative, easy to understand permissions system, without requiring deployment of extra private transaction management software.</li><li>Transaction participants are modeled as a native DAML data type (Party)</li><li>It has a “<em>decimal</em>” data type and can natively do decimal arithmetic which, believe it or not, most smart contract programming stacks can’t do.</li><li>There is a nice <a href="https://marketplace.visualstudio.com/items?itemName=DigitalAssetHoldingsLLC.daml">Visual Studio Code integration</a>.</li><li>It’s not only a language but also a runtime specification.</li><li>There is a variety of <a href="https://docs.daml.com/deploy/index.html">DAML ledger implementations</a>, both open source and commercial, built on top of a wide range or persistence layers, ranging from relational databases to full blown block chains.</li><li>A runtime implementation will typically expose both a <a href="https://docs.daml.com/app-dev/ledger-api.html">gRPC API and a JSON API</a>. This makes it extremely easy for back-end applications written in basically any language to to interact with a DAML runtime.</li></ul><h4>DAML is Easy To Get Started With</h4><p>After <a href="https://docs.daml.com/getting-started/installation.html">installing the SDK</a> you can immediately generate a project by typing</p><blockquote>daml create-daml-app myapp</blockquote><p>Once you’ve done that you can open up the DAML files in VS Code and start writing code and scenarios (tests)</p><p>Running the scenarios for the first time will show you all created contracts with their content and disclosure details in a panel. After that, as soon as you modify the code, the results and disclosure details will be updated in real time.</p><p>If you want to interact with your smart contracts you need to start the ledger. For that you go to the <em>myapp</em> directory and type</p><blockquote>daml build</blockquote><p>to compile the project and then</p><blockquote>daml sandbox — ledgerid my_ledger_id ./.daml/dist/myapp-0.1.0.dar</blockquote><p>to start the ledger up.</p><h3>Programing Model</h3><h4>Real Life</h4><p>DAML smart contracts are modeled after paper contracts. In real life, most of the times when you need to enter into an agreement with other parties, you:</p><ul><li>get a contract template</li><li>fill in the blanks</li><li>sign it</li><li>pass it on to the next stakeholders, they read it and if they agree to the terms, they sign it.</li></ul><p>Once all the stakeholders have signed, the contract takes effect.</p><p>The contract is visible to all the signers of course, but can also be disclosed to certain observers. Observers are typically regulators, court registries, etc.</p><p>During the lifetime of the contract, the terms may allow you to exercise various choices, such as backing out of the contract, or modifying the terms or whatnot.</p><p>If you choose to modify the terms then the old paper contract gets archived, a new template is filled in with the modified data, and the new version takes effect.</p><p>But you can also choose to exercise a choice that does not require to amend the contract, such as sending a notice to someone, or making a payment.</p><h4>DAML</h4><p>Now let’s see how the real-life concepts above map to DAML.</p><p>When you code in DAML, you don’t write contracts, you write contract templates. Here’s a simple example extracted from the default project generated by the CLI.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6813b916adda5f48d1df6741fb17630a/href">https://medium.com/media/6813b916adda5f48d1df6741fb17630a/href</a></iframe><p>This source code represents a DAML template.</p><p>It is identical in purpose to a paper contract template, in that it codifies the contract’s terms, variables — the blanks — and whatever choices may be exercised during the contract’s life cycle.</p><h4>Declaration and Data</h4><p>Lines 3–5 declare the template’s name, followed by the keyword “<em>with</em>” and a set of variables.</p><p>These variables are the blanks from the paper contract template, which you will fill in when instantiating the contract. They constitute the contract’s data model.</p><h4>Permissions</h4><p>Lines 6–8 add details about some of the variables. Party “<em>username</em>” is a contract signatory and the parties stored in “following” are observers.</p><p>The signatories own the contract and can archive it. Observers can read it. But what exactly does this mean?</p><p>Each time a contract is created or archived, all signatories and observers will receive an event describing it, provided the respective party has subscribed to those event types. Also, when any of the signatories and observers query the ledger, they’ll get back any contracts they are named on.</p><p>This is the mechanism private transactions rely on: each party will only be notified about, and can only see contracts on which they’ve been named.</p><h4>Contract Prose</h4><p>Lines 10–11 are important as well. they lay out in plain human readable text the terms of the contract. This way any human who accepts it and interacts with it gets a chance to read it and know exactly what it is about.</p><h4>Contract Key</h4><p>Lines 13–14 declare an optional contract key.</p><p>This is effectively a business primary key, as opposed to the contract’s technical primary key which is the contract ID. It allows anyone who’s entitled to, to look up the contract using this business key.</p><p>In this particular example the contract key uses a single data element, but it may as well be a composite.</p><h4>Choices</h4><p>Lines 15–23 are showing a choice. Without diving into the DAML language details, the way you should read it is:</p><p>The choice “Follow” returns a reference to a contract of type User and takes a Party, referred to as “userToFollow”, as a parameter.</p><p>Additionally, line 18 declares a controller. The controller declaration defines which parties can exercise the option. In this particular case, it is the “username” party, which also happens to be the signatory. This means only the signatory of the User contract can add to the list of users to be followed.</p><p>Controllers must be one or some of the parties declared in the data model section.</p><h3>Testing</h3><p>DAML tests are called scenarios. You can define a test scenario at the end of your template declaration or in a separate file.</p><p>Here’s an example bellow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05183e0103c6d83eee9d9bb64a3aa46f/href">https://medium.com/media/05183e0103c6d83eee9d9bb64a3aa46f/href</a></iframe><p>The test scenario is very easy to read:</p><p>Lines 2–4 provision parties that will be used during the scenario to simulate interactions with the ledger.</p><p>In the following lines, each party creates their own User contract. First two users will create their contracts with no followers, while “User 3” three will create a user contract with two users to follow — “User One” and “User Three”</p><p>Lines 15 and 16 show “User one” exercising the “Follow” choice on his contract.</p><p>When the test is executed successfully for the first time you’ll see the following results panel in VSCode. After that, it will automatically update as you change your source code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*6EjiLlbDCD3xmBrNu0yE5g.png" /></figure><p>This shows each contract that has been created on the ledger along with their attributes and disclosure details.</p><p>In the disclosure details — the right-most columns — <em>S</em> means signer and <em>O</em> means observer.</p><p>You can also see that the “User One” contract was initially created with no users to follow. Later, “User One” has exercised the “Follow” choice, the initial version of the contract has been archived and a new version was created, with “User Two” in the “following” section.</p><h3>Conclusion</h3><p>As the title mentions, this first article on DAML is meant as an appetizer, show you how powerful it is and how easy it is to get started.</p><p>There will be more articles like this one, diving into the details and drawing parallels with imperative programming languages.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2542e590bcf3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Publish Open Source Java Libraries to Maven Central]]></title>
            <link>https://entzik.medium.com/how-to-publish-open-source-java-libraries-to-maven-central-70f9232462f5?source=rss-5e7b331e1e59------2</link>
            <guid isPermaLink="false">https://medium.com/p/70f9232462f5</guid>
            <category><![CDATA[github]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[maven]]></category>
            <category><![CDATA[gradle]]></category>
            <dc:creator><![CDATA[Emil Kirschner]]></dc:creator>
            <pubDate>Tue, 05 Nov 2019 21:01:44 GMT</pubDate>
            <atom:updated>2019-11-05T21:01:44.950Z</atom:updated>
            <content:encoded><![CDATA[<p>I have recently published a small Java library on Maven Central: written in plain Java, built with Gradle using Gradle’s Kotlin DSL, integrated using GitHub actions and published to Maven Central using Gradle’s “maven-publish” plugin.</p><p>Here are simple step-by-step instructions on how to reproduce and automate the process.</p><h3>Sonatype OSS Project Setup</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*1B_ZWzyniTw9foz52sfZ2A.png" /></figure><p>You will need to open a ticket with Sonatype <a href="https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&amp;pid=10134">here</a> and ask them for an open source project.</p><p>You will need to sign up for an account if you don’t already have one.</p><p>Sonatype are very quick to respond, friendly and very helpful. They’ll guide you to the process, just follow their guidelines.</p><p>If the maven group you choose matches an internet domain, Sonatype will ask you to complete a DNS challenge to prove that you own it.</p><p>You can see the ticket I opened for my library and the exchanges I had with Sonatype <a href="https://issues.sonatype.org/browse/OSSRH-52294">here</a>.</p><p>Sonatype user name and password will need to be configured as CI secrets to be passed along to your build when publishing artifacts.</p><h3>Generate a PGP Key Pair</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*ieACP71ECSD-UmRGIfuX0w.png" /></figure><p>I generated my key pair using the Gnu Privacy Guard distribution which can be downloaded <a href="https://gnupg.org/download/index.html">here</a>. After generating the keys you will need to export both the private key and the public key in ASCII armored format.</p><p>You should store the keys and the related password somewhere safe — I put them in a secure note in my 1Password vault.</p><p>You will also have to securely store your private key and key password in your CI infrastructure and pass it along to your build.</p><p>The public key needs to be uploaded to a few well known public key servers. Sonatype will look it up when you promote your artifacts and verify that nobody has tempered with them. I have uploaded my public key to:</p><ul><li><a href="https://keyserver.ubuntu.com/">https://keyserver.ubuntu.com/</a></li><li><a href="http://keys.gnupg.net/">http://keys.gnupg.net/</a></li><li><a href="https://pgp.mit.edu/">https://pgp.mit.edu/</a></li><li><a href="https://pgp.key-server.io/">https://pgp.key-server.io/</a></li></ul><h3>Gradle Build</h3><p>In order for your library to be accepted to the Central Repository, your gradle build must:</p><ul><li>produce JavaDoc and sources Jar files along the main library Jar file</li><li>sign artifacts using your private PGP key</li><li>configure the publishing task and set up your POM</li><li>upload signed Jar files to the staging repository</li></ul><h4>Apply required plugins to your build.gradle.kts file</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/496de020cb9f17c4318db840fe13227f/href">https://medium.com/media/496de020cb9f17c4318db840fe13227f/href</a></iframe><p>You may of course apply other plugins to your build but “maven-publish” and “signing” are the important ones.</p><h4>Define Gradle tasks to build Sources and JavaDoc jar files</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98c875a08c575096d8a3fd0d7a06fe1a/href">https://medium.com/media/98c875a08c575096d8a3fd0d7a06fe1a/href</a></iframe><p>The code here is quite straightforward: we define “Jar” type tasks and we instruct gradle how to put in those jar files and how to name them.</p><p>In <em>lines</em> <em>3</em> and <em>8</em> we ask Gradle to build Jar files using Java source code files in the project and the output of the javadoc task, respectively.</p><p>The “archiveClassifier” attribute set in <em>lines 2 and 7</em> is a string Gradle will use to derive the name of the jar file from the name of the main library jar. For example, if the library jar file is mylib.jar, the jar holding source codefiles will be named “<em>mylib-sources.jar</em>” and jar containing the libraries JavaDoc files will be named “<em>mylib-javadoc.jar</em>”</p><h4>Set up Maven publications and your project’s POM</h4><p>You will need the code block bellow to set up your your maven publication:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9d2699167a3171c8a314916d73e5486/href">https://medium.com/media/d9d2699167a3171c8a314916d73e5486/href</a></iframe><p>This is pretty big so lets take it line by line:</p><p>In <em>lines 1 and 2</em> we get the user name and password needed to publish to Sonatype repositories. You will get these credentials when you will create your Sonatype account.</p><p>You will pass these to your build using the “-P” command line switch: “<em>./gradlew … -PMAVEN_UPLOAD_USER=username -PMAVEN_UPLOAD_PWD=password</em>”</p><p>Next we have the “publishing” block and its two sections: “repositories” and “publications”.</p><p>The “repositories” section (<em>lines 5 to 15</em>) defines where we publish artifacts while the “publications” sections (<em>lines 17 to 48</em>) defines which artifacts we publish related metadata (the Project Object Model, or POM)</p><p><strong>Target Repositories</strong></p><p>Starting with <em>line 7</em> we define the “MavenCentral” repository. If you look carefully you will notice we are actually defining two repositories, one for publishing snapshot versions of our artifacts (<em>line 8</em>) and one for publishing release versions (<em>line 9</em>). The publication will be routed dynamically at run-time to the proper URL based on your project’s version (<em>line 10</em>).</p><p>Last, in the credentials section (<em>lines 11 to 14</em>), we instruct the “maven-publish” plugin to use the user name and password we mentioned before to connect to either one of the Sonatype repositories.</p><p><strong>Publications</strong></p><p>Starting with <em>line 18</em> we create a publication named “MavenJava” and we tell it which artifacts make up this publication. In our case it’s the main library, source code and JavaDoc Jar files.</p><p>Starting with <em>line 23</em> we define the project’s POM — basically metadata for the artifact you publish. This will allow MavenCentral and other repositories to index, search and properly present your library on the repository user interface.</p><h4>Configure Artifact Signing</h4><p>To have Gradle sign the artifacts before publishing, you need to add the following code block to your build file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a60cc867ece93974559647e2d751c0f0/href">https://medium.com/media/a60cc867ece93974559647e2d751c0f0/href</a></iframe><p>It is important to add it after the “publishing” block we have described earlier, in order for it to be able to find the correct publication artifacts to sign.</p><p>In lines 1 and 2 we use the same mechanism described earlier to get hold of the PGP private key and matching password you want to sign the artifacts with. Private key is expected to be in “ASCII armored” format.</p><p>Just like with repository credentials, the private key and associated password will be passed to Gradle using “-P” command line switches.</p><h3>GitHub Actions: Continuous Integration Pipeline</h3><p>Since my open source project is hosted on GitHub I used GitHub Actions to build, test and publish.</p><p>Adding GitHub actions to your project is quite easy, all you need to do is follow the intuitive UI and adapt a YAML file that is generated for you in your project, in the “.github/workflows” folder. In my case the YAML file looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0324ae307bee0b8526d1ec688445912f/href">https://medium.com/media/0324ae307bee0b8526d1ec688445912f/href</a></iframe><p>You will notice in line 17 I pass Sonatype account credentials and signing key/password to Gradle using “-P” command line switches. The values passed in there are GitHub secrets.</p><p>You can define these secrets in your github repository “Settings” tab. Make sure the name of the secret in “Settings” matches the name of the secrets in your GitHub actions file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*UjyxQ5ecICzG_Rkl4s9m6Q.png" /></figure><p>And this is it, you’re all set. Each time you push to your repository you will build and publish artifacts. If your project version ends in “-SNAPSHOT” you will publish to the snapshots repository, otherwise you will publish to the staging repository.</p><h3>Releasing to Maven Central</h3><p>When you publish a non-snapshot version of your artifact, Sonatype will create a dedicated staging repository for that publication.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pJF0HyC1Y3Pr6U8KMlMiDQ.png" /></figure><p>To release it to Maven Central you will have to log in to <a href="https://oss.sonatype.org/">https://oss.sonatype.org</a> and click on the “Staging Repositories” menu item in the left navigation bar. Here you’ll get a chance to inspect your publication: make sure the version is right, POM information is correct and so on.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/373/1*4_Onc0xv2RlsB3axmf1G1g.png" /></figure><p>If you’re happy with the state of the artifact you must “close” the staging repository by clicking on the “close” button in the toolbar. When you do so Sonatype will go through a check list to verify your publication matches Maven Central requirements.</p><p>If you get errors you need to fix them and publish again.</p><p>If everything checks out, you can select the staging repository again and click the “Release” button. This will copy your artifact to the “Release” repository from where they will be synced to Maven Central.</p><p>Synchronizing to maven Central takes about 20m in my experience but sometimes more. The first release always takes longer — mine had taken almost 24 hours. Don’t forget to comment on the JIRA ticket when you release for the first time so that Sonatype can activate the sync process.</p><p>Well, that’s it. Hope this was helpful.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=70f9232462f5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Home Climate Monitoring with AWS Lambda and Raspberry Pi]]></title>
            <link>https://entzik.medium.com/home-climate-monitoring-with-aws-lambda-and-raspberry-pi-ed26b02b69eb?source=rss-5e7b331e1e59------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed26b02b69eb</guid>
            <category><![CDATA[spring-cloud-function]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[iot]]></category>
            <dc:creator><![CDATA[Emil Kirschner]]></dc:creator>
            <pubDate>Thu, 07 Jun 2018 21:58:39 GMT</pubDate>
            <atom:updated>2018-06-07T22:02:17.933Z</atom:updated>
            <content:encoded><![CDATA[<p>I am building a home climate monitoring system for my house. Active sensors are built around <a href="https://www.raspberrypi.org/">Raspberry Pi</a> computers paired with <a href="https://www.adafruit.com/product/393">AM2302</a> temperature / humidity sensors. The Raspberry Pis read humidity and temperature data from the sensors every minute, add metadata such as home id, room name, tenant ID — yeah, making it multi-tenant because I plan to share the infrastructure with friends — and make a HTTP POST call to an <a href="https://aws.amazon.com/lambda/?sc_channel=PS&amp;sc_campaign=acquisition_FR&amp;sc_publisher=google&amp;sc_medium=english_lambda_b&amp;sc_content=lambda_e&amp;sc_detail=aws%20lambda&amp;sc_category=lambda&amp;sc_segment=223917591092&amp;sc_matchtype=e&amp;sc_country=FR&amp;s_kwcid=AL!4422!3!223917591092!e!!g!!aws%20lambda&amp;ef_id=Ww2qEAAAAIHh4ldN:20180607213608:s">AWS Lambda </a>function written in Java using the <a href="https://cloud.spring.io/spring-cloud-function/">Spring Cloud Function</a> framework exposed over the web using the <a href="http://aws api gateway">AWS API Gateway</a>. The lambda function will store the climate readings in a <a href="https://aws.amazon.com/rds/?sc_channel=PS&amp;sc_campaign=acquisition_FR&amp;sc_publisher=google&amp;sc_medium=english_rds_b&amp;sc_content=rds_bmm&amp;sc_detail=%2Baws%20%2Brds&amp;sc_category=rds&amp;sc_segment=186839029858&amp;sc_matchtype=b&amp;sc_country=FR&amp;s_kwcid=AL!4422!3!186839029858!b!!g!!%2Baws%20%2Brds&amp;ef_id=Ww2qEAAAAIHh4ldN:20180607213818:s">AWS RDS</a> <a href="https://www.postgresql.org/">PostgreSQL</a> instance. The database schema is maintained using <a href="https://www.liquibase.org/">LiquiBase</a>. Other AWS Lambda functions will read the database, process readings and serve temperature and humidity curves to an <a href="https://angular.io/">Angular</a> web application where we can monitor the whole house.</p><p>Let’s see how its done, piece by piece.</p><h3>Software Components</h3><h4>Reading the sensors</h4><p>On the Raspberry Pi, a <a href="https://www.python.org/">python</a> script is invoked every minute to read temperature and humidity data from the AM3202 sensor, enhance it with metadata and post it to the AWS Lambda Function. The code is fairly simple as seen in the snippet bellow. In order to run it you’ll first need to <a href="https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated">install the Adafruit DHT python library</a>.</p><p>In the code bellow, you’ll have to replace two tokens:</p><ul><li>in line 26, use the AWS API gateway URL you that is mapped to the function</li><li>in line 28, you need to use the API key you have defined for the usage plan that guards your API</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7ffcb9e50d2a69ede1749e288888945/href">https://medium.com/media/a7ffcb9e50d2a69ede1749e288888945/href</a></iframe><p>So:</p><ul><li>connect your AM3202 sensor to your Raspberry Pi,</li><li>get this python file on the same,</li><li>schedule it with CRON,</li><li>you’re almost set.</li></ul><p>First you’ll have to define the lambda function that responds to the API and stores all these records.</p><h4>The back-end</h4><p>The back-end is implemented as an AWS Lambda function written in Java, using the Spring Cloud Function framework. This lambda function is exposed on the internet using the AWS API Gateway.</p><h4>Spring Cloud Function</h4><p>The advantage of using Spring Cloud Function is that it is actually Spring Boot, so you can take advantage of the entire ecosystem out of the box. For this project we’re going to use <a href="http://projects.spring.io/spring-data/">Spring Data</a> to connect to a PostgreSQL instance on AWS RDS instance. We are also going to LiquiBase to manage the schema of the Postgres instance.</p><p>You’ll also able to run it on a variety of public cloud infrastructures. As of today, Spring Cloud Function supports AWS Lambda, Azure Functions and Apache OpenWhisk .</p><p>I have chosen to deploy the function on AWS lambda, so a dedicated adapter class was added to the project.</p><p>The project can be found on HitHub here: <a href="https://github.com/entzik/iot-home-climate-monitoring">https://github.com/entzik/iot-home-climate-monitoring</a></p><h4>Code dive-in</h4><p>As mentioned, a Spring Cloud Function is a <a href="https://spring.io/projects/spring-boot">Spring Boot</a> application. It is also expected to define a bean of type <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html">java.util.function.Function</a>, <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html">java.util.function.Consumer</a> or <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html">java.util.function.Supplier</a>, like in the code snippet before.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db7f07f8c8e65795701fbc475e6546fa/href">https://medium.com/media/db7f07f8c8e65795701fbc475e6546fa/href</a></iframe><p>In this case we use a function that gets a ClimateReading reading as input, saves it to the database and returns the saved object.</p><p>The object is constructed from the JSON document produced by the python script above and saved using a very simple Spring Data repository:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9392f8f3e39d4ef63f1d9e9a00d0e43/href">https://medium.com/media/b9392f8f3e39d4ef63f1d9e9a00d0e43/href</a></iframe><p>Since we are only using the “save” operation we don’t need to add anything else to this interface for now.</p><p>You may have noticed the bean is named. You can name the bean any way you want, as long as you specify the function bean name in your application.properties file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/797e15e776effa982edcc84e6bc80864/href">https://medium.com/media/797e15e776effa982edcc84e6bc80864/href</a></iframe><p>Because we want to run it on AWS Lambda and expose it to the internet over the AWS API Gateway, we need to add one more adapter class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5f197a542fe5104a5d5b18609000e0c/href">https://medium.com/media/b5f197a542fe5104a5d5b18609000e0c/href</a></iframe><p>The handler will receive the payload of the HTTP POST request, deserialize it and invoke the function. It will then get the saved object, serialize it to JSON and include it in the HTTP response.</p><p>So that’s pretty much it. The Spring Cloud Function is now complete to run on its own. Just run <em>./gradlew clean assemble</em> and you’ll get an artifact named iot-<em>home-climate-monitoring-1.0.0.BUILD-SNAPSHOT-aws.jar</em> under <em>/build/libs</em>, and that’s your packaged function that you will deploy on AWS Lambda.</p><h3>Setting up the database</h3><p>The database in which climate readings are stored is managed by LiquiBase. LiquiBase integrates nicely with SpringBoot and each time a function comes up it will upgrade the database, if necessary.</p><p>Spring Boot will look for LiquiBase change logs when the following properties are added to application.properties</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/369672a55af472540e5fe3d0ff1db240/href">https://medium.com/media/369672a55af472540e5fe3d0ff1db240/href</a></iframe><p>The main change log describes the table that holds climate readings:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aca5cffd02ee5ad83d5d086cae4039c4/href">https://medium.com/media/aca5cffd02ee5ad83d5d086cae4039c4/href</a></iframe><h3>Setting up the the AWS environment</h3><p>In order to do this you will of course need an AWS account and your CLI installed and running</p><p>NOTE — <em>bellow is a series of CLI commands and AWS console operations. All this can of course be automated, but that will be topic of another post</em>.</p><h4>Create the database</h4><p>It is fairly easy to create an RDS database using the command line bellow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d87f60fa48ac65e1b5f76f161c9fb9fb/href">https://medium.com/media/d87f60fa48ac65e1b5f76f161c9fb9fb/href</a></iframe><p>This will create a database that will live in the AWS free tier. You will have to specify a connection password.</p><p>You may want to change the region to have the DB closer to where you.</p><p>After creating the database you should make note of the information highlighted bellow, you will need it in order to configure the lambda function.</p><p>You will need to endpoint bellow to configure the DB_HOST_NAME environment variable and security group information to enable the function to access this database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PzAZR0w1LL2Z_OEqMaAyaA.png" /></figure><h4>Create a role for your lambda function</h4><p>In your AWS console go to IAM and create a role that will allow your function to access AWS resource — such as the database you have just created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1DpJCgFRCaSF6AsSUeKrVA.png" /></figure><h4>Create the lambda function</h4><p>After creating the database, the next thing you do create and deploy the lambda function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fe0d7b81df9d024ef4239869103d948/href">https://medium.com/media/4fe0d7b81df9d024ef4239869103d948/href</a></iframe><p>This will create a lambda function on AWS and deploy your JAR file.</p><p>Note that:</p><ul><li>you need to specify the role you have created for your function and assign it— line 7</li><li>we need to set a timeout of 120 seconds to cope with rare situation where cold start gets really cold… — line 11</li></ul><h4>Create the API, API key and Usage Policy</h4><p>In the AWS console go to your Lambda function and create an API Gateway trigger.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qdS9brVymvtpX77arl_sAA.png" /></figure><p>We will see a little bit later how to configure your API, for now let’s continue configuring the lambda function.</p><p>You will have to set values to environment variables defined in when you created the function via the command line in order to configure access to your database. To DB_HOST_NAME is supposed to be the same as the database endpoint — see screenshot above, while user name and password must match the value you have specified when you created the database using the command line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FcLvVcKRKqY4nqCNrv2MUA.png" /></figure><p>The you need to configure the VPC and access groups to be able to access your database. Make sure you use the same VPC and security group values</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BsvAi6x9HWhhQjfGRJoN0w.png" /></figure><p>Now you have to configure the trigger API for internet access. If you go to the API Gateway Console and click on the API you have created to trigger your function, it will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lJbnGq53eWy3UDLKkt3S7w.png" /></figure><p>I recommend creating a stage called <em>production</em>. When you click on the stage you will see the URL where you can invoke the</p><p>You can either leave the API public — risky move, don’t do it — or decide to protect it with a usage plan of your choice. In my case I have created a a usage plan associated to an API key and then I added to usage plan to the API stage.</p><h3>Hardware Components</h3><p>Well this one is easy. I used a RaspberyPi and a wired humidity / temperature sensor which I connected to GPIO pin number 2. You can of course choose to connect it to another GPIO pin, in which case you will have to reflect that in line 10 of the python script.</p><p>Here are links to components I used:</p><ul><li><a href="https://www.amazon.fr/gp/product/B01N6PB489/ref=oh_aui_detailpage_o00_s00?ie=UTF8&amp;psc=1">AM2302 sensor</a> — this is very good option for the sensor because it come pre-wired with the required resistor and a set of small tables cabled that makes it very easy to connect it to your GPIO port of choice on the Raspberry Pi</li><li>The <a href="https://www.amazon.fr/gp/product/B01CD5VC92/ref=oh_aui_detailpage_o04_s00?ie=UTF8&amp;psc=1">Raspberry Pi</a> itself — is is an expentive model model, if you plan to equip multiple rooms you may want to look at the <a href="https://www.amazon.fr/Raspberry-Pi-Zero-W/dp/B078YXBKXM/ref=sr_1_3?ie=UTF8&amp;qid=1528407020&amp;sr=8-3&amp;keywords=raspberry+pi+zero">Raspberry Pi Zero W</a>, or if like me, you are not very fond of doing much soldering, a <a href="https://www.kubii.fr/kits-raspberry-pi/2079-kit-pi-zero-wh-3272496009523.html#">Raspberry Pi Zero WH</a>.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed26b02b69eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On Asynchronous, Non-Blocking I/O]]></title>
            <link>https://entzik.medium.com/on-asynchronous-non-blocking-i-o-4a2ac0af5c50?source=rss-5e7b331e1e59------2</link>
            <guid isPermaLink="false">https://medium.com/p/4a2ac0af5c50</guid>
            <category><![CDATA[asynchronous]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Emil Kirschner]]></dc:creator>
            <pubDate>Sun, 18 Feb 2018 22:49:33 GMT</pubDate>
            <atom:updated>2018-02-24T19:59:18.843Z</atom:updated>
            <content:encoded><![CDATA[<p>Non — blocking, <strong>asynchronous I/O APIs make your applications more resilient, responsive and cost effective</strong>. In this blog post we will look at the problems blocking I/O is causing, at how non-blocking I/O solves the problems and then apply these principles to the every-day use case of file uploading.</p><h3><strong>Synchronous Blocking I/O</strong></h3><p><strong>Traditional I/O operations are blocking</strong>. This means that while data is being transferred between main memory and peripheral devices such as network adapters or storage controllers <strong>the thread that has initiated the operation is blocked and the CPU is waiting idle for the I/O operation to complete</strong>. Under certain conditions, this behavior can trigger catastrophic side effects.</p><p>Suppose you serve web requests using <strong>blocking I/O: each request is associated to a thread</strong>. <strong>When a web request is accepted, a thread is acquired from a thread pool</strong>. The application will use that thread to run database queries, invoke downstream services and so on until all information required to put together a response become available. <strong>After a response is provided, the application will release the thread back to the thread pool in order for it to be reused</strong>.</p><p>This takes a fair amount of time, and often <strong>most of this time is being spent waiting for I/O operations to return</strong>. If you experience a spike in requests, <strong>threads will be taken out of the thread pool faster than your app can return them</strong>. As a result, <strong>you will exhaust your thread pool</strong> very quickly and <strong>new threads will be created to serve incoming requests</strong>.</p><p><a href="https://stackoverflow.com/questions/5483047/why-is-creating-a-thread-said-to-be-expensive"><strong>Creating a new thread is expensive</strong></a> and will increase response latency even more. In addition to that, <strong>as the number of threads increases, </strong><a href="https://en.wikipedia.org/wiki/Context_switch"><strong>context switching</strong></a><strong> will take more and more time</strong>. <strong>Your system</strong> will spend more time trying to figure out which thread needs to be scheduled next and setting the stage for it than processing requests and <strong>will eventually become non-responsive</strong>.</p><p><strong>Blocking I/O also costs you more money</strong>. While CPU cores are waiting idle, polling for the result of the blocking I/O operation, they cannot be used to perform useful calculations. As a result, <strong>you will need more CPU cores</strong> to run your business logic within service level objectives, which means <strong>you will spend more money than necessary</strong>.</p><h3><strong>Asynchronous, Non-Blocking I/O</strong></h3><p><strong>A non-blocking I/O operation initiates the data transfer by sending instructions to specialized hardware components and return immediately</strong>. The hardware components will transfer the data in the background. When the transfer is complete <strong>results are returned by invoking a callback provided when the operation was initiated</strong>.</p><p>Because the I/O operation returns immediately the <strong>thread handling the request is not blocked</strong>, <strong>it can immediately be returned to the thread pool and reused</strong>. Another thread will be later used to run the callback delivering the results.</p><p>This solves the first blocking I/O problem we mentioned above: <strong>you will not exhaust the thread pool any more</strong>, your system will be more responsive and will handle request spikes a lot better and <strong>you will pack more compute cycles on the same number of cores </strong>because all that idle CPU time does not exist any more, making it cheaper to run.</p><p>With non-blocking I/O the CPU is not waiting for the I/O operation to complete. All those precious <strong>cycles wasted waiting for blocking I/O to complete can now be used to perform business logic</strong>. As a result you can run more business logic on the same number of CPU cores, and <strong>your system becomes more cost effective to run</strong>.</p><h3>Real World Use Case: Non-Blocking HTTP File Upload</h3><p>One of the use cases where most the IO / compute workload ratio is very high is uploading file to web server.</p><p>With blocking I/O the thread handling the connection will first wait for chunks of data to arrive over the internet, then wait again for each chunk to be written to disk before returning a result to the caller. When the result is ready it will wait for it to be delivered over the network before the request becomes complete and the thread returned to the pool.</p><p>With non-blocking I/O the flow looks very different. After the connection is accepted, chunks of data are being delivered to a callback as they arrive over the network. That callback will initiate a non-blocking write to the destination file, and return immediately, freeing the thread. When the write operation completes, another callback is invoked with the result of the operation. After the last write operation succeeds, the response is being written to the socket.</p><p>If you think the flow above sounds like callback hell, you’re right. Luckily frameworks such as <a href="https://github.com/ReactiveX/RxJava">RxJava</a> or <a href="https://projectreactor.io/">Project Reactor</a> simplify the task by making it possible to write that flow in a fluent, readable style.</p><p><a href="https://docs.spring.io/spring/docs/5.0.0.BUILD-SNAPSHOT/spring-framework-reference/html/web-reactive.html">WebFlux</a> is a framework for reactive web programming base on <a href="https://projectreactor.io/">Project Reactor</a>. When used in conjunction with <a href="https://projects.spring.io/spring-boot/">Spring Boot</a> and the AsynchronousFileChannel class from the <a href="https://docs.oracle.com/javase/tutorial/essential/io/fileio.html">Java NIO</a> framerwork, it enables us to upload files in a reactive manner, using non-blocking I/O operations exclusively.</p><p>I have put together a fully reactive, non-blocking file upload over at GitHub. We’ll have a close look at how it works in another post, until then you can have a sneak peek at <a href="https://github.com/entzik/reactive-spring-boot-examples/blob/master/src/main/java/com/thekirschners/springbootsamples/reactiveupload/ReactiveUploadResource.java">ReactiveUploadResource.java</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4a2ac0af5c50" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Proxy Based Service Mesh]]></title>
            <link>https://entzik.medium.com/proxy-based-service-mesh-96cd4b74c198?source=rss-5e7b331e1e59------2</link>
            <guid isPermaLink="false">https://medium.com/p/96cd4b74c198</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[istio]]></category>
            <category><![CDATA[linkerd]]></category>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Emil Kirschner]]></dc:creator>
            <pubDate>Sun, 19 Nov 2017 22:25:02 GMT</pubDate>
            <atom:updated>2018-02-17T20:18:38.666Z</atom:updated>
            <content:encoded><![CDATA[<p>The proxy based service mesh is an emerging technology that simplifies building distributed micro-services systems by using specialized proxies to provide cross-cutting infrastructure functionality such as service discovery, load balancing, circuit breaking, metrics monitoring, distributed tracing and more at container orchestration level. By eliminating boilerplate code from your service you are free to use any technology and any programing language.</p><p>Traditionally, if you wanted to build a distributed micro-services system, you had to find a set of components and frameworks that provide service discovery, load balancing, circuit breaking, then you had to specifically craft your services to work with these components. Often these frameworks are more or less tied to a specific language or framework.</p><p><a href="https://netflix.github.io/">Netflix OSS</a> for example is really easy to start with when used with <a href="https://cloud.spring.io/spring-cloud-netflix/">Spring Cloud Netflix</a>. Just by adding a few annotations on a <a href="https://projects.spring.io/spring-boot/">Spring Boot</a> app you are able to fire up a <a href="https://github.com/Netflix/zuul">Zuul</a> proxy and a <a href="https://github.com/Netflix/eureka">Eureka</a> server. By adding a different set of annotations you mark your micro-service as a <a href="https://github.com/Netflix/eureka/tree/master/eureka-client">Eureka Client</a> and there it is, registering to make itself available. If you need to call a downstream service you use <a href="https://github.com/OpenFeign/feign">Feign</a>. And you can guard your downstream calls using <a href="https://github.com/Netflix/Hystrix">Hystrix</a>.</p><p>However as soon as you leave the Java / Spring Boot realm things become more complicated.</p><p>If you need to add a service written in C++ or Go, you need to build the integration with Netflix OSS yourself. That’s even more boilerplate and you have to do it each time you add a new language to the stack.</p><p><strong>The Service Mesh</strong></p><p>The rise of containers and container orchestration technologies has enabled a new kind of infrastructure that allows us to break free of discovery/load balancing/circuit breaking frameworks. This new piece of infrastructure is the “<strong>service mesh</strong>” — and when I say new, I mean that by the time of this writing it does not even have a Wikipedia page yet.</p><p>So what is it? A service mesh is an infrastructure layer —mainly a collection of proxies, one per logical service — that integrates with container orchestration solutions such as <a href="https://docs.docker.com/engine/swarm/">Docker Swarm</a> or <a href="https://kubernetes.io/">Kubernetes</a> and provides service discovery, load balancing, circuit breaking, fault injection, security, monitoring, tracing and more out of the box in a non-intrusive way.</p><p>It does not care what technology or programming language you used to write your micro-services, as it operates at container level. You can write your micro-services as simple HTTP servers in Java, C++, Rust, Go, NodeJS, it really does not matter.</p><p>You can effectively think of a service mesh as infrastructure level <a href="https://en.wikipedia.org/wiki/Aspect-oriented_programming">Aspect Oriented Programing</a> for your distributed, containerized app. The proxies in the service mesh act like an Aspect in AOP. They wrap a containerized micro-service just like an <a href="https://www.eclipse.org/aspectj/">AspectJ</a> aspect can wrap and instrument a java method, simplifying the system by separating cross-cutting concerns.</p><p><strong>Under the hood</strong></p><p>Getting all this for free in a non-intrusive way is cool, but how does it work? Let’s take it one by one:</p><p><strong>Service Discovery and Load Balancing</strong></p><p>A service mesh will hook into the orchestration layer — Docker Swarm or Kubernetes — and get notified each time a container is started and stopped.</p><p>When the first container carrying “service1” instance is started, the service mesh will create a proxy and apply iptables configuration that will catch all traffic to “service1” and manage it. As more instances of “service1” come up, the service mesh will be notified about it and the new instance added to the proxy’s configuration.</p><p>As traffic flows in, the proxy will provide:</p><ul><li>service discovery by configuring the software defined network to resolve host name “service1” to itself</li><li>load balancing by evenly distributing incoming requests to available service instances</li></ul><p>These two features effectively replace Eureka and Ribbon in the Netflix OSS stack.</p><p><strong>Circuit Breaking</strong></p><p>Circuit breaking is a fail fast mechanism. If the underlying service instance becomes slow and does not return a call within a configured time, the circuit breaker will fail the request and return an appropriate error code to the client. The client can then retry and eventually reach a more responsive instance. This provides a much better user experience.</p><p>Typically, the slow instance will be marked inactive and given some time to recover before receiving any traffic again.</p><p>In a service mesh, the proxy sitting in front of all instances of a service is acting as a circuit breaker as well. This effectively replaces the Hystrix circuit breaker from the Netflix OSS stack.</p><p><strong>Fault Injection</strong></p><p>Distributed cloud native applications must be designed to be fault tolerant. The hardware on which your application runs in the cloud can fail at any moment. Machines can be taken out for scheduled maintenance. Any instance of any service may become unresponsive at any time. When such an incident takes down an instance of a downstream service, your application must handle the situation gracefully without degrading user experience, or minimally do so.</p><p>But since such situations occur randomly in the cloud, it’s hard to reproduce them in a controlled environment and study their effect on the system. That would be a very useful thing to do.</p><p>To solve this issue the good people at Netflix came up with <a href="https://github.com/Netflix/SimianArmy/wiki/Chaos-Monkey">Chaos Monkey</a> and the whole suite of related tools, but these are not easy to deploy and operate.</p><p>With a service-mesh the same functionality is achieved through proxy configurations. We can introduce two types of random perturbations per service:</p><ul><li>delay a percentage of requests to observe the effects of increasing that service’s latency on the distributed system, make sure the system as a whole can handle it</li><li>fail a percentage of the incoming requests with random error codes and make sure the system can survive that.</li></ul><p>Measuring system throughput and end-user perceived latency while shifting these perturbations to various degrees during typical workloads allows you to determine which downstream service are critical and which are not.</p><p><strong>Rate Limiting, Quotas, Monitoring and Tracing</strong></p><p>A service mesh proxy handles all the traffic to a service, it knows what goes right and what goes wrong and it can enforce usage policies such as quotas and rate-limiting.</p><p>The proxy will notice every failure and SLA violation. This makes the it the perfect place to monitor service performance.</p><p>Because each call from a front-facing micro-service all the way down to the last downstream service goes through the proxies, that’s also the perfect place to implement tracing.</p><p>A good service mesh could automatically install monitoring and tracing infrastructure pieces such as <a href="https://prometheus.io/">Prometheus</a> and <a href="http://zipkin.io/">Zipkin</a>, along with visualization tools such as <a href="https://grafana.com/">Grafana</a>.</p><p><strong>The current Service-Mesh Landscape</strong></p><p>Today there are two players in the service mesh area: <a href="https://linkerd.io/">Linkerd</a> and <a href="https://istio.io/">Istio</a>.</p><p>Linkerd was first around and is a more mature, production proven solution. It is most notably known for being used in production by Monzo, a new online-only UK bank.</p><p>Istio on the other hand, is the emerging challenger. It has not reached production quality yet but is moving very fast and will be supported on top of Google’s Cloud Platform from the beginning. It is built on top of the <a href="https://www.envoyproxy.io/">Envoy</a> proxy developed at <a href="https://www.lyft.com/">Lyft</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96cd4b74c198" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>