Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Elixir/OTP explained to NodeJS Developers

Elixir/OTP explained to NodeJS Developers

Presentation made for Elixir Montreal Meetup at Shopify.

Avatar for Jean-Pierre Bécotte

Jean-Pierre Bécotte

May 10, 2017
Tweet

Other Decks in Technology

Transcript

  1. ABOUT ME • Full-stack web developer (since 2003) • Background:

    JS, PHP, a little bit of Django, Rails… • Worked on a few projects with Node.js • Transitioning to Elixir: bridge-at-home.com
  2. OUR JOURNEY 1. What do they have in common 2.

    Use case: the micro-service architecture (SOA) 3. How Elixir/OTP can help? 4. Conclusion
  3. WHAT DO THEY HAVE IN COMMON? • APIs • Websocket

    Handling (chat applications, for example) • Dealing with thousands of concurrent requests • Good at taking advantages of I/O They both run «server side», in the backend, so they are good for:
  4. THE SUCCESS OF NODE.JS • One OS thread • Event

    Loop (observer pattern) • Callbacks • Promises • New async / await syntax (ES2016) « There’s a lot of wasted time with I/O »
  5. - A Node.JS developer « As long as we are

    in the single thread scheme, everything is fine ! »
  6. BREAKING THE SINGLE THREAD • One task takes for ever

    and jams everybody else • Scaling horizontally • Splitting the application into many concurrent threads
  7. Team 1 Team 2 Team 3 One single thread Node.js

    app ? • Monolithic architecture • Tightly coupled • Hard to maintain • Ex: One task may crash the whole app
  8. Team 1 Team 2 Team 3 AP1 1 AP1 2

    AP1 3 • What if API x crashes or doesn’t respond? • Controllers, supervisors? • Orchestration
  9. ISSUES/CHALLENGES • Defensive programming (any code, internal or external, can

    crash the app) • Scaling horizontally • Deployment and orchestration of a distributed system is complex and difficult to achieve • Implementing PubSub… it needs a broker (Redis)
  10. process • Not an OS process ! • Lightweight •

    No locks, no mutex, no semaphores, no context switching… • Isolated from each other: no shared memory • 10 000s of processes can coexist on one single machine
  11. process process ✉ Two processes can exchange data data is

    copied messages are sent asynchronously
  12. IMMUTABILITY • Poor performance • Slow • Complicated • ??!!!

    • Less error prone • Code is easier to read • Easier to test • Make your life simpler • Not that slow
  13. ERLANG BEAM VM • Elixir compiles BEAM bytecode • Runs

    on Windows, Mac OS/X, Linux… • Production ready and mature for more than 30 years • Takes advantage of multiple CPU cores • Can run on multiple machines (nodes) • Hot code swapping
  14. SUPERVISION TREES • Workers vs supervisors • With supervisors, we

    tell what to do when a process crashes • Multiple recovery strategies
  15. APPLICATIONS COMPOSITION • Projects are first-class citizens • External and

    internal dependencies • Modules • Integrated apps • Umbrella apps ☂ AP1 1 AP1 2 AP1 3 … … …
  16. Node.JS World Elixir World Dependency management NPM MIX (and HEX)

    Web server Express Cowboy, Plug Websockets Socket.IO Ranch MVC Express / Sails Phoenix PubSub … Phoenix Persistency … / Mongo Ecto
  17. Javascript Elixir This is not Java This is not Ruby

    OO & Functional Functional & ! OO Symbols (ES6) :atoms (x) => x + 1 fn x -> x + 1 end
  18. Javascript Elixir Destructuring (almost similar to…) Pattern matching Rich data

    structures Arrays, Objects, Maps, Sets, WeakMaps and WeakSets as examples Rich data structures Tuples, Lists, Binaries, Strings, Maps, Keyword Lists, Structs Functional features : Map, Filter, Reduce… ES2019 ??? Protocols, macros, module attributes(from Python), sigils…
  19. ISSUES/CHALLENGES • Defensive programming : Let it crash! • Scaling

    horizontally : Thanks to OTP, Elixir apps scale with less effort • Deployment and orchestration of a distributed system is complex and difficult to achieve : Thanks to OTP, Elixir apps are composed from other apps to achieve a great ecosystem • Implementing PubSub needs a broker (Redis) : Due to its distributed nature, implementing PubSub with Elixir is a breeze.
  20. CONCLUSION • Elixir is a powerful and beautiful language (way

    better than Erlang in my opinion) • Takes advantage of all the cool features of OTP / BEAM machine • Has a great community • Great to build strong fault-tolerant, distributed, scalable and concurrent applications, such as… • Games??? • Any world class, business grade, web applications