8 ms·
How to write fast code in Ruby on Rails
- samzer 7y agoWould these suggestions apply for Django? I'm assuming Django also faces similar issues
- winrid 7y agoYeah, some of them are just common "best practices".
- jjav 7y agoStep 1: Step away from Ruby. I loove ruby, it's my first choice for scripting wherever performance doesn't matter. No way around the fact that it very slow though.
- pmontra 7y agoPerformance matters rarely in my experience. There is a world of low load services out there, with servers sitting idle most of the time waiting for requests. And that's not the case of Shopify which is doing well with Ruby anyway.
- danmaz74 7y agoPerformance matters in web applications, but that is almost never determined by the speed of execution of the web framework programming language. The performance bottlenecks are almost always in the database and/or the architecture. It's also very easy to scale horizontally the execution of the web code. That's why, for most web software, programmer productivity is more important than raw language performance
- enraged_camel 7y agoHorizontal scalability is not always easy, but it is always expensive. Startups especially tend to discover the latter the hard way when they receive surprise bills from AWS.
- danmaz74 7y agoScaling horizontally a Rails application where the bottleneck is the Ruby code is always very easy - just add more instances working on the same DB. It is usually more difficult because the bottleneck is in the DB, not the Ruby code. You're right about price though; if the cost of compute is relevant in your business model - which, again, usually isn't the case - then doing that computation with Ruby on AWS isn't the best choice.
- maitredusoi 7y ago> surprise bills from AWS Surprise bills from AWS means it is time for refactoring ( and thus optimization time ;)
- pcmoney 7y agoIs ruby that slow? I always understood it was comparable to python? Plus if it is performant enough for Shopify and Github it seems like it would be performant for 90%+ of use cases? The speed of development and the flexibility of development is much greater than most stacks once you add rails.
- Mikeb85 7y agoIt is comparable to Python. Which is very slow.
- jonny383 7y agoIf ruby was performant enough for Shopify, this article would not have been written.
- jrochkind1 7y agoHm, a lot of the article focuses on optimizing your interaction with a database (rdbms/sql), which has nothing to do with the performance of ruby (some of it arguably has to do with _Rails_). Even more of the article has to do with Rails, and not ruby in general. But the larger point, are you suggesting that in a language that is "performant enough", developers need no performance advice, they can write however they want and get adequate performance, for any performance needs? Maybe, although I'm dubious. The existence of advice for programming does not mean a language/platform isn't "something enough" until we have AI writing code. At any rate, if such a language exists it is not ruby. But I disagree with that suggestion. The existence of optimization advice does not mean that a language "is not performant enough". If Shoppify is still happily using ruby/rails, I would say it indeed demonstrates they are performant enough for them.
- jonny383 7y agoNope I'm not. But Shopify is large enough to have a first-class engineering team that would (presumably) be writing efficient code, which is nothing special. So if this first-class engineering team is still writing about _fast code in Ruby_, doesn't that imply Ruby isn't fast?
- deleted 7y ago[deleted]
- jonny383 7y agoCouldn't agree more. "fast code" and Ruby are mutually exclusive.
- jashmatthews 7y ago> No way around the fact that it very slow though. You can run Sinatra on TruffleRuby today and it's very competitive with e.g. Go web frameworks. It's not quite production ready but has commercial support from Shopify (Chris Seaton) and Oracle Labs. It will get there soon. There's also no reason I know of that you couldn't build a simple optimizing JIT for CRuby like LuaJIT. I'm trying a proof of concept at the moment.
- mjfisher 7y agoSpeaking as someone who uses Rails regularly, I agree - it is considerably slower than many other language choices. It probably isn't a good call for a performance critical bottlenecked service that can't scale horizontally. For everything else, it's great, and it's almost always a good default to choose speed of development and engineer productivity over runtime performance.
- krn 7y ago> For everything else, it's great, and it's almost always a good default to choose speed of development and engineer productivity over runtime performance. Doesn't Elixir on Phoenix[1] provide the elegance of Ruby on Rails without its drawbacks? [1] https://phoenixframework.org/ https://phoenixframework.org/
- mercer 7y agoI'm pretty much all in on Phoenix, but I suppose a good reason to stick with RoR would be that 1) it's easier to find (experienced) devs, and 2) there are (still?) many more packages available which can make quick development easier.
- kweinber 7y agoOur research has indicated that Elixir has basically stopped growing in the general market.... really only getting any growth from converted erlang devs. Have you seen anything different? Most enthusiastic vendors I’ve talked to have moved away from elixir due to low adoption and demand. Tiobe, google trends, etc tell the same story. I’d love to find evidence to the contrary but the low numbers of elixir devs seems to be both a supply AND demand problem... ie lack of traction and interest.
- Aduket 7y agoWhat research? Can you write a blog post about this please?
- 7y ago
- pjmlp 7y agoTruffleRuby and RubyMotion prove otherwise. But yeah, we keep mixing implementations with languages.
- pjungwir 7y agoOne of my favorite tricks is to generate the JSON for your API responses in Postgres instead of in Ruby. You can get 100x speedups, but the downside is that you wind up with big nasty SQL queries that duplicate Ruby logic and are hard to maintain. There was a nice gem that would automatically produce JSON-generating SQL [0], but it is abandoned now. It only supports Rails 4 and ActiveModelSerializers 0.8, which are both quite old. I just published a similar gem [1] that works for Rails 5 and AMS 0.10. Unlike the old gem, mine outputs JSON:API (common in Ember projects). I hope to add the traditional JSON format too. AMS 0.10 makes this easier, since it introduced the concept of "adapters". My gem is super new and doesn't yet cover a lot of serializer functionality, but I'm hopeful about supporting more & more. Feedback is welcome! On the writing side, I've found activerecord-import [2] to be very useful. It batches up INSERTs and UPDATEs and for Postgres it even knows how to use INSERT ON CONFLICT DO UPDATE. I also have a handful of Postgres extensions that use arrays to do fast math/stats calculations. [3, 4, 5] If you are thinking about moving something into C, it's natural to consider a gem with native extensions, but perhaps a Postgres C extension will be even better. [0] https://github.com/DavyJonesLocker/postgres_ext-serializers https://github.com/DavyJonesLocker/postgres_ext-serializers [1] https://github.com/pjungwir/active_model_serializers_pg https://github.com/pjungwir/active_model_serializers_pg [2] https://github.com/zdennis/activerecord-import https://github.com/zdennis/activerecord-import [3] https://github.com/pjungwir/aggs_for_arrays https://github.com/pjungwir/aggs_for_arrays [4] https://github.com/pjungwir/aggs_for_vecs https://github.com/pjungwir/aggs_for_vecs [5] https://github.com/pjungwir/floatvec https://github.com/pjungwir/floatvec
- caseyohara 7y agoOne way to mitigate spreading logic between SQL and Ruby is by using SQL views with something like Scenic [0] which can back an ActiveRecord model just like an ordinary table. This is especially nice for complex reporting because it centralizes your query logic in a single view as a sort of virtual table that can be queried from both ActiveRecord and normal Postgres queries. [0] https://github.com/scenic-views/scenic https://github.com/scenic-views/scenic
- ramraj07 7y ago
- peter_retief 7y agoI miss RoR, I think I will churn out a project for old times sake!
- crispyporkbites 7y agoRails 6.0 is excellent. For single developer and small team web apps it's pretty much the pinnacle of productivity.
- peter_retief 7y agoThanks for that, nice to know that its still relevant and productive!
- Nextgrid 7y agoHow does this compare to Django? Would it be worth it for me to learn Rails?
- vinceguidry 7y agoDepends on what you're looking for and how much time you want to put into it. What Rails does, nothing else even holds a candle to. But it's hard-won mastery. Rails, when learned properly, enables complete and total dominion over the entire sphere of web development. Nothing else compares, nothing else even comes close. Anything you'd ever want to do with the web, save the kind of scaling that led Twitter to replatform 5 years in, can be done 10x faster and 10x more reliably with Rails. The main bottleneck is understanding. There's a zen to Rails that you have to appreciate before you can unlock its potential. It greatly saddens me that Ruby and Rails has been falling out of favor.
- gray_-_wolf 7y agoHm or maybe for APIs just replace the whole thing with sinatra and sequel. Most of advices still apply in the article still apply though.
- dwheeler 7y agoThis is a really good post. That said, I would have listed caches first. Caches are the real key to excellent performance in Ruby on Rails.
- BilalBudhani 7y agoRuby On Rails is an amazing piece of software. I love the fact that solving problems in Ruby so intuitive & expressive - You just have to focus on building the solution rather than constantly cracking the language. I wrote a post how why one should still learn Ruby - https://bilalbudhani.com/why-you-should-learn-ruby-regardless-of-what-they-say/ https://bilalbudhani.com/why-you-should-learn-ruby-regardles...
- gingerlime 7y agoOne comment about the caching section. Rails is generally really clever getting the cache key for you automatically. Instead of doing Rails.cache.fetch("blog_#{blog.id}_posts_#{posts.max(:updated_at)}") do blog.posts.as_json end You can (and should) actually use something like Rails.cache.fetch(Post.all) do blog.posts.as_json end Or even specific scopes, e.g. Rails.cache.fetch(blog.posts.published) do blog.posts.published.as_json end The cache key will include the query hash, item count in the results and the max updated_at automatically for you. In some rare cases, you might want more control and then probably best to use something like `ActiveSupport::Cache.expand_cache_key`[0] e.g. key = ActiveSupport::Cache.expand_cache_key(["blog posts", blog.posts]) Rails.cache.fetch(key) do ... end [0] https://api.rubyonrails.org/classes/ActiveSupport/Cache.html#method-c-expand_cache_key https://api.rubyonrails.org/classes/ActiveSupport/Cache.html...
- stephen 7y agoFwiw I never really got this approach to caching b/c the keys are always based on calling the db, and calling the db is usually what you're specifically trying to avoid with caching, not just the transform of db content to json/html, which should be cheap?
- ninkendo 7y agoThe active record queries like User.all are lazily loaded, so it’s totally possible to pass them around like keys without them being executed. That said, I wouldn’t use a query as a cache key since there may be more than one thing I want to cache about that query (GP’s example is their as_json representation, but what if I wanted something different to be computed? Like maybe html snippets? I would expect the cache key to mention everything about the thing it’s caching.)
- gingerlime 7y ago> what if I wanted something different to be computed? Like maybe html snippets? I would expect the cache key to mention everything about the thing it’s caching. This is exactly where you'd use something like ActiveSupport::Cache.expand_cache_key(["blog posts", blog.posts]). You can add bits of info to your cache keys to distinguish them from one another. It's probably also worth mentioning that the `cache` view helper does parts of it for you already, and takes into account the digest of the view code itself among other things. Rails provides a really neat abstraction here that does make caching easier. It has some rough edges, and some gotchas, but overall it's pretty smart.
- faebi 7y agoA long time ago I made "The complete guide to rails performance" by Nate Perkobec. It's not free but it really taught my a lot about on the ways to achieve very good performance with Ruby and Rails. Does anybody have any good resources on how to achieve better performance with ruby and something like async, concurrent ruby, threadpools and forking?
- durkie 7y agowhat are modern practices for scaling rails apps when the database is the bottleneck? I have a GIS-heavy app that is built with rails, but leans on PostGIS a lot for the heavy computation. Simply getting bigger and bigger database instances doesn't seem to scale very well, and moving the GIS computation code in to ruby is not an option either (RGeo is good for some things, but can't replace PostGIS for most of my needs)
- bluedino 7y agoHave you read this: http://s3.cleverelephant.ca/2017-cdb-postgis.pdf http://s3.cleverelephant.ca/2017-cdb-postgis.pdf
- durkie 7y agoYes -- it's a good talk, but it focuses mostly on tricks you can do (spatial indexing, geometry subdividing) on the PostGIS side to make lookups/calculations faster, but discussion of actually scaling to multiple databases is a bit light. There is mention of Citus (which Microsoft has purchased and rebranded as Hyperscale), but it has a minimum cost that is more than I can afford right now. I didn't know if there were other architectures to consider for rails -- like having a few read replicas for web traffic, and then a single large writable master for sidekiq/background jobs, for instance.
- jacobkg 7y agoRead replicas is a good way to go. Rails 6 has good built-in support for them: https://www.citusdata.com/blog/2019/05/23/rails-6-multiple-databases/ https://www.citusdata.com/blog/2019/05/23/rails-6-multiple-d...
- malyk 7y agoPostgres 10 introduced native table partitioning and it became much easier to use in postgres 11. This will let you divide large tables into smaller ones that can be queried more efficiently Rails 6 introduced multiple databases natively in activerecord. You can more easily have read only databases or segment off high write workloads. Foreign tables are a way to allow bringing in data from multiple databases into one so it “looks” like everything is in a single db. This got better/faster, particularly with joins in postgres 12. Also consider things like materialized views or even plain views which can help bring together just the right data you need ahead of time. Just a couple options off the top of my head.
- raintrees 7y agoI vaguely remember an article about a fatal flaw in Ruby on Rails that was said to be not fixable? Is that ancient history and has been overcome, or was it not even a thing and I read bogus information? I have developed in Django and avoided RoR due to this possibility, but am always interested in learning more, provided the tools have a decent shelf life...
- castwide 7y agoYou might be thinking of an old SQL injection vulnerability that allowed updating models from unchecked request parameters, aka the mass assignment problem[0]. That type of thing can be a concern in any web application, regardless of the framework. Modern Rails does a decent job of discouraging it out of the box. [0] https://arstechnica.com/information-technology/2012/03/hacker-commandeers-github-to-prove-vuln-in-ruby/ https://arstechnica.com/information-technology/2012/03/hacke...
- breatheoften 7y agoI’m forced to learn rails at a new job — and I really dislike it. There’s so much magic and things are built in an obscure/extremely inefficient way to work around/“take advantage” of frameworks limitations/features. There is so much code that is not part of the codebase driving the behavior of the system — and that third party code is very tightly coupled to the actual behavior that will be observed by running the code. Figuring how anything actually works is made massively harder than required and developers are encouraged towards designs that will require ridiculously inefficient database interactions. Rails is terrible I think for experienced developers because there are no mechanisms in the codebase — instead there are layers and layers of “conventions” which often only exist to try to avoid bugs that would’ve been much better to catch or prevent with a combination of a type system, high level tests, and less mutation.
- etaioinshrdlu 7y agoThis is a decent criticism of Rails, but there are things you can do to make it smoother: try to avoid doing unusual things. A big point of using a framework is to leverage an ecosystem other people's libraries (and stackoverflow questions!), and they will typically only work well if your code is relatively "normal". If your code looks like "beginner" code in the framework, you're doing it right. As a point of comparison, I switched to Django for a few years after a few years of Rails. Django was a lot less magical and was more debuggable. But Rails' magic actually encourages a better project structure. Django doesn't care about your structure. I also found the thoroughness and completeness of the ecosystem surrounding Rails to be stronger. That is the real selling point to me. For most things you want to do in Rails, you will find a library that integrates with the project and DB structure you already have.
- srazzaque 7y agoDisclaimer: I haven't used Rails since around 3, but my assumption is the DX hasn't changed too much. Plus if Rails N was marketed at Rails "N-1" devs, then new devs coming in late have a lot of learning to do. > avoid doing unusual things If by "unusual" you mean "not the Rails way", then this makes sense. But the problem is, the Rails way doesn't always line up with the way an experienced programmer might want to do something. In the specific example of fetching data from a DB - an experienced programmer might intuitively avoid multiple round-trips to the DB, and want to find the appropriate hook point(s) in a request handler to optimise this. Making 20 requests for 20 models is "unusual". Rails doesn't make things like this easy. It's quite opinionated on how you should work. The core assumption in play here is that databases will either be fast, or can be made faster, essentially externalising the problem. Or if you want to address this - find a Gem to do it, or dive into the layered guts of it to figure it out. Related - if I recall correctly, foreign keys were not deemed an important enough feature for inclusion in Active Record, and the Rails way is to embed the logic for joins into your models rather than the database. I'm sure many of us here could name several scenarios where this is just plain wrong.