15
elixir for a concurrent world

PromptWorks Talk Tuesdays: Ray Zane 9/13/16 "Elixir Processes"

Embed Size (px)

Citation preview

elixir for a concurrent world

concurrencywhen two or more tasks can start, run, and complete inoverlapping time periods.

os processes

Pros

They have separate sections of memory.No side effects: If VIM crashes, Chrome is unaffected.

Cons

Unfortunately, expensive...

os threads

Pros

Lighter than OS processesThey do not block each otherThey run on separate CPUs, so they are truly parallel

Cons

Still pretty heavy weightShared memory

green threadsThreads that are scheduled by a virtual machine, rather than theOS.

Pros

They don't take up a lot of memory.

Cons

Green thread implementations normally cannot assign work tomultiple processors.A blocking I/O operation can block all green threads.

so how heavy is "heavy"?A POSIX thread has a configurable stack size. On mymachine, the default is 8Mb.JVM, .NET, RubyVM, and CPython all use 1Mb by default.Therefore, memory usage will add up quickly.

[Erlang] is a concurrent language – by that I mean thatthreads are part of the programming language, they do notbelong to the operating system. That's really what's wrong withprogramming languages like Java and C++. It's threads aren't inthe programming language, threads are something in theoperating system – and they inherit all the problems that theyhave in the operating system. One of the problems is granularityof the memory management system. The memory managementin the operating system protects whole pages of memory, so thesmallest size that a thread can be is the smallest size of a page.That's actually too big. -- Joe Armstrong

goroutinesInitial stack size is 2kb (as of 1.4), but then is dynamicallyallocatedgoroutines are green threads that are multiplexed as neededonto native threads, so there isn't a 1:1 mapping.

erlang processes

so...Not an OS process, but still isolated.No side effects. If a process crashes, other processes are notaffected.Managed by the VM, rather than the OS.Immutable data structures sidestep shared mutable state.Processes communicate through message passing.Fault-tolerant

go on...Lightweight memory footprint.Dynamically allocated stack.All of this applies to Elixir as well.

{_, bits} = :erlang.process_info( spawn(fn -> nil end), :memory )

bits / 8 # 340.0

who cares?we are now able to easily push our systems to over 2 million tcpconnections! -- WhatsApp (https://blog.whatsapp.com/196/1-million-is-so-2011)

so how do processes talk?

Sources:

https://www.youtube.com/watch?v=hlmMi0pWzJEhttp://blog.nindalf.com/how-goroutines-work/http://www1.erlang.org/doc/efficiency_guide/processes.htmlhttps://speakerdeck.com/7grok/processes-threads-and-the-death-of-moores-lawhttp://learnyousomeerlang.com/https://www.infoq.com/presentations/erlang-software-for-a-concurrent-world