Skip to main content
Elixir/OTP for PHP developers
Nacho Martín
My name is Nacho Martín.
I write code at Limenius.
We build tailor-made projects,
and provide consultancy and formation.
We have been using PHP for many years.
But for certain tasks we use Elixir/Erlang & we are very happy with it.
Investing in a new language?
Learning alternatives
Learning alternatives Learning complements
Syntax?
Platform
Overall philosophy
Why was it built
Orientation of the community
(Simplified)
Request
Nginx
WrapperFastCGI PHPFastCGI
Response
WrapperFastCGI PHPFastCGI
Response
WrapperFastCGI PHPFastCGI
Response
WrapperFastCGI PHPFastCGI
Response
WrapperFastCGI PHPFastCGI
Response
Socket
(Simplified)
Request
Nginx
WrapperFastCGI PHPFastCGI
Response
WrapperFastCGI PHPFastCGI
Response
WrapperFastCGI PHPFastCGI
Response
WrapperFastCGI PHPFastCGI
Response
WrapperFastCGI PHPFastCGI
Response
Socket
?
Most of what we are going to say applies to both
Erlang:The Movie
Hello Mike.
Speaking about Elixir means speaking about Erlang
• Similar syntax.
• Battle tested.
• Very stable.
• More low-level feeling in code & community.
Elixir core features
Everything flows from Cheap processes
• Low footprint
• Fast start
• Isolated
• Share nothing
• Use as many as you need
pid = spawn(fn ->
IO.puts "In #{inspect self()} process"
end)
#PID<0.42.0>
pid = spawn(fn ->
IO.puts "In #{inspect self()} process"
end)
#PID<0.43.0>#PID<0.42.0>
Message passing
#PID<0.43.0>#PID<0.42.0>
send pid, {:hello, "world"}
receive do
{:hello, msg} -> msg
{:something, msg} -> "won't match"
end
Monitors
Monitors
pid = spawn(fn ->
:timer.sleep 500
raise(“I’m dying.”)
end)
Monitors
pid = spawn(fn ->
:timer.sleep 500
raise(“I’m dying.”)
end)
Monitors
pid = spawn(fn ->
:timer.sleep 500
raise(“I’m dying.”)
end)
ref = Process.monitor(pid)
Monitors
pid = spawn(fn ->
:timer.sleep 500
raise(“I’m dying.”)
end)
ref = Process.monitor(pid)
Monitors
pid = spawn(fn ->
:timer.sleep 500
raise(“I’m dying.”)
end)
ref = Process.monitor(pid)
receive do
{:DOWN, ^ref, :process, ^pid, msg} ->
IO.puts "Received :DOWN from #{inspect pid}"
end
Monitors
pid = spawn(fn ->
:timer.sleep 500
raise(“I’m dying.”)
end)
ref = Process.monitor(pid)
receive do
{:DOWN, ^ref, :process, ^pid, msg} ->
IO.puts "Received :DOWN from #{inspect pid}"
end
Monitors
pid = spawn(fn ->
:timer.sleep 500
raise(“I’m dying.”)
end)
ref = Process.monitor(pid)
receive do
{:DOWN, ^ref, :process, ^pid, msg} ->
IO.puts "Received :DOWN from #{inspect pid}"
end
Links
Process.flag(:trap_exit, true)
spawn_link(fn ->
raise(“I am dying.”)
end)
Links
Process.flag(:trap_exit, true)
spawn_link(fn ->
raise(“I am dying.”)
end)
Links
Process.flag(:trap_exit, true)
spawn_link(fn ->
raise(“I am dying.”)
end)
receive do
{:EXIT, pid, msg} ->
IO.inspect ":EXIT received from #{inspect pid}"
IO.inspect msg
end
Links
Process.flag(:trap_exit, true)
spawn_link(fn ->
raise(“I am dying.”)
end)
receive do
{:EXIT, pid, msg} ->
IO.inspect ":EXIT received from #{inspect pid}"
IO.inspect msg
end
Links
Process.flag(:trap_exit, true)
spawn_link(fn ->
raise(“I am dying.”)
end)
receive do
{:EXIT, pid, msg} ->
IO.inspect ":EXIT received from #{inspect pid}"
IO.inspect msg
end
😍OPEN TELECOM PLATFORM😍
OTP Supervision Trees
Supervisor
GenServerGenServerSupervisor
GenServerGenServer GenServer
{:ok, pid} = GenServer.start_link(Stack, [:hello])
GenServer
defmodule Stack do
use GenServer
@impl true
def init(stack) do
{:ok, stack}
end
end
Client GenServer
{:ok, pid} = GenServer.start_link(Stack, [:hello])
GenServer
defmodule Stack do
use GenServer
@impl true
def init(stack) do
{:ok, stack}
end
end
GenServer.call(pid, :pop)
#=> :hello
Client GenServer
@impl true
def handle_call(:pop, _from, [head | tail]) do
{:reply, head, tail}
end
{:ok, pid} = GenServer.start_link(Stack, [:hello])
GenServer
defmodule Stack do
use GenServer
@impl true
def init(stack) do
{:ok, stack}
end
end
GenServer.cast(pid, {:push, :world})
#=> :ok
GenServer.call(pid, :pop)
#=> :hello
Client GenServer
@impl true
def handle_call(:pop, _from, [head | tail]) do
{:reply, head, tail}
end
@impl true
def handle_cast({:push, item}, state) do
{:noreply, [item | state]}
end
{:ok, pid} = GenServer.start_link(Stack, [:hello])
GenServer.call(pid, :pop)
#=> :world
GenServer
defmodule Stack do
use GenServer
@impl true
def init(stack) do
{:ok, stack}
end
end
GenServer.cast(pid, {:push, :world})
#=> :ok
GenServer.call(pid, :pop)
#=> :hello
Client GenServer
@impl true
def handle_call(:pop, _from, [head | tail]) do
{:reply, head, tail}
end
@impl true
def handle_cast({:push, item}, state) do
{:noreply, [item | state]}
end
Supervisor
defmodule KV.Supervisor do
use Supervisor
def start_link(opts) do
Supervisor.start_link(__MODULE__, :ok, opts)
end
def init(:ok) do
children = [
Stack,
AnotherChild,
AndAnother
]
Supervisor.init(children, strategy: :one_for_one)
end
end
Supervision strategies
one_for_one
Supervision strategies
one_for_one
Supervision strategies
one_for_one
Supervision strategies
one_for_one
Supervision strategies
one_for_all
Supervision strategies
one_for_all
Supervision strategies
one_for_all
Supervision strategies
one_for_all
Supervision strategies
one_for_all
Supervision strategies
one_for_all
Supervision strategies
rest_for_all
Supervision strategies
rest_for_all
Supervision strategies
rest_for_all
Supervision strategies
rest_for_all
Supervision strategies
rest_for_all
Supervision strategies
rest_for_all
Let it crash vs defensive programming
:ok = do_something_that_can_crash
Not :ok? process crashes
Supervisors take care of regenerating the system
try {
somethingThatCanCrash();
} catch (Exception $e) {
recoverMaybe();
}
Distribution
Node.spawn_link :”bar@computer-name", fn -> Hello.world end
Node foo@computer-name Node bar@computer-name
Useful scenarios
Broadcast messages
PHP
User User’ User’’
Broadcast messages
PHP
User User’ User’’
Have a look at
Notify selected users
PHP
User User’’
Some auth
Presence
Sarah
Daniel
Ellen
3 min ago
Lukas
2 hours ago
PHP
User
DB
General case
PHP
User
DB
General case
PHP
User
DB
User’
General case
Thinking of problems as particular DBs
Focus in the dataFocus in the logic
Think as DB/cacheThink as app/game
Logic vs data
Focus in the dataFocus in the logic
Think as DB/cacheThink as app/game
It is a continuous:
We tend to be somewhere in the middle
Logic vs data
Demo
Things to consider
• Not as easy as other platforms
• Not a massive language
• Releases & deployment difficult (compared with PHP)
To summarize
• Very different platform
• Very good for some problems
• Great source of ideas
• Useful to know even if we don’t end up using it
• And even if we don’t want to adopt these ideas!
To know more
• https://elixir-lang.org/ Especially the guide MIX AND OTP
• Phoenix Framework https://phoenixframework.org/
• Learn You Some Erlang for Great Good https://learnyousomeerlang.com/
Thanks! @nacmartin
nacho@limenius.com