196 points · by ksec · 4y ago · 135 comments
At the same time you can still run a single $20 / month server for "smaller" apps even with Rails + Sidekiq + Action Cable + Postgres + Redis all running on the same server to power your solo developed SAAS app with thousands of customers while having a p95+ response time of <= 100ms.
PRs like this just mean things are going to get even better than they already are and is probably a precursor to converting a bunch of Rails internals to use Fibers over time. The best part about it is I as an end user don't need to know the details, I just know my CPU and memory usage will go down over time while runtime performance continues to get better than it already is.
I wonder how long it'll take before the pendulum swings so hard back into the direction of Rails that it shatters even the original growth spikes of Rails from all those years ago. With Hotwire Turbo being a thing now it's very possible to build very nice feeling apps without writing a ton of JS while leveraging good old HTML and HTTP with sprinkles of Stimulus and WebSockets.
The more I think about it, the more I talk myself into believing Ruby / Rails really are a once in a generation combination. It's truly that good for getting real shit done in a pleasant way.
It just hurts to see how my team, and previous teams I worked with struggle with all the SPAs and microservices, and Go backends and GraphQL nonsense when what we're doing are just fancy crud forms with maybe one or two really interactive widgets overall.
So much time and money wasted just for following fashion.
Rails has its strong sides. And its weak sides. Trade-offs are to be investigated. An extremely weak side of Rails, is how it is very tightly coupled to the database. Which -its strong side- is perfect for simple CRUD applications. But it falls down quickly when you have much more event-driven or domain-logic heavy applications.
And yes, I know Rails can be used for this. But just like I can, potentially write a web-app in Bash, it's not where it shines and it will bring you pain.
For Rails, the pain quickly becomes evident when a lot of business-rules are spread out through "Validators". Or when migrations become a pain to manage. Or when you have more and more complexity moved into async jobs. All of these are signs that the CRUD-nature and/or the tight database-coupling is harming you more than helping you.
Choosing some other architecture that better fits your domain is not "following the latest fashion". It's sane, proper due-diligence.
It's also a forthright description. Developers chase fads, the new shiny, just like everyone else does. Neither are we immune to cargo-culting; I'd go so far as to make the absurd comparison to web apps written in shell script to be an instance of this.
I've been behind the curtain on large Rails apps. Every issue you mentioned is either reasonably mitigated or exists more-or-less equally on other frameworks.
you can be disciplined about clean architecture/DDD/hexagonal architecture, etc, but you have completely ignore the patterns encouraged by Django, Django Rest Framework, etc etc.
Hide the ORM behind Repository interfaces
Marshall ORM objects into true Domain objects
Do not call to the database in the HTTP adapter/validators
Etc etc
You mean that Rails — being an MVC framework — is tightly coupled to (or defined in terms of, even!) the schema you've chosen for your business domain, as understood by your RDBMS.
If that's a problem for you, it's very likely because you're using your RDBMS wrong. (E.g., not taking advantage of abstractions like views+triggers to provide schema [API] stability atop storage [implementation] mutability.)
An RDBMS schema is an encapsulating API for your data, just like a GraphQL schema is an encapsulating API for your data. It's just not in fashion to use it as one — very likely because the spectre of "non-standard SQL" and inter-RDBMS portability has scared all the framework developers away from actually using RDBMSes for what they're for (instead using them as glorified indexed document stores.)
How does this cause pain in rails specifically? Any 3 tier architecture is going to have the same headaches.
I ran Rails in production years back and swore it off then. We had constant memory leaks that seemingly came from Rails itself, and the only solution we had was "just restart the server." We also had no typing then, so every bug was a runtime bug. Hopefully it's improved in the years since...
I've been happily running Go backends for the past 7 years now, and they're stable and fast and easy to refactor.
This hasn't been a serious problem in a decade.
> We also had no typing then, so every bug was a runtime bug. Hopefully it's improved in the years since...
The ecosystem has been introducing gradual typing, but even at high scale, types were not remotely the most common type of problem I ever ran into, and certainly not "every" bug.
(ex-Braintree engineer, we processed billions of requests on Rails)
Nothing is perfect. In my book restarting a server because of memory leaks is an acceptable trade off versus all the crazynes of having to decide and maintain on how to tie together a database, error validation, background jobs, translations, react, redux, an API to interface with the backend, logging, etc, etc, etc. Also, Go is just part of your system, I'm pretty sure you also have a frontend stack, and complexity increases a lot as I explained here. Compare that to Laravel+Livewire or Rails+ Hotwire. Night and day. I'm pretty sure any serious business will take the restarts any day vs the increased complexity and developer time.
> I've been happily running Go backends for the past 7 years now, and they're stable and fast and easy to refactor.
Yes, and I bet something built in 7 years with Go would have taken 7 months with Rails or Laravel.
On the static typing stuff, I'm with you on that. PHP (and Laravel) are a bit closer to that, but nothing is perfect. And for Web Development using Laravel or Rails is a good trade off.
Curious - were these actual memory leaks or Rails memory bloat due to how ruby allocates memory and holds on to them?
Nowadays there are alternative middle ground solutions such as Livewire, Hotwire, LiveView, Unpoly, Htmx, etc which provide a great way to organize the code and keep it maintainable.
I think that if we were more pragmatic and less about following the current fashion or trying to do what FANG does (which probably is just the opposite of what everyone else needs) we wouldn't be in such a high need of developers... which... I shouldn't be saying out loud probably :-)
After working with Rails for 10 years, I ended up preferring the opposite end of the spectrum of just using small frameworks like Express and just writing our own boilerplate.
I agree with Elm's take on boilerplate: it's not boilerplate and glue code that makes application development hard, and I don't think it's always a net positive to build abstraction around it like you see in large frameworks. As a Rails app grows, I just find myself spending more and more time unrolling abstraction to figure out what's going on.
Most things, like authentication, I prefer to just read in the application code itself to see what's being written to the cookie rather than dissect, say, the highly-abstracted Devise gem in a Rails app to debug session issues.
That's not engineering. That's playing with Legos just because we can.
This isnt even mentioning the compelling environmental arguments against a computationally taxing dynamic language like Ruby, which is going to be louder in the next few years.
I'm certain there are other, far more valid reasons to migrate to Java, .net, Go, Rust or whatever. Rails' opinionated setup might simply not fit your use-case. Lacking language features (Interfaces, Typing, etc). Poor libraries(in your domain). Complex deployment- and operating stack. Too few good and experienced developers who know and like Ruby around and so forth and so on.
There are numerous reasons to move away from Rails and Ruby. Speed is but one. And often the easiest to solve in other ways.
¹ Actually, in my previous role as Rails consultant I often did performance tuning for Rails. 99.99999% of the times the executing time nor the GIL, nor the GC, are the problem. But the database is. Poor DBA, in my experience is the thing that makes most Rails apps slow. For which I blame Rails, because it makes it so damn easy to forego any sane design on the DBA, and just hobgobble together some `@user.devises.active.order(:used_at).messages.new` in your views, which fires the worst ever query on your database.
Cost of development has always been the more significant cost over compute power for these businesses. Considering the entire web uses under 10% of the global energy rewriting your entire stack in c++ for the small performance improvement is not going to make a dent in that.
Why would businesses do this for such a small environmental improvement when they could spend half the money putting in solar and produce significantly more green energy than their stack uses?
Interesting, how do you know about this? I was under the impression GitHub were still mostly happy with Rails.
https://github.blog/2020-05-20-three-bugs-in-the-go-mysql-dr...
> Although GitHub.com is still a Rails monolith, over the past few years we’ve begun the process of extracting critical functionality from our main application, by rewriting some of the code in Go—mostly addressing the pieces that need to run faster and more reliably than what we can accomplish with Ruby.
Github is moving away from activerecord for exactly these reasons:
https://github.blog/2020-05-20-three-bugs-in-the-go-mysql-dr...
> Although GitHub.com is still a Rails monolith, over the past few years we’ve begun the process of extracting critical functionality from our main application, by rewriting some of the code in Go—mostly addressing the pieces that need to run faster and more reliably than what we can accomplish with Ruby.
full disclosure, I'm the author of SQLAlchemy for Python. But I certainly wouldn't expect Github to migrate from Ruby to Python for speed reasons, it would be like trading up your Ford Pinto for a Dodge Dart - not worth a rewrite :). Right now if you have middleware that is to be crushed with billions of reqs per hour, interpreted languages are simply not going to cut it.
I do not think "moving away from activerecord" is an accurate summary of what you yourself quoted, "extracting critical functionality from our main application… mostly addressing the pieces that need to run faster and more reliably than what we can accomplish with Ruby."
Anyway, ruby and python are indeed pretty close performance-wise, for whatever reason python has become more popular (and it clearly has), it's not performance! Yet people always bring up performance when talking about why ruby isn't more popular... doesn't seem to be hurting python much.
This is true but in a narrow sense. Rails and Ruby are inherently inefficient, both in the performance and memory profiles.
Rails is theoretically threadsafe, but not really - I've been hit by a threading issue just within the first hour of testing, and even if Rails was, the ecosystem isn't. Which means, one needs multiple processes, which requires lots of memory.
Speed isn't great, either. I can't really do apples-to-apples comparison, but I think it's realistic to state that equivalent, statically typed, applications are very significantly faster (without explicit optimizations), which requires more computing units.
We now have better fibres for IO bound code and ractors for thread safe CPU bound code.
Personally I don’t think ractors are completely solid but I don’t think it will long until they are.
I think Ruby’s future is pretty rosey compared to a few years ago.
I would even say that small projects can avoid Sidekiq and Redis by using Good Job and Postgres making them even cheaper to start with.
A ton of new Rails features/development you mentioned is inspired by work the Phoenix team built, who were also once core Rails contributors.
In fact, if you're tired of hearing about this, you might want to expand your bubble a little bit.
(I have no involvement with them, just a passerby.)
That is why he mentions it. It’s not “another web framework”, it’s changing the expectations for what you should be able to do without all of the crazy plumbing that was mentioned earlier.
He replied with that because it’s quite literally the answer to the problems pointed out. Not “another web framework, k thanks”.
Which is where the argument began. Emphasis on the 100ms. But lets not derail ( no pun intended ) into that again.
>PRs like this just mean things are going to get even better than they already are and is probably a precursor to converting a bunch of Rails internals to use Fibers over time.
And the fact this actually got merged. For the longest time watching rails I thought no one was interested. Samuel Williams seems to be the only one pushing for it.
Ruby and rails is great for a PoC or a quick crud app, but it is not close to being performant which usually doesn't matter but sometimes it does.
We had a high throughput server that needed a fair amount of in memory data to service the requests. The in memory data would update, one set of the data would update very quickly while the larger data set would update slowly.
Two things on this: 1) Ruby is not concurrent, it is multi-threaded but not concurrent. When ruby starts up you generally create a new process per CPU to ensure you can utilize all cpus. This is generally fine for a CRUD app but does not work well if you really need to share in memory data structures in a low latency way, it will result in any changes to memory being repeated for each process, so a 32 core machine is 1/32 as efficient with memory. 2) Ruby, at least at the time, had a non-compacting garage collector. This over time leads to memory fragmentation which ends up causing ruby to run out of pages, as such you run out of ram. This mean with Ruby you have to be careful if you get closer to the limit of how much ram you have on a machine and you are allocating/freeing large spans of memory, if freeing memory doesn't free the page you can still run out of memory. This appears as a memory leak for the machine but ruby appears to still just be using the same amount of memory. Only fix for this is having rolling restarts. Java using a compacting garage collector, which can help alleviate this issue, of course it does come with some pauses.
I also found the ruby schedule is not pre-emptive. This might have changed but at the time it meant if you had a lot of threads that were waiting on external IO that the ruby machine's performance deteriorated. Java's scheduled appeared to be more performant. Looking at this post makes me realize I should turn this into a blog post with links to the actual numbers to demonstrate each issue clearly.
Static typing, lack of static analysis tooling, and autoformatting are sorely missed. Whenever I work on a ruby/rails codebase I feel like I've gone a decade in the past. The DX is not that great and many other ecosystems have comparable DX with a modern toolset.
> As Rails continues to replace the usage of threads with fibers
That's not what we're doing... Rails doesn't really use threads anyway. We just introduced an indirection so that the request state can be stored either on thread local or fiber local variables.
> An isolation level determines how database tractions are propagated to other users and systems.
The author is confusing `active_support.isolation_level` which is either Fiber of Thread with database transaction isolations. The concept is similar, hence why the name was chosen, but it's two very different things.
> Ideally, in the foreseeable future, we can expect good performance improvements to Rails I/O operations.
Not really no. I/O will still be IOs, you may gain a tiny bit because of the cheaper "context switches", but it's gonna be very marginal for the vast majority of Rails apps.
The goal is to allow the community to use fiber based server if they wish, but I (the author of most of these changes) don't except any substantial performance improvement from it.
Is active record patern popular outside of Rails?
But it has serious downsides[1], which is a valid reason to forego it for applications that you already know will be complex in domain-logic, transaction-boundaries or where distribution and concurrency are crucial features.
Indeed, the tight coupling to the database is a real and felt problem. It makes migrations and deployments hard. And it makes layering your architecture almost impossible. e.g. I'm currently working on a Ruby (not Rails) application following the Hexagonal Architecture. Where ActiveRecord is powering the persistency adapter; away and aside. AR is not happy in that role. And we use hardly any features of AR, so it is overkill and will be fased out soon (we didn't design it this way, we merely moved the AR out of an existing tangled mess into its dedicated layer/adapter; the next logical step is to remove AR entirely).
[0] https://martinfowler.com/eaaCatalog/activeRecord.html [1] https://en.wikipedia.org/wiki/Active_record_pattern#Criticis...
ActiveRecord is an example of an ORM. ORMs are a very common way to connect an object oriented language with a relational database. Since many languages are object oriented, and the databases many people use are relational databases, this is a common solution.
I’ve done this before. Develop business logic as a separate gem and then include that in a Rails project.
ActiveRecord is actually entirely optional in Rails, it’s just the default.
You can also use POROs as presenters and mixin ActiveModel if you want things like validations.
Then couple that with Sequel as an alternative to ActiveRecord to run data access objects.
Sequel is often regarded as a more permanent alternative to ActiveRecord. It’s not a drop in replacement though so that might be a consideration for team skills/support.
In this architecture, you’re using these classes to replace models. Which gives you a layer of abstraction between the UI and the database.
Querying becomes more difficult and some libraries won’t work out of the box, but it’s quite easy to implement.
Most people don’t do this because this architecture isn’t the default, takes more time, and requires a larger application to justify doing a lot of work twice.
Inside the application you are actually trying to get all the benefits of having an Active Record model, not all the benefits of having a Car/Cat/Dog model. So you don't actually have a Car model, you have an Active Record model that represents a Car. Thinking about it that way I think sets the right expectations for what you're trying to achieve.
EDIT: I might be wrong here it could be that each Fiber released the connection when it's de-scheduled. I'd still like to get a grasp on how this improves performance ...