Elixir Phoenix use cases ? Why start using it?

Hello,

We have been using Elixir and Phoneix for creating REST APIs for a few months now.

But I can’t see the benefits of this switch (from nodejs / Nestjs) in terms of performance, speed etc ?

So :

  1. Can you please tell me why are you using it ? (did you switch from another technology ?)
  2. What are the major benefits ? To explore them
  3. Is there a specific domain to used it in ? (AI, mailing etc.) ?
  4. In terms of deployment, scaling etc. Do you find it efficient ?

Many thanks

1 Like
  1. It’s a joy to develop in. Switched from F#/C# and Python (part way through the project).

  2. The concurrency model of the BEAM. Speed/ease of development, documentation, REPL, community.

  3. Pretty much whatever domain you like. Web, distributed, data, orchestration, embedded, machine learning, desktop, mobile (mobile is in it’s infancy).

  4. Yes it’s efficient. It’s no Rust/C, but you can use those from Elixir. Deployment is fairly trivial.

7 Likes
  1. It’s fun. Its design paradigm (or better say the groundwork of Erlang) fits better with my worldview.
    It feeds me. As a single developer, deciding most things on my own, nobody ever complained why I’m using it. (switched from former PHP to Ruby / Node.js; & I have never regretted)

  2. I really like dynamic typing for the benefit of fast progress.
    I manage to do less bugfixing and more time for progress.
    Documentation is wonderful compared to other languages.

  3. Everything network related it’s perfect. But also for IoT (nerves) and starting in AI there are valid options.

  4. You can scale very very far without really thinking about it.
    If you really need to scale beyond that, you’ve got a perfect groundwork.
    Energy efficient - probably not the best option. :wink:
    Efficient in terms of development speed - definitely.
    Efficient in general - it does its job very good.

2 Likes

I switched from other functional languages because Elixir has a larger ecosystem with an emphasis on supporting practical engineering tasks like database backed web applications. Both Ecto and phoenix are nice to use. Mix has also been pleasant.

I would choose it over node/nest because I can spend more time developing and less time upgrading and migrating within churning ecosystem.

3 Likes

Elixir and Phoenix are good for:

  • Any web app with server-side rendering.
  • Any web app with client-side rendering (you can use partial hydration with JSON API server that Phoenix can also do). Phoenix integrates well with every frontend framework but you’ll have to do some plumbing with webpack, rollup, esbuild and others.
  • Any API server (REST and GraphQL through HTTP[S] are best supported though we have functional libraries for gRPC as well).
  • Any long-term persistent servers – general background workers, crawlers, data collectors and aggregators, and many others.
  • Any app backend that requires persistent bi-directional connections to its clients. Phoenix’s WebSockets support is very good.
  • And lately some ML is on the rise though I’m not sure how well-supported it is at the moment.

You can use Elixir for likely no less than 80% of all possible projects out there.

It’s at least as fast as the hot paths in JS, so it’s pretty fast. We’re talking average 15-35 milliseconds per request in a web app even when DB is involved.

As others mentioned it’s no C/C++ or Rust but (a) you can use these in your super hot paths if you want, and (b) most apps don’t need such a beastly speed so you’re 98% likely to get away with pure Elixir.

Elixir is a secret weapon. Many people can’t see that because they want to arrive at an already super popular ecosystem. Rest assured however, companies using Elixir are very happy with their choice, and they can get away with hiring less devs, too.

10 Likes

I am just curious, why switch then? You must be hoping for something, right? What is that something you were hoping for, but didn’t get? Speed? I am not sure an Elixir stack can beat NodeJS for throughput, but I am quite confident that a Elixir stack shall have a better latency histogram, like 99% percentile latency. Is that something you want?

4 Likes

I think the biggest benefit that no one has mentioned yet is maintainability over multiple years.

The ecosystem is stable yet steadily improving. And you may have noticed already that the language lends itself to making easier to understand code which means less bugs or maintenance headache.

If you want to have a long-running stable system with minimal maintenance cost, I’m not sure how the JS ecosystem can compete.

10 Likes

