SlideShare a Scribd company logo
You ain't gonna need to
write a GenServer
by Ulisses Almeida
Summary
■ What is GenServer?
■ How can you harm your app by putting
GenServer in the wrong place?
■ Why do people think that they need a
GenServer all the time?
■ Considerations before writing a GenServer
I’m Ulisses Almeida
■ Elixir Developer at The
Coingaming Group
■ @ulissesalmeida
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
Why Tallinn? !
★ Low renting costs
★ Free transportation for
residents
★ E-estonia 💻
★ Safe
★ Low taxes
★ Accessible travels
๏ Cold and Dark ☃
๏ Estonian is the first
language !
๏ Russian is the second
language $
๏ Eastern/nordic culture
is different
Learn Functional Programming with Elixir
■ 20% off with
Elixir_Ukraine_2019
promo code
Who 

We Are
The Coingaming Group are the
crypto gaming pioneers who
operate the world’s leading bitcoin
gaming and betting brands.
https://coingaming.io/
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
Ok, GenServers
A quick recap
https://twitter.com/ulissesalmeida/status/1125455706470088704
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
GenServer
■ Useful for: Client-Server
relationship
■ Keep state
■ Execute code
asynchronously
(handle_cast)
■ Synchronously too
(handle_call)
■ Provide tracing and error
reporting features
■ Can be supervised
A process with a
powerful set of
features
Shades of a GenServer
■ Agent
■ Task
■ Task.Supervisor
■ Supervisor
■ DynamicSupervisor
■ GenStage
■ …
CalculatorServer
Baby
1+1?
1.
2.
3.
From: Baby

1+1?
4.
1+1=2
5.
CalculatorServer
Baby
7.
6.
From: 

MyCalcGenServer

2
8.
Live demo
“…maybe I should have
recorded a video”
Wut!?
hahahahah GenServer

bottleneck

omg what I have 

done!?
Holy %#@!
~ 200 times slower

~ 10 times more
memory
120km/h
1.6km/h
~75 times slower
10 times bigger
What happened!?
🤔
CalculatorServer
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAA
Without GenServer…
🤔
CalculatorServer
CalculatorServer
☠🔥
🔥 🔥
🔥
Omg the server is on fire!
!
Why people want it so
much?
Can we play just with
functions?
Erlang OTP
■ Process hierarchy
■ Restarting strategies
■ Concurrency
■ GenServer plays the
central role
wait, you said
concurrency…🤔
GenServer checklist
■ Model state accessed by multiple
processes
■ Run multiple tasks concurrently
■ Communicate between servers
■ Gracefully handle clean startup and exit
concerns
https://pragprog.com/book/tvmelixir/adopting-elixir
CalculatorServer checklist
❌Model state accessed by multiple
processes
❌Run multiple tasks concurrently
❌Communicate between servers
❌Gracefully handle clean startup and exit
concerns
https://pragprog.com/book/tvmelixir/adopting-elixir
CalculatorServer has no
reason to be a GenServer
A GenServer must
never be used for
code organization
purposes
https://hexdocs.pm/elixir/GenServer.html
GenServer to organize 

domain, business

contexts
GenServer to model 

runtime properties

