<?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 Ralph Caraveo on Medium]]></title>
        <description><![CDATA[Stories by Ralph Caraveo on Medium]]></description>
        <link>https://medium.com/@deckarep?source=rss-9fc3bef97981------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*DsuZQ7tz3FAO_mxT.jpg</url>
            <title>Stories by Ralph Caraveo on Medium</title>
            <link>https://medium.com/@deckarep?source=rss-9fc3bef97981------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 08:13:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@deckarep/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[Go: When deferring a lock smells]]></title>
            <link>https://medium.com/@deckarep/go-when-deferring-a-lock-smells-b265381a9f65?source=rss-9fc3bef97981------2</link>
            <guid isPermaLink="false">https://medium.com/p/b265381a9f65</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[multithreading]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Ralph Caraveo]]></dc:creator>
            <pubDate>Tue, 16 May 2023 20:14:15 GMT</pubDate>
            <atom:updated>2023-05-17T02:16:17.959Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Many locks on a fence" src="https://cdn-images-1.medium.com/max/1024/0*9rKpKnFr6tvGo-F6" /><figcaption>Photo by <a href="https://unsplash.com/es/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Perhaps you have a code repo written in Go that has made the decision to synchronize some shared data using one or more vanilla mutexes or even the variant read/write mutexes in the standard library.</p><blockquote>What’s the plural of mutex by the way?</blockquote><p>We know that in Go, the threading model is based on goroutines (coroutines): sometimes called fibers in other languages, and other times called green threads. The point being, that these are not OS-level threads which are very heavy-weight in general and do not scale even close to what Go’s goroutines are capable of.</p><p>In fact, it’s normal to have an application with 10’s of thousands or 100’s of thousands of goroutines and even millions are reported for a single application. It really depends on the nature of the app and if the app is heavily IO bounded yes, <em>you can</em> get away having a massive amount of goroutines if needed — but should you?</p><p>Okay so what’s the point of all of this? The point is that <em>goroutines are extremely lightweight</em>. <em>Context switching is extremely lightweight</em> and cheap therefore it stands to reason that synchronization in its many forms is also <em>cheap and lightweight</em>. This is all very good for developers like you and me and it means that for the most part we don’t have to worry too much about the cost of using these extremely lightweight concurrency constructs.</p><p>Now let’s zoom in a little bit and analyze how sometimes using a mutex can get us into trouble in terms of lock contention.</p><p>Here are some operations that are extremely lightweight and cheap in general:</p><pre>// It’s inexpensive to Lock() and Unlock()<br>// Perhaps we could just use an atomic variable, but we’re talking about locks<br>func code() {<br>    mu.Lock()<br>    myVal+=1<br>    mu.Unlock()<br>}<br><br>// Also relatively inexpensive to RLock() and RUnlock()<br>func code() {<br>    mu.RLock()<br>    // read only related code<br>    mu.RUnlock()<br>}<br><br>// Using defers also can be very cheap...and usually not a problem<br>func code() {<br>    mu.Lock()<br>    defer mu.Unlock()<br>    myVal+=100<br>}<br><br>// What&#39;s cool about defer is it helps us avoid trouble!<br>// Here&#39;s a case where it is not leveraged at all.<br>func code() error {<br>    // Without defer, we introduced a deadlock because we return an error<br>    // and failed to call Unlock on an early return!<br>    mu.Lock()<br><br>    stats.counter +=1<br><br>    stats, err := getStats()<br>    if err != nil {<br>       // oops, forgot to Unlock() here<br>       return err<br>    }<br><br>    // ...<br><br>    mu.Unlock()<br>}<br><br>// defer is our friends and help us avoid this problem.<br>// Instead, we don&#39;t have to sprinkle Unlock() every time we early return.<br>func code() error {<br>    // With defer, we&#39;re covered no matter how many early returns we have<br>    // Of course, we&#39;re covered at the final implicit return as well.<br>    mu.Lock()<br>    defer mu.Unlock()<br><br>    someCounter+=1<br><br>    stats, err := getStats()<br>    if err != nil {<br>       // mu.Unlock() is deferred to here<br>       return err<br>    }<br><br>    // ...<br><br>    // mu.Unlock() is deferred to here as well<br>}</pre><p>This is all well and good and the practice of using: defer + Unlock() is encouraged in general. It helps us avoid dead-lock scenarios which can be tricky to troubleshoot and track down. Yes, the Go runtime has partial dead-lock detection but it will not catch everything.</p><p>Also, today we’re actually talking about <em>lock contention</em> so please keep reading.</p><p>Let’s take a step back and remind ourselves that if goroutines are super cheap along with context switching and synchronizing than why do we really need to worry about lock contention in the first place?</p><p>I want you to think about locking in a different way: <strong>Think about lock lifetimes.</strong> Another way to say this: <em>How long do you hold a lock for?</em> The actual act of locking or unlocking <em>is fast</em> and will always remain fast. In fact locks are not really slow at all in Go. <strong>How long a lock is held is what can be slow</strong>.</p><p>In the very last piece of code did anyone see anything troubling with the code block? I’ll copy it here so you can review it once more. I know this code is artificial and contrived and you don’t have the full picture of what this code is actually doing but please analyze the code closely to see where lock contention could be introduced.</p><pre>// We&#39;re using defer like good gophers but...<br>// could this code possibly introduce lock contention???<br>func code() error {<br>    mu.Lock()<br>    defer mu.Unlock()<br><br>    someCounter+=1<br><br>    stats, err := getStats()<br>    if err != nil {<br>       // mu.Unlock() is deferred to here<br>       return err<br>    }<br><br>    // ...<br><br>    // mu.Unlock() is deferred to here as well<br>}</pre><p>Well I have not shared the implementation of getStats()but perhaps the function does this:</p><pre>func getStats() (*SimpleStats, error) {<br>    s := &amp;SimpleStats{}<br>    // Just imagine simple code here<br>    // ...<br>    return s, nil<br>}</pre><p>In the above contrived code, the code simply returns a pointer-type of SimpleStats and practically does nothing useful. No real heavy lifting is done in the form of computation. No IO work is done. Big deal, this code is fast because it’s not doing much at all.</p><p>Now what if getStats() was doing something like this?</p><pre>func getStats() (*SimpleStats, error) {<br>    resp, err := http.Get(&quot;http://www.isitlunchyet.com&quot;)<br>    if err != nil {<br>        return nil, err<br>    }<br>    <br>    // Here&#39;s our friendly defer again...<br>    defer resp.Body.Close()<br><br>    // Yes, i&#39;m aware of the json decoder...<br>    body, err := io.ReadAll(resp.Body)<br>    if err != nil {<br>        return nil, err<br>    }<br><br>    var stats SimpleStats<br>    err = json.Unmarshal(body, &amp;stats)<br>    if err != nil {<br>        return nil, err<br>    }<br><br>    return &amp;stats, nil<br>}</pre><p>Let’s get back to analyzing the code in question and now do we see how lock contention could possibly be introduced?</p><pre>// Hmmm...<br>func code() error {<br>    mu.Lock()<br>    defer mu.Unlock()<br><br>    someCounter+=1<br><br>    stats, err := getStats()<br>    if err != nil {<br>       // mu.Unlock() is deferred to here<br>       return err<br>    }<br><br>    // ...<br><br>    // mu.Unlock() is deferred to here as well<br>}</pre><p>Do you see it?</p><p>We’re now at the pinnacle of this article and something that I hope is becoming more obvious by now. If getStats() is for example an IO bound function perhaps doing an HTTP request, the latency on the request will in fact be <em>orders of magnitude slower</em> than what our code is doing so far. In fact, it could be very slow. It could be having to fetch stats on some server on the other side of the world. Or perhaps that server could be busy aggregating the stats data and may spend huge amounts of time just having to serialize a response back to our initial request. So many things may contribute to its slowness.</p><p>This means that the defer Unlock() gets us into trouble because our defer is contingent upon the function ending. The function ending is now contingent upon how long it takes for the getStats() endpoint to return to us. This means that what originally looked like a best practice is now seemingly causing our service to degrade significantly because other code that wants to acquire a lock is now waiting for a very long period of time.</p><p>Bad, bad, bad! We’ve <em>introduced lock contention</em> because the lock lifetime has increased by <strong>many orders of magnitude</strong> because we’re now waiting on an IO response from a function that can easily be slow relative to the cost of rest of the code.</p><p>These are the things that a Go programmer working on concurrent code-bases have to think through. As a programmer writing concurrent/multi-threaded code you must get into the habit of think about your code in terms of <strong>logic</strong>, in terms of <strong>event ordering</strong>, and in terms of <strong>lifetimes</strong> in order to avoid problems like this.</p><p>Can the code be fixed? Yes! let’s just introduce some <em>finer and more granular locking</em>.</p><pre>// Fixed now!<br>func code() error {<br>    // Forget defer, we&#39;re in control let&#39;s make the locking more granular.<br>    // After all, we only need to synchronize this stupid counter.<br>    mu.Lock()<br>    someCounter+=1    <br>    mu.Unlock()<br><br>    // If getStats() takes awhile...the lock was Unlocked already!<br>    // No contention!<br>    stats, err := getStats()<br>    if err != nil {<br>       return err<br>    }<br>}</pre><p>Friends, don’t forget you are in final control and sometimes code must be refactored to mitigate problems like this. I like defer too, but in this case it gets us into serious trouble and our critical section could be more tightly managed by simply calling Lock() and immediately following up with Unlock().</p><p>Here’s some helpful mutex/locking guidelines to consider:</p><ul><li>First off, why are using a lock in the first place? Maybe a channel is better?</li><li>Think about how long a lock is held</li><li>Avoid locking over IO, and possibly over expensive operations</li><li>Even waiting on a channel could extend a locks lifetime.</li><li>Keep your locks as granular as possible for a snappy application!</li><li>But not too granular, you can introduce other problems</li><li>Keep locks encapsulated</li><li>In read-heavy scenarios, use a RWMutex</li><li>Avoid deadlocks, lock-contention and inversion of priority</li><li>Repeat after me: locks aren’t really slow at all in Go</li><li>Defer the Unlock() when it makes sense</li><li>None of this means anything if you don’t instrument your code to really see what’s going on…</li></ul><p>Hey, if this blog post <em>helped you catch a bug</em> in your codebase <strong>I want to know about it in the comments!</strong> Please share the dirty details. This problem although contrived for demonstration purposes comes up now and again. I encourage you to think about lock lifetimes to hopefully catch problems like this.</p><p>Now for a poem:</p><pre>In Go, be aware, take your stand,<br>For lock contention is at hand.<br>Mutex locks may seem quite benign,<br>But they can tangle code in twine.<br><br>Threads all clamoring for a share,<br>In a dance of caution, beware.<br>A race for resources, quite intense,<br>A deadlock could be the consequence.<br><br>So design with care, do not be rash,<br>Or else your program may just crash.<br>Take heed, be wise, don&#39;t skip the lesson,<br>Avoid the plight of lock contention.<br><br>By: Gideon Percival Thorne the IV</pre><p>That’s it for now and if you read the entire article and found it helpful I would appreciate some feedback or comments!</p><p>Cheers,</p><p>-deckarep</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b265381a9f65" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Go 1.19 Atomic Wrappers and why to use them]]></title>
            <link>https://medium.com/@deckarep/the-go-1-19-atomic-wrappers-and-why-to-use-them-ae14c1177ad8?source=rss-9fc3bef97981------2</link>
            <guid isPermaLink="false">https://medium.com/p/ae14c1177ad8</guid>
            <category><![CDATA[multithreading]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ralph Caraveo]]></dc:creator>
            <pubDate>Tue, 09 May 2023 23:03:13 GMT</pubDate>
            <atom:updated>2023-05-10T13:28:02.356Z</atom:updated>
            <content:encoded><![CDATA[<p>The Go 1.19 Atomic Wrappers and why to use them</p><p>Here’s a short blog on Go’s (<strong>1.19+</strong>) <a href="https://go.dev/doc/go1.19#atomic_types"><strong>sync/atomic </strong>additions to the library</a> and why you’d want to use them. This article will assume that you have some reasonable in-depth knowledge of <em>concurrent programming in Go</em>.</p><figure><img alt="Synthwave Gopher — complements of Midjourney" src="https://cdn-images-1.medium.com/max/1024/1*tWEy2DVkkuABLtV5akOi8g.png" /></figure><p>While coding in Go, any code that gets into multi-threaded or concurrent execution will undoubtedly run into synchronization problems around keeping your data free of the <em>dreaded and evil data-races</em>. Recall that a data-race may occur any time you introduce <em>multiple goroutines accessing shared state where at least one goroutine writes/mutates said shared state along with one or more reads</em>.</p><p>This problem is not exclusive or special to Go. In fact, nearly all languages that offer some degree of concurrent programming will suffer from such problems and therefore require the careful and proper synchronization of shared state. (Okay, maybe not the likes of Rust or other purely functional languages)</p><p>In Go, we can synchronize with several off-the-shelf options such as <strong>channels</strong>, <strong>mutexes</strong> or <strong>atomics</strong>. Most Go enthusiasts will chant the old mantra: “<em>Do not communicate by sharing memory; instead, share memory by communicating” </em>which largely implies the use of channels because channels coupled with goroutines are practically the true stars of the Go programming language.</p><p>Here’s the problem: Although the mantra is ideal in its wisdom, many, many projects do not adhere to it. In most reasonably complex projects you’ll find a myriad of goroutines, channels, atomics and mutexes sprinkled throughout the code. So I say let’s not be afraid to further understand the <strong>sync/atomic</strong> package and more importantly why the new wrapper API might be useful.</p><p>Before Go 1.19 dropped, using the <strong>sync/atomic </strong>meant importing the package name and using its built-in functions to gate and ensure that primitive values were accessed atomically.</p><pre>// Pre Go 1.19<br><br>// Just a regular unsigned primitive 64-bit integer, starting at 0<br>var myCounter uint64<br><br>// Increment it atomically<br>// Effectively: myCounter++<br>atomics.AddUint64(&amp;myCounter, 1)<br><br>// Read it atomically<br>// Effectively: result := myCounter<br>result := atomics.LoadUint64(&amp;myCounter)<br><br>// Other atomic function calls as needed.<br>// ...<br>//</pre><p>So the code above demonstrates only the API semantics of using the <strong>sync.atomic</strong> package. Normally you would call the appropriate atomic functions within the context of goroutines as needed depending on whether you are reading or writing shared state.</p><p><strong>Side note</strong>: what happens behind the scenes when you invoke these functions is <em>architecture specific</em> and usually implemented in assembly for the target platform that your code runs on. Additionally, the Go team’s <strong>sync/atomic </strong>implementation offers the only and default <em>sequentially consistent </em>flavor of atomic access. Other languages such as C++ or Rust offer offer finer grained control but Go takes a conservative approach here and leans on offering a slower but more strict form of atomic locking that is <em>harder to screw up</em>.</p><p>Here’s the kicker with the original <strong>sync/atomic </strong>package: Nothing prevents you or any one of your fellow coding pals from just directly accessing/mutating the shared state WITHOUT using the atomic helper functions.</p><pre>// Pre Go 1.19<br><br>// Anywhere in the code someone can do this:<br>myCounter++<br><br>// This<br>myCounter = 19<br><br>// This<br>myCounter = 0<br><br>// Or this<br>myCounter = myCounter<br><br>// WITHOUT properly ensuring atomic access!!</pre><p>In other words, perhaps all your hard work to ensure that some shared state was handled properly using the <strong>sync/atomic </strong>package has now been circumvented by another developer (or you in the future because you forgot) and just happened to introduce a new function that either reads or writes some data incorrectly (read: unsynchronized).</p><p>So shoutout to Uber for creating this library a long while back because they recognized how this problem manifests in large codebases: <a href="https://github.com/uber-go/atomic">https://github.com/uber-go/atomic</a></p><p>The first sentence on their readme: <strong>Simple wrappers for primitive types to enforce atomic access.</strong></p><p>Fast-forward to Go 1.19 and beyond and a new set of <strong>sync/atomic</strong> types have been added to the standard library which offers <a href="https://github.com/uber-go/atomic/issues/119">nearly but not all</a> the same behavior.</p><p>For all the primitives in Go, there is a matching type now that effectively wraps, gates and guards a raw primitive value behind an API of truly atomic access. So long as you create a wrapped version of shared state, no one can accidentally mutate/read the shared state without ensuring proper atomic access in the context of concurrent execution. Not even future you!</p><p>So how does the previous code block change to support this new way?</p><pre>// Go 1.19 and beyond<br><br>// Creates a WRAPPED uint 64-bit integer from the start!<br>// Notice we&#39;re creating a type of atomic.Uint64 vs a uint64 directly.<br>var myCounter atomic.Uint64<br><br>// Increment it atomically<br>// Effectively: myCounter++<br>myCounter.Add(1)<br><br>// Read it atomically<br>// Effectively: result := myCounter<br>result := myCounter.Load()<br><br>// Other atomic function calls as needed.<br>// ...<br>//</pre><p>It’s no longer possible to mutate/read shared state without the atomic factor being enforced at an API level.</p><pre>// Go 1.19 and beyond<br><br>// Won&#39;t compile<br>myCounter++<br><br>// Nope<br>myCounter = 19<br><br>// Not this either<br>myCounter = 0<br><br>// Or this<br>myCounter = myCounter<br><br>// Atomic access is fully enforced! Our fellow Gophers are happy!</pre><blockquote>This is the way</blockquote><p>Now <strong>Go</strong> forth and write code that is safer, stricter and enforces proper atomic access throughout your wonderful code repos and if you found this article insightful I’d appreciate some thumbs-up, claps, high-fives, Tweets, fist-bumps and of course constructive comments!</p><p>Cheers,</p><p>@deckarep</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae14c1177ad8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go’s Extended Concurrency: Semaphores (Part 1)]]></title>
            <link>https://medium.com/@deckarep/gos-extended-concurrency-semaphores-part-1-5eeabfa351ce?source=rss-9fc3bef97981------2</link>
            <guid isPermaLink="false">https://medium.com/p/5eeabfa351ce</guid>
            <category><![CDATA[multithreading]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[semaphore]]></category>
            <dc:creator><![CDATA[Ralph Caraveo]]></dc:creator>
            <pubDate>Tue, 04 Sep 2018 05:36:18 GMT</pubDate>
            <atom:updated>2018-09-04T05:36:18.465Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*C_tvcIQjhtulm4hU7Elf8g.png" /><figcaption>Gophers waiting in line to go to the discotheque</figcaption></figure><p>Goroutines, Channels and Mutexes — if you’ve spent any significant time with Go’s tried and true synchronization primitives at all you should be well adapted to seeing these concepts expressed in everyday code. From very large open-source projects, to quick and dirty scripts; Go’s model of concurrency is not only powerful but a first class concept to the language.</p><p>Today I’d like to give an honorable mention to another powerful Go synchronization primitive out in the wild that I find myself reaching for more and more. The <strong>semaphore</strong>:<em> a construct which can be used to constrain or control access to a shared resource access via multiple threads or goroutines.</em></p><h4>Semaphores in the real world</h4><p>Are you hungry? Do you want to come with me to In-N-Out to go and get a burger? Imagine if you joined me along with 2000 other readers RIGHT NOW and we all went to get a burger…RIGHT NOW.</p><p>Now imagine if In-N-Out didn’t have a the concept of lines and we all just tried to order at the same time! What would happen? Chaos would happen! We would all enter In-N-Out at the same time, pushing each other, stomping on each other. We’d all be trying to order at the same time and probably shouting over each other. Meanwhile the employees would be confused, stressed out and would probably be crying as they scramble to fulfill all of our orders at the EXACT SAME TIME.</p><p>Assuming the employees grit their teeth and attempted to work through this calamity what would happen? Well, it’s very likely that our orders would take hours to fulfill. It’s also very likely that some orders would go missing. Some orders would be slightly wrong…and yet some orders would be vastly wrong. What the heck? I didn’t order this half-cooked 4x4 animal style with grilled onions. I don’t even like grilled onions and why is this burger only half-cooked?</p><p>Okay you get the point. Luckily, In-N-Out operates in a civilized fashion these days. The restaurant has around 3 or 4 lines where customers will stand and order by waiting their turn. Assuming In-N-Out has 4 lines, with 4 registers and 4 employees to receive orders this really means In-N-Out has a system in place that acts as a semaphore. The semaphore is the concept that allows In-N-Out to receive 4 orders concurrently (actually in parallel), causing everyone else to sit and wait. Of course there is likely a 5th order occurring at the drive-thru but that is besides the point.</p><p>This system works as long as everybody plays by the rules. This system allows In-N-Out to conduct business in a sane way. To take orders fast enough to keep the business moving but not so fast that chaos occurs.</p><p><strong>An aside: </strong>Aren’t we really just talking about queues — another well known data-structure that allows FIFO access? It’s true, that the above scenario does have a relationship to utilizing queues and yes, each line can be thought of as a small queue. However, in the example above I’m less concerned about the actual lines/queues and more concerned with what’s happening at the head of all 4 lines. You see, In-N-Out can only truly ever allow access to 4 registers at a time. People standing in lines may go to the bathroom, they may jump to a shorter line, or let their friends cut. The primary concern here again is that we only ever have 4 cash registers and so how do we enforce a system where we don’t ever allow more than 4 people to order at a time. In this real-life example, perhaps the manager comes out and sends people away if they’re trying to order out of turn. “Hey, we can only take 4 orders at a time, so get in line buddy…” This means the manager can be thought of a enforcing semaphore logic.</p><p>This is why you need to know and understand semaphores.</p><h4>Building a more technical mental model</h4><p>Let us think about what the point of a <strong>semaphore</strong> is and why we’d ever want to use it. First don’t let the name throw you off too much. Suppose you have a shared resource that can be freely accessed by one or more threads. This shared resource could be a remote server, a shared in-memory datastore, a complex video transcoding pipeline. It doesn’t really matter what is being shared. What matters it that a system that is shared should be shared in a reliable way that doesn’t allow for bombardment of the system.</p><p>Specifically, if you were trying to control access to a pipeline it would be a shame if anyone could access your pipeline and kicked off 10,000 jobs to transcode some video. Such a situation can bring your transcoding pipeline to an immediate halt. This would be very bad for business.</p><p>Think of a <strong>mutex</strong>, which <em>controls multi-threaded access to code</em>. The <strong>mutex</strong> guards a critical section only ever allowing a <strong>single thread</strong> exclusivity over this code. This is important to avoid data-races and helps to avoid all sorts of problems.</p><p>Now let’s compare a <strong>mutex</strong> to a <strong>sempahore</strong>. If a mutex is concerned with ensuring a <em>single thread ever accesses code exclusively</em> a semaphore is concerned with ensuring <em>at most N threads can ever access code exclusively</em>. This really just means that a semaphore is a more generalized version of a mutex (mostly).</p><p>With that out of the way, you might be wondering what’s the point of giving exclusive access to N threads? The point is that in this scenario you are purposefully constraining access to a resource therefore protecting that resource from overuse.</p><p>One important point to understand is that this control and protection is <em>only good if each thread is playing the game properly</em>. Each thread must be aware of the semaphore and not do anything to circumvent using the semaphore otherwise you’ll end up back in square one. This is why it’s sometimes a good idea to abstract a way such details behind an API. The API acts as the only conduit into a service and within such a service control is limited via a semaphore.</p><p>Let’s recap before we continue:</p><ol><li><strong>mutex</strong>: constrains access to a <strong>1</strong> single thread, to guard a critical section of code.</li><li><strong>semaphore</strong>: constrains access to at most <strong>N threads</strong>, to control/limit concurrent access to a shared resource.</li></ol><p>When using a semaphore how do you figure out the value of <strong>N</strong> for how many threads to limit? Unfortunately there is no hard and fast rule and the final number of N is going to depend on many factors. A good place to start is by benchmarking and actually hitting your shared resource to see where it starts to fall over in terms of performance and latency. I can certainly tell you right now that creating a semaphore allowing access to up to 1 million goroutines is probably a bad thing. Forgive the silliness but the point is that you don’t want to reach for a semaphore if all you’re going to do is allow wide open access.</p><p>Now that we have some definitions out of the way let’s see how we could use a semaphore with some very basic examples. In the following two sections notice how minimal the code is to use such a powerful construct.</p><h3>Bounding concurrency with the use of semaphores</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*OtPYTt5JhsFEw8VIBt_vaw.jpeg" /><figcaption>Go semaphores!</figcaption></figure><p><strong>Problem</strong>: Code that utilizes many goroutines can run the risk of bombarding some remote resource with hundreds or even thousands of goroutines. A bombarded system can quickly become overloaded to the point where you experience <em>extreme latency spikes</em> or worse <em>service outages</em>.</p><pre>// remoteDeleteEmployeeRPC will delete an employee over the network.<br>func remoteDeleteEmployeeRPC() {<br>    ...<br>}</pre><pre>func main() {<br>   for _, employee := range employeeList {<br>       <strong>go</strong> remoteDeleteEmployeeRPC(employee.ID)<br>   }<br>}</pre><p>First, the code above is not quite realistic or robust. I’m purposefully <strong>not bothering with error handling</strong> and I’m <strong>not handling any type of return value</strong> and one could argue that such an operation could be done in a batched fashion.</p><p>The point I want to illustrate however is that the code above can easily get out of hand depending on the size of the employeeList variable. If the employeeList variable holds 5, 10 or even 50 employee records within it…you may find that the remoteDeleteEmployeeRPC invocation doesn’t even break a sweat.</p><p>You may additionally find that this code causes a catastrophic outage simply because the concurrency is <em>potentially unbounded</em>. You might say however, “but it is bounded by the size of the employeeList slice” which technically is accurate. However, unless you can always guarantee that employeeList will only ever be <em>a reasonable size</em><strong><em> </em></strong>of which downstream services will handle gracefully, you can practically assume that this code is dangerous in the long term. As your system grows and complexity increases, it’s not unheard of to suddenly find yourself faced with a runaway storm of concurrent calls to your remote endpoints. Such an issue can occur in a single instance scenario where a single client is bombarding a remote resource. Now consider an army of clients executing unbounded calls…this is a recipe for disaster.</p><p><strong>Option 1</strong>: Bounded channel</p><pre>// remoteDeleteEmployeeRPC will delete an employee over the network.<br>func remoteDeleteEmployeeRPC() {<br>    ...<br>}</pre><pre>// sem is a channel that will allow up to 10 concurrent operations.<br><strong>var sem = make(chan int, 10)</strong></pre><pre>func main() {<br>   for _, employee := range <strong>employeeList</strong> {<br>       <strong>sem &lt;- 1</strong><br>       go func(){ <br>           remoteDeleteEmployeeRPC(employee.ID)<br>           <strong>&lt;-sem</strong><br>       }()<br>   }<br>}</pre><p>The technique above requires nothing other than a <strong>buffered channel</strong> to accomplish the goal. In this particular case, we’re exploiting the properties of a buffered channel to simply<em> limit concurrency</em>. For each iteration of the loop over employeeList an attempt is made to place a single integer in the sem channel. If there is room, progress is made and the code below will immediately execute kicking off the goroutine. Once the remoteDeleteEmployeeRPC call returns, the sem is drained of the (single) integer therefore allowing the progress of yet another iteration to occur.</p><p>So what happens when the sem is full? Let’s think about what is happening for a minute. If the sem is full, this means that we must have <em>at least</em> <strong>10</strong> active goroutines trying to make progress over the network. If an 11th iteration occurs when an attempt is made to send a single integer value into the bounded sem channel, this <em>will cause the code to </em><strong><em>block</em></strong>. The blocking is a fundamental property of of the bounded channel and this is a <em>good thing</em>! That 11th iteration is effectively held there until a single slot opens up in the channel. A single slot <em>will eventually </em>open up when one of the original 10 RPC calls returns to <em>drain a single integer off the channel</em>. A remote RPC call should return either upon success, an error or a timeout situation. At this point, the 11th iteration that was previously blocked can make forward progress.</p><p>Please note that in this example above, the <em>buffered channel</em> is utilized in such a way where we are exploiting its behavior but not necessarily using it to send meaningful data through it. Additionally, we are able to utilize the sem from multiple goroutines because after all, it is a channel and <em>channels are inherently threat-safe</em>. This is okay, and it works very well but let’s move to the big-brother version of the semaphore.</p><p><strong>Option 2</strong>: Weighted Semaphore<br><strong>Implementation: </strong><a href="https://github.com/golang/sync/tree/master/semaphore">https://github.com/golang/sync/tree/master/semaphore</a></p><p>This implementation of a weighted semaphore is quite a bit more sophisticated then a bounded channel. In fact, the bounded channel approach works well and wonderfully illustrates how a semaphore truly works but it was really just a segue into understanding this package.</p><p>How is the weighted semaphore different? It has a few tricks up its sleeve but it’s not very different at all from the key concept of limiting concurrent execution.</p><p>First and foremost the weighted semaphore must be brought in as a dependency and imported appropriately.</p><p><strong>import “golang.org/x/sync/semaphore”</strong></p><p>Next we need to instantiate the weighted semaphore in order to use it.</p><p><strong>var sem = semaphore.NewWeighted(int64(10))</strong></p><p>Next we need to know how to increment/decrement the semaphore to actually control and limit our concurrency.</p><pre>// Acquiring is as easy as<br><strong>sem.Acquire(ctx, 1)</strong> // equivalent to <strong>sem &lt;- 1</strong> (using channel approach)</pre><pre>// Releasing is as easy as<br><strong>sem.Release(1)</strong> // equivalent to <strong>&lt;- sem</strong> (using channel approach)</pre><p>Now let’s highlight some key differences to this implementation. The weighted semaphore additionally has a TryAcquire function which will <em>attempt</em> to acquire room to execute some operation but much differently <em>will not block if the semaphore could not be acquired</em>. This behavior allows your code to perhaps do other things instead of having your code enter a blocked state.</p><p>Also, you’ll notice that Acquire, Release, TryAcquire all allow you to specify <em>how many units of concurrency</em> you want to execute against. This is why this implementation is known as a <strong>weighted</strong> <strong>semaphore</strong> and gives your code the ability to more finely control concurrent resource usage. Perhaps you want to bound your concurrency against a number of remote RPC calls where not all invocations are equivalent in terms of cost. This is why the weighted semaphore gives more fine grained control.</p><p>The last two differences are somewhat minor but important to call out. Primarily the weighted semaphore has the ability to <em>return error states</em> and it also supports the awareness of a context.Context argument. Practically all modern Go code utilizes context.Context and proper error handling as a best-practice with networked calls. I’ll leave it as an exercise to the reader to potentially learn more these practices.</p><p>Wrapping up the weighted semaphore let’s pull everything together for a quick view of what the usage might look like with our existing code.</p><pre>// remoteDeleteEmployeeRPC will delete an employee over the network.<br>func remoteDeleteEmployeeRPC() {<br>    ...<br>}</pre><pre>// sem is a weighted semaphore allowing up to 10 concurrent operations.<br><strong>var sem = semaphore.NewWeighted(int64(10)) </strong></pre><pre>func main() {<br>   // a context is required for the weighted semaphore pkg.<br>   ctx := context.Background()<br>   for _, employee := range employeeList {<br>       if err := <strong>sem.Acquire(ctx, 1)</strong>; err != nil { <br>           // handle error and maybe break<br>       }<br>       go func(){ <br>           remoteDeleteEmployeeRPC(employee.ID)<br>           <strong>sem.Release(1)</strong><br>       }()<br>   }<br>}</pre><p>Consider for a minute the the use of the weighted semaphore almost parallels the use of a bounded channel. The truth is that the API for using them is nearly identical and they both do a wonderful job of limiting access.</p><p>Using a semaphore boils down to the following:</p><ol><li>you need a way to instantiate a semaphore with some defined limit.</li><li>you need a way to Acquire() every time access into a shared resource is requested</li><li>you need a way to Release() every time access into a shared resource is finished</li></ol><p>And so this ends <strong>Part 1</strong> on semaphores. Do not underestimate the power of controlling your concurrency by limiting it. Don’t let your concurrent goroutines escape you and your system only to <em>bring down another system</em>. Such a tool is a great way to add resiliency to your application and is a very common thing to do.</p><p>Well, this wraps up <strong>Part 1 of Go’s Extended Concurrency</strong> story and if you enjoyed this article and found it remotely useful I’d love to hear from you. If you found this article to be too basic/intermediate or too advanced I’d love to hear from you. If you found this article to be inaccurate or confusing I’d really love to hear from you. Finally, if you learned anything at all please clap and subscribe to my feed as I periodically publish articles such as this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5eeabfa351ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The new kid in town — Go’s sync.Map]]></title>
            <link>https://medium.com/@deckarep/the-new-kid-in-town-gos-sync-map-de24a6bf7c2c?source=rss-9fc3bef97981------2</link>
            <guid isPermaLink="false">https://medium.com/p/de24a6bf7c2c</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Ralph Caraveo]]></dc:creator>
            <pubDate>Sun, 10 Sep 2017 05:18:19 GMT</pubDate>
            <atom:updated>2017-09-10T05:18:19.186Z</atom:updated>
            <content:encoded><![CDATA[<p><em>A learning and exploratory analysis of the new </em><em>sync.Map type in Go 1.9.</em></p><p>Go 1.9 is out now and I was eager to get my hands on the new <a href="https://golang.org/pkg/sync/#Map">sync.Map</a> container that exists in the sync package. First of all — why does this even exist and when should you use it? Unfortunately the answer is: you really should not use it unless <strong>a.) you’ve identified a bottleneck</strong> in your application with the pre-existing and type-safe map and <strong>b.) </strong>your use case <strong>matches the exact use case</strong> of which the new sync.Map was designed for.</p><p>Let’s be real. If you reach for the new sync.Map construct without having first identified and measured that you’ve got contention with the existing map implementation you might be trading some headaches and pain for more headaches and pain. A few guarantees go out the window when you use the hot new concurrent implementation.</p><p>First you lose the obvious one which is type safety. I don’t know about you but with the limited set of data structures provided in the Go standard library, the obvious choice for anything key/value based is your basic map builtin. Let’s not trade type safety and a simple generic data structure for one that is not.</p><p>Also, if you read the docs on the iteration functionality of the new sync.Map you’ll notice that some additional guarantees are thrown out the window in terms of the observed snapshot that you’ll see as you <a href="https://golang.org/pkg/sync/#Map.Range">Range</a> over the collection of data.</p><blockquote>Range does not necessarily correspond to any consistent snapshot of the Map’s contents</blockquote><p>The next thing on the list that you may lose is…<strong>performance</strong>? In many cases, the existing implementation of the builtin map is well performant enough and will many times beat the sync.Map implementation hands down in sheer operations per time-unit. In fact if you’re able to leverage a builtin map without the use of locking semantics you may well already have a winner in your hands.</p><p>And I bring up locking for a reason: The builtin map has a dirty little secret that most of you should know by now. It’s not thread-safe (or concurrent safe) when reading to/writing to happens with more than one goroutine. This is why the standard library provides a few tools at your disposal in the form of synchronization primitives. You’ve got channels, you’ve got mutexes, you’ve got r/w mutexes and you’ve got atomic operations.</p><p>And, depending on your use case it’s not uncommon to reach for one or two of these constructs so that you can enjoy production code that is free of data-races. Which tools you use are beyond the scope of this article but let’s go back to the builtin map when faced with concurrent updates of this data structure.</p><p>I like to choose my synchronization primitives carefully before I even think about writing code and you should too. First, if I’m not sharing any state, than I can write code that is completely void of synchronization primitives because I have chosen a shared-nothing architecture. This usually is always the ideal case, but not always the reachable dream that it is.</p><p>Anything that is reasonably complex is usually always going to fall into some choke-point where code needs coordination and/or synchronization…that is unless you live in the purest of pure worlds (I’m looking at you Haskell). And here is where a common idiom shows up to wrap up the basic and builtin map into a nice little package where synchronization is baked in.</p><p>In those cases where you are mostly doing mutation, you can reach for the tried and true sync.Mutex. This data-structure will provide you with the necessary locking to protect a built-in map from concurrent updates.</p><p>The problem with the sync.Mutex arises however when you need to read from your builtin map. Whether it’s getting a count of the number of elements, simply iterating over the map or just checking if a value exists in a map — all of those are read operations that technically <strong>should</strong> be allowed to make progress with other read operations so long as no mutation occurs.</p><p>Enter the sync.RWMutex where Go again has your back. The contract proposed here is that all your read operations must promise to take a read-lock: RLock when they’re guaranteed to NOT mutate the state of the map. Conversely any operation that mutates the map must agree to take the regular write-lock: Lock such that writers are guaranteed exclusivity under update scenarios.</p><p>The great thing with the sync.RWMutex is we can help to reduce some contention when we have scenarios where high-read rates occur. At least, that was the hope…</p><p>Here’s an example of a builtin map that has been wrapped in a similar API as that of the sync.Map</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24170cf97a2b074204d55602cf01d18a/href">https://medium.com/media/24170cf97a2b074204d55602cf01d18a/href</a></iframe><p>Notice in the code above that Load is the only receiver method that does not mutate the state of the internal map. This is important because now Load is free to take a read-lock now affording concurrent progress to those goroutines that are executing this method.</p><p>That was until under additional scrutiny and performance analysis of the standard lib, that various members of the Go team discovered less than ideal performance when a sync.RWMutex was utilized in systems that were deployed on many, many cores. And by many I’m not really talking about your typical 8 or 16 core setup but primarily those server setups that are well beyond that. Where the number of cores really starts to show in the form of highly contentious code such as when the sync.RWMutex is used.</p><p>And now we’ve arrived as to why the sync.Map was created. The Go team identified situations in the standard lib where performance wasn’t great. There were cases where items were fetched from data structures wrapped in a sync.RWMutex, under high read scenarios while deployed on very high multi-core setups and performance suffered considerably.</p><p>In fact, there’s a quite nice story about it here that was done as a lightning talk at the <strong>2017 GopherCon</strong>: <a href="https://www.youtube.com/watch?v=C1EtfDnsdDs">An overview of sync.Map</a>. If you’re considering using the implementation please watch this talk as there are some possible performance traps with it. Also, this talk while short definitely goes much more in-depth of why this was created and where it was designed to shine.</p><p>Let’s examine the usage of the sync.Map in the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b3050b703e956d778be6fb6c4ceddbc/href">https://medium.com/media/0b3050b703e956d778be6fb6c4ceddbc/href</a></iframe><p>Notice that the API is clearly different from the regular built-in map usage. Additionally, because this implementation of the map is not type-safe when we fetch (Load) an item from the map we must do a type conversion…which may succeed or fail. Be careful.</p><p>In addition to Load and Store we also have methods for Delete, LoadOrStore and finally Range which was mentioned earlier. It is an exercise for the reader to understand the usage of those methods in detail.</p><blockquote>So how does the sync.Map perform?</blockquote><p>Well, my performance analysis is on-going currently. I’ve only begun to start analyzing this thing and I’m trying my best to write correct benchmarks that mimic different scenarios. Furthermore, I’m trying to find the target scenario that this container was designed for so the best performance case can be demonstrated for the <em>ideal scenarios</em>.</p><p>The documentation on the performance characteristics are as stated below:</p><blockquote>It is optimized for use in <strong>concurrent loops</strong> with keys that are <strong>stable over time</strong>, and either few <strong>steady-state stores</strong>, or stores <strong>localized to one goroutine per key</strong>.</blockquote><blockquote>For use cases that do not share these attributes, <strong>it will likely have comparable or worse performance</strong> and worse type safety than an ordinary map paired with a read-write mutex.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*g0W-pAa00oXaRqo8tAlPDg.png" /></figure><p>So as I understand this above, I might have the chance to use this if I have an existing map that shows signs of contention where the keys are not constantly changing for the life of the application. To me this practically translates to high-read scenarios with relatively few updates to the map or where many updates may occur in bursty scenario.</p><p>The first benchmark shows how the regular builtin map wrapped with a RWMutex compares with the sync.Map for Store operations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04a5ea1713ce33a0948c0395ce188d23/href">https://medium.com/media/04a5ea1713ce33a0948c0395ce188d23/href</a></iframe><p>Next we show a benchmark of Delete operations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a4fc22e13870ea1ad9b17c9b6ddc964/href">https://medium.com/media/3a4fc22e13870ea1ad9b17c9b6ddc964/href</a></iframe><p>Next we show a benchmark of Load operations, where the Found case should always find a result in the maps. And the NotFound case will <em>almost</em> always not find a result in the maps.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2e7ed609b54c7e526f0522e47a21f95/href">https://medium.com/media/d2e7ed609b54c7e526f0522e47a21f95/href</a></iframe><p>As you can see, with these benchmarks in many of the cases the regular built-in map protected by a RWMutex almost always wins out and so far and we haven’t introduced benchmarks with more than a single goroutine. Let’s keep going with this to see what happens when we write a benchmark that is designed to measure performance where scaling across cores becomes a factor.</p><blockquote>¯\_(ツ)_/¯</blockquote><p>Let’s fire up a <strong>32-core High CPU box on Digital Ocean</strong> to see if any trends are revealed. For this I created a box in San Francisco, running <strong>Ubuntu 16.04.3 (x64)</strong> and I picked the most expensive system they have which costs a whopping $640 a month for this box. For me, running these benchmarks I paid for just maybe 2 hours worth so maybe just a couple bucks. This is not a commercial for Digital Ocean but I think their stack is pretty slick and <em>they use Go internally</em> so why not?</p><p>For this benchmark, I want to measure both map implementations under load <strong>in the ideal scenario</strong> at least for now. In this scenario I’ll setup both maps to store a random set of data and I’ll set GOMAXPROCS to something different on each variation of the benchmark and create N number of goroutine workers that match the GOMAXPROCS setting.</p><p>Again, I’ll be running these tests to mimic <em>a high-read scenario</em> where the set of keys is populated and fixed before the clock on the benchmark starts. On the next update of this post, I might create a benchmark to mimic perhaps an equal number of writes but for now let’s consider this a somewhat artificial but ideal use-case.</p><p>On with the show. Here’s how we’ll define our concurrent benchmarks but first a quick synopsis of the code:</p><p>The benchmarks are actually functions invoked by other functions where we have a workerCount hardcoded. This allows us to configure the GOMAXPROCS and number of workers per benchmark.</p><p>To ensure we don’t hit Go’s complier optimizations, we capture the output of the Load methods. We don’t necessarily care what the results are but we want to ensure that Go doesn’t perform any dead code elimination because returned results are unused.</p><p>The primary part of the code will kick off a goroutine as a worker which will iterate as far as necessary to satisfy Go’s benchmarking facility through the use of the b.N value. With each goroutine in flight, we’ll perform our Load operation and eventually signal that we’re done using a sync.WaitGroup.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92bf4a1d23c53a3d02c75ab6a3e2df74/href">https://medium.com/media/92bf4a1d23c53a3d02c75ab6a3e2df74/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*8sCf9LsFhVcZLMw45kn0nQ.png" /><figcaption>This graph illustrates the performance of the BenchmarkXXXStableKeysFoundX noted above.</figcaption></figure><p>As you can see right off the bat, we have great performance with our <strong>regular map</strong> which is protected by a RWMutex. Things are moving along great until we start hitting 4 cores where not only contention becomes an issue but our scaling factor across cores as well. If you keep your eyes on the red line, once we start getting passed around 8 cores forget it. We have way too much read contention with a RWMutex to the point where performance suffers greatly by the time we hit the 32 core mark.</p><p>The blue line however is the sync.Map showing a fairly predicable behavior as we continue to scale across cores. I would say at this point that with my <strong>rudimentary</strong> testing and analysis we can see where the sync.Map shines…<em>in the ideal scenario it was built for</em>.</p><p>So how is sync.Map able to realize this performance? Well that is a story for another blog-post and explores more advanced topics such as <strong>lock-free programming</strong> and <strong>atomic operations</strong> at the cost of reasoning and readability. Maybe we’ll get into it…maybe we won’t as I’m not sure at this point.</p><p>If you’re still with me you might have questions about this benchmark or suggestions on improving it. Please help me make this benchmark better by forking it or creating issues: <a href="https://github.com/deckarep/sync-map-analysis">https://github.com/deckarep/sync-map-analysis</a>.</p><p>There are additional test scenarios that I’d like to analyze and for this blog-post I wanted to start with a basic ideal scenario of what I think the sync.Map was designed for. And at any rate, this was just an excuse to play with the new data structure and learn more about it.</p><p>Please leave feedback and comments and remember this overly used but still very relevant quote BEFORE you even think about using the sync.Map structure.</p><blockquote><strong>“Premature optimization is the root of all evil.” — </strong>Donald Knuth</blockquote><p>Happy Go Programming!</p><p>@deckarep</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=de24a6bf7c2c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Debouncing Consumer Queues in Go]]></title>
            <link>https://medium.com/@deckarep/debouncing-consumer-queues-in-go-956aaef517c8?source=rss-9fc3bef97981------2</link>
            <guid isPermaLink="false">https://medium.com/p/956aaef517c8</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Ralph Caraveo]]></dc:creator>
            <pubDate>Wed, 06 Sep 2017 04:49:23 GMT</pubDate>
            <atom:updated>2017-09-06T04:49:23.427Z</atom:updated>
            <content:encoded><![CDATA[<p>I’m going to make this a short one since I have a tendency to ramble. At my current place of work, we have a pretty extensive infrastructure based on Go and Kafka. Our pipeline is complex and we’re always looking for ways to keep the machine well-oiled and as smooth as can be. Most of us in this industry have a stake in our infrastructure doing our parts to keep things solid.</p><p>In one particular situation we have a Kafka consuming service written in Go which is a medium size cluster of workers. This particular cluster’s job is to index data into Elastic Search as events enter this area of our pipeline from services upstream. Nothing too earth shattering here so far but we had a recent fire where we realized that in some cases we can get into a state with our consumers where we’re re-processing our indexing work for a given document over and over…kinda like a machine gun. Obviously this is not ideal for a number of reasons and we identified the source of our runaway event stream. Fixing this problem truly lies within making sure our upstream service doesn’t get into a runaway state where it’s just asking for the same work to be done over and over again. Maybe we can chat about that in another post.</p><p>But…is there anything we can do to help mitigate our issue at the consumer level? Is there a way to make our consumer a bit smarter so we don’t have to bang so hard on Elastic Search should we get an unreasonable stream of events from somewhere upstream?</p><p>As I worked through this problem with some of my counter-parts at work I realized that I had solved this in the past already in a previous setting. For those of you who might have done Desktop UI work or even Web UI work — sometimes either the browser or OS can pump user events into your app a pace much higher than ideal. In those situations, you may end up reprocessing your work to calculate something such a position on the screen or a particular state of a UI component. Sometimes this processing step is expensive, time consuming and usually unnecessary to recalculate so often.</p><p>This is where I had remembered that back in the day I utilized a technique called “<a href="http://underscorejs.org/#debounce"><strong>debouncing</strong></a>” which can be considered as a form of rate-limiting or throttling. And as it turns out doing something like this in Go works extremely well when coupled with the Select construct and Go’s powerful concurrency primitive: the channel.</p><p>I’m going to propose some code which recreates a very simplified solution in Go to <strong>debounce</strong> a consumer that is consuming from an upstream producer. In the example code below, we have 3 things which illustrate the problem at hand.</p><ol><li>We have a Consumer which reads off some queue (or topic in the Kafka case) and when faced with re-processing the same exact data, just simply won’t be able to keep up at the rate of events published.</li><li>We have a function or task that does some useful work but is either time consuming or expensive in terms of CPU.</li><li>We have a Producer that produces a stream of events where in our case we know will overwhelm our Consumer.</li></ol><p>So let’s be clear that this strategy won’t work for all cases especially if you’re aggregating streams of data. But in our case we know that if we’re processing some work currently, and we’re asked to reprocess that same work it’s reasonable to expect that in a high throughput scenario some of that reprocessing is completely useless and really just adds contention to our overall pipeline.</p><p>The proposed solution is as follows:</p><ol><li>An event enters our system with the key of: “abc” and we start processing (indexing) the data.</li><li>If another event enters our system with a key other than “abc”, we can process that in parallel along with more keys up to a reasonable bound. That is all fine and dandy and not the problem domain for this post.</li><li>If while processing “abc” we get the <strong>same</strong> event to reprocess “abc” we will enqueue that work in a queue with a depth size of 1. Perhaps this new event came in and we genuinely should re-index Elastic Search. Also, in our case we’d rather not process the same index in parallel so this queue affords us sequential indexing for the same-key units of work.</li><li>If yet another event comes into our consumer with key “abc” and we already have our queue full let’s just throw that event out the window because we already have a re-indexing event in our queue that must be processed once the original “abc” unit of work completes.</li></ol><p>One side note: Our current implementation suggests that our consumers consume off our queue asynchronously depending on the amount of free workers in our worker pool. Also, this model that I’m talking about is a bit simplified in the example below and the code I will show is just a small Go script that illustrates a non-distributed example. However the principal still applies.</p><p>On to the code already:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b900e96a278499364aafe3189e7d4f7/href">https://medium.com/media/6b900e96a278499364aafe3189e7d4f7/href</a></iframe><p>Here we have a single <strong>main.go</strong> file with <strong>4 functions</strong>. The <strong>main</strong> function kicks off our consumer and producer in a goroutine each and will block waiting for a signal.</p><p>The <strong>startConsumer</strong> is easy, it’s just going to loop indefinitely reading from a <strong>buffered queue</strong> with a depth size of 1.</p><p>The <strong>startProducer</strong> is modest as well: It’s simply going to loop indefinitely and generate a random number on each iteration. If we generate a 99 or greater we’ll enqueue an event if our buffer is not full otherwise we toss out that event. This really illustrates the power of the using a <strong>select</strong> construct to write data or…get off the pot. This function is meant to illustrate a producer that really produces too fast to be meaningful and where we don’t really care if we have to queue depth of 1 or 20…as long as we know we have another round of work to process it accounts for ALL of that work.</p><p>The <strong>timeConsumingWorker</strong> represents the actual function that does the actual work and simulates this by taking at least a second to complete for each invocation.</p><p>The great thing about this code is that it’s using very basic Go constructs to either write data on the channel OR if the queue is full, we already have work accounted for and can throw our excess work away. You’ll notice that we have 2 <strong>atomic</strong> variables which keep track of how many items were tossed vs processed. <em>The tossed portion represents all those events that were wasteful and would have bombarded our system by asking Elastic Search to constantly re-index data</em>.</p><p>And that pretty much sums up a small strategy for <em>debouncing queues in Go</em>. This technique can be applied to really any evented publish/subscribe system where you don’t really need to reprocess the same data but you do care that you don’t miss that perhaps crucial and last event.</p><p>Thanks for reading and always happy to get feedback on my posts.</p><p>Cheers!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=956aaef517c8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Rationale of Rust]]></title>
            <link>https://medium.com/@deckarep/the-rationale-of-rust-79d5dc353e46?source=rss-9fc3bef97981------2</link>
            <guid isPermaLink="false">https://medium.com/p/79d5dc353e46</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[rust]]></category>
            <dc:creator><![CDATA[Ralph Caraveo]]></dc:creator>
            <pubDate>Thu, 27 Apr 2017 20:17:25 GMT</pubDate>
            <atom:updated>2017-04-27T20:17:25.333Z</atom:updated>
            <content:encoded><![CDATA[<p>Today I’ll write a short and sweet post that talks about the rationale of Rust, why you’d want to learn such a language and the many benefits you’ll find in the various corners of the language.</p><p>First, let’s get this out of the way. Rust does have a steeper learning curve than your average modern language out there. Those who open up a fairly complicated codebase often scoff and dismiss its syntax as being a bit ugly, verbose and confusing. And while the language authors are working hard to minimize the boilerplate around syntax and enhance the ergonomics of the language there still exists an increased expenditure in both writing and reading Rust code. Admittedly, should you find yourself working in Rust at some point, you will spend more brain power up front.</p><p>Let’s harp on this point again so that you can first accept it before going forward.</p><blockquote>When reading or writing Rust, you will spend more brain power up front.</blockquote><p>Let’s also acknowledge that the Rust authors are constantly taking a hard look at the language. They are interested in writing a language that succeeds and that you ultimately find as a valuable tool in your tool box. They’re not interested in providing a language that is cryptic, noisy and just plain hard to follow. But what they’re faced with is a hard challenge known as the trifecta: <strong>safety, speed and concurrency</strong>.</p><p>I’ll bring another point home: If you are one of those developers that compares a language like Python to Rust and quickly dismisses Rust for being too complicated you are not making a fair comparison here. Python was never originally designed to address the trifecta. Speed was traded for developer productivity and concurrency was traded for…well it’s been kind of absent/broken in Python for awhile now and is now being addressed with Python 3. Don’t get me wrong, Python has its place in the world and I still love the feeling of being able to quickly lay down some code that reads and writes easily.</p><p>Also, if you working in a domain where you can make trade-offs where you can sacrifice speed, perhaps don’t need concurrency and just need something safe, then congratulations you don’t really need to bother with Rust. But what if you do want or need all three of these options? Which language do you reach for? The obvious big two that come to mind is C and C++…but even these languages don’t achieve the trifecta. Both can be used successfully with a very disciplined programmer with many years of experience but both are still very, very unsafe if you are not careful.</p><p>Even if you are super careful, you are still human and will make mistakes. Thorough testing, detailed code reviews and years of experience are your allies. But what if I told you there exists a language where you don’t need a decade of experience to be good at it? There exists a language where you don’t even need to be very disciplined on the subject of correctness? And there exists a language that will help you exploit the speed of the hardware you’re running?</p><p>You see the point that you are missing with your naivety is that all of those burdens above are farmed off to Rust’s wicked compiler. Rust’s compiler is disciplined, it’s experienced and it will help you to exploit speed and concurrency all while being safe.</p><p>Therein lies the rationale on why Rust’s learning curve is higher than other languages. You need to put in a little more effort upfront to help the compiler and in return your resulting binary is fast, safe and concurrent all while being correct. The point here is that you end up with rock-solid software that allows you to mostly focus on what your business logic. For everything else, the compiler has your back.</p><blockquote>Rust helps you write <strong>correct</strong> applications that are fast, concurrent and safe.</blockquote><p>I’m going to play devils advocate here and provide various responses to this article so far from a few different naive perspectives:</p><blockquote>C/C++ Guru</blockquote><p><strong>C++ Guru: </strong>@deckarep obviously you must not realize that the OS you’re running on was written in C or C++ and you obviously don’t realize that we now have modern extensions to these languages like C99, C++98, C++03, C++11, C++14, C++17, C++20 which all help to write smart, modern object-oriented code.</p><p><strong>My response:</strong> That’s great…I don’t have the time or energy to read all these specifications, I also don’t want to work in a language that needs backwards compatibility going back as far as the Jurassic period AND I want to work in this space too without having 15 years of experience and btw, it’s common knowledge that OOP sucks!</p><blockquote>Just Another Perl Hacker</blockquote><p><strong>JAPH:</strong> @deckarep want to know why Rust is lame? I can literally rewrite the the Rust compiler in Perl in like 1/50th of the code size. Boom: Mic Drop.</p><p><strong>My response:</strong> Good for you, I can appreciate fun cryptic coding puzzles just like the next person but Perl is slow, the syntax is sdrawkcab and I’m not interested in maintaining a 3-line implementation of the Raft protocol when you decide to join that next company.</p><blockquote>Gopher</blockquote><p><strong>Gopher:</strong> @deckarep you nailed it; this is why Google created Go and actually built-in concurrency from the ground up where you can send data through channels this way your application is properly synchronized…oh and it’s type safe too!</p><p><strong>My response: </strong>mmkay…how exactly is this: interface{} type-safe? Also, good luck tracking down data-race riddled code. Engineers still need to be incredibly disciplined here <strong>which many are not</strong> and the race-detector is NOT bullet-proof. I see it all the time…</p><blockquote>Node.js Hacker</blockquote><p><strong>Node.js Dev</strong>: @deckarep, I don’t care about Rust I’ve got my safety and speed of the V8 engine and I’ll just utilize promises to ensure I can have my concurrency cake and eat it too.</p><p><strong>My response</strong>: GTFO scrub…also <a href="https://www.pluralsight.com/guides/node-js/yarn-a-package-manager-for-node-js">read this</a>.</p><p>Before you get up in arms about my artificial responses first off let me say…ANY language has its problems, Rust included. I’ve used all of the languages above at some point or another. Some I like more than others but I can appreciate them all in the right scenario.</p><p>This post remember, was about the rationale of Rust and so all of my artificial responses provide clues as to why people are working so incredibly hard to make this language succeed.</p><ul><li>Rust doesn’t want to be tied down by 30+ years of backwards compatibility. Imagine how inventive you can be and what you can try when you start fresh.</li><li>Rust eschews object-oriented design. The world has moved on…we’ve lived and learned and in the process have realized that true object-oriented design is hard to do well especially when things get deep. Favor composition.</li><li>Rust still wants to be readable and is trying really hard to get this right. An incredible amount of thought is being put into the ergonomics of the language and they’re doing what they can to hide and abstract away details without sacrificing speed. It’s getting better with every release…and it’s looking pretty damn good.</li><li>Rust also understands that concurrent software scales well but also needs to be safe. It’s not enough to introduce concurrency without having a mechanism for correctness. Why? Because the last thing you want is to deploy a program that has undefined behavior in production because you have unsynchronized reads and writes of a Hashtable. This is why you fight the compiler…but in reality it’s doing you a solid.</li><li>Lastly, Rust’s target audience is wide yet focused. The Rust implementors know that it can be used for systems-programming, embedded programming, networked applications, command-line applications, games, etc. Why? Because they are trying to appeal to the lowest common denominator…I’m sure if Rust powers the micro-controller of a rocket one day…it’s going to do just fine for your mobile game.</li></ul><p>In conclusion, I’d like for you to sit down and think about this post. Wouldn’t it be great if you added Rust to your tool-belt? Yes there is a learning curve but in the end you make yourself more marketable and when you learn the odds and ends about this language and really understand why it’s here and what it’s trying to solve you’ll walk away a better engineer then when you came.</p><p>If you enjoyed this article, please leave comments as it motivates me to write more. Also please see my other articles where I talk about the <a href="https://medium.com/@deckarep/paradigms-of-rust-for-the-go-developer-210f67cd6a29">Paradigms of Rust from a Go developer perspective</a> and one where I talk about pulling off proper synchronization in <a href="https://hackernoon.com/dancing-with-go-s-mutexes-92407ae927bf">Go with mutexes</a>.</p><p>Cheers!</p><p>@deckarep</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79d5dc353e46" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Paradigms of Rust for the Go developer]]></title>
            <link>https://medium.com/@deckarep/paradigms-of-rust-for-the-go-developer-210f67cd6a29?source=rss-9fc3bef97981------2</link>
            <guid isPermaLink="false">https://medium.com/p/210f67cd6a29</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[rust]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ralph Caraveo]]></dc:creator>
            <pubDate>Sun, 19 Jun 2016 00:13:35 GMT</pubDate>
            <atom:updated>2016-06-19T00:13:35.036Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><strong>Reader Note</strong>: This article aims to provide some technical insight into the paradigm shifts I’ve been exposed to while researching and learning about the Rust programming language when it comes to concurrency. After spending 3.5 years well-vested in the Go programming language, this is my attempt to share these insights and I encourage you; the reader, to also explore these new insights that Rust provides. At the end of the day if you dismiss Rust as not for you, that’s ok but hopefully you can walk away with new concepts to think about.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*NO8C9y2QNEb5iPjtXISATg.png" /><figcaption>It’s a good time to be a coder with languages like Rust and Go at our disposal</figcaption></figure><p>I am at an advantage over many software engineers working in the industry today. The advantage I have is that I recognize that <em>programming languages are just tools</em>. Tools that sometimes vary widely and sometimes overlap in features. The important part is to realize that some tools are better suited for a job than others. For example, you generally don’t want to write a AAA game title in pure Python. The reasons are obvious, Python does not give you the tight control over memory and speed to justify building the next Call of Duty. <em>This is not to say it can’t be done</em> but well you know…</p><p>What I observe in the industry today are software engineers that are fan-{boys, girls} with their languages. They say stuff like “My language is superior because it has generics” or “There is too much cruft in C++ for me to justify using it.” These fan-{boys, girls} engage in discussions and spend hours trying to convince each other that their language is “the best at everything” while the<em> rest of us are building shit and getting things done</em>. When you subscribe to the best practices and standards of a single language built around a single community, I find that you may be severely limiting your growth potential when it comes to learning new languages and more importantly <strong>new paradigms</strong>.</p><p>Hidden within the various languages that exist today, are <em>a set of paradigms that can completely change the way you are used to thinking</em>. Sometimes these paradigms are so focused and so specific to a language that they are only applicable in that particular language. Other times I find, and this is the great part; that you can <strong>take those paradigms and apply them to the languages you currently utilize</strong>. When that happens, congratulations, you’ve expanded your mind and your skill set and additionally you now have a fresh way of tackling stale old problems.</p><p>Now don’t get me wrong; you’ve banked your career on a couple of languages, you love these languages and you get shit done. There is nothing wrong with having admiration or love for a language that gives you the power to express yourself elegantly. We know that software engineering is both an art and a science. And a big part of the pride we have in software development is the power in knowing that we’ve solved a problem more elegantly, more performant and more robustly than the next gal or guy.</p><p>Additionally, the communities that grow around some languages are just as important as the languages themselves. Having a community to share your thoughts, challenge your ideas, and having the codebase to build on top of each other’s efforts can be paramount to tackling the problems you’re trying to solve.</p><p>Which brings us to the meat of this article. I’ve been spending the last 3.5 years <strong>fully vested in Go</strong> development. The Go Programming Language is a fantastic general purpose language for building performant highly-concurrent systems. I’ve heard it described as the language of “The Cloud” and while there are many languages today that have been utilized to build cloud solutions, Go brings a special kind of flair to the table.</p><blockquote>By the way, the language <strong>is called Go</strong> not Golang. Golang is used as a more selective keyword to help us search the internets for Go related content.</blockquote><p>Let’s talk about why one would want to <strong>utilize Go</strong> for tackling some problems:</p><ul><li>You have the need to integrate with a <strong>body of code written in C</strong></li><li>You need to leverage <strong>concurrency or parallelism</strong> for getting the most of your software</li><li>You want to work in a <strong>type-safe</strong> language</li><li>You want to work in an <strong>imperative language that compiles fast</strong></li><li>You want to work in a <strong>distilled</strong> language that is <em>specifically designed to reduce the cognitive over head</em> required to read and write code</li><li>You want to leverage <strong>event-driven programming</strong> without the usual baggage: callback hell, deferreds, futures, promises, empty promises…or whatever the cool kids are calling them these days.</li></ul><blockquote>The items listed above are no accident. Go was designed specifically with those bullet-points in mind.</blockquote><p>That list is a good list. But it only scratches the surface about Go and why you’d want to use it. There are more facets to this language that I’ll talk about. Go also has the concept of <strong>lightweight threading</strong> and <strong>coroutines</strong> and even borrows on a style of concurrent modeling called the <strong>CSP model</strong> better known as <a href="https://en.wikipedia.org/wiki/Communicating_sequential_processes"><strong>Communicating Sequential Processes</strong></a>. If you haven’t heard of these concepts go read about them, I’ll wait.</p><blockquote>The CSP model brings about its own paradigm shift in how you build and model solutions with concurrency. If you are writing Go code today without understanding the CSP model I highly encourage you to learn about it.</blockquote><p>This is what I’m getting at, if you are one of those persons who has heard of Go but never used it, knocks Go because it doesn’t have generics or simply went through the <a href="https://tour.golang.org/welcome/1">Go Tour</a> for 20 minutes and realized the language sucks because function parameters <em>are backwards</em>…you are only hurting yourself. There are some wicked concepts behind Go that you should definitely take a look at even if the takeaway are just its paradigms. Also, it’s worth noting that Go borrows paradigms from past languages. Most languages do.</p><p>Now let’s <strong>get into some Rust</strong> and perhaps why’d you want to use Rust:</p><ul><li>You need a language that offers <strong>both safety and control</strong></li><li>You need a language that has practically <strong>no runtime overhead </strong>— you can’t afford GC pauses</li><li>You need <strong>high performance</strong> on par with C or C++</li><li>You want a language that <strong>won’t seg-fault in production</strong></li><li>You want a language that is <strong>absolutely void of data-races</strong></li><li>You want to work in a language that is <strong>free from baggage and influence for the last 30 years </strong>(<em>i’m looking at you C++</em>)</li></ul><blockquote>By the way, the language <strong>is called Rust</strong>…not Rustlang. Also, the bullet-points listed above are no accident either. Rust was specifically designed with these targets in mind.</blockquote><p>This list is also a good list. It’s not by any means a complete list and Rust is an entirely different beast than Go. Rust additionally comes with <em>an ownership and lifetime model that allows you to express code in such a way that the compiler can utilize to its advantage</em>. This translates into removing an entire class of problems that typically plague lower-level/unsafe languages. Dangling pointers got you down? Use after free causing headaches? Double frees plaguing your data structure? Rust has your back, no…<strong>the <em>Rust compiler</em> has your back and this is absolutely cutting-edge stuff</strong>.</p><blockquote><strong>Side note:</strong> I was in the break room at my place of work a few days ago. I was talking to some colleagues about how Rust is growing in popularity and on the <a href="http://stackoverflow.com/research/developer-survey-2016#technology-most-loved-dreaded-and-wanted">Most Loved, Dreaded, Wanted list on Stackoverflow.com</a> for 2016. Someone said to me, “Ugh, [I] looked at Rust…it’s too complicated.” This person has some good experience with Go so what did I say back? I said, “Sure, it’s more complicated than Go I do agree, but what you pay for up front in writing Rust code, you get back 10-fold with some runtime guarantees that are extraordinarily impressive.” This reminded me of how I felt when I first read through Rust’s language spec a year and a half ago. Luckily a dear friend encouraged me to take another look.</blockquote><p>So I promised this article would teach you about some paradigms of Rust for the Go developer. Let’s dive in.</p><blockquote><strong><em>Paradigm Shift</em>: Rust doesn’t like data-races</strong></blockquote><p>This excerpt of code below is taken straight from Go’s documentation in regards to the <a href="https://golang.org/doc/articles/race_detector.html">race detector</a>. A tool that is designed to catch data races but <em>only if you have proper tests that </em><strong><em>exercise the concurrency of your code</em></strong>. The race detector <strong>is NOT bullet-proof</strong>. It will not give you false positives — if it found a data-race…it is a data-race. But… there is absolutely no guarantee that it will find all data-races. Furthermore the race detector must <em>evaluate your program while it is running</em>. Additionally, some teams writing production Go code today don’t even bother to wire it up into their continuous-integration system….¯\_(ツ)_/¯</p><p>The code below demonstrates how Go’s maps are <em>not thread-safe by default</em>. I see code written like this all the time. People play splish-splash with goroutines, channels, and shared state and then try to push such code into production and then wonder why it explodes horrifically.</p><pre>func main() {<br>	c := make(chan bool)<br>	m := make(map[string]string)<br>	go func() {<br>		m[&quot;1&quot;] = &quot;a&quot; // First conflicting access.<br>		c &lt;- true<br>	}()<br>	m[&quot;2&quot;] = &quot;b&quot; // Second conflicting access.<br>	&lt;-c<br>	for k, v := range m {<br>		fmt.Println(k, v)<br>	}<br>}</pre><p><a href="https://play.golang.org/p/puYYXUdiQL">Playground Link</a></p><p>A quick run down of the code above goes like this:</p><ul><li>A channel is created and used <strong>solely</strong> for the purpose of <em>ensuring the main goroutine doesn’t exit until the anonymous goroutine finishes</em> running.</li><li>A local <strong>map m</strong> was created and captured (closed over) in the anonymous goroutine where <strong>it is mutated</strong>.</li><li><strong>That same local map</strong> is <strong>also mutated</strong> in the main thread.</li><li><strong>No synchronization is happening</strong> to ensure the map is safely mutated across these two goroutines.</li><li>At the end, the results are printed out to the screen via a for-loop.</li><li>Those results could be completely correct or not. In fact, <em>all bets are off</em> — when you have a data-race the behavior is simply <strong>undefined</strong>.</li><li>Go will <em>happily compile and run the code above</em></li></ul><p>The astute reader will realize the data race above can be fixed via the <em>smallest tweak to the code</em>. Below, we synchronize the mutations by effectively introducing a happens-before case by reading on <strong>channel c</strong> before mutation on the main thread can occur.</p><pre>func main() {<br>   c := make(chan bool)<br>   m := make(map[string]string)<br>   go func() {<br>        m[&quot;1&quot;] = &quot;a&quot; &lt;-- this will always happen first<br>        c &lt;- true<br>   }()<br>   &lt;-c // Reading from the channel here synchronizes mutation<br>   m[&quot;2&quot;] = &quot;b&quot; &lt;-- this will always happen after</pre><pre>   for k, v := range m {<br>       fmt.Println(k, v)<br>   }<br>}</pre><p><a href="https://play.golang.org/p/p5JOXLzzgq">Playground Link</a></p><p>Rust on the other-hand won’t even compile such code. It basically says: “Faaaaaaaack You, I refuse to compile this shit.”, while it holds up two middle fingers to your face in plain site. And believe you me, when you are woken up at 3:00am to troubleshoot an insidious data-race that is super hard to track down this is where you are going to wish your compiler would have also told you to fuck off.</p><p>Let’s look at a close approximation of what the Go code above might look like in Rust:</p><pre>use std::sync::mpsc::channel;<br>use std::collections::HashMap;<br>use std::thread;</pre><pre>fn main() {<br>     let (c_tx, c_rx) = channel();<br>     let mut m = HashMap::new();<br> <br>     thread::spawn(move || {<br>         m.insert(“1”, “a”);<br>         c_tx.send(true).unwrap();<br>     });</pre><pre>     m.insert(“2”, “b”);<br>     c_rx.recv().unwrap();</pre><pre>     for (k, v) in &amp;m {<br>          println!(“{}, {}”, k, v);<br>     }<br>}</pre><p><a href="https://is.gd/8dwiZW">Playground Link</a></p><p>There are some very real differences here worth noting. Yes, at first glance it is a little more complicated. We have to import our thread, channel and HashMap namespaces accordingly. There are additional syntax semantics that are not present in the Go version. The channel construct actually requires us to deal with two halves, the sending side: <strong>c_tx</strong> and the receiving side: <strong>c_rx</strong>. And, we are not using a lightweight threading model (like Go’s code does) because <strong>thread::spawn</strong> will actually map 1:1 to a native OS thread. While the threading model is completely different, the principles we’re dealing with are exactly the same. Multiple threads are sharing access and mutating the same map and <em>bad things will happen</em>.</p><p>But by far <strong>the most profound aspect about the Rust code above is that it will not compile</strong>. This is not because the Rust compiler wants to fight with you. It’s not because Rust sucks. It’s because the folks at Mozilla have worked very hard to ensure that<em> the code above never ever compiles and therefore never slips into production only to cause eventual chaos.</em></p><p>This is a complete paradigm shift from Go’s model that is worth understanding and considering. You mean to tell me that my program won’t even compile if a data-race exists? Yes! Sign me up please, that is a real mental-shift in thinking for building concurrent systems.</p><blockquote><strong>Sidenote</strong>: The code above in Rust form can be synchronized in a similar way just like we did with the Go version. <em>The fix can be that trivial</em>. But, for the sake of this article let’s take a dive into the depths of Rust to see how such a language protects us from this ever happening in the first place.</blockquote><p>So what is the compiler telling us about the code above when it fails to compile? It actually vomits the error messaging below:</p><pre>&lt;anon&gt;:14:5: 14:6 error: <strong>use of moved value: `m`</strong> [<a href="https://doc.rust-lang.org/error-index.html#E0382">E0382</a>]<br>&lt;anon&gt;:14     m.insert(&quot;2&quot;, &quot;b&quot;);<br>              ^<br>&lt;anon&gt;:14:5: 14:6 help: <strong>see the </strong><a href="https://doc.rust-lang.org/error-index.html#E0382"><strong>detailed explanation for E0382</strong></a><br>&lt;anon&gt;:9:19: 12:6 note: <strong>`m` moved into closure environment here because it has type `std::collections::hash::map::HashMap&lt;&amp;&#39;static str, &amp;&#39;static str&gt;`, which is non-copyable</strong><br>&lt;anon&gt;: 9     thread::spawn(move || {<br>&lt;anon&gt;:10         m.insert(&quot;1&quot;, &quot;a&quot;);<br>&lt;anon&gt;:11         c_tx.send(true).unwrap();<br>&lt;anon&gt;:12     });<br>&lt;anon&gt;:9:19: 12:6 help: <strong>perhaps you meant to use `clone()`?</strong><br>&lt;anon&gt;:17:20: 17:21 error: <strong>use of moved value: `m`</strong> [<a href="https://doc.rust-lang.org/error-index.html#E0382">E0382</a>]<br>&lt;anon&gt;:17     for (k, v) in &amp;m {<br>                             ^<br>&lt;anon&gt;:17:20: 17:21 help: <strong>see the </strong><a href="https://doc.rust-lang.org/error-index.html#E0382"><strong>detailed explanation for E0382</strong></a><br>&lt;anon&gt;:9:19: 12:6 note: <strong>`m` moved into closure environment here because it has type `std::collections::hash::map::HashMap&lt;&amp;&#39;static str, &amp;&#39;static str&gt;`, which is non-copyable</strong><br>&lt;anon&gt;: 9     thread::spawn(move || {<br>&lt;anon&gt;:10         m.insert(&quot;1&quot;, &quot;a&quot;);<br>&lt;anon&gt;:11         c_tx.send(true).unwrap();<br>&lt;anon&gt;:12     });<br>&lt;anon&gt;:9:19: 12:6 help: <strong>perhaps you meant to use `clone()`?</strong><br>error: <strong>aborting due to 2 previous errors</strong></pre><p>OMG…wtf that is a lot to swallow. Yes, it’s more complicated and I’m going to gloss over some details but over time you will learn to grok such messages. In fact, the compiler messages are <em>practically poetic</em>, they tell a very vivid story about why your code does not compile due to the violations of Rust’s ownership and lifetime model. Rust tells us that <em>ownership of </em><strong><em>map m</em></strong><em> has been transferred to the spawned thread. </em>Therefore <strong>map m</strong> can no longer be used on line 14 otherwise that would have introduced a data-race.</p><p>Let’s discuss ownership the details of ownership and why we’re seeing mentions of <strong>a move</strong> in the error above. Rust enforces the concept of having <strong><em>one owner of data</em></strong>. Think of an owner as being a <em>variable binding to some data</em>. As the owner, you can temporarily lend out access to the data you own via a shared reference. Below outlines some principle axioms in regards to the ownership model.</p><p>Burn these into your head:</p><ul><li>There is only ever <strong>one owner of data</strong></li><li>Ownership can be transferred — <strong>a move</strong></li><li>Owned values can be <strong>borrowed temporarily</strong></li><li>Borrowing <strong>prevents moves from occurring</strong></li></ul><p><strong>Gut-check-refresher</strong>: what is a data-race again?</p><ul><li>2 + threads accessing the same data (reading)</li><li>at least 1 is unsynchronized</li><li>at least 1 is writing</li></ul><p>Rust just did us a solid and caught the perfect storm that could have happened in production. You owe the Rust compiler a beer. Additionally, <em>the data-race was caught at compile-time — not at runtime</em>. The compiler and language semantics working together, remove all the guess work about where data-races may be hiding and <em>you didn’t have to write a single concurrent test to expose it</em>.</p><blockquote><strong>Loaded question</strong>: so is Rust’s supremely verbose and almost story-like error messaging really more complicated or is trying to pin-point data-races in production due to an app exhibiting undefined behavior more complicated?</blockquote><blockquote><strong><em>Paradigm shift</em>: Shared memory in Rust is opt-in</strong></blockquote><p>Let’s now consider how Rust allows us to go about fixing the problems above by first addressing concerns over shared memory. Again, this is Rust’s ownership and lifetime model at work enforcing some very specific rules so that your code never does <em>bad things in production</em>. Let’s examine what opting into shared memory looks like for Rust with a slightly different example.</p><pre>use std::sync::mpsc::channel;<br>use std::thread;<br>use std::collections::HashMap;<br>use std::sync::Arc;</pre><pre>fn main() {<br>    let (tx, rx) = channel();<br>    let mut m = HashMap::new();<br>    <br>    m.insert(“a”, “1”);<br>    m.insert(“b”, “2”);<br> <br>    let arc = Arc::new(m); // Tells Rust we&#39;re sharing state</pre><pre>    for _ in 0..8 {<br>        let tx = tx.clone();<br>        let arc = arc.clone();<br>        thread::spawn(move || {<br>            let msg = format!(“Accessed {:?}”, arc);<br>            tx.send(msg).unwrap();<br>        });<br>    }</pre><pre>    drop(tx); // Effectively closes the channel</pre><pre>    for data in rx {<br>        println!(“{:?}”, data);<br>    }<br>}</pre><p><a href="https://is.gd/tIVIZj">Playground Link</a></p><p>Here is a description of the code above:</p><ul><li>The code above doesn’t suffer from any race-conditions currently. That is because a <strong>happens-before</strong> case occurs where all mutations to <strong>map m</strong> happen in the main thread <em>before the threading logic is ever run</em>.</li><li>We then wrap our <strong>map m</strong> with an <a href="https://doc.rust-lang.org/std/sync/struct.Arc.html"><strong>Atomic Reference Count (Arc)</strong></a><strong> </strong>wrapper. This is the <em>key principle that allows us to opt-in to sharing memory therefore satisfying the Rust compiler</em>.</li><li>Additionally, although we are sharing access to the <strong>map m</strong> in each thread, <em>no mutation is occurring in the threaded part of the code</em>.</li><li>You’ll also notice <em>two clone operations occur</em>. This is necessary because each thread will act as a producer to the single channel instance. Also, the Arc clone allows each thread to have the read-only handle to the <strong>map m</strong> bumping the reference count accordingly.</li><li>Also worth mentioning is that because we’re using an <a href="https://doc.rust-lang.org/std/sync/struct.Arc.html"><strong>Atomic Reference Counted</strong></a> wrapper, Rust will ensure that as each thread goes <em>out of scope</em>, the reference count will be decremented and proper cleanup can occur when the last thread no longer needs access to the map.</li><li>Then in the threaded portion of our code, we actually are able to create a string from the contents of the <strong>map m</strong> and send accordingly through a channel. More importantly, <em>the compiler knows that we are doing this </em><strong><em>safely across all threads because no mutation is occurring in the threaded code and because our Arc wrapper is ensuring that a thread will never see a dangling pointer.</em></strong></li></ul><p>Lastly, we indicate we’re done with the channel via the call to <strong>drop</strong> and loop over the receiving side of the channel printing the results. <strong>drop</strong> is the explicit method provided by the <a href="https://doc.rust-lang.org/std/ops/trait.Drop.html">Drop trait</a> and will be called when it’s lifetime expires too. A call to it will “free” the resource before the program ends.</p><p>Now that we’ve covered how you can handle sharing memory in Rust using an <strong>Arc </strong>wrapper what of the case where we need to <em>share memory when mutation occurs</em>. Let’s go back to our original Rust code above that was modeled after the Go version and tweak it such that it compiles.</p><pre>use std::sync::mpsc::channel;<br>use std::collections::HashMap;<br>use std::thread;<br>use std::sync::{Arc, Mutex};</pre><pre>fn main() {<br>   let (c_tx, c_rx) = channel();<br>   let m = HashMap::new();<br> <br>   // Wrap m with a Mutex, wrap the mutex with Arc<br>   let arc = Arc::new(Mutex::new(m));</pre><pre>   let t_arc = arc.clone();<br>   thread::spawn(move || {<br>       let mut z = t_arc.lock().unwrap();<br>       z.insert(“1”, “a”);<br>       c_tx.send(true).unwrap();<br>   });</pre><pre>   // Extra scope is needed to avoid the deadlock<br>   {<br>       let mut x = arc.lock().unwrap();<br>       x.insert(“2”, “b”);<br>   }<br> <br>   c_rx.recv().unwrap();</pre><pre>   for (k, v) in m.iter() {<br>       println!(“{}, {}”, k, v);<br>   }<br>}</pre><p><a href="https://is.gd/0lxTqs">Playground Link</a></p><p>Let’s break down the code above:</p><ul><li>Notice that in this case we are sharing memory with <strong>map m</strong> in the main thread as well as the spawned thread therefore we reach for our <strong>Arc</strong> <strong>wrapper</strong> to <strong>opt-in</strong> to Rust’s requirements around shared memory.</li><li>Additionally, not only are we sharing memory but mutation is now occurring within the spawned thread as well as the main thread and this logic is now <strong>synchronized </strong>due to proper locking of a shared resource now occuring.</li><li>Rust’s lock facility handles unlocking when it falls out of scope automatically so we have to take an extra step to limit the scope of the main threads mutation to prevent a <strong>deadlock</strong>.</li><li>Now the code above is almost complete however still does not compile due to our next paradigm that follows.</li></ul><blockquote><strong>Paradigm shift: Lock data not code</strong></blockquote><p>Let’s diverge for a minute. Both Go and Rust have mutexes offering the ability to protect the integrity of shared state. A key difference with Rust is that in Rust you lock data directly not code. At first this difference seems almost mundane but reveals a powerful concept.</p><p>Suppose I write some crafty code in Go that is very delicate and<strong> I am super careful, super disciplined</strong> to ensure all proper locking happens around a map. But then, someone decides to come along next week and add some contributions to this code. They happen to stumble upon the same map that contains some useful data they need. They then reference this map and loop over it getting at the data as needed. Everything is wonderful except for one very, very important thing. <em>They failed to take a read-lock on the map</em>. The implications of this are huge. These means they aren’t being good <em>citizens-of-synchronization</em> and are now at risk of reading dirty data that has not been synchronized properly.</p><p>In Rust this situation simply cannot occur. Thanks again to Rust’s ownership model since the <strong>map m</strong> has now <em>moved</em> into the Mutex guard, the Mutex is now fully responsible for that data and will only allow access to it when you take a lock().</p><p>The above code will not compile due to the following error:</p><pre>&lt;anon&gt;:33:19: 33:20 error: <strong>use of moved value: `m`</strong> [<a href="https://doc.rust-lang.org/error-index.html#E0382">E0382</a>]<br>&lt;anon&gt;:33     for (k, v) in m.iter() {<br>                            ^<br>&lt;anon&gt;:33:19: 33:20 help: <strong>see the </strong><a href="https://doc.rust-lang.org/error-index.html#E0382"><strong>detailed explanation for E0382</strong></a><br>&lt;anon&gt;:11:35: 11:36 note: <strong>`m` moved here because it has type `std::collections::hash::map::HashMap&lt;&amp;&#39;static str, &amp;&#39;static str&gt;`, which is non-copyable</strong><br>&lt;anon&gt;:11     let arc = Arc::new(Mutex::new(m));<br>                                            ^<br>error: <strong>aborting due to previous error</strong></pre><p>The fix is rather easy, since <em>ownership moved into the Mutex</em> we just need to make sure that we kindly go through the mutex for the last step in the code. Therefore the last few lines of code change from this:</p><pre>for (k, v) in m.iter() {<br>    println!(“{}, {}”, k, v);<br>}</pre><p>To now this:</p><pre>let a = arc.lock().unwrap();<br>for (k, v) in a.iter() {<br>    println!(“{}, {}”, k, v);<br>}</pre><p>Alas, for completion we have now finally arrived at this code below which compiles just fine.</p><pre>use std::sync::mpsc::channel;<br>use std::collections::HashMap;<br>use std::thread;<br>use std::sync::{Arc, Mutex};</pre><pre>fn main() {<br>   let (c_tx, c_rx) = channel();<br>   let m = HashMap::new();</pre><pre>   // Wrap m with a Mutex, wrap the mutex with Arc<br>   let arc = Arc::new(Mutex::new(m));</pre><pre>   let t_arc = arc.clone();<br>       thread::spawn(move || {<br>       let mut z = t_arc.lock().unwrap();<br>       z.insert(“1”, “a”);<br>       c_tx.send(true).unwrap();<br>   });</pre><pre>   // Extra scope is needed to avoid the deadlock<br>   {<br>       let mut x = arc.lock().unwrap();<br>       x.insert(“2”, “b”);<br>   }</pre><pre>   c_rx.recv().unwrap();</pre><pre>   let a = arc.lock().unwrap();<br>   for (k, v) in a.iter() {<br>       println!(“{}, {}”, k, v);<br>   }<br>}</pre><p><a href="https://is.gd/F2n4Cr">Playground Link</a></p><p>If you are still with me, we covered a lot of material to consider. These three paradigms really offer an alternative way of thinking:</p><ul><li>Rust goes a long way to <em>helping you avoid data-races</em></li><li>Rust <em>requires you to opt-in to shared memory</em> which removes many problems associated with sharing state</li><li>Rust <em>locks data and not code</em>, leveraging the ownership model to ensure proper synchronization is occurring at all times</li></ul><p>This now concludes this blog post on the <strong>Paradigms of Rust for the Go developer</strong>. I wanted to be specifically clear and call out a few things. The examples in this essay are a bit contrived but are simple and useful to consider for the purpose of illustrating the mechanics in play.</p><p>Also, this post was not meant in any way to bash one language over another. In fact, the things I learned in my research with Rust I think will ultimately make me a better software engineer no matter what language I use. Go is where it’s at for me today due to the nature of the systems that I build for a living. The lightweight-threading model that Go brings affords me a significant advantage over a language like Rust currently. Conversely, I’m sure there will be a time to reach for a language like Rust. When that time comes, I’m sure I’ll have a blast working in such a powerful lower-level language that avoids the many pitfalls that typically plague such languages.</p><p>One more thing! If you enjoyed this article, I thrive on feedback. Please share it and follow me on <a href="http://twitter.com/deckarep">Twitter</a>. Also, if this article contains subject matter that excites you, come join me at <a href="https://twitter.com/sendgrid">SendGrid</a> where we get to play with distributed systems all day. (There will be some real work involved too.)</p><p>And if you enjoyed this article see my post titled: <a href="https://medium.com/@deckarep/dancing-with-go-s-mutexes-92407ae927bf#.cl8o9npcq">Dancing with Go’s Mutexes</a></p><p>Happy coding!</p><p>@deckarep</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=210f67cd6a29" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dancing with Go’s Mutexes]]></title>
            <link>https://medium.com/hackernoon/dancing-with-go-s-mutexes-92407ae927bf?source=rss-9fc3bef97981------2</link>
            <guid isPermaLink="false">https://medium.com/p/92407ae927bf</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ralph Caraveo]]></dc:creator>
            <pubDate>Thu, 26 Nov 2015 03:01:41 GMT</pubDate>
            <atom:updated>2017-07-11T23:08:29.606Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Reader-level: </em><strong><em>Intermediate </em></strong><em>— this article assumes you have some basic familiarity with Go and its concurrency model and are at least a little familiar with data synchronization in the form of locking and channel communication.</em></p><p><strong>Reader Note</strong>: A dear friend of mine has inspired this post. As I’ve helped him troubleshoot some data races and have tried my best to give him some decent advice around the art of data synchronization I realized that this advice could benefit others. Should you find yourself inheriting a code-base where certain design decisions have already been made or if you just want to understand Go’s more traditional synchronization primitives than this article might be fore you.</p><p>When I first started working with the Go programming language I immediately bought into Go’s slogan of “<a href="https://blog.golang.org/share-memory-by-communicating">Don’t communicate by sharing memory; share memory by communicating.</a>” For me, this meant writing all concurrent-based code the “proper” way, by always-always using channels. My thought being that if you leverage channels, you are sure to avoid the pitfalls of contention, locking, deadlocks, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*DBAjKWWqh5B05A9qAYii2g.png" /></figure><p>As I progressed with Go, learning to write idiomatic Go and learning about best-practices; I would stumble upon fairly large code-bases, where quite often you would find people using Go’s <strong>sync/mutex</strong> primitive, <strong>sync/atomic</strong> as well as a few other “lower-level” and perhaps “old-school” synchronization primitives. My first thought was, well they’re doing it wrong and they clearly haven’t watched any of Rob Pike’s talks regarding the merits of channel-based concurrency where he often references the design influence from <a href="https://en.wikipedia.org/wiki/Communicating_sequential_processes">Communicating Sequential Processes</a> by Tony Hoare.</p><p>The reality was harsh. The Go community recites the slogan above over and over but <strong>peeking into many open source projects, mutexes are abound and plenty</strong>. I struggled with this conundrum for awhile but ultimately saw the light as it was time to get my hands dirty and push channels aside for a change. Now let’s fast-forward to 2015 where I’ve now been writing Go for around 2.5 years, and since have had a an epiphany or two in regards to the more traditional based synchronization approaches such as mutex-based locking. Go ahead, ask me again now in 2015? Hey @deckarep, do you still only write concurrent applications using channels only? Today I answer no, and here’s why.</p><p>First, let’s not forget the importance of being pragmatic. When it comes to protecting shared state with either traditional locking or channel based synchronization; let’s start with the following question: “So which approach should you use”? And it turns out there is a <a href="https://github.com/golang/go/wiki/MutexOrChannel">nice little write up that summarizes the answer nicely</a>:</p><blockquote>Use whichever is most expressive and/or most simple.</blockquote><blockquote>A common Go newbie mistake is to over-use channels and goroutines just because it’s possible, and/or because it’s fun. Don’t be afraid to use a <a href="http://golang.org/pkg/sync/#Mutex">sync.Mutex</a> if that fits your problem best. Go is pragmatic in letting you use the tools that solve your problem best and not forcing you into one style of code.</blockquote><p>Please note the keywords in that example: <strong>expressive, simple, over-use, afraid, pragmatic. </strong>I can admit a few things here: I was afraid when I first picked up Go. I was a newcomer to the language, and I needed to spend time with the language before drawing conclusions so quickly. You will draw your own conclusions as well in reference to the article above, and as we dig into some best practices using mutex-based locking and what to watch out for. The article referenced above additionally has some nice guidelines around mutex vs channels.</p><blockquote>When to use Channels: passing ownership of data, distributing units of work and communicating async results</blockquote><blockquote>When to use Mutexes: caches, state</blockquote><p>Ultimately every application is different and it may take some experimentation and false starts. For me, I follow the guidelines above but let me elaborate on them. When you need to protect access to a rather simple data structure such as a slice, or a map, or even something custom built, and if the interface to the said data structure is straightforward, start with a mutex. Additionally, it always helps to encapsulate the dirty details of the locking within your API. End-users of your data structure need not concern themselves with how your structure does its internal synchronization.</p><p>If your mutex-based synchronization starts becoming unwieldy and <strong>you are playing the mutex dance</strong>, it’s time to move to a different strategy. Again, recognize that mutexes are useful and straightforward for <strong>simple scenarios</strong> to protect minimally shared state. Use them for what they are <strong>but respect them and don’t let them get out of control</strong>. Take back control of your application’s logic, and if you are fighting with mutexes then please consider re-thinking your design. Perhaps moving to channels would better suit your application logic, or even better, <strong>don’t share state period</strong>.</p><blockquote>Threading isn’t hard — locking is hard.</blockquote><p>Understand I am not advocating to use mutexes over channels. I am simply saying become familiar with both methods of synchronization, and should you find that your channel-based solution seems to be overly complicated, well you have another option. The topics in this article are here to help you write better, more maintainable and robust code. We as engineers <strong>have to be conscientious of how we deal with shared state and avoid data-races in multi-threaded applications</strong>. Go makes it incredibly easy to produce high performing concurrent and/or parallel applications but the pitfalls are there, and care must be taken to build a correct application. Let’s get into the details then:</p><p><strong>Item 1</strong>: When declaring a struct where the mutex must protect access to one or more fields, place the mutex above the fields that it will protect as a best practice. <a href="https://github.com/golang/go/blob/f78a4c84ac8ed44aaf331989aa32e40081fd8f13/misc/cgo/test/cthread.go#L16">Here is an example of this idiom within Go’s own source code</a>. Keep in mind this is purely convention and does not affect your application’s logic.</p><pre>var sum struct {<br>    sync.Mutex     // &lt;-- this mutex protects<br>    i int          // &lt;-- this integer underneath<br>}</pre><p><strong>Item 2</strong>: Hold a mutex lock only for as long as necessary. Example: If you can avoid it, don’t hold a mutex during an IO-based call. Instead, ensure to only protect your resource for only as long as needed. If you did something like this in a web handler for example, you effectively negate the effects of concurrency by serializing access to the handler.</p><pre>// In the code below assume that `mu` solely exists <br>// to protect access to the cache variable<br>// NOTE: Excuse error-handling for brevity</pre><pre>// Don&#39;t do the following if you can avoid it<br>func doSomething(){<br>    mu.Lock()<br>    item := cache[&quot;myKey&quot;]<br>    http.Get() // Some expensive io call<br>    mu.Unlock()<br>}</pre><pre>// Instead do the following where possible<br>func doSomething(){<br>    mu.Lock()<br>    item := cache[&quot;myKey&quot;]<br>    mu.Unlock()<br>    http.Get() // This can take awhile and it&#39;s okay!<br>}</pre><p><strong>Item 3</strong>: Utilize defer to Unlock your mutex where a given function has multiple locations that it can return. This means less book-keeping for you and can mitigate deadlocks when someone comes long 3 months from now and adds a new case for returning early.</p><pre>func doSomething(){<br>    mu.Lock()<br>    defer mu.Unlock()</pre><pre>    err := ...<br>    if err != nil{<br>        //log error<br>        return  // &lt;-- your unlock will happen here<br>    }</pre><pre>    err = ...<br>    if err != nil{<br>        //log error<br>        return  // &lt;-- or here here<br>    }</pre><pre>    return  // &lt;-- and of course here<br>}</pre><p>However beware of just willy-nilly relying on defers in every case. The following code is a trap that can happen when you think that defers are are cleaned up in block scope rather than function scope.</p><pre>func doSomething(){<br>    for {<br>        mu.Lock()<br>        defer mu.Unlock()<br>         <br>        // some interesting code<br>        // &lt;-- the defer is not executed here as one *may* think<br>     }<br>   // &lt;-- it is executed here when the function exits <br>}</pre><pre>// Therefore the above code will Deadlock!</pre><p>Lastly, consider not using the defer statement at all when you have extremely simple functions that don’t have multiple return paths to squeeze out a little performance. Deferred statements do have a slight overhead cost that is very often negligible. <em>Regard this as a very premature and mostly unnecessary optimization</em>.</p><p><strong>Item 4</strong>: Fine-grained locking can lead to better performance at the cost of more complicated bookkeeping while course-grained locking may be less performant, yet yield much simpler bookkeeping. Again, be pragmatic in your design. If you find yourself playing the “mutex dance” it may be time to either refactor your code or move to channel based synchronization.</p><p><strong>Item 5: </strong>As mentioned earlier in this post, it’s always nice if you can hide or encapsulate the method of synchronization used. Users of your package need not concern themselves with the intricacies of how your shared state is protected.</p><p>In the example below let us consider the case where we provide a get() method call, that will only pull from the cache if there is at least one or more items in the cache. Well, since we need to take a lock to get the item out of the cache and get the caches count as well — <strong>this code will deadlock</strong>.</p><pre>package main</pre><pre>import (<br> “fmt”<br> “sync”<br>)</pre><pre>type DataStore struct {<br> sync.Mutex // ← this mutex protects the cache below<br> cache map[string]string<br>}</pre><pre>func New() *DataStore{<br> return &amp;DataStore{<br>   cache: make(map[string]string),<br> }<br>}</pre><pre>func (ds *DataStore) set(key string, value string) {<br> ds.Lock()<br> defer ds.Unlock()</pre><pre> ds.cache[key] = value<br>}</pre><pre>func (ds *DataStore) get(key string) string {<br> ds.Lock()<br> defer ds.Unlock()</pre><pre> if ds.count() &gt; 0 { &lt;-- count() also takes a lock!<br>  item := ds.cache[key]<br>  return item<br> }<br> return “”<br>}</pre><pre>func (ds *DataStore) count() int {<br> ds.Lock()<br> defer ds.Unlock()<br> return len(ds.cache)<br>}</pre><pre>func main() {<br>/* Running this below will deadlock because the get() method will       take a lock and will call the count() method which will also take a  lock before the set() method unlocks()<br>*/</pre><pre> store := New()<br> store.set(“Go”, “Lang”)<br> result := store.get(“Go”)<br> fmt.Println(result)<br>}</pre><p>A suggested pattern for dealing with the fact that <strong>Go’s locks are not re-entrant</strong> is as follows:</p><pre>package main</pre><pre>import (<br> “fmt”<br> “sync”<br>)</pre><pre>type DataStore struct {<br> sync.Mutex // ← this mutex protects the cache below<br> cache map[string]string<br>}</pre><pre>func New() *DataStore {<br> return &amp;DataStore{<br> cache: make(map[string]string),<br> }<br>}</pre><pre>func (ds *DataStore) set(key string, value string) {<br> ds.cache[key] = value<br>}</pre><pre>func (ds *DataStore) get(key string) string {<br> if ds.count() &gt; 0 {<br> item := ds.cache[key]<br> return item<br> }<br> return “”<br>}</pre><pre>func (ds *DataStore) count() int {<br> return len(ds.cache)<br>}</pre><pre>func (ds *DataStore) Set(key string, value string) {<br> ds.Lock()<br> defer ds.Unlock()</pre><pre> ds.set(key, value)<br>}</pre><pre>func (ds *DataStore) Get(key string) string {<br> ds.Lock()<br> defer ds.Unlock()</pre><pre>return ds.get(key)<br>}</pre><pre>func (ds *DataStore) Count() int {<br> ds.Lock()<br> defer ds.Unlock()<br> return ds.count()<br>}</pre><pre>func main() {<br> store := New()<br> store.Set(“Go”, “Lang”)<br> result := store.Get(“Go”)<br> fmt.Println(result)<br>}</pre><p>Notice in the above code that there is a matching exported method for each non-exported method. The exported methods that operate at the public API level will take care of locking and unlocking. They then forward to their respective non-exported methods which do not take any locks at all. This means that all exported invocations of your code will only take a lock once to avoid the re-entrant issue.</p><p><strong>Item 6:</strong> In all the examples above we utilized the basic <strong>sync.Mutex </strong>lock which can simply: Lock() and Unlock() only. The sync.Mutex lock provides the same mutual exclusion guarantee, no matter if the goroutine is reading or writing data. There exists as well the <strong>sync.RWMutex</strong> which offers a little more control with the semantics of the locking during read scenarios. When would you want to use the RWMutex over the standard Mutex?</p><blockquote>Answer: Use the RWMutex when you can absolutely guarantee that your code within your critical section does not mutate shared state.</blockquote><pre>// I can safely use a RLock() for count, it does not mutate<br>func count(){<br>  rw.RLock() // &lt;-- notice the R in RLock (read-lock)<br>  defer rw.RUnlock()  &lt;-- notice the R in RUnlock()</pre><pre>  return len(sharedState)<br>}</pre><pre>// I must use Lock() for set, it mutates the sharedState<br>func set(key string, value string){<br>  rw.Lock() // &lt;-- notice we take a &#39;regular&#39; Lock (write-lock)<br>  defer rw.Unlock() // &lt;-- notice we Unlock() has no R in it</pre><pre>  sharedState[key] = value  // &lt;-- mutates the sharedState<br>}</pre><p>In the above code, we can assume that the `sharedState` variable is some type of object — it could be a map perhaps where we can query it’s length. Since the function`count()` above respects the rule that no mutation is happening on the `sharedState` variable, this means it is safe for an arbitrary number of readers (goroutines) to call this method concurrently. In certain scenarios this could reduce the number of goroutines in a blocking state, and potentially warrant a performance gain in a read-heavy scenario. But remember, when you have code that mutates shared state as in the `set()` you must not use a rw.RLock() command but rather the rw.Lock() command.</p><p><strong>Item 7: </strong>Get to know <a href="https://golang.org/doc/articles/race_detector.html">Go’s Bad-Ass and built-in Race Detector</a>. The race-detector has found scores of data races even within Go’s standard library. This is why the race detector is there and why there are quite a few talks and articles that will explain this tool better than I can.</p><ul><li>If you aren’t yet running unit/integration tests under the race detector as part of your continuous build/delivery system, set it up now.</li><li>If you don’t have good tests that exercise the concurrency of your application, the race detector won’t do you any good.</li><li>Don’t run it in production unless you really need to, it will cost you a performance penalty</li><li>If the race detector found a data race, it’s a real data race.</li><li>Race conditions can still show up channel-based synchronization if you aren’t careful.</li><li>All the locking in the world won’t save you if a goroutine somehow reads or writes shared data that is not within a critical section.</li><li>If the Go team can write data races unknowingly so can you.</li></ul><p>In summary I hope this article offers some solid advice around dealing with Go’s mutexes. Please play with Go’s lower-level synchronization primitives, make your mistakes, respect and understand the tools. Above all, be pragmatic in your development and use the right tool for the job. Don’t be scared like I was originally. If I always listened to every negative thing said about multi-threaded programming and locking I wouldn’t be in this business today writing kick-ass distributed systems while getting to use a kick-ass language like Go.</p><p>Note: I love feedback, if you found this useful please ping me, tweet it or give me constructive feedback.</p><p>Cheers and Happy Coding!</p><p>@deckarep</p><figure><a href="http://bit.ly/HackernoonFB"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0hqOaABQ7XGPT-OYNgiUBg.png" /></a></figure><figure><a href="https://goo.gl/k7XYbx"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vgw1jkA6hgnvwzTsfMlnpg.png" /></a></figure><figure><a href="https://goo.gl/4ofytp"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gKBpq1ruUi0FVK2UM_I4tQ.png" /></a></figure><blockquote><a href="http://bit.ly/Hackernoon">Hacker Noon</a> is how hackers start their afternoons. We’re a part of the <a href="http://bit.ly/atAMIatAMI">@AMI</a> family. We are now <a href="http://bit.ly/hackernoonsubmission">accepting submissions</a> and happy to <a href="mailto:partners@amipublications.com">discuss advertising &amp; sponsorship</a> opportunities.</blockquote><blockquote>If you enjoyed this story, we recommend reading our <a href="http://bit.ly/hackernoonlatestt">latest tech stories</a> and <a href="https://hackernoon.com/trending">trending tech stories</a>. Until next time, don’t take the realities of the world for granted!</blockquote><figure><a href="https://goo.gl/Ahtev1"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35tCjoPcvq6LbB3I6Wegqw.jpeg" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92407ae927bf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackernoon/dancing-with-go-s-mutexes-92407ae927bf">Dancing with Go’s Mutexes</a> was originally published in <a href="https://medium.com/hackernoon">HackerNoon.com</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>