Why are you creating REST APIs? Is it for web app? If that’s the case have you looked at Phoenix LiveView? With it you could avoid creating any kind web facing API and save huge amount of dev time and save even more with simpler web app development model. Also you would be mostly working with Elixir when it comes to UI, so pretty much full stack Elixir.

I came to Elixir / Erlang from Rust that said…

Concurrency… Previously I worked in fin tech, privately. I built highly concurrent permutation testing / modeling applications. In Rust you can use C bindings and control thread affinity, and must to get a 1-1 model to avoid context switching, etc. BEAM does that for you.

As with any language spinning up threads in Elixir has some overhead with that under consideration… its not as heavy as OS threads. The smaller you can break down a set of problems the more concurrent you can become and provided you don’t loose efficiency due to the cost of threading you can tackle real world problems very quickly.

For example, notwithstanding bloat for piping through Phoenix among other things, I build a file parser that takes semi structured .log files at 11.1gb and can extract data in 33 seconds with ~6 delimiters. (Hardware : 32gb ram, Intel© Core™ i9-10885H CPU @ 2.40GHz × 8, m,2 drive)

Ive not tested this in Rust… but building such an architecture would take much more work… and it still may not be as fast…

2 Likes

I do both Elixir and Rust, a lot (especially Rust async + tokio lately), and I know Rust can do this 2.5x - 3.0x faster but yeah, having in mind that it would take longer to develop then it’s not certain it’s worth it to develop such a toy problem in Rust – unless such payloads have to be ingested several times per minute.

During 20 years of development, coming from PHP → Node.js → Go → Crystal lang, a little bit of Flutter/Dart, Rust and others. I feel Elixir to be a good middle ground for productivity, maintainability, robustness, ecosystem, community and fun to code.

I find in it the main qualities I liked in other languages, plus the things specific to Elixir / Phoenix / Erlang. There is a lot to do with Elixir. :slight_smile:

It’s some benefits but it’s not exclusive, nothing prevents you from doing some Node.js / Next.js for some projects and Elixir for others (this is what I do).

3 Likes

You said you can’t see the speed/performance benefits. There might be no speed/performance benefits, or if there are any, they might be so small that it doesn’t really matter. If you were switching from Nodejs in order to get more speed/performance, you might need to look at a truly high performance language like Rust.

If Elixir is not performing well enough for your use-case, I invite you to create a new post and ask about how to fix that, I have not yet heard of a web application that has performance needs that Elixir can not serve. Elixir might need to reach out to another language to resize images or encode video or do the one or two things that are slow, but it is fast enough that users can’t tell they are waiting for it.

If what you really want is scalability on minimal hardware (which is a result of the speed you could get from Rust/Go/C), then you need to ask yourself if you will spend more on bigger servers for Elixir, or hiring more programmers to write code in a less productive language. If you have Billions of users (which would mean you certainly have Billions of dollars), you might spend more on servers by switching to Elixir, if you do not, you will probably spend more on Programmers by using a higher-performing-less-productive language.

I see many other reasons to switch to Elixir in the answers to your other questions.

  1. I mainly use Elixir because I enjoy it. I came from Ruby.
  2. As a programmer, productivity/maintainability is probably the main benefit if I had to choose one. It takes less time to develop things in Elixir than in almost any other language I can think of. It is not only a high-level language, but it has built-in-clean-simple abstractions for very complex concepts like concurrency, distributed programming, in-memory-key-value store, pub-sub. Stability from the built-in fault tolerance is probably the more significant characteristic that I rarely think about.
  3. Realtime applications. If the user needs to see a change on their screen because there was some state change from another user or some other system. That said, any web application can be built using Elixir.
  4. Yes, deployment/scaling is efficient. Particularly if you want the simplicity of less nodes and just scaling vertically by making the nodes bigger, it efficiently uses all the cores on the system.
6 Likes

Which language you like best, in terms of productivity and developer happiness?

Take it from someone who has been in the deep muddy water of JavaScript development, both in frontend and backend, Elixir & Phoenix are better duo than anything I came across.