of your system
Fear of missing
out?
Am I a proper Elixir
developer If I'm not
doing GenServers?
I have been working with Elixir for ~3 years
■ I hardly often write a GenServer
■ I mostly wrote GenServers in recruitment
processes and for fun
■ I deleted some unnecessary production
GenServers
■ I know excellent developers that never
wrote one
Elixir developers, no
shame in never had to
create a GenServer.
Ecosystem got you
covered
Or… things that you should consider
before writing a GenServer
Let's think about a typical
web application
Phoenix
Elixir
Database
Legacy
Legacy of the Legacy
Users
You ain't gonna need write a GenServer - Ulisses Almeida  | Elixir Club Ukraine
Users & App
Users -> App checklist
✔ We need to handle multiple users
connections concurrently
✔ Other processes might send messages to
that connection
✔ We have to handle connections openings
and closings gracefully.
GenServers!
However…
■ Each user’s request
has a process that runs
concurrently
■ You can focus on the
functional part
■ (functional part?
functional
programming 🥁)
Phoenix and
Cowboy handles
that for you
App &
Database
App & Database checklist
✔Databases are an external service
communication
✔We have to provide a limited number of
connections to a lot of processes
✔These connections have to run concurrently
✔We need to handle connection openings
and closings gracefully
GenServers!!!!
However…
■ Ecto supports a pool of
connections
■ Ecto handle that
concurrently
■ Work here is also done
■ You can focus on the
business 💰Ecto handles that
for you
App &Other
Services
App & Other Services checklist
✔ Usually, we communicate through a network
✔ We might need to offer a pool as a backpressure
mechanism to not bring down weak services
✔ These pools have to run multiple requests
concurrently
✔ Gracefully handle connections opening and
closings
GenSeerveerss!!!
However…
■ You can start simple,
for example, no pool
■ Watch out for big
timeouts
■ Circuit breakers can
help you out
■ Choose the libraries,
and you can focus on
features
Hackney and other
libraries handles
that for you
NoSQL storage
Not everything is stored in SQL databases
■ Examples: caching,
session, users
presence
■ Transient or Persistent?
■ How big will it be?
■ Write vs Read
✔ This data will be
accessible by multiple
processes
GenSeeerveerS!!!!!
WITH ETS!!!!!!!
Phoenix
ElixirElixir
ETS table
Phoenix
Elixir
ETS table
Phoenix
Elixir
ETS table
User
However…
■ A better abstraction reveals
intention, for example,
Cachex vs GenServer + ETS
■ How does your deploy
work?
■ Shutdown and booting the
app will lose GenServer/
ETS data
■ Redis for the rescue
■ Discuss the right tool for
the job, compare the
tradeoffs
You might want to
use a specific
abstraction.
Serializable
operations
Serializable
operations
Some resources
can’t be changed at
same time
1
1
5
I want to withdraw 2
I want to withdraw 6
Company Wallet
✔ Manage state accessed
by multiple processes
…GenServer?
However…
■ You would need an
Erlang cluster to run a
global GenServer
■ What happens with
network partition?
■ Use your database
power
■ Transactions or locks in
the database
■ Discuss with your team,
compare the tradeoffs
You environment
might not allow.
a.k.a Heroku
Background
tasks
Tasks outside of a request/response cycle
■ Welcome e-mail
■ Push notifications
■ Scheduled tasks
✔ We need to spawn or
schedule these
background tasks
✔ We need to run the
background tasks
concurrently
Dynamic Supervisors 

& Task!!!
However…
■ GenServers aren’t
persistent
■ Shutting downs might
lose data
■ RabbitMQ has persistent
queues
■ Libraries that use Redis
to store the jobs and
their arguments
■ Discuss with your team,
compare the tradeoffs
How to handle the
failures? Do you
need some
persistence?
We covered a lot 

of considerations before 

“let's write a GenServer” decision
And they are still not
over
GenServers are
fundamental
Elixir frameworks and
libraries cover most of
the use cases that you
would need to write one
But it doesn’t mean
you should not learn
about GenServer
You need to know
GenServer to tune
their configurations
When stuff goes
wrong, you’ll need
to debug and fix it.
You won’t might need
to write a GenServer
95% of time…
But that 5%…
Then, keep
learning!
Remember
Putting a GenServer in
the wrong place brings
more harm than good
It’s good to compare
and to discuss the
tradeoffs with your
team.
There’s no shame
in never need to
write a GenServer
Thank you
References
■ Adopting Elixir - https://pragprog.com/book/tvmelixir/adopting-
elixir
■ Spawn or not spawn - https://www.theerlangelist.com/article/
spawn_or_not
■ You may not need a GenServer - https://pragtob.wordpress.com/
2019/04/24/you-may-not-need-genservers-and-supervision-trees
■ GenServer (Elixir Docs) - https://hexdocs.pm/elixir/GenServer.html
■ Elixir discussion forum - https://elixirforum.com/t/you-may-not-
need-genservers-and-supervision-trees/12947

