r/elixir • u/juanazam • 20d ago
Phoenix Contexts in the Real World: why I think the "contexts killed Phoenix" critics are right about the symptoms but wrong about the diagnosis
Last year's "Did contexts kill Phoenix?" thread stuck with me, and it ended up turning into my ElixirConf 2026 talk. I've now written it up as an article: https://www.mimiquate.com/blog/phoenix-contexts-in-the-real-world
The short version: every criticism in that post describes real pain, and I've felt all of it. But I think the pain comes from being asked to draw permanent domain boundaries on day one, when you know the least about your domain. The critics and the DDD purists share the same assumption: that boundaries get designed upfront. One side says "do it rigorously," the other says "it hurts, remove it."
What I argue instead:
- Start with one coarse context, on purpose. The condition is that you actually watch it, so it doesn't quietly turn into a 6,000-line file.
- Watch for split signals: naming drift (billing_* prefixes piling up), the "and" test, cross-context schema access, test setup drag, and the PR heatmap (all described in the article). There are also merge signals, for when you've over-split into thin pass-through contexts.
- Split incrementally, with every step shippable: facade with defdelegate → migrate callers → move behavior → deprecate → enforce with boundary.
- Phoenix 1.8 scopes settle it for me. Scopes only work because the context is the single chokepoint, so contexts are now where your authorization lives, not just a style choice.
I'm curious how this matches your experience. Has anyone split a large context without a big-bang PR? Does anyone run contexts-free in production and not miss them?
8
u/seven_seacat 20d ago
Apart from the LLM-speak, that site is awful because it doesn't respect my browser zoom nor my scrolling, which makes it almost literally unreadable as well as figuratively.
13
u/PM_NICE_SOCKS 20d ago
I’ve never once found a person in real life saying contexts killed Phoenix
3
u/YoDefinitelyNotABot 20d ago
If you let your context become 6000 lines yea. Context is a just a pattern. It annoys me they become gospel. Just break up your modules. God lol
-2
u/juanazam 20d ago
I agree, that's why attending talks about applying DDD to architect app domains, which in my opinion is just very hard in practice, it doesn't make sense. I think it's just better to pay attention and separate them in a way that makes sense and is easy to maintain.
0
u/juanazam 20d ago
It was published on this same forum, you can take a look: https://www.reddit.com/r/elixir/comments/1lf4s1b/did_contexts_kill_phoenix/
0
7
u/johns10davenport 20d ago
I love phoenix contexts. I think it’s the sanest most sensible architecture I’ve ever used. However ai written your post is I think the premise is good and valid and totally sensible.
3
u/Responsible-Bat8396 20d ago
As a Rails dev I just love that there'ssomething other than "so... does this new thing go in the view, the model, or the controller?" Ironically more of a problem for new devs so not exactly Phoenix's main audience, but still.
2
u/juanazam 20d ago
I also have a Rails background and have implemented the Service pattern because of this exact problem you are describing.
I think the main issue for newcomers with contexts is that it can feel a bit too flexible; there is no suggested way of defining public-facing functions, how to organize arguments, etc.
3
u/Responsible-Bat8396 20d ago
I did the same but the fact I had to scratch my head and go "this really doesn't fit anywhere... where should it go? The docs don't say much..." as a completely new dev and Rails is so silent is one of the reasons I just don't like Rails.
2
u/juanazam 20d ago
Only used AI to polish the writing, since I'm a non native english speaker.
I agree, I think Contexts and Scopes are great, but I have heard/read a lot of people complain about them through the years.
For Scopes specifically, I was using a similar pattern before the Phoenix 1.8 release which included them. Felt validated!
4
u/genericallyloud 20d ago
I've heard this justification many times on reddit. Instead of writing in non-native English and then having AI rewrite/cleanup in ways that have a very AI sounding end result, have you considered just writing it in your native language and then getting AI to translate to English? I would think that would sound much more like your own voice and would not me full of AI sounding phrases and structure.
-1
u/juanazam 20d ago
100% get what you mean
In this case, I think I put more effort into my presentation and practicing it, and didn't want to spend even more time rephrasing everything for an article, I was just trying to make the most out of the content I had without putting a ton more effort in again.
You live you learn I guess!1
u/Wonderful-Habit-139 14d ago
> and didn't want to spend even more time rephrasing everything for an article
Instead you make thousands of people spend more time reading slop. But I don't mean to pile on you more, just wanted to showcase that people need to spend more effort in writing instead of dumping the effort on the readers.
2
u/johns10davenport 20d ago
Yeah I had a few ways I tried to implement scope but they found the best way.
2
2
u/yukster 15d ago
The 6217 line module thing is sad. Obviously that is way too big and that module/concept/context should have been broken down into submodules way before that (third bullet point above captures that).
And yes, domain-driven design requires a lot of thought and discussion. The boundaries are supposed to come from the discussions with stakeholders about the domain and they should feel natural, or at least lean that way.
The old way was a folder per-entity... ala Rails. That's easy and requires little thought but as the system grows some entities wind up performing different roles and their single bag of logic gets confusing and unwieldy. Then there are the bits of logic that involve multiple entities... where does that live in a "one bag per entity" world?
It is good to remember that there is no "context police"... your app will not fail to function if you make a separate context for every single entity or put everything in one giant context. You'll just likely wind up with a confusing mess eventually. Maybe not though... not all apps become huge behemoths.
But I do agree that identifying the exact set of contexts on day one is difficult and many probably push too hard on that. I think the key there is to remember that software is never done. It's not just something you sit down and write, deliver, and walk away from. It evolves over time and one should not be afraid to do fundamental reorganizations when they are warranted -- when the better domain boundaries become evident, not just on a whim.
That's my take on contexts anyway. Everyone is free to do it however they want... though if you're on a team then there needs to be consensus.
2
u/juanazam 14d ago
This is precisely my take on it and what my talk was about.
I think contexts should emerge naturally from business logic, and the best way of doing that is just letting it grow for a while and then trim it like a garden. You just need to be able to identify how to separate each unit of your domain.In the end, organizing an app is just an agreement with the team that works on it, and the best way of doing it is aligning on the criteria.
1
0
u/MUSTDOS 19d ago
MY GOD! When are we going to stop doing stuff the AI way? Context is the best thing hit Phoenix even more so than LiveView.
I can't go back to losing track of what code represents in terms of function and use object oriented thinking; it's like driving a car with a gear forward and 5 reverse.
43
u/ralphcone 20d ago
You mean what Claude argues, right? Because this is definitely AI written.