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.

All Aboard The Stateful Train

2,116 views

Published on

Going beyond a simple Phoenix web application.

Published in: Software
  • Be the first to comment

  • Be the first to like this

All Aboard The Stateful Train

  1. 1. 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
  2. 2. Who am I? Eric Oestrich Wizard at SmartLogic
  3. 3. Who is SmartLogic? We build custom web and mobile apps. Based in Baltimore, we've built over 150 apps since 2005.
  4. 4. ● 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
  5. 5. Standard Application
  6. 6. Standard Application ● Stateless HTTP ● Controllers go straight to the database ● Does not leverage OTP ● MVC, Rails-like
  7. 7. Standard Application Examples ● Wordpress ● Hubspot ● Wikipedia ● Shopify
  8. 8. Stateful Application
  9. 9. Stateful Application ● Stateless HTTP ● Websockets/TCP connections ● Heavy use of OTP ● "Background" processes
  10. 10. Stateful Application Examples ● Slack ● Discord ● Twitch
  11. 11. 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
  12. 12. Supervision Trees & Processes
  13. 13. Why Stateful?
  14. 14. 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”
  15. 15. Did I mention Erlang is great at this? https://unsplash.com/photos/M5tzZtFCOfs
  16. 16. Let's review some stateful applications
  17. 17. exventure.org ExVenture: A MUD Server
  18. 18. What is a MUD? ● Multi-User Dungeon ● Online text adventure ● Connects with telnet
  19. 19. 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. >
  20. 20. MidMUD The flagship ExVenture instance midmud.com
  21. 21. Review of ExVenture ● A "living" world ● Everything is always on ● The database is there for startup ● Lots of inter-process communication
  22. 22. Demo midmud.com
  23. 23. Could this be stateless?
  24. 24. Not Really ● Poll based? ● Worker that updated the world? ● Gross
  25. 25. grapevine.haus Grapevine: A MUD Community Site
  26. 26. Grapevine ● Steam for MUDs ● Cross game chat ● Web client
  27. 27. Review of Grapevine ● Close to a standard application ● State involved is websocket and telnet ● Caches
  28. 28. Demo ● Game listings, stats ● Cross game chat ● Web client
  29. 29. Could this be stateless?
  30. 30. Possibly ● Chat applications can be poll based ● Live caches could hit the database ● Still gross
  31. 31. Is stateful better?
  32. 32. Do I want this?
  33. 33. Depends
  34. 34. Pros
  35. 35. Soft realtime capabilities Erlang is built for soft realtime
  36. 36. Website feels "alive" Websockets can update your website without a refresh
  37. 37. Reduced Database Queries Less round trips to the database for faster response times
  38. 38. Cons
  39. 39. Requires heavy usage of OTP You must architect out how your application is laid out in memory, beyond what Phoenix gives you.
  40. 40. Caches are Hard™ There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors.
  41. 41. 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
  42. 42. Rolling Restart
  43. 43. Here be Dragons ● Mailbox size ● Message passing copies ● Clustering woes
  44. 44. Let’s Go Stateful
  45. 45. 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
  46. 46. Supervision Trees Watch over your GenServers
  47. 47. :ets ● Erlang Term Storage ● Very fast reads/writes ● Concurrent ● Downside: copies memory a lot http://erlang.org/doc/man/ets.html
  48. 48. Put It All Together
  49. 49. Diagram of ExVenture
  50. 50. Connecting to the World
  51. 51. Players Acting on the World
  52. 52. Updating a Skill from the Admin
  53. 53. Using a Skill
  54. 54. Grapevine Chat
  55. 55. Grapevine Web Telnet Client
  56. 56. Wrap Up
  57. 57. Elixir gives you the tools to build a stateful application with ease
  58. 58. Ram and CPUs are cheap, use 'em
  59. 59. LiveView is an easy way to start stateful
  60. 60. Finally, you might not need it
  61. 61. GitHub ● oestrich on GitHub ● oestrich/ex_venture ● oestrich/grapevine
  62. 62. Titans of Text titansoftext.com
  63. 63. Season 3 of Smart Software podcast.smartlogic.io
  64. 64. Season 3 of Smart Software podcast.smartlogic.io
  65. 65. Season 3 of Elixir Wizards podcast.smartlogic.io
  66. 66. First title line Second line here Subtitle goes here — no fancy color block kthough too fussy smartly. smartlogic.io Thanks! smartlogic.io
  67. 67. 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

×