More Related Content

KEY
Capybara-Webkit
bostonrb
 
PDF
Web application intro
Tobias Pfeiffer
 
PDF
WAG the Blog
Evan Volgas
 
PDF
Node PDX: Intro to Sails.js
Mike McNeil
 
PDF
Drupal 8: Huge wins, a Bigger Community, and why you (and I) will Love it
Ryan Weaver
 
PPT
Introduction To JavaScript
Khadija Khalid
 
PPTX
JavaScript : A trending scripting language
AbhayDhupar
 
KEY
Nodeconf npm 2011
Florent Jaby ヅ
 
Capybara-Webkit
bostonrb
 
Web application intro
Tobias Pfeiffer
 
WAG the Blog
Evan Volgas
 
Node PDX: Intro to Sails.js
Mike McNeil
 
Drupal 8: Huge wins, a Bigger Community, and why you (and I) will Love it
Ryan Weaver
 
Introduction To JavaScript
Khadija Khalid
 
JavaScript : A trending scripting language
AbhayDhupar
 
Nodeconf npm 2011
Florent Jaby ヅ
 

What's hot (20)

PPTX
Journey To The Front End World - Part3 - The Machine
Irfan Maulana
 
PPS
Scalable Web Architectures - Common Patterns & Approaches
Cal Henderson
 
PPTX
Ruby on Rails - An overview
Thomas Asikis
 
PPTX
Intro to Rails
lvrubygroup
 
PDF
Ride The Bus!
Mogens Heller Grabe
 
KEY
Standardizing and Managing Your Infrastructure - MOSC 2011
Brian Ritchie
 
PDF
High Performance WordPress
vnsavage
 
PDF
DrupalCamp LA 2012: COOK UP SOME STACKS OF DRUPAL GOODNESS WITH CHEF
ccmcnerdy
 
PPTX
HyperDB, MySQL Performance, & Flavors of MySQL
Evan Volgas
 
PDF
Serverless is more findev than devops
Yan Cui
 
PPTX
AngularJS + NancyFx + MongoDB = The best trio for ultimate SPA by Bojan Velja...
Bojan Veljanovski
 
PPTX
How NOT to get lost in the current JavaScript landscape
Radosław Scheibinger
 
PDF
Be Happy With Ruby on Rails - Ecosystem
Lucas Renan
 
KEY
Architecting single-page front-end apps
Zohar Arad
 
PDF
Smooth Animations for Web & Hybrid
FITC
 
PPTX
Simplicity - develop modern web apps with tiny frameworks and tools
Rui Carvalho
 
PPTX
Untangling - fall2017 - week 7
Derek Jacoby
 
PDF
The Real World - Plugging the Enterprise Into It (nodejs)
Aman Kohli
 
PPTX
Wordpress vs Google Blogger/ Wampserver
Kshitij Wagle
 
PDF
The JavaScript Delusion
JUGBD
 
Journey To The Front End World - Part3 - The Machine
Irfan Maulana
 
Scalable Web Architectures - Common Patterns & Approaches
Cal Henderson
 
Ruby on Rails - An overview
Thomas Asikis
 
Intro to Rails
lvrubygroup
 
Ride The Bus!
Mogens Heller Grabe
 
Standardizing and Managing Your Infrastructure - MOSC 2011
Brian Ritchie
 
High Performance WordPress
vnsavage
 
DrupalCamp LA 2012: COOK UP SOME STACKS OF DRUPAL GOODNESS WITH CHEF
ccmcnerdy
 
HyperDB, MySQL Performance, & Flavors of MySQL
Evan Volgas
 
Serverless is more findev than devops
Yan Cui
 
