AstonJ

AstonJ

Discussion: Don't add a database layer to your Phoenix application

I’ve just started the Phoenix part of the utterly brilliant online course by @pragdave. On generating the Phoenix app he uses the --no-ecto flag and explains why:

I don’t think anybody needs to add a database layer into their Phoenix application. Because Phoenix is purely concerned with communicating on the web, and anything you use a database for should be in its own separate service. So I don’t see any point at all in putting Ecto into any Phoenix application that you generate from scratch, you always want to put your logic into separate applications in the way we’ve done so far.

Do you agree? Is this how you build your Elixir/Phoenix apps? Can you think of pros/cons?

If you haven’t got Dave’s course I highly recommend it. There’s much, much more like this and I’ve learned such a massive amount from it already - particularly on HOW to approach modern development using Elixir, rather than ABOUT Elixir itself. It’s definitely one of the best online course I have ever done… and I really don’t say that lightly!

Most Liked

pragdave

pragdave

Author of Programming Elixir

My concern is not that ecto is available in Phoenix. It’s rather that first 1.2 and the 1.3 actually tightened the coupling between the two. as a result, people will come into Elixir and assume that they should write monolithic applications. If instead phoenix.new generated a separate parallel database access application, alongside the phoenix app, then people would have the same capabilities but without the temptation to write “yet another Rails app”.

Sure, we can let people discover this over time. But if we do that, and if people start running into the same problems with Phoenix that drove them away from Rails, what have we gained? People will just move on to the next big thing.

Instead, if we explain decent coding principles, and live by them, then we can help people have a better long-term experience. This will be good for them, and good for the language.

Dave

35
Post #7
josevalim

josevalim

Creator of Elixir

It is important to say that nobody is advocating to “put everything in the same app, forever”. :slight_smile: To me a great sign for splitting your Phoenix projects apart is exactly when you need to communicate to extra data sources, be it batch processing from CSV files or an API from a third party vendor.

Some have mentioned microservices in this conversation but what is being proposed, which is to split into multiple applications that depend on the same database, is against one of the core tenets of microservices/SOA which is operational isolation.

I understand some may still prefer to build their projects with multiple applications talking to the same database because it gives them more isolation at the application design level. But I also understand those who prefer to split things apart when you can actually guarantee they are fully decoupled. YMMV.

The only thing I am 100% sure is that if Phoenix generated multiple applications by default, we would be discussing how it is a terrible choice for most applications, how it is verbose, how it gives a false sense of security due to the operational coupling and how it is in general over-engineering. :smile: I am not saying I agree with any of those, just pointing out those discussions will happen regardless of choice.

That’s why I am glad we were able to grow Phoenix+Ecto to the point were all of those choices are possible. I like to say languages, frameworks and libraries should “enable rather than provide”. Languages more than frameworks, frameworks more than libraries.

And while we need to pick one approach for generators, the fact Phoenix just calls modules from the web layer, allows you to put your business logic anywhere: in the same app or different ones. And in there you can even get rid of the database altogether. It doesn’t care.

brightball

brightball

It’s possible that we are overthinking this and if I’ve got to choose between one of these approaches, I think Phoenix is getting it right by encouraging and enabling separation rather than forcing it.

Completely separate applications come with a lot of hidden complexity that doesn’t generally need to be introduced until you really require it. The whole microservice vs monolith discussion generally boils down to isolation of dependency trees and resource consumption at its root. If a monolith is built with an object oriented language, you end up with this horrifying blur of inheritance chaining that’s incredibly messy to untangle. If you go microservices from the start it’s good long term but you pay for it up front with potentially unwarranted complexity.

With the approach from Phoenix and Umbrellas you don’t have a tangled mess, thanks to the way Elixir is designed. You’ve got smaller parts in their own zones that are significantly easier to separate when you finally NEED to.

Learning curve and on-boarding are a huge deal for a language. Determining trade-offs that get people moving quickly in the right direction is a key and it’s one of those things that is hard because you’re always sacrificing something. It’s something programmers debate about constantly because business factors like turnover, training, learning curve and time to market are real factors that matter beyond ideal architecture. Ideal architecture seems a lot easier when those other factors aren’t included IMO.

In that regard and from a community standpoint, I really do think that Phoenix 1.3 has nailed that balance. Like anything else in programming, it’s always a question of trade-offs and hidden costs.

If we aim for architectural purity instead of that balance, we aren’t going to be much better off than the “OMG Benchmarks!” crowd. If productivity, learning curve, training time, etc aren’t a factor then why aren’t we just using Erlang?

Where Next?

Popular in Discussions Top

jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
100phlecs
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
pdgonzalez872
If this has been asked here before, please point me to where it was asked as I didn’t find it when I searched the forum. Maybe a mailing ...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New

We're in Beta

About us Mission Statement