davideisinger.com

My personal website
Log | Files | Refs | README

codefol-io-bskabg.txt (16686B)


      1 [1]Codefol.io book logo
      2 
      3 [2]Codefol.io
      4 
      5   • [3]All Articles
      6   • [4]Articles by Topic
      7   • [5]Favourite Articles
      8   • [6]RSS
      9   • [7]About
     10 
     11 When Should You NOT Use Rails?
     12 
     13 A chimpanzee in a white coat types at a keyboard lit by glowing LEDs.
     14 He's gorgeous, yes. But is his talent natural… or is it Ruby on Rails?
     15 
     16 I was [8]recently on Jason Swett’s podcast again. He’s a great interviewer and
     17 I always have fun with him.
     18 
     19 By Twitter request we talked about… When would you not use Rails? It’s a great
     20 question.
     21 
     22 For the entertaining version, [9]listen to the podcast. For the just-the-facts
     23 extra-complete version, I’m writing this post.
     24 
     25 When Is Rails the Wrong Choice?
     26 
     27 I’ll start with a few simple, obvious times you wouldn’t use Rails, and then
     28 I’ll talk about some technically interesting times.
     29 
     30 First, and most important, is team familiarity. If your team doesn’t already
     31 know Rails and isn’t especially interested in learning it then Rails is the
     32 wrong choice. This should be obvious, but it still deserves first billing.
     33 
     34 Second, when you know some other framework fits better. I’ll talk more below
     35 about when that is. But sometimes you have a specific concern that trumps
     36 everything else. If you need to use a Java-language machine learning library
     37 and you don’t want to use JRuby for some reason, Rails isn’t your framework. If
     38 you’re writing a WordPress plugin, you’ll be doing it in PHP. Often there’s one
     39 specific compatibility concern that overrides everything else.
     40 
     41 You can also think of it as: use it where Rails’ good points hold and its bad
     42 points don’t. So we’ll also talk about the good and bad points.
     43 
     44 Separately: you’d normally only use Rails as an HTTP server, so some tasks just
     45 aren’t Rails-shaped.
     46 
     47 When is Rails Too Much?
     48 
     49 [10] A pirate puppet with an eyepatch, safety goggles and a huge scraggly
     50 mustache watches over a purple crystal ball at his feet.' He’s too much lab
     51 assistant for your lab.
     52 
     53 Some places not to use Rails can include:
     54 
     55 Really Small Tasks that Won’t Grow: if a server does very little, Rails is
     56 often too much. Not going to touch a database? Then the DB setup isn’t helping
     57 you, is it? Just a tiny low-traffic intermediate server with no caching? A lot
     58 of Rails is more trouble than it’s worth.
     59 
     60 Be careful with tasks that grow, though — making a tiny server scale up to do a
     61 lot more can be ugly. If you’re already serving HTTP pages to a human with a
     62 web browser, consider that you may have to add features to it later. Something
     63 like that is already fairly large from the word “go”.
     64 
     65 When It’s ‘Just’ an API Server: Rails has less to offer an API server that
     66 speaks JSON over the wire. A lot of its HTTP security doesn’t matter for that
     67 case: CSRF protection is entirely about dealing with HTML and Javascript. Many
     68 kinds of XSS attacks are dependent on a browser as the weak link, or on putting
     69 unescaped user input into HTML. Redirection vulnerabilities assume automatic
     70 redirection, which APIs usually don’t do. You can prevent SQL injection attacks
     71 with just an ORM, a simpler ORM, or even the raw Ruby MySQL and Postgres gems,
     72 which support question-mark arguments.
     73 
     74 Rails security really shines when you’re navigating the bewildering world of
     75 HTML and browser security. Small projects that mostly speak a structured format
     76 read by machines will get less from Rails. Securing something like an integer
     77 ID or a hash of strings is just easier than ensuring your HTML contains no
     78 script tags or anything exploitable.
     79 
     80 Related to that is when you’re doing in-browser rendering and Rails is ‘just’
     81 serving JSON. It’s a weird kind of in-between case. A lot of Rails security and
     82 convenience functions no longer help you, but you’re still doing things where
     83 internal libraries (ActiveRecord, ActiveJob, ActionMailer) can be highly
     84 useful. But if you’re never rendering HTML on the server and you’re very sure
     85 you never will, Rails will probably help you less.
     86 
     87 When Is Rails Not Enough?
     88 
     89 Rails is also designed for a small team and a medium-sized codebase. A huge
     90 team (lots of programmers) or a huge codebase (lots of controllers, models and/
     91 or lines of code) will tend to drag down the standard Rails-app structure.
     92 
     93 Ruby allows for a lot of [11]non-local effects. Whether that’s monkeypatching,
     94 writing to a database or creating new types at runtime, Ruby isn’t designed for
     95 a team of 200 programmers where you don’t trust some of them. There are too
     96 many ways for them to cause you trouble. You can use [12]good tooling to scale
     97 Ruby to larger teams, but even that will [13]tend to have exceptions and
     98 difficulties. That’s not really Ruby’s sweet spot.
     99 
    100 In most cases you can cut up a large project into smaller projects. If one
    101 Rails app is too big, you can often separate it into multiple apps, or a
    102 thinner app with more back-end services, or an app and a separate microservice,
    103 or… One way or another there is usually a way to separate out smaller pieces.
    104 Ruby strongly encourages that, as do I.
    105 
    106 There are also not-quite-Rails structures that can scale better. Avdi Grimm’s
    107 (now retired) [14]Objects on Rails was an attempt in that direction, as is [15]
    108 the Hexagonal architecture for Rails, which in turn has a lot in common with
    109 the older and more general [16]N-tier architecture.
    110 
    111 But at some point you might want to consider a different framework. [17]Hanami
    112 is an obvious choice, designed to be less quick and nimble than Rails for
    113 getting a tiny app off the ground, but more scalable if you want to use the
    114 same code with a lot more contributors.
    115 
    116 I’d still start out in Rails, personally. If you’re building something quickly
    117 to see if anybody cares, I know of no framework that comes close to its
    118 productivity. Wait to rewrite (in a more rigid framework) until you’re
    119 successful and you can afford the drag on your development speed.
    120 
    121 The other worry here can be performance. If you’re rewriting a project that is
    122 already as large as the current Basecamp… then you’re [18]actually fine for
    123 performance. Rails still scales great for them. But if you’re looking at
    124 something a hundred times larger (which by definition means B2C, not B2B) then
    125 you might have a situation where your server costs are substantially greater
    126 than your engineering payroll. In that case it can make sense to slow down your
    127 engineers to pay lower server costs. To check this, see what your
    128 EC2-or-equivalent costs are just for your application servers, which are what
    129 run Rails. And check your payroll just for web engineers, which is who writes
    130 in Rails. Normally the engineering payroll is much larger and you should stick
    131 with trading cheap machine time for expensive engineering time. But at some
    132 point the balance may tip and you should consider raising your engineering
    133 payroll to cut your server costs.
    134 
    135 When Does Rails Have the Wrong Assumptions?
    136 
    137 [19] A pirate, a bear and a chimp sit at a wicker table. The bear looks into a
    138 very old-fashioned microscope as the other two look on. They’re checking the
    139 microscope for real-world use cases where Rails might be wrong.
    140 
    141 Before checking if Rails’ assumptions are right for you, we should see what
    142 those assumptions actually are.
    143 
    144 Before you take my word for it, I recommend taking [20]David Heinemeier
    145 Hansson’s word for it in the form of The Rails Doctrine. It’s a great document
    146 and it covers a lot of ground.
    147 
    148 Indeed, if you want to better understand why Rails isn’t amazing for large,
    149 low-trust teams, you should read [21]“Provide Sharp Knives” in the Rails
    150 Doctrine several times. A lot of Rails’ tradeoffs are entirely by design.
    151 
    152 Rails also has some simpler assumptions: it assumes you’re writing an
    153 interactive app with server-rendered HTML. It assumes that security is vital
    154 (Rails trades a lot for security) but that you don’t want to build your own
    155 custom security system in most cases. And it assumes that you either have a
    156 small, excellent team doing prototyping work (“Provide Sharp Knives”) or that
    157 you have a possibly-mediocre team that needs powerful built-in guidelines ([22]
    158 “The Menu is Omakase.”)
    159 
    160 Rails also assumes you want high developer velocity at a cost of technical
    161 debt. In other words, it’s designed for building very quickly. That makes sense
    162 when technical execution is not your biggest risk. For instance: if you’re
    163 building a small startup, and you’re pretty sure you can build the site but
    164 people may not buy your product, you are dominated by market risk. That’s when
    165 Rails is perfect. You want to build very quickly. And even if you build
    166 perfectly, you’re probably going to have to throw away the result for
    167 nontechnical reasons, like “people don’t want to buy it.”
    168 
    169 As part of “high dev velocity, technical debt is okay” Rails assumes things
    170 like, “you’ll want to use a lot of gems” and “dependencies that work are fine
    171 if they speed you up.”
    172 
    173 Rails assumes you don’t mind scaling out application servers horizontally (by
    174 bringing more of them online.) It’s designed to scale well if you can do that.
    175 Rails assumes CPU is fairly cheap and it’s usually right about that. Relatedly,
    176 Rails assumes that the database is usually your most serious performance
    177 bottleneck, which is how web applications usually work.
    178 
    179 Rails also assumes you’ll have some calculation or data transformation in your
    180 application. It assumes that it’s okay to use some CPU because you’ll be doing
    181 that anyway.
    182 
    183 (When does that last assumption matter? Let’s talk about Evented Servers and
    184 see.)
    185 
    186 What Isn’t Rails Good At?
    187 
    188 [23] The Node.js logo. Sometimes you need it, or something like it.
    189 
    190 While Rails is great at a lot of things, there’s one particular task that it’s
    191 not amazing for: shim servers.
    192 
    193 By “shim servers” I mean servers that do very little calculation but integrate
    194 answers from a few other back-end services and relay the result. Imagine a
    195 server that queries two JSON services and combines the result with simple
    196 string-manipulation, for instance. It does very little calculation, but it
    197 juggles a lot of events.
    198 
    199 And that’s the relevant word: “events.”
    200 
    201 There is a specific kind of app architecture embodied by Node.js and its
    202 relatives called “Evented” programming. It can support many thousands, or even
    203 millions, of simultaneous connections with a tiny amount of server resources.
    204 It can be both high-throughput and low-latency. Its benchmark numbers are
    205 matchless… for the cases where it works.
    206 
    207 Rails can’t match Evented programming at what Evented programming is good at.
    208 Basically no framework can. There are Evented frameworks for Ruby (e.g. [24]
    209 EventMachine, [25]Async.) Rails is built differently.
    210 
    211 If Evented is so much better, why don’t we use it for everything? Because it
    212 doesn’t work for everything. I emphasise calculation per-request because an
    213 Evented server will fall down and die if you try to make it do very much
    214 calculation per-request. Having one server handle a million connections is no
    215 good if each connection winds up using a hundred milliseconds of CPU time —
    216 that’s simply too many connections and the latency will be terrible.
    217 
    218 In other words, Rails and Node.js are different tools for different projects.
    219 If you’re thinking, “I should either use Rails or Node for this” I would
    220 recommend looking deeper into your project (and/or your framework) until it’s
    221 obvious which one is the right answer. They do different things.
    222 
    223 Look, I Just Scroll to the Bottom for the Summary and Criticise It On Reddit
    224 
    225 [26] A chimpanzee in a lab coat stares down at his lap, containing sections of
    226 wooden train track, and his hand holding a toy train. I’m sure this is the
    227 right answer, but I have forgotten the question.
    228 
    229 Rails is the wrong choice if your team doesn’t want to use it or doesn’t know
    230 how.
    231 
    232 Rails is the wrong choice in cases where a different framework is specifically
    233 better, or you have a specific library you need to be compatible with that
    234 isn’t Rails-friendly.
    235 
    236 Rails might be the wrong choice if you’re not rendering HTML on the server,
    237 especially if your project is very small and/or doesn’t use a database.
    238 
    239 Rails is the wrong choice is you’re not doing prototyping-flavoured work,
    240 preferably with a small, highly-competent team.
    241 
    242 Rails is the wrong choice if your dev team or your app code is too big and you
    243 can’t subdivide the project.
    244 
    245 Rails is the wrong choice if your project wants an Evented server like Node.js
    246 or EventMachine.
    247 
    248 This article is the wrong choice if you’d rather [27]listen to an entertaining
    249 podcast on the same topic.
    250 
    251 If you’re wondering when Rails is the right choice, [28]the Rails Doctrine is a
    252 great first step.
    253 
    254 Aug 6 2020
    255 [29]ruby [30]rails
    256 
    257 You Hunger to Get Better
    258 
    259 Subscribe to get free ebook chapters and an emailed coding class now, plus
    260 videos and articles a few times a month.
    261 
    262 Why this specific newsletter? You want to be an expert. Expertise comes from
    263 learning the fundamentals, deeply. And that comes from the best kind of
    264 practice. I write with that in mind. I won't waste your time.
    265 
    266 (Yes, I also sell things. They're good, but I'm fine if you don't buy them.)
    267 
    268 [32][                    ][33][                    ] Sign-up
    269 Comments
    270 
    271 Please enable JavaScript to view the [35]comments powered by Disqus. [36]
    272 comments powered by Disqus
    273 
    274 Read Next
    275 
    276 [37]Computer Science: Just the Useful Bits
    277 
    278 [38]ruby [39]rails
    279 
    280 [40] Computer Science: Just the Useful Bits
    281 
    282 [41]Let's Build Course Software: Email Reminders
    283 
    284 [42]ruby [43]rails [44]letsbuild [45]rubymadscience
    285 
    286 [46] Let's Build Course Software: Email Reminders
    287 
    288 [47]Free Rebuilding Rails Video Chapters
    289 
    290 [48]career
    291 
    292 [49]The Urban Legend of the 10X Developer
    293 
    294 [50]career
    295 
    296 [51] The Urban Legend of the 10X Developer
    297 
    298 [52] Mastodon [53] GitHub [54] Linkedin [55]Mastodon
    299 
    300 [56]Codefol.io © 2020 . Horace theme by [57]JustGoodThemes..
    301 
    302 [58] Back to top
    303 
    304 
    305 References:
    306 
    307 [1] https://codefol.io/
    308 [2] https://codefol.io/
    309 [3] https://codefol.io/posts
    310 [4] https://codefol.io/topics
    311 [5] https://codefol.io/tags/favourite
    312 [6] https://codefol.io/feed.xml
    313 [7] https://codefol.io/about
    314 [8] https://www.codewithjason.com/rails-with-jason-podcast/noah-gibbs-3/
    315 [9] https://www.codewithjason.com/rails-with-jason-podcast/noah-gibbs-3/
    316 [10] https://rubymadscience.com/img/assistant_pirate_with_sphere_bigthumb.png
    317 [11] https://en.wikipedia.org/wiki/Side_effect_(computer_science)
    318 [12] https://sorbet.org/
    319 [13] https://sorbet.org/docs/troubleshooting#escape-hatches
    320 [14] https://www.goodreads.com/book/show/13481927-objects-on-rails
    321 [15] https://medium.com/@vsavkin/hexagonal-architecture-for-rails-developers-8b1fee64a613
    322 [16] https://en.wikipedia.org/wiki/Multitier_architecture
    323 [17] https://hanamirb.org/
    324 [18] https://m.signalvnoise.com/only-15-of-the-basecamp-operations-budget-is-spent-on-ruby/
    325 [19] https://rubymadscience.com/img/dr_bear_microscope_bigthumb.png
    326 [20] https://rubyonrails.org/doctrine/
    327 [21] https://rubyonrails.org/doctrine/#provide-sharp-knives
    328 [22] https://rubyonrails.org/doctrine/#omakase
    329 [23] https://codefol.io/posts/when-should-you-not-use-rails/node_js_logo.png
    330 [24] https://github.com/eventmachine/eventmachine
    331 [25] https://github.com/socketry/async
    332 [26] http://rubymadscience.com/img/rails_internals_bigthumb.png
    333 [27] https://www.codewithjason.com/rails-with-jason-podcast/noah-gibbs-3/
    334 [28] https://rubyonrails.org/doctrine/
    335 [29] https://codefol.io/tags/ruby/
    336 [30] https://codefol.io/tags/rails/
    337 [35] http://disqus.com/?ref_noscript
    338 [36] http://disqus.com/
    339 [37] https://codefol.io/posts/introducing-computer-science-just-the-useful-bits/
    340 [38] https://codefol.io/tags/ruby/
    341 [39] https://codefol.io/tags/rails/
    342 [40] https://codefol.io/posts/introducing-computer-science-just-the-useful-bits/
    343 [41] https://codefol.io/posts/series-build-coding-course-email-reminders/
    344 [42] https://codefol.io/tags/ruby/
    345 [43] https://codefol.io/tags/rails/
    346 [44] https://codefol.io/tags/letsbuild/
    347 [45] https://codefol.io/tags/rubymadscience/
    348 [46] https://codefol.io/posts/series-build-coding-course-email-reminders/
    349 [47] https://codefol.io/posts/free-rr-video-chapters/
    350 [48] https://codefol.io/tags/career/
    351 [49] https://codefol.io/posts/urban-legend-of-the-10x-developer/
    352 [50] https://codefol.io/tags/career/
    353 [51] https://codefol.io/posts/urban-legend-of-the-10x-developer/
    354 [52] https://ruby.social/@codefolio
    355 [53] https://github.com/noahgibbs
    356 [54] https://www.linkedin.com/in/noahgibbs
    357 [55] https://ruby.social/@codefolio
    358 [56] https://codefol.io/posts/when-should-you-not-use-rails/#
    359 [57] https://justgoodthemes.com/
    360 [58] https://codefol.io/posts/when-should-you-not-use-rails/#page