Let me answer each of your question, one by one.

  1. I switched form Node.js. I used it along with Socket IO to make presence tracking app and distributed locking.

    • The Node.js environment is riddled with memory leak. (The only Node.js program without memory leak is a Hello World program) (Elixir & Phoenix app have per process garbage collection, so memory won’t bloat for entire application)
    • It is opaque, i.e. You can’t inspect what’s happening inside in production. (You can with Elixir & Phoenix, you can even terminate a single process that might be causing trouble, without crashing the whole server!)
    • It’s not multi threaded, so you have to run multiple instances using PM2 or Docker. (Elixir & Phoenix have that natively)
    • Multiple Node.js instances don’t communicate with each other, instead you have to host a separate Redis instance, and use its PubSub mechanism or use some other message queue. (Elixir & Phoenix run on top of BEAM virtual machine, which has those primitives built in!!)
    • JavaScript is an unreliable mess, which you will replace with TypeScript anyway, which is just a fake type checker. (With Elixir, you won’t need Type checking whatsoever, because of the first class testing features and type checkers don’t prevent all bugs anyways, just type bugs)
  2. a. Major benefits:

    • The Phoenix Framework has Live View, which is like next level compared to the terrible Single Page Application. (I can fill a page about how it’s a mind-blowing piece of tech if you want to know)
    • Elixir has LiveBooks, where you can learn, make Machine Learning models, even create business intelligence tools, because it can connect directly to all other Elixir Phoenix nodes with ease.
    • Elixir has Pipelining, i.e. |>, for better readable and maintainable code.
     def on_mount(:only_admin, _params, session, socket) do
      {:cont, socket}
        |> assign_user(session)
        |> verify_user()
        |> verify_lock()
        |> verify_email()
        |> verify_role(~w(super_admin admin)a)
        |> subscribe_user()
     end
  1. b. Some other benefits:
    • The paradigm for coding is functional, and not OOP, so you won’t drown in the rabbit hole of Design Patterns.
    • Elixir has pattern matching, which eases error handling and reduces lines of code we have to write:
   def env(atom) when is_atom(atom), do: Application.get_env(:derpy_coder, :environment) == to_string(atom)
   def env(str) when is_binary(str), do: Application.get_env(:derpy_coder, :environment) == str
   def env(list) when is_list(list), do: Application.get_env(:derpy_coder, :environment) in list
  1. Domains:

    • The language and the BEAM virtual machine lend themselves to soft real time application really well.
    • Better & Modern web applications. See: Phoenix Framework
    • Soft Realtime, collaborative applications. See: Phoenix LiveView
    • Internet of Things using Nerves project. See: Nerves Project
    • Mobile Application. See: Phoenix LiveView Native
    • Desktop Application. See: Elixir Desktop
    • Artificial Intelligence training and serving. Elixir Nx, Axon
    • Here’s a list of Awesome Collections: Awesome Elixir, Awesome Phoenix LiveView
  2. Deployment

    • It takes up less memory, compared to Java or Node based apps.
    • It uses less server resources and just one instance deployed onto a server can utilise all cores of that machine. So you don’t have to use a tool like PM2 to run multiple instances like in case of Nodejs.
    • There was a blog post about how a company using Ruby, with 80 servers came down to only 5 after switching to Elixir.
    • Discord uses Elixir, and WhatsApp used to use it.
    • Current build process can output single binary, so you can deploy it in a old school way like I prefer, or you can Dockerize it to run it inside Kubernetes, or throw the binary at Hashicorp Nomad for orchestrating standalone binaries.
    • Separately deployed nodes can communicate with each other, without using ETCD or Redis or Kafka separately, it’s built in.
    • With fly .io, you can deploy your server to the edge, as if it’s a CDN, except your code is running closer to the user, and you won’t have to deal with any extra tech layer. See: LiveBeats.

I have been falling down the rabbit hole of Front End frameworks, for what feels like forever. It was like a never ending Distro hopping. Everything that comes out in front end space solves one or the other problem, but leaves out a gaping hole behind them.

