background preloader

Concurrency & State

Facebook Twitter

Concurrency, Parallelism, and State. And Zombies. | Clojure for the Brave and True. In this chapter you'll learn what concurrency and parallelism are and why they matter. You'll learn about the challenges you'll face when writing parallel programs and about how Clojure's design helps to mitigate them. Finally, you'll learn a big boatload of tools and techniques for writing parallel programs yourself, including: futures, promises, delays, atoms, refs, vars, pmap, and core.reducers. Also, there will be zombies. Onward! 1. Why Parallel Programming Matters Why should you bother with any of this? This is mostly because increasing clock speed requires exponentially more power, making it impractical. Besides this practical consideration, I think you'll also find that parallel programming is actually fun and fascinating with Clojure. 2.

Concurrent and parallel programming involves a lot of messy details at all levels of program execution, from the hardware to the operating system to programming language libraries to the code that you're typing out with your very own fingers. Terminology - How do Clojure futures and promises differ? Channels in Clojure - (map str *thoughts*) Posted by David Jack | Filed under go, clojure I have been kicking the tires on Google's Go. I was especially impressed with its RPC framework example. I wanted to implement a similar framework in Clojure. Let's start with the client interface: (ns rpc-example (import [java.util.concurrent BlockingQueue LinkedBlockingQueue])) (defn rpc [#^BlockingQueue q f & args] "Performs a blocking call using the given queue.

" (let [result (promise)] (.put q {:fn f :args args :result result}) @result)) While Clojure does not currently provide anything like Go's channels, Java's BlockingQueue is close enough1. The handler function takes requests off the queue, processes them, and delivers the result: (defn handler [#^BlockingQueue q] "Repeatedly processes requests on the given queue. " Finally, I define some functions to manage the execution of handlers. (defn serve "Create n handlers for the given queue. " Putting the pieces together: PS.

<a href=" Promise/Deliver use cases. Futures and promises in Clojure (asynchronicity and concurrency, part 2) « goodmike blogs here. A few weeks ago I posted about my initial impressions of CommonJS’s approach to concurrency, in particular the use of promises. Today I add my look at promises and futures in Clojure. I’m aware that I’m kind of writing these blog posts backwards.

I’m learning more as I go, which means before I’m done I will no doubt regret things I have written in these opening posts. Oh well. Clojure offers more than one option for achieving concurrency. In Clojure, as in CommonJS, a promise is also a stand-in for an asynchronously-computed value. A promise is a synchronization construct that can be used to deliver a value from one thread to another.

A future, however, represents a unit of computation that can be executed on another thread. Futures represent asynchronous computations. Sean Devlin, the creator of the excellent screencasts at Full Disclojure, has a screencast covering promises and futures. (def a-promise (promise)) (deliver a-promise :fred) Some general background: Dataflow concurrency Yep.

State

Futures.pdf. Concurrency, Parallelism, and State. And Zombies. | Clojure for the Brave and True. Docs - clojure.core/future. Docs - clojure.core/promise. Finagle. Promise/Deliver use cases. Futures and promises in Clojure (asynchronicity and concurrency, part 2) « goodmike blogs here. Josephwilk/hystrix-event-stream-clj. Netflix/Turbine. Ztellman/aleph. Netty: Home. Netflix/RxJava. Hystrix/hystrix-contrib/hystrix-clj at master · Netflix/Hystrix. Building Clojure services at scale. At SoundCloud I’ve been experimenting over the last year with how we build the services that power a number of heavily loaded areas across our site. All these services have been built in Clojure with bits of Java tacked on the sides. Here are some of my personal thoughts on how to build Clojure services: Netflix or Twitter At some-point when you require a sufficient level of scaling you turn to the open source work of Twitter with Finagle or Netflix with Hystrix/RxJava. Hence with Clojure, Netflix projects are my preference.

Failure, Monitoring & Composition Complexity In a service reliant on other services, databases, caches any other external dependencies its a guarantee at some-point some of those will fail. While we can think about degrading gracefully in the case of failure, ultimately we want to fix wants broken as soon as possible. Your service needs to call other services. Fault Tolerance How should be build fault tolerance into our Clojure services? Single thread pool Pants. 1. 2.