Use Phoenix for a simple API server?

Hey,

I’m thinking about writing my first Elixir backend server, and I’ve been wondering if I should use Phoenix or a simple cowboy server for it.
What I’m going to do is have a simple api server which queries a db and most probably would send rest requests to another server when needed (this part of the architecture wasn’t decided yet)
This backend server would have to auth and in the future probably authorize users but it won’t handle the user session.
I wonder if Phoenix would be an overkill for such a simple backend server, as from my experience rails is.
Would appreciate any input.

Thanks,
Shrike

1 Like

Phoenix is primarily a set of plugs (and optionally a nice websocket handler) on top of cowboy, it can be as slim as you want it to be, and as such you should use it, just remove what you don’t need from your endpoint if you so wish, and you can grow it later if you need. :slight_smile:

6 Likes

Thanks for the super fast reply.
It’s just when using ruby I found out that Sinatra was the best fit for this kind of servers, Rails just requires way too much fuss to make such simple severs.
Guess I’m still thinking ruby.

4 Likes

Yeah, Phoenix is not at all heavy, it is rather very lightweight, especially as you can pretty trivially pick and choose what of it you want to use. The default generated endpoint is showing off a lot of common features (not even all) and those you do not need to leave in if you don’t need them. Like if you don’t need static file hosting then remove that plug call, or if you don’t need a router then remove that call (finish up with a final single plug or so). Etc… etc… You can pick and choose and mix and match what you want. :slight_smile:

4 Likes

You haven’t seen nothing yet :lol:

According to legendary people like Dave Thomas (i.e PragDave) Phoenix is much more than a web framework :003:

I know I’ve said this a million times on the forum now, but I highly recommend his Elixir course - it blew my mind, and is one of the best courses I have ever done :023:

(In case anyone is wondering, no I am not on commission!! Wish I was though, I reckon I could sell his course all day long no sweat :044:)

3 Likes

Hear hear! It is a whole way of Structuring the program, it is well worth following. :slight_smile:

2 Likes

Yup! It gets even more mind-bending when you factor that you can use more than one Phoenix layer in your app - can anyone imagine using more than one Rails ‘app’ in their apps? Nope, neither can I :lol:

4 Likes

I’ll take your word and go through it before starting the work on the server, hopefully I won’t have to start with ruby and Sinatra and do a move later, time frame is going to be really short for this project.

3 Likes

Nice :023:

I would recommend reading Programming Elixir first if you haven’t, though depending on your experience you may not need to (I would anyway though as it’s an excellent book, as it’s Elixir in Action).

Let us know how you get on :slight_smile:

1 Like

If time frame is super short then use what you know. ^.^;

If you want to learn something new, Elixir and Phoenix will get you going with something fast that will run very well though. :slight_smile:

2 Likes

Well this is going to be my first experience with Elixir but I’m not afraid to try new stuff.
Already did a move from Perl to Ruby without previous experience with Ruby on a production server.
The main decision maker would be how much time I’ll have for this server, Ruby + Sinatra would require a day or so Elixir most probably a week or so.
First iteration of this server wouldn’t need to be prefect it would just need to work and be secure. In the future it would need to be fast, but that’s a problem for the future;)
So the decision would be time, I do know and expect that in the second stage of this project it would need to be lighting fast Elixir is the perfect fit, and I want to have my first experience with Elixir when I can make mistakes and don’t have to worried about benchmarks.

2 Likes

Go for it :023: it is actually aimed at programmers so you are the target market :003: (It’s just that personally I am the sort of person who likes to read or learn as much about something as I can before jumping in :lol:)

As ODL commented, it will be fast from the get go - Phoenix response times are in microseconds :smiley:

1 Like

Well that’s why I bought programming elixir, programming Phoenix and functional web development and been browsing the in the last month.
But the sad truth is until you write something you don’t know the language and the first few times you use it, you duck only after a few months of using a language you start to be mediocre (in my case) when using it

2 Likes

To see what a very simple Plug webservice looks like, here’s one of mine with only one endpoint that discovers how long people have followed a Twitch channel: http://gitlab.com/Nicd/Peliturbiini-followed-since/

3 Likes

@Nicd thanks, took a look at that and that’s basically what I’ll need to start with my basic server now to find some simple ecto app I can steal ;p

2 Likes

Well, I think that I owe you an update here…
The project is now on the first stage unfortunately it started 2 weeks ago or maybe 3 and needs to be up and running in less than 2 weeks.
So I decided to stay with ruby and Sinatra, the good news is that 90% of this project is based on what I learned here and I think of it as the Elixir way(as I see it)
Basically what I’m doing is creating a Json based web services project that uses lots of threads that can always crash and send their state to another server if and when it happens.
I even have a gen server that is responsible of checking loads start and stop every other server, and of course is chosen with each restart.
So I basically wanted to say thanks, this community enriched me more than I could ever imagine, and I’m doing my best to use what I learned here, hopefully stage 2 would be Elixir but if not Elixir then my time here as a silent observer would be my inspiration.

3 Likes

We are not using Elixir in anger yet, but I think it would take a considerable effort to properly implement supervisors and gen_servers in ruby.

1 Like

I should have said that my gen server is not going to be even 10% as smart as elixir more in the lines of removing computers from LB and adding them when they are up. (but the idea came directly fro it)
It would also be responsible of running test to make sure that every node is working as it should and every bot is doing what it is suppose to do.
The nodes themselves would be responsible of re-sending failed attempts to the Load Balance and letting the gen server know that they are down when it happens, which would initiate a replacement (shutting down the failed node or deciding the error was acceptable or not)

1 Like

Well, I would give you one more update now and 2 more one when finished and another when I will finally move to Elixir.
From last week we are up, managed to do it within our 1 month time frame.
So what I have for now 12 computers each can do all the tasks I need.
State is decided by a transferable json, and when a request is failed it is retried and on second fail goes back to lb, second fail sends an error to queue and returns a success to my genserver.
Next stage I would fix 3 things:

  1. Better genserver decision process for now first server started is genserver
  2. Auto version update
  3. Seamless move when genserver fails.
    And of course make each node start as a service - yeah I know … but this one was less important considering I needed to get the whole system working.
    Next update would hopefully be after everything is working, and in less than 3 weeks, nothing is critical and I do have so many other important things I should do.
    Last update would be a move to elixir but that would probably be later than I want :frowning:
    Forgot to say everything is threaded:)
1 Like