For instance, React, solved some problem, but left open the server side rendering. So Next.js came along.

REST api felt unwieldy, so GraphQL came along, now state needs to be maintained in both backend and front end. So you end up with business logic scattered across both places.

Routing story gets bad or has several fragmented implementation. State management remains a mess.

Then someone from business comes along and asks you to make some of it real time, and now you are caught in the JavaScript web hell.

The 2 ton heavy npm package gets replaced by pnpm, which breaks everything. You can’t reliably run a Node.js project after a hiatus.

Webpack bundler keeps getting slower, so you move to something else, which has some other headache of its own.

Believe me when I say, you are saving yourself a ton of headache and sparing yourself from paying technical debt by moving to Elixir/Phoenix.

It saved me from yak shaving me life away.


See the following talks to get a visual explanation of all the things I pointed out:

P.S. I would have put a lot more links, but new users are not allowed to add more than 5 links. (I started learning Elixir & Phoenix 3 or 4 months back and joined the forum today :sweat_smile:)

15 Likes

When comparing Elixir & Rust, I would say I felt more productive with Elixir than anything else, same goes for happiness.

Rust felt slow while developing, because of its slow compiler.

It’s way behind in web development field, in terms of features. And it doesn’t even come close to Phoenix Framework.

Its memory, thread safety and speed is first class, and can be easily embedded into an Elixir project with Rustlers.

I like both of them, but I would use Elixir for everything, and Rust for solving the bottle necks in the application.

4 Likes

In term of productivity and DX, it’s Elixir without any hesitation.

Then JS, Crystal and Dart.

For small project, I had a lot of fun and productivity with Crystal but the compile time was too long for medium / big project (more than 30 seconds for one API REST). Also the doc is incomplete, which wastes time.

So Elixir far ahead, then JS and Node.js (but for a big or medium backend I find less fun and less efficient than Elixir).

2 Likes

Can you elaborate about single binary? Are your thinking about Elixir own releases or something like bakeware?

I found this paragraph of your comment about different way of deploying apps interesting…

I don’t think Whatsapp ever used Elixir. They use Erlang , same VM so many same benefits apply. I’m quite sure they are still using Erlang. Are you mixing up Whatsapp with Facebook Messenger?

Yup my bad, I realised my mistake after typing out the essay.

They used Erlang, and then moved on to something else when they joined Facebook, for homogeneity.

Also, @sanjibukai, I didn’t know bakeware got deprecated. (I just saw the title of a YouTube video, kept it in watch later!! :sweat_smile:)

I’m just a beginner, I might be wrong in many aspect, don’t hurt me. :sweat_smile:

1 Like
  so_survery_get(:techs, :webfw, 2022) #=> %{
    dotnet: ["Blazor", "ASP.NET Core", "ASP.NET"],
    elixir: ["Phoenix"],
    js: ["React.js", "Node.js", "Vue.js", "Next.js", "Svelte", "Angular",
     "Express", "Nuxt.js", "Angular.js", "jQuery", "Deno", "Gatsby", "Fastify"],
    php: ["Laravel", "Symfony"],
    python: ["Django", "Flask", "FastAPI"],
    ruby: ["Ruby on Rails"],
    scala: ["Play Framework"]
  }
  |> filter(fn {lang, _} -> not microsoft?(lang) end) #=> [
    elixir: ["Phoenix"],
    js: ["React.js", "Node.js", "Vue.js", "Next.js", "Svelte", "Angular",
     "Express", "Nuxt.js", "Angular.js", "jQuery", "Deno", "Gatsby", "Fastify"],
    php: ["Laravel", "Symfony"],
    python: ["Django", "Flask", "FastAPI"],
    ruby: ["Ruby on Rails"],
    scala: ["Play Framework"]
  ]
  |> filter(fn {lang, _} -> functional?(lang) end) #=> [
    elixir: ["Phoenix"],
    scala: ["Play Framework"]
  ]
  |> filter(fn {lang, _} -> can_normal_people_understand?(lang) end) #=> [
    elixir: ["Phoenix"]
  ]
14 Likes