AngularJS + NancyFx + MongoDB = The best trio for ultimate SPA by Bojan Velja...
Bojan Veljanovski
 
How NOT to get lost in the current JavaScript landscape
Radosław Scheibinger
 
Be Happy With Ruby on Rails - Ecosystem
Lucas Renan
 
Architecting single-page front-end apps
Zohar Arad
 
Smooth Animations for Web & Hybrid
FITC
 
Simplicity - develop modern web apps with tiny frameworks and tools
Rui Carvalho
 
Untangling - fall2017 - week 7
Derek Jacoby
 
The Real World - Plugging the Enterprise Into It (nodejs)
Aman Kohli
 
Wordpress vs Google Blogger/ Wampserver
Kshitij Wagle
 
The JavaScript Delusion
JUGBD
 
Ad

Similar to You ain't gonna need write a GenServer - Ulisses Almeida | Elixir Club Ukraine (20)

PDF
Gearman - Northeast PHP 2012
Mike Willbanks
 
PPTX
Repeating History...On Purpose...with Elixir
Barry Jones
 
PDF
Gearman: A Job Server made for Scale
Mike Willbanks
 
PDF
A sip of Elixir
Emanuele DelBono
 
PDF
Phoenix for Rubyists
Doug Goldie
 
PDF
Elixir Programming Language 101
Around25
 
PDF
Jfokus 2015 - Immutable Server generation: the new App Deployment
Axel Fontaine
 
PDF
Bootstrap |> Elixir - Easy fun for busy developers
David Schmitz
 
PPTX
From Ruby to Elixir
Maksym Pugach
 
PDF
Online Talk - Elixir and Phoenix UNIZA OCT 2020
Oliver Kriska
 
PPTX
Elixir - After 2 years in action + code WebUp
Oliver Kriska
 
PDF
App engine devfest_mexico_10
Chris Schalk
 
PDF
Elixir par
Michel Perez
 
PPTX
A sip of elixir
Uttam Kini
 
PDF
All Aboard The Stateful Train
SmartLogic
 
PDF
SUSE: Alien Life Forms
Kangaroot
 
PPTX
Elixir Phoenix
Tanuj Soni
 
PDF
Elixir concurrency 101
Rafael Antonio Gutiérrez Turullols
 
PDF
Elixir and elm
Mix & Go
 
PDF
Ignite Devops Fast Moving Software
SpamapS
 
Gearman - Northeast PHP 2012
Mike Willbanks
 
Repeating History...On Purpose...with Elixir
Barry Jones
 
Gearman: A Job Server made for Scale
Mike Willbanks
 
A sip of Elixir
Emanuele DelBono
 
Phoenix for Rubyists
Doug Goldie
 
Elixir Programming Language 101
Around25
 
Jfokus 2015 - Immutable Server generation: the new App Deployment
Axel Fontaine
 
Bootstrap |> Elixir - Easy fun for busy developers
David Schmitz
 
From Ruby to Elixir
Maksym Pugach
 
Online Talk - Elixir and Phoenix UNIZA OCT 2020
Oliver Kriska
 
Elixir - After 2 years in action + code WebUp
Oliver Kriska
 
App engine devfest_mexico_10
Chris Schalk
 
Elixir par
Michel Perez
 
A sip of elixir
Uttam Kini
 
All Aboard The Stateful Train
SmartLogic
 
SUSE: Alien Life Forms
Kangaroot
 
Elixir Phoenix
Tanuj Soni
 
Elixir concurrency 101
Rafael Antonio Gutiérrez Turullols
 
Elixir and elm
Mix & Go
 
Ignite Devops Fast Moving Software
SpamapS
 
Ad

More from Elixir Club (20)

PDF
Kubernetes + Docker + Elixir - Alexei Sholik, Andrew Dryga | Elixir Club Ukraine
Elixir Club
 
PDF
Integrating 3rd parties with Ecto - Eduardo Aguilera | Elixir Club Ukraine
Elixir Club
 
