CONCURRENCY MODEL
Problem in high-demand system and some
concurrency patterns in the practical aspect.
1
Hieu Phan
Chaos Engineer
git@hieuphq
[email protected]
2
Agenda 1. Why concurrency?
2. Describe the problem
Problem in high-demand system
and some concurrency patterns in
3. Concurrency in Modern languages
the practical aspect.
4. Practices
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
Benefits Increased amount of work Improved throughput, computational
accomplished at a time speed-up
Make Context switching Multiple CPUs for operating multiple
use of processes
Units Probably single Multiple
required
6
OS thread & Green thread
Green thread OS thread
Basic created and scheduled by Virtual created and scheduled by Kernel of
machine without using OS libraries operating system
Platform Platform dependent Platform independent
Dependent
Multiprocessor Run only on one CPU Run on distinct CPUs
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:
- Hard to Test and Debug
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
Both models are based on message passing
CSP is fully synchronous. Actors sending is asynchronous.
A channel writer must block until a channel A message sender will not block whether
reader reads. the reader is ready to pull from the mailbox
Stateful - communicate by channel. Stateless - communicate by message,
scale across several machines, more
decoupled
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
● Do not share any memory
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