concurrency_model
concurrency_model
1
Hieu Phan
Chaos Engineer
git@hieuphq
[email protected]
2
Agenda 1. Why concurrency?
3
Why concurrency?
Make difference with parallelism
4
Why concurrency?
● Improved throughput.
● Simultaneous and fully symmetric
use of multiple processors for
computation and I/O.
● Improved responsiveness.
● Minimized system resource usage.
● Program structure simplification.
5
Concurrency & Parallelism
Basic Running multiple computations at running multiple computations
the same time simultaneously
6
OS thread & Green thread
7
Describe the problem
Problem in the real project
8
Models for Concurrent Programming
Shared memory: concurrent modules
interact by reading and writing shared
objects in memory.
Message passing: concurrent modules
interact by sending messages to each
other through a communication
channel. Modules send off messages,
and incoming messages to each
module are queued up for handling.
9
Shared memory
Problem:
- Interleaving
- Race condition
10
Message Passing
Problem:
11
Concurrency in Modern languages
Concurrency in common programing language: Javascript,
Go, Elixir, Rust
12
Concurrent Programing Approach
JavaScript: via web workers, in a browser environment, promises, and callbacks.
Go: for system programming, with a concurrent programming model based on
CSP
Elixir: dynamic and functional meta-programming aware language running on
the Erlang VM.
Erlang: uses asynchronous message passing with nothing shared
Rust: for system programming, using message-passing with move semantics,
shared immutable memory, and shared mutable memory
13
Javascript
JavaScript is a single-threaded
language and, at the same time, also
non-blocking, asynchronous, and
concurrent
14
Javascript
Callback function
Promise model
async / await keyword in ES2017
15
Golang
Communicating sequential processes - CSP
1. Each process is built for sequential execution
2. Data is communicated between processes via channels -
No shared state.
3. Scale by adding more of the same.
16
Golang
Go routine
Channel
Patterns:
- fan-out
- fan-in
- publish-subscribe
17
Elixir
Concurrent in Elixir is based on the
Actor Model.
Actors are single-threaded processes
that can send and receive messages
amongst themselves
18
Actor Model vs CSP
CSP in Go Actor Model in Elixir
19
Rust
Ownership and type systems are a
powerful set of tools to help manage
memory safety and concurrency
problems
20
Rust
Ownership and type systems are a
powerful set of tools to help manage
memory safety and concurrency
problems
21
Rust
Channel in standard library
22
Rust
Share state between thread
Mutex<T>
Arc<T>
23
Practices
Concurrency in common programing language: Javascript,
Go, Elixir, Rust
24
Race condition
● Old way: using lock
25
Database scaling
● replica
● sharding
26
Database scaling
27
Conclusion
Do not communicate by sharing
memory; instead, share memory by
communicating.
28
Reference
Resources & Reference links
● https://en.wikipedia.org/wiki/Actor_model
● https://en.wikipedia.org/wiki/Communicating_sequential_proce
sses
● https://brain.d.foundation/Engineering/Concurrency+in+Javasc
ript
● https://www.karanpratapsingh.com/blog/csp-actor-model-conc
urrency
29
Thank You
30
Q&A
31