PDF
— An async template - Oleksandr Khokhlov | Elixir Club Ukraine
Elixir Club
 
PDF
BEAM architecture handbook - Andrea Leopardi | Elixir Club Ukraine
Elixir Club
 
PDF
— Knock, knock — An async templates — Who’s there? - Alexander Khokhlov | ...
Elixir Club
 
PDF
Performance measurement methodology — Maksym Pugach | Elixir Evening Club 3
Elixir Club
 
PDF
Erlang cluster. How is it? Production experience. — Valerii Vasylkov | Elixi...
Elixir Club
 
PDF
Promo Phx4RailsDevs - Volodya Sveredyuk
Elixir Club
 
PDF
Web of today — Alexander Khokhlov
Elixir Club
 
PDF
ElixirConf Eu 2018, what was it like? – Eugene Pirogov
Elixir Club
 
PDF
Implementing GraphQL API in Elixir – Victor Deryagin
Elixir Club
 
PDF
WebPerformance: Why and How? – Stefan Wintermeyer
Elixir Club
 
PDF
GenServer in Action – Yurii Bodarev
Elixir Club
 
PDF
Russian Doll Paradox: Elixir Web without Phoenix - Alex Rozumii
Elixir Club
 
PDF
Practical Fault Tolerance in Elixir - Alexei Sholik
Elixir Club
 
PDF
Phoenix and beyond: Things we do with Elixir - Alexander Khokhlov
Elixir Club
 
PDF
Monads are just monoids in the category of endofunctors - Ike Kurghinyan
Elixir Club
 
PDF
Craft effective API with GraphQL and Absinthe - Ihor Katkov
Elixir Club
 
PDF
Elixir in a service of government - Alex Troush
Elixir Club
 
PDF
Pattern matching in Elixir by example - Alexander Khokhlov
Elixir Club
 
Kubernetes + Docker + Elixir - Alexei Sholik, Andrew Dryga | Elixir Club Ukraine
Elixir Club
 
Integrating 3rd parties with Ecto - Eduardo Aguilera | Elixir Club Ukraine
Elixir Club
 
— An async template - Oleksandr Khokhlov | Elixir Club Ukraine
Elixir Club
 
BEAM architecture handbook - Andrea Leopardi | Elixir Club Ukraine
Elixir Club
 
— Knock, knock — An async templates — Who’s there? - Alexander Khokhlov | ...
Elixir Club
 
Performance measurement methodology — Maksym Pugach | Elixir Evening Club 3
Elixir Club
 
Erlang cluster. How is it? Production experience. — Valerii Vasylkov | Elixi...
Elixir Club
 
Promo Phx4RailsDevs - Volodya Sveredyuk
Elixir Club
 
Web of today — Alexander Khokhlov
Elixir Club
 
ElixirConf Eu 2018, what was it like? – Eugene Pirogov
Elixir Club
 
Implementing GraphQL API in Elixir – Victor Deryagin
Elixir Club
 
WebPerformance: Why and How? – Stefan Wintermeyer
Elixir Club
 
GenServer in Action – Yurii Bodarev
Elixir Club
 
Russian Doll Paradox: Elixir Web without Phoenix - Alex Rozumii
Elixir Club
 
Practical Fault Tolerance in Elixir - Alexei Sholik
Elixir Club
 
Phoenix and beyond: Things we do with Elixir - Alexander Khokhlov
Elixir Club
 
Monads are just monoids in the category of endofunctors - Ike Kurghinyan
Elixir Club
 
Craft effective API with GraphQL and Absinthe - Ihor Katkov
Elixir Club
 
Elixir in a service of government - Alex Troush
Elixir Club
 
Pattern matching in Elixir by example - Alexander Khokhlov
Elixir Club
 

Recently uploaded (20)

PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
Software Development Methodologies in 2025
KodekX
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 

You ain't gonna need write a GenServer - Ulisses Almeida | Elixir Club Ukraine