First title line
Second line here
Subtitle goes here — no fancy
color block kthough too fussy
smartly.
smartlogic.io
All Aboard
the Stateful Train
Going Beyond a Simple
Web App with Elixir
Eric Oestrich
smartlogic.io
Who am I?
Eric Oestrich
Wizard at SmartLogic
Who is
SmartLogic?
We build custom web
and mobile apps.
Based in Baltimore,
we've built over 150
apps since 2005.
● What does a standard application look like?
● Why would you go stateful?
● What does a stateful application look like?
● Is stateful better?
● Let's go stateful!
Agenda
Standard Application
Standard Application
● Stateless HTTP
● Controllers go straight to the database
● Does not leverage OTP
● MVC, Rails-like
Standard Application Examples
● Wordpress
● Hubspot
● Wikipedia
● Shopify
Stateful Application
Stateful Application
● Stateless HTTP
● Websockets/TCP connections
● Heavy use of OTP
● "Background" processes
Stateful Application Examples
● Slack
● Discord
● Twitch
OTP and Processes
● Used to be Open Telecom Platform
● OTP is the standard library of Erlang
● GenServers, supervision trees, :ets, all the fun stuff
● Processes are the base building block of Erlang
concurrency
Supervision Trees & Processes
Why Stateful?
Why Stateful?
● We're on a platform that can handle it
● Local caches
● Speed, less network to the database
● Resilience, craft your processes to withstand anything
● Websites feel “alive”
Did I mention Erlang is great at this?
https://unsplash.com/photos/M5tzZtFCOfs
Let's review some stateful
applications
exventure.org
ExVenture: A MUD Server
What is a MUD?
● Multi-User Dungeon
● Online text adventure
● Connects with telnet
What is a text adventure?
This is an open field west of a white house, with a
boarded front door.
There is a small mailbox here.
A rubber mat saying 'Welcome to Zork!' lies by the
door.
>
MidMUD
The flagship ExVenture instance
midmud.com
Review of ExVenture
● A "living" world
● Everything is always on
● The database is there for startup
● Lots of inter-process communication
Demo
midmud.com
Could this be
stateless?
Not Really
● Poll based?
● Worker that updated the world?
● Gross
grapevine.haus
Grapevine: A MUD Community Site
Grapevine
● Steam for MUDs
● Cross game chat
● Web client
Review of Grapevine
● Close to a standard application
● State involved is websocket and telnet
● Caches
Demo
● Game listings, stats
● Cross game chat
● Web client
Could this be
stateless?
Possibly
● Chat applications can be poll based
● Live caches could hit the database
● Still gross
Is stateful better?
Do I want this?
Depends
Pros
Soft realtime
capabilities
Erlang is built for soft realtime
Website feels "alive"
Websockets can update your website without a refresh
Reduced
Database Queries
Less round trips to the database for faster response times
Cons
Requires heavy
usage of OTP
You must architect out how your application is laid out in
memory, beyond what Phoenix gives you.
Caches are Hard™
There are 2 hard problems in computer science: cache
invalidation, naming things, and off-by-1 errors.
Harder to Deploy
● Warm caches on boot
● Long lived connections will drop
○ How will you deal with this?
● Clustered applications need to handle a rolling restart
Rolling Restart
Here be Dragons
● Mailbox size
● Message passing copies
● Clustering woes
Let’s Go Stateful
GenServers
● The basic building block of OTP
● Handle messages out of the HTTP process
● Background tasks
● Any kind of state you want to hang on to
Supervision Trees
Watch over your GenServers
:ets
● Erlang Term Storage
● Very fast reads/writes
● Concurrent
● Downside: copies memory a lot
http://erlang.org/doc/man/ets.html
Put It All Together
Diagram of
ExVenture
Connecting
to the World
Players
Acting on
the World
Updating a
Skill from
the Admin
Using
a Skill
Grapevine
Chat
Grapevine
Web Telnet
Client
Wrap Up
Elixir gives you the
tools to build a
stateful application
with ease
Ram and CPUs are
cheap, use 'em
LiveView is an easy
way to start stateful
Finally, you might
not need it
GitHub
● oestrich on GitHub
● oestrich/ex_venture
● oestrich/grapevine
Titans of Text
titansoftext.com
Season 3 of Smart Software
podcast.smartlogic.io
Season 3 of Smart Software
podcast.smartlogic.io
Season 3 of Elixir Wizards
podcast.smartlogic.io
First title line
Second line here
Subtitle goes here — no fancy
color block kthough too fussy
smartly.
smartlogic.io
Thanks!
smartlogic.io
First title line
Second line here
Subtitle goes here — no fancy
color block kthough too fussy
smartly.
smartlogic.io
We build custom web
We’ve built 150+ applications since 2005.
Need a custom web/mobile app? We can help.
and mobile applications.
888-544-SMRT / contact@smartlogic.io

All Aboard The Stateful Train