Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

57 views

Published on

Slides of Ulisses Almeida, Elixir Developer at The Coingaming Group, at Elixir Club Ukraine, Kyiv, 28.09.2019
Next conference - http://www.elixirkyiv.club/

Description of presentation
GenServers plays a central role in Elixir OTP applications.
However, when you overuse it, it can become an anti-pattern and introduce bottlenecks in your system.
In this talk you'll learn:
* What's a GenServer
* When GenServer can harm
* GenServer FOMO (fear of missing out)
* How Elixir ecosystem get you covered

After this talk, you'll understand how putting a GenServer in the wrong place can slowdown in your entire system.
You'll understand how the Elixir frameworks and libraries enable you to have reliable concurrency applications.
You'll feel no shame of being an Elixir developer and never had to write yourself a GenServer.

Follow us on social networks @ElixirClubUA and #ElixirClubUA
Announce and materials from conf - https://www.fb.me/ElixirClubUA
News - https://twitter.com/ElixirClubUA
Photo and free atmosphere - https://www.instagram.com/ElixirClubUA
*Organizer’s channel - https://t.me/incredevly

Published in: Technology
  • Be the first to comment

  • Be the first to like this

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

  1. 1. You ain't gonna need to write a GenServer by Ulisses Almeida
  2. 2. 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
  3. 3. I’m Ulisses Almeida ■ Elixir Developer at The Coingaming Group ■ @ulissesalmeida
  4. 4. 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
  5. 5. Learn Functional Programming with Elixir ■ 20% off with Elixir_Ukraine_2019 promo code
  6. 6. 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/
  7. 7. Ok, GenServers A quick recap
  8. 8. https://twitter.com/ulissesalmeida/status/1125455706470088704
  9. 9. 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
  10. 10. Shades of a GenServer ■ Agent ■ Task ■ Task.Supervisor ■ Supervisor ■ DynamicSupervisor ■ GenStage ■ …
  11. 11. CalculatorServer Baby 1+1? 1. 2. 3. From: Baby
 1+1? 4. 1+1=2 5.
  12. 12. CalculatorServer Baby 7. 6. From: 
 MyCalcGenServer
 2 8.
  13. 13. Live demo “…maybe I should have recorded a video”
  14. 14. Wut!?
  15. 15. hahahahah GenServer
 bottleneck
 omg what I have 
 done!?
  16. 16. Holy %#@! ~ 200 times slower
 ~ 10 times more memory 120km/h 1.6km/h ~75 times slower 10 times bigger
  17. 17. What happened!? 🤔
  18. 18. CalculatorServer AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAA
  19. 19. Without GenServer… 🤔
  20. 20. CalculatorServer
  21. 21. CalculatorServer ☠🔥 🔥 🔥 🔥 Omg the server is on fire! !
  22. 22. Why people want it so much? Can we play just with functions?
  23. 23. Erlang OTP ■ Process hierarchy ■ Restarting strategies ■ Concurrency ■ GenServer plays the central role
  24. 24. wait, you said concurrency…🤔
  25. 25. 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
  26. 26. 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
  27. 27. 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
  28. 28. Fear of missing out? Am I a proper Elixir developer If I'm not doing GenServers?
  29. 29. 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
  30. 30. Elixir developers, no shame in never had to create a GenServer.
  31. 31. Ecosystem got you covered Or… things that you should consider before writing a GenServer
  32. 32. Let's think about a typical web application
  33. 33. Phoenix Elixir Database Legacy Legacy of the Legacy Users
  34. 34. Users & App
  35. 35. 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.
  36. 36. GenServers!
  37. 37. 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
  38. 38. App & Database
  39. 39. 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
  40. 40. GenServers!!!!
  41. 41. 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
  42. 42. App &Other Services
  43. 43. 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
  44. 44. GenSeerveerss!!!
  45. 45. 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
  46. 46. NoSQL storage
  47. 47. 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
  48. 48. GenSeeerveerS!!!!!
  49. 49. WITH ETS!!!!!!!
  50. 50. Phoenix ElixirElixir ETS table Phoenix Elixir ETS table Phoenix Elixir ETS table User
  51. 51. 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.
  52. 52. Serializable operations
  53. 53. 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
  54. 54. …GenServer?
  55. 55. 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
  56. 56. Background tasks
  57. 57. 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
  58. 58. Dynamic Supervisors 
 & Task!!!
  59. 59. 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?
  60. 60. We covered a lot 
 of considerations before 
 “let's write a GenServer” decision And they are still not over
  61. 61. GenServers are fundamental
  62. 62. Elixir frameworks and libraries cover most of the use cases that you would need to write one
  63. 63. But it doesn’t mean you should not learn about GenServer
  64. 64. You need to know GenServer to tune their configurations
  65. 65. When stuff goes wrong, you’ll need to debug and fix it.
  66. 66. You won’t might need to write a GenServer 95% of time…
  67. 67. But that 5%…
  68. 68. Then, keep learning!
  69. 69. Remember
  70. 70. Putting a GenServer in the wrong place brings more harm than good
  71. 71. It’s good to compare and to discuss the tradeoffs with your team.
  72. 72. There’s no shame in never need to write a GenServer
  73. 73. Thank you
  74. 74. 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

×