Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrency model: Erlang vs Clojure

We are going to write a concurrent program using Clojure, which is going to extract keywords from a huge amount of incoming mail which will be cross-checked with a database.

One of my teammates has suggested to use Erlang to write this program.

Here I want to note something that I am new to functional programming so I am in a little doubt whether clojure is a good choice for writing this program, or Erlang is more suitable.

like image 274
Quazi Farhan Avatar asked Jun 05 '11 16:06

Quazi Farhan


People also ask

Why is Erlang good for concurrency?

One of the main reasons for using Erlang instead of other functional languages is Erlang's ability to handle concurrency and distributed programming. By concurrency is meant programs that can handle several threads of execution at the same time.

Why use Clojure?

Clojure is a good choice for a wide variety of projects. You can use it from social networking industry to Big Data solutions. Initially, Clojure language was targeted for working with JVM. So, the most popular modern Clojure implementation uses the Java Virtual Machine.

Is Erlang faster than go?

Go is fast and efficient than Erlang in terms of its syntax and is used for many advanced services. For real-time data and applications which are distributed, Erlang is chosen over Go. For fault tolerance, Erlang is preferred over Go.

Is Elixir better than Erlang?

They are both good candidates for building large and distributed systems that have high-availability requirements. However, Elixir fares better than Erlang in this regard. It supports concurrency without any notable degradation of performance.


2 Answers

The two languages and runtimes take different approaches to concurrency:

  • Erlang structures programs as many lightweight processes communicating between one another. In this case, you will probably have a master process sending jobs and data to many workers and more processes to handle the resulting data.

  • Clojure favors a design where several threads share data and state using common data structures. It sounds particularly suitable for cases where many threads access the same data (read-only) and share little mutable state.

You need to analyze your application to determine which model suits you best. This may also depend on the external tools you use -- for example, the ability of the database to handle concurrent requests.

Another practical consideration is that clojure runs on the JVM where many open source libraries are available.

like image 32
nimrodm Avatar answered Oct 16 '22 20:10

nimrodm


Do you really mean concurrent or distributed?

If you mean concurrent (multi-threaded, multi-core etc.), then I'd say Clojure is the natural solution.

  • Clojure's STM model is perfectly designed for multi-core concurrency since it is very efficient at storing and managing shared state between threads. If you want to understand more, well worth looking at this excellent video.
  • Clojure STM allows safe mutation of data by concurrent threads. Erlang sidesteps this problem by making everything immutable, which is fine in itself but doesn't help when you genuinely need shared mutable state. If you want shared mutable state in Erlang, you have to implement it with a set of message interactions which is neither efficient nor convenient (that's the price of a nothing shared model....)
  • You will get inherently better performance with Clojure if you are in a concurrent setting in a large machine, since Clojure doesn't rely on message passing and hence communication between threads can be much more efficient.

If you mean distributed (i.e. many different machines sharing work over a network which are effectively running as isolated processes) then I'd say Erlang is the more natural solution:

  • Erlang's immutable, nothing-shared, message passing style forces you to write code in a way that can be distributed. So idiomatic Erlang automatically can be distributed across multiple machines and run in a distributed, fault-tolerant setting.
  • Erlang is therefore very well optimised for this use case, so would be the natural choice and would certainly be the quickest to get working.
  • Clojure could do it as well, but you will need to do much more work yourself (i.e. you'd either need to implement or choose some form of distributed computing framework) - Clojure does not currently come with such a framework by default.

In the long term, I hope that Clojure develops a distributed computing framework that matches Erlang - then you can have the best of both worlds!

like image 89
mikera Avatar answered Oct 16 '22 20:10

mikera