Rob Keefer, PhD

@rbkeefer
A Brief Introduction to Concurrent
Programming
pomiet.com @rbkeefer
Goal
Goal of Concurrency is to get more done in less time.
Multiple independent tasks
Repeat same task
Solution: Parallelism
pomiet.com @rbkeefer
Parallelism
pomiet.com @rbkeefer
Direction
Processes
Problems with Concurrency
Traditional Solutions
New Approaches
pomiet.com @rbkeefer
Turn the Crank
pomiet.com @rbkeefer
Processor
pomiet.com @rbkeefer
Processor
pomiet.com @rbkeefer
Processor
pomiet.com @rbkeefer
Processor
pomiet.com @rbkeefer
Take Advantage of the Tech
pomiet.com @rbkeefer
Problems
1. Starting / maintaining processes
2. Communication between processes
• Remote procedure call (RPC)
• Message passing
3. Shared Resources
• Deadlock
• Livelock
• Race conditions
• Starvation
pomiet.com @rbkeefer
Five Volunteers
pomiet.com @rbkeefer
Problems: Deadlock
pomiet.com @rbkeefer
Problems: Livelock
pomiet.com @rbkeefer
Problems: Race Condition
pomiet.com @rbkeefer
Problems: Starvation
pomiet.com @rbkeefer
Approaches
Traditional Object Oriented
Modern Functional
General Philosophy: Failure Prevention
General Philosophy: Embrace Failure
Monitor
Mutual Exclusion (Mutex)
Prioritization Algorithms
Support process death and resurrection
Support atomic functions
Fairness algorithms embedded in VM
pomiet.com @rbkeefer
Monitor
pomiet.com @rbkeefer
Mutex
pomiet.com @rbkeefer
def call(process_name, message, timeout  5000)
ref = Process.monitor(process_name)
try do
send(process_name, {:message, {ref, self()}, message})
rescue
_ -> :error
end
receive do
{:reply, ref, reply} ->
Process.demonitor(ref, [:flush])
reply
{:DOWN, ref, :process, _, reason} ->
{:error, reason}
exit(reason)
after timeout -> exit(:timeout)
end
end
Elixir - Lock Prevention
<- exit on timeout
pomiet.com @rbkeefer
def answer(process_name, message, timeout  5000)
receive do
{:knock, ref, reply} ->
reply # "Who's there?"
{:DOWN, ref, :process, _, reason} ->
{:error, reason}
exit(reason) # No one knocking
after timeout -> exit(:timeout)
end
. . .
def knock(process_name, message, timeout  5000)
receive do
{:reply, ref, reply} ->
reply # Tell who you are
{:DOWN, ref, :process, _, reason} ->
{:error, reason}
exit(reason) # No one home
after timeout -> exit(:timeout)
end
. . .
Elixir - Race Condition
<- exit on timeout
<- exit on timeout
pomiet.com @rbkeefer
Note on Garbage Collection
Credit: Greg Mefford
pomiet.com @rbkeefer
A Few Facts
Traditional Object Oriented
Modern Functional
Bring data and behaviors together
Simulation of behavior (heuristics)
Overhead: Java threads 512 Kb / thread
Require thread pools / execution management
Shared heap
Hard limits on memory use
Separate data from behaviors
Data manipulation (algorithms)
Overhead: Elixir 512 b / process
Processes easily accessible
Individual private heap
Unbounded memory
pomiet.com @rbkeefer
Scale with Resilience
Resilience: ability of a system to adapt or keep
working when challenges occur
pomiet.com @rbkeefer
Questions to Consider
Think Different:
Fault Tolerance:
What happens if a
dependency starts failing
slowly?
What’s the worse-scenario for
total failure?
Efficiency:
Is it possible to precompute
data?
Are you doing as little work as
possible?
Is load testing part of the
regression test suite?
Operability:
Where are the nonlinearities in
load or failure responses?
What are the capacity needs?
How does the system scale?
pomiet.com @rbkeefer
Continue the Conversation
Rob Keefer, PhD
rob.Keefer@pomiet.com
@rbkeefer

Brief Introduction to Concurrent Programming