What are some of the practical examples of concurrency

Elixir’s main selling point is concurrency,what are some of the practical examples where concurrency wins?

Thank you !!

1 Like

Wins over what? Doing things sequentially? If so, do you really need examples?

But the strengths of elixir is not only concurrency, it’s fault tolerance as well, which comes because of the wonderful OTP framework.

3 Likes

So concurrency is doing things at the same time…so elixir can do different things at the same time via OTP?

Yes, concurrency is doing different things at the same time.

OTP is built on top of that and some BEAM-built-ins that let you easily monitor other (BEAM) processes. Basically OTP abstracts away the main building blocks of concurrency, the processes. It adds supervision of those processes (and even other supervisors [which are itself processes]) on top. And this is what makes it so fault tollerant.

If there were no concurrency, we were not able to do this kind of supervision.

But still, I do not really get your original question, what do you want to know what were “concurrency winning against”?

3 Likes

You can have a look at Task when You want to do multiple things at the same time. A common use case example is web scrapping.

2 Likes

As a noobie …im overwhelmed about how every programming want to have concurrent features…be it java (java8), scala ,ruby …i mean even earlier the code used to work before people started giving concurrency a serious thought …right ? …so i wanted to know what problems of today can concurrency solve ?

Thank you ! You pattern-matching, process building atoms !!! :smile: :stuck_out_tongue:

Two tasks

  • A (10 seconds)
  • B (15 seconds)

If A has to complete before B can begin then only sequential processing is possible (single-tasking)
AAAAAAAAAABBBBBBBBBBBBBBB
Total: 25 secs

If A and B are independent then concurrent processing is possible (multi-tasking)
ABABABABABABABABABABBBBBB
Total: 25 secs

Sequential A B on 2 cores (parallel)

  1. AAAAAAAAAABBBBBBBBBBBBBBB 25 secs
  2. ???
    Total: 25 secs

Concurrent A B on 2 cores (parallel)

  1. AAAAAAAAAA
  2. BBBBBBBBBBBBBBB
    Total: 15 secs

Concurrency in Elixir:

  • The unit of concurrent processing is a process created with Kernel.spawn/3 and the like.
  • Within a single process all processing is sequential.
  • A process can Kernel.send/2 a message to another process.
  • A process can Kernel.receive/1 a message from another process.
  • Messages between processes are used to coordinate concurrent activities, essentially implementing a protocol (the general kind, not talking about defprotocol).

Also see: Programming Erlang 2e, Sample Chapter 1: 1.2 Benefits of Concurrency (page 8)

14 Likes

Great stuff!