14 ms·
It's not Ruby that's slow, it's your database
- revskill 4y agoSo, in real world, i will just cache every thing with redis, and problem solved ?
- saybar 4y agoOr use PolyScale and get the cache without changing the code.
- likeabbas 4y agoNo, Ruby is slow. Its GC is ungodly slow. And you can’t truly multithread, thus you can’t properly parallelize or maximize concurrency.
- joshmn 4y agoHow many developers will really need it though? It's fast enough for Stripe's API, it's fast enough for Shopify; hardware in exchange for productivity is a pretty fair trade.
- likeabbas 4y agoStripe’s API is one of the slowest of any payment processor, and it’s a major source of contention within the company
- favorited 4y agoExcept most companies won't attract the same top-tier Ruby/Rails talent. For example, it looks like 1/3 of the current Rails Core Team work at Shopify, including the person who is the #1 all-time Rails contributor.
- JohnBooty 4y agoI may be misunderstanding you, but can you elaborate on why you believe you need "top-tier" Ruby/Rails talent to run a performant Rails application? In my experience, you need no more than mid-level talent. You need a certain % of people who are strong at working with databases to ensure good (normalized, indexed, etc) database design. You also need a certain level of organizational will to avoid letting a Rails monolith get absolutely out of control, but this is less "a need for top-level talent" and more "just stick to boring old Ruby/Rails best practices." Those are, of course, things you need when working with any stack. I don't find them to be Ruby/Rails specific.
- byroot 4y agoRails Core team member working at Shopify here. You seem to be misinterpreting what we’re doing at Shopify. We pretty much never work on the product or anything like that, and we don’t go around the app chasing perf issues either. At best we work on the infra or dev tools to encourage or enforce better patterns, or sometimes in some kind of support capacity for teams having Ruby or Rails specific issues, but Shopify would do fine without us. We’re mostly here to ensure Rails and the Ruby ecosystem Shopify heavily depends on is maintained and healthy. Now maybe you need to tier talent to scale a Rails infra (I don’t think so), but Shopify’s presence in the Rails or Ruby core teams is no indication of that.
- clintonb 4y ago> It's fast enough for Stripe's API... Stripe is moving to Java.
- Mikeb85 4y agoEvery large enough organisation eventually moves to Java (or C++, or Rust). When you have more bodies than things to do you just optimize... But how did all these startups get so big?
- JohnBooty 4y agoIf you need maximum requests per second for sufficiently high volumes of traffic - ie, Stripe/Twitter/FB/whatever stuff - then yeah Ruby/Python/etc doesn't cut it. If your needs get extreme enough (high frequency trading?) then Java doesn't cut it either. Most (I suspect > 99%) use cases don't fit these criteria. I know this is skewed a perception is skewed a little by the HN crowd, where many folks legitimately are trying to build the next Stripe or Twitter, but I think the world of modern software development is somewhat poisoned by this belief that we all need to follow FAANG-scale practices and architectures.
- kaba0 4y agoJava is used in HFT, and for the variant where it is not fast enough, general purpose CPUs are not fast enough so not even hand-written assembly can compete anymore. They use FPGAs for that kind.
- likeabbas 4y agoJava is used in HFT for markets that have a time window, but only because they tune the GC to never kick in during market hours
- drogus 4y agoBoth Stripe and Shopify have more resources than an average small startup
- treeman79 4y agoRuby web guy for 15 years. 95% of the time the problem is poor database choices. Usually to much contention / locks. On the rare projects where I need something like rabbitmq performance. I don’t use ruby.
- KptMarchewa 4y agoJust use optimistic locking on all the things bro
- marginalia_nu 4y agoIt can be argued that if it's mostly a glue language translating requests into db queries and db responses into HTML it doesn't need to be blazingly fast. Although that does rather define the shape of the application to look a certain way. You can certainly do a lot more with a faster backend language. Not everything translates well to a database query. Throwing more hardware at the problem doesn't get around this.
- gedy 4y agoYeah antidotal, but I was doing some tests of an existing Rails app API compared to Hasura (erlang GraphQL server) talking to same DB, and querying same data was like 100x faster in Hasura..
- mberning 4y agoIt depends on the workload. You absolutely can parallelize many processes using tools like Sidekiq. My single threaded ruby app might take hours to query data sequentially from an API. But with sidekiq suddenly you have to throttle your concurrent processes because the API is breaking under the stress.
- Mikeb85 4y ago> No, Ruby is slow. Its GC is ungodly slow. Ruby is quicker than Python and that's the most popular programming language there is at the moment. > And you can’t truly multithread, thus you can’t properly parallelize or maximize concurrency You definitely can. You've been able to fork processes for longer than I can remember. Ractors just became a thing, making multithreading cheaper and easier. Ruby's also had multithreaded servers for at least a decade. Not that it even matters that much, most people are using on-demand cloud servers that just spin up more servers when needed, making multi-threading less useful anyway. Even if you forget Ractors exist, it's like saying C can't do multithreading because it's not in the language definition...
- likeabbas 4y ago> Ruby is quicker than Python Python is slow as shit too. I would never use either language in a user path production environment. > fork processes Forking a process takes upwards of 50ms vs forking a thread is in microseconds. That might not seem like a lot, but a use will be able to tell the difference between a 50ms page load time and a 150ms page load time. Also, multi processing is not optimal for sharing memory like a local cache. My ultimate point is there’s truly no reason to use these languages when Java, Go, and Rust are around
- BeefWellington 4y agoRuby is quicker than Python and that's the most popular programming language there is at the moment. Do you have any reputable sources for this? All I can find are benchmarks run by companies with clear vested interests in showing one language is faster than the other. Also, this isn't quite the strongest argument for its performance; popularity hasn't been tied to performance for a long time (see: all those years NodeJS was leading language popularity lists, or Java).
- Mikeb85 4y agohttps://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/ruby-python3.html https://benchmarksgame-team.pages.debian.net/benchmarksgame/... Of course, in the real world it doesn't really matter. Both are used in places where performance doesn't really matter as long as they're fast enough. But anyone with experience with both knows they're both slow (well, some Python fanboys try to pretend otherwise).
- pdntspa 4y agoJRuby does not have GIL and offers true multithreading
- thom 4y agoI feel bad that I’ve forever tarred JRuby with the atrociously slow performance of ThoughtWorks’ Mingle. If you think Jura’s sluggish…
- berkes 4y agoI deliberately startd my article with a few paragraphs showing that Ruby indeed is very slow. > Let's be clear: ruby is slow. The garbage collector, JIT compiler, its highly dynamic nature, the ability to change the code runtime and so on, all add up to a sluggish language. There's no argument there. The main point, however, is that in many cases (where Ruby is used) this hardly matters, because other stuff than Ruby is the bottleneck. The database being the most obvious culprit.
- Tainnor 4y agoI don't understand why the author first describes how bad N+1 queries are and then later claims that they are sometimes good? Yes, of course that can be true in very specific circumstances, but in many cases I've seen atrocious performance due to N+1 queries and fixing them was the first step in making an unresponsive website perform. Don't also really agree that adding validations, joins etc. to your DB is "coupling your application logic to the DB" or that it makes the app slower (???). The thing that is coupling your business logic to the DB is a bad architecture which, unfortunately, most ORMs (including ActiveRecord) encourage. The fact that you can access a propery on an ActiveRecord object in a view and have this secretly make a database call, is one of the reasons for those infamous N+1s.
- brightball 4y agoThe thing about Ruby is there are plenty of gems that will help protect your code base from many of the performance hiccups. Off the cuff: bullet, strong_migrations, activerecord-import, lol_dba I'm sure there are more. This has been a bit of a passion point for me for a few years. I even taught a class on it. Ruby is a wonderful language to use with a database. ActiveRecord too...you just have to know how to avoid some of the foot guns. Many Rails devs get committed to doing things "the Rails way" to ship faster and focus on application level logic, but even the ActiveRecord guides say that when it's time to performance tune you're going to have to get into the database innards. ActiveRecord is geared towards team productivity. It's perfectly capable of great efficiency too, you just have to be more deliberate about it. The "scopes" structure is wonderful for piecing together reusable parts of queries though. It makes leveraging the fancy parts much easier.
- Tainnor 4y agoLibraries are great, but they're not a fix for a broken architecture. It's been quite a while that I haven't worked with in Ruby on Rails, but the fact that "architecture" was often considered a dirty word and the "Rails way" was touted as the solution to all problems in my experience often led to systems where everything was coupled to everything. > ActiveRecord is geared towards team productivity. ActiveRecord is geared towards making things easy at the start. The problem is that every ActiveRecord object always carries a dependency to the DB around, making it difficult to enforce a clean separation between different layers of your system.
- rahimnathwani 4y ago"Keep all logic out of the database. It already is the slowest point. And hardest to scale up." I don't think this is true in all cases. Sure, ORMs are awesome, but sometimes you need to write SQL queries by hand, and those queries necessarily implement some business logic (even if they're just retrieving data). Nice explanation here: https://tapoueh.org/blog/2017/06/sql-and-business-logic/ https://tapoueh.org/blog/2017/06/sql-and-business-logic/
- smt88 4y agoYeah, this is bad advice. For example, Postgres has fantastic "upsert" and data integrity checks that reduce round trips to the database, meaning they're incredibly fast and result in less-complex client code.
- lmm 4y agoThere is always an alternative to writing SQL queries by hand, and it's usually a better one IME. Any ORM worth its salt will let you do the query in your blog post via the ORM, as a single query.
- rahimnathwani 4y ago"Say now we want to display the album list sorted by album’s duration, shortest first." How would we do this in an ORM like ActiveRecord or Django ORM, without generating multiple queries?
- strken 4y agoI haven't used Rails or ActiveRecord for a long time, but from memory it gives you an escape hatch in the form of Arel that lets you represent parts of your query as SQL while still using the ORM for the rest. Edit: I think it's something like Album .select(:id, :name, "SUM(songs.length) AS total_length") .left_outer_joins(:songs) .group(:id) .order("SUM(songs.length) ASC") I think you don't even need to explicitly wrap it in Arel.
- 4y ago
- jojobas 4y agoFrom my experience with Python and Ruby, it's not the database itself, it's the database driver that spends quite some time on serialization and deserialization every time you want to send around some data, even more so when you pack some objects to store as semistructured/json/xml. So no, it's not the database, it's your interpreted language.
- yoro46 4y agoLanguages aren't interpreted, implementations are.
- jojobas 4y agoIt's safe to call a language for which there's no production compiler an interpreted language.
- falcolas 4y agoWell, that’s neither Python nor Ruby. Both compile their code, and can do so ahead of time too.
- FridgeSeal 4y agoNone of the major Python implementations do any AoT compilation. Python is probably the poster-child language of "interpreted". It's run, line-by-line through an interpreter, and turned into byte-code, which is executed by the VM. There's no compilation, and in CPython, very little optimisation. PyPy is obviously different on that last point as it's a JIT, but the larger point still stands.
- segfaultbuserr 4y ago> It's run, line-by-line through an interpreter, and turned into byte-code, which is executed by the VM. Just a small technical correction: you can pre-complie to .py source files to .pyc bytecode files without waiting for the interpreter to do it line by line, many distro package managers already perform this step automatically when a package is installed. Though you're right in the sense that it doesn't really improve performance, CPython itself is the slowest part, not bytecode generation. > There's no compilation, and in CPython, very little optimisation. +1. This was actually a deliberate design choice [0]. Guido van Rossum himself said, "Python is about having the simplest, dumbest compiler imaginable." CPython was designed for ease of learning and use, it was never designed to be a high-performance language. Pure Python code (without C extensions like NumPy) can never be good at number-crunching tasks. [0] https://nullprogram.com/blog/2019/02/24/ https://nullprogram.com/blog/2019/02/24/
- mrinterweb 4y agoIn my experience 9/10 web application performance problems are related to database interactions. Is the query using an index? Are you hammering your DB with 300 N+1 queries to render an API response. These are usually the biggest offenders when poor performance is observed. If you're interactions with you DB are bad, it doesn't matter what programming language you use, performance will be mostly equally awful regardless of language.
- pixl97 4y agoBad indexing, no indexing, and fragmented indexes seem to cause no end of issues, and there always seem to be shortages of people capable of fixing it properly.
- keithnz 4y agonot to mention ORMs that query a lot of data through non ideal joins.
- imchillyb 4y agoIn order to fix a problem, one must be able to first identify the problem. Many people are still unaware of how indexing works, even though the devices they utilize daily perform this action routinely.
- riwsky 4y agoThey’re out there—but I just ran EXPLAIN ANALYZE on my company’s search for them, and the query planner’s not too happy. It has to scan the entire resume heap before doing an on-site join with candidates, all while dealing with resource contention from queries of other employers. I know recruiters and e.g. triplebyte, stackoverflow all offer indexes to speed up this search, but in our case those indexes wouldn’t fit in cash.
- ColonelPhantom 4y agoWhy does the index need to fit in "cash" (cache RAM)? Generally an index on disk, especially SSD, is far faster than traversing the entire dataset, because it allows the query executor to quickly narrow it down. Even if this requires some disk IO, it's a lot faster than doing all the disk IO for the entire dataset.
- drewbug01 4y agoI think my favorite part is buried in footnote 5: > Ironically, the performance issue becomes less articulated in this non-http, non-rails context, yet in these cases people generally dismiss ruby as option, for its performance-issues. Which, catch-22, is one of the reasons Ruby is hardly used outside of Rails (and/or Web). It's a shame that most people only write Ruby in the context of Rails. It's a lovely language, and performant enough for a wide array of tasks. It can be startlingly elegant and exceedingly productive. Of course, it's valid to talk about Ruby performance in the context of Rails, as the author does. However, it's gratifying to see a more detailed discussion on what Ruby folks mean when they say "your database is usually the bottleneck," and the author does a good job at examining different aspects. (All that said: Rails is still a really good choice. So much of our work is just writing CRUD apps, and it's kinda boring. Rails makes it boring and easy, and it's a really good tradeoff.)
- spoiler 4y ago> It's a shame that most people only write Ruby in the context of Rails I fully agree with this! I used to use Rails for a few years (not professionally, FWIW), but didn't enjoy Rails that much. Even though, it's hands down the best "in class" framework, it beats anything in the JS or Python ecosystems (and claiming Django is anything close to Rails is just offensive to Rails). But anyway, I've yet to find something that's as good as Ruby for daily scripts or task automation. Ruby is one of those few languages you can just read (I can't explain it well), and coming back to old Ruby code, the "wtf is going on here" moments are fairly scarce (sans code that touches metaprogramming/eigenclasses/DSLs, but even then it's more straightforward than in other languages). Lately I tend to just use Fish for most things (which are simple), but still reach for Ruby when the task is a bit more involved. Like, yeah there's Python, but I never liked Python. That's not to say Ruby isn't without any warts, but that's just... Technology, I guess lol.
- drewbug01 4y agoI don’t have some kind of grand, unified theory about this - but the people I know who genuinely enjoy Ruby on its merits seem to think about code differently. Not different-as-in-bad, but just… different. I too find it hard to explain. > Ruby is one of those few languages you can just read I think this has something to do with it, though. The stdlib has a lot of different ways to “say” the same thing (TIMTOWTDI, anyone?) and used well it can be quite legible. But some programmers I know find that really frustrating. Maybe it has something to do with how one visually processes and reads code? How one might associate semantic meaning to things? I don’t know how, but it feels like there’s something interesting there to study.
- more_corn 4y ago<Screams in Active Record> The default ORM in Ruby… not great at scale. I had a large client with moderate traffic, they were slow because they were absolutely crushing their DB. Now granted it was not JUST ruby, but ruby was a big part of it. We profiled their slow queries and found stuff no human would think is sane. Active record is great for the developer and terrible for database optimization. Which is fine until you try to scale. Then you fall over on your face. Ruby is slow because it constructs retarded database queries that no human would ever use. Think ten lines long and multiple joins.
- JohnBooty 4y agoThe question is: is ActiveRecord worse at query generation than other ORMs? My answer is no, it is not. In my experience ORMs are all pretty much equal at this. (However, I would love to be wrong!) I don't know of any ORM that intelligently analyzes your database and comes up with highly optimized queries. I'm not sure such a thing is possible. The database's own query analyzer can optimize query execution because it has intimate knowledge of the database's own data structures, indexes, data cardinality, etc. I accept ORMs as a necessary evil for most work. However, I enjoy ActiveRecord because unlike some ORMs it makes it fairly easy to simply use raw handwritten SQL when needed.
- slimdave 4y ago
- riwsky 4y agoDatabase: slow. Ruby: not slow. Our next steps are clear: we must reimplement the database using ruby.
- knodi123 4y agoI already did that. I decided to keep things simple, so I just built a web app that proxies raw SQL to mysql and returns the results. So it's even better - your database is written in ruby AND has an http json api for wider compatibility!
- thorncorona 4y agohttps://archive.ph/A8DYB https://archive.ph/A8DYB
- bvirb 4y agoThe Sequel ORM (mentioned in this article) is absolutely amazing. There is a plugin for Rails to swap out ActiveRecord with it as well. It does a good job of letting the DB do its job. Like you can define complex constraints on tables in your database and then the corresponding models will automatically detect them and have the related app-level validations. So you can keep more data integrity enforcement in the database where your transactional guarantees are but still have nice error messages in your app without duplication. If you're looping over a collection of records it can detect if you call an association method on one of them and load the same association for all the records you're looping over in a single query, avoiding most N+1 issues. Lots more cool stuff too. I haven't used ActiveRecord in quite a while, so I'm not sure whether it has since absorbed some of the Sequel behavior, but it's definitely worth a look.
- twotwotwo 4y agoThis is kind of relevant now: Mastodon is seeing a lot of growth all the sudden, the popular server for it is in Rails, and scaling up quickly is a challenge for server operators used to the levels of traffic from two months ago. Twitter did rewrite their frontend to JVM languages because it was enough of a bottleneck to be worth it. If you can get Mastodon substantially further by serving some views using something besides Rails, or even going around ActiveRecord in places, that is a pretty different short-term to-do list than if the immediate problems are in the data layer. (Long term you have to scale both of course!)
- kstrauser 4y agoUgh, tell me about it. I had an interesting weekend dealing with traffic loads that grew by about 7x in a few days. I wrote about my misadventures at https://blog.freeradical.zone/post/surviving-thriving-through-2022-11-05-meltdown/ https://blog.freeradical.zone/post/surviving-thriving-throug... . The gist of it is that a RoR “Sidekiq” task queue gets CPU bound after about 25-30 worker threads doing things like making REST API calls to remote servers, querying a database, insert status updates, etc. I can’t help but to think that the equivalent written in Go, or even Ruby-without-Rails, could handle many times the traffic with fewer CPU and RAM resources.
- devoutsalsa 4y agoSounds like a job for MUMPS.
- kstrauser 4y agoYeah, I mentioned Elixir in the post as an example of a better alternative.
- hamandcheese 4y agoDid you actually observe CPU at 100%, or just that your sidekiq workers were falling behind? Solid chance that you are still IO bound and could push past 25 workers. The main bottleneck is probably RAM.
- scottcodie 4y agoBuilding your modern crud app is still incredibly hard to get right while still being performant in any language. IMO, neither ruby nor the database are at fault. - Materialization pipelines are hard, especially ones that need transactional constraints. - Passing async connections from the http request all the way through to the database is hard. - Data distributions can change which can impact indexes, join ordering, and the optimal materialization pipeline.
- rubyist5eva 4y agoI’m a Rails developer by trade, been doing it for over a decade. Bad indexing and lazy N+1 are 90% of the performance problems in a typical rails application. The other 10% is when people do aggregates or joins in application code instead of sql. I absolutely despise ActiveRecord because it makes non-trivial aggregates a pain to write. Sequel is a much better ORM but good luck getting a team on board.
- JohnBooty 4y agoI absolutely despise ActiveRecord because it makes non-trivial aggregates a pain to write I'm a SQL guy at heart. I certainly agree with you... I loathe writing any non-trivial query in ActiveRecord's query builder. However, I find ActiveRecord does a really good job of getting out of the way when I deem it easier to write some raw SQL. Generally do one of two things. If I have a really big nasty query, I'll implement it as a (standard or materialized) database view. I may query it directly or map a model to it. Or, I'll just use `MyModel.find_by_sql("blahblahblah")` which happily accepts my handwritten SQL. I don't have a link handy but DHH stated ages ago that the ability to do stuff like that was an explicit goal of ActiveRecord.
- rubyist5eva 4y agoI would like some kind of middle ground, which is why lately I tend to lean toward using something like Sequel and mapping my own domain entities. Mixing domain entities with persistance has proven to me to be a colossally bad idea. I'm in the process of writing a rather large new feature for our application that heavily uses aggregates and window functions, and writing queries that try to use these sql feature while also taking advantage of scope chaining is just an exercise in pure frustration. I think the more I lean on "advanced" SQL features, the more I just think I've probably outgrown Rails personally because it doesn't easily enable the kind of solutions that I need.
- JohnBooty 4y agowhy lately I tend to lean toward using something like Sequel and mapping my own domain entities I'm definitely itching to use something leaner like Sequel in a project. Before coming to Rails I used some nice lean C# ORMs like PetaPoco and Dapper and I really enjoyed that approach. I think the more I lean on "advanced" SQL features, the more I just think I've probably outgrown Rails personally because it doesn't easily enable the kind of solutions that I need. I definitely agree that the "treat the database as a dumb, interchangable storage solution" mentality that infects Rails and other development communities is misguided IMO. Leaning into more advanced database stuff quite frequently the way to go for non-trivial solutions. In practice, I still find Rails pretty amenable to a data-first approach. Applications tend to be a mix of advanced database stuff and simple CRUD operations. Realistically, a lot of developers are good at one but not the other. So on a given team, a mix of developers is able to play to their strengths.
- dwheeler 4y agoYes, index the database. Another thing that can make Rails apps quite fast is using caching (Rails has some builtins that make that easy). In some cases you end up skipping a database lookup and running little Ruby code, which is a huge speedup when you can do it.
- ehnto 4y agoCaching seems to be a general purpose approach that got lost the longer my career went on. I suspect as networks and computers got faster people stopped thinking the added complexity was worth it, but it's still invaluable in bigger applications.
- thom 4y agoCaching on web apps got harder the more stuff moved to the client, I’m not sure it’s just laziness.
- berkes 4y agoLuckily there is more to Ruby than Rails. I love writing Ruby code, but dislike Rails (with a passion - as I point out in my article in both footnotes and between the lines ;) (bad) performance of your Rails code is very much about Rails. And very much about how easy Rails makes it to abuse your database. But much less about the performance of the language Ruby.
- boloust 4y agoI was left confused after reading this article. 1. It claims Rust is ~10x faster than Ruby, based on a benchmark that reads a 23mb file, and then iterates over the data a single time. In my experience, Rust is between 20-100x faster than Ruby in purely CPU-bound workloads. But the author's main contention is that most work is IO-bound instead of CPU-bound, so probably not a big deal. 2. The author claims "it hardly matters that Ruby halts all code for 15ms to do garbage collection, if the fastest database-query takes 150ms". I've written applications that query Postgres databases with tens of millions of rows, where the 99th percentile response times are <10ms. I'm not sure why it just needs to be taken as a given that databases will take 150ms to return any data. 3. This flame graph from the article[0] seem to show that the vast majority of the request time is spent in Ruby parsing timestamps, rather than in the database. This seems to make the opposite point that the author is trying to make. I'm not familiar with this stack, so maybe I'm missing something- can anyone explain? [0]: https://berk.es/images/inline/flamegraph_sequel_read.svg https://berk.es/images/inline/flamegraph_sequel_read.svg
- sa46 4y agoI'm also confused. 2. Agreed. 150 ms is extraordinarily slow for a point lookup from a single table. A simple lookup should take around 1 ms since it'll be cached in memory. 3. Also agreed. The actual database time looks to be the first flame. Hovering over it shows PG::Connection::exec which accounts for 2.5% of the time. I was curious about date parsing and dug up the source [1]. Seems like you could gain a ton of speed back by using a postgres specific timestamp parsing routine. In Go, it's 40 lines [2] [1] https://github.com/ruby/date/blob/d21c69450a57a1931ab61903854b31c0e5c9ec21/ext/date/date_parse.c#L2137 https://github.com/ruby/date/blob/d21c69450a57a1931ab6190385... [2]: https://github.com/jackc/pgx/blob/a968ce3437eefc4168b39bbc4b1ea685f4c8ae66/pgtype/timestamptz.go#L275 https://github.com/jackc/pgx/blob/a968ce3437eefc4168b39bbc4b...
- wonnage 4y agoThat’s covered on literally the line after the chart: > The parsing (juggling of data) takes the majority of time: DateTime::parse
- 4y ago
- rubyfan 4y agoOutside of Rails I used to write map/reduce jobs using ruby and a jruby jar file to deploy the job to our hadoop cluster. Startup time for each job is super slow due to jruby but developer ergonomics mattered to me more than efficiency here. Slow but who cares.
- seanhunter 4y agoThe biggest database performance problem that I have seen time and time again over the 20+ years I've been using sql databases now is caused by people not understanding that databases are relational (ie designed to perform efficiently over tables, not individual rows) and so running code that essentially causes hundreds/thousands of individual queries where they could just do one big query. Any time you are doing a select query in a loop you should sit down and take a long hard look at your code. Almost always Do big expensive slow query to get everything I need in one hit for each row of result do something with it is going to be much faster than for each thing in some collection in my program get the database row that corresponds to just that one thing do something with it Round trip io, query parse time, cache effects etc are just going to totally destroy the performance of the second one, and optimising the db or programming language perf is going to do almost nothing to improve things if you're doing it this way. Lots of ORM type abstraction layers make it hard sometimes to see that's what you're doing, so people blame db performance but they're actually doing an insanely inefficient algorithm. My biggest win ever from this was taking an 8 hour operation down to 15s just by moving some sql out of a loop as per the above.
- derefr 4y agoOn the other hand, weird things can happen at scale inside an RDBMS. For example: window queries, where you're trying to extract `row_number() = 1` of each window-partition. (Or "DISTINCT ON" queries, which are basically syntax sugar for this.) These can be extremely slow, because in many RDBMSes† they generate a query plan that reads+materializes all the rows of each window-partition, in order to then just discard all-but-the-first row-tuple of each window-partition. (In other words, the filtering-out by rank doesn't get pushed back through the query plan to turn the whole-index scan into an index-prefix scan.) The (surprising) solution to this over-materialization, is to use a recursive CTE, using the previous query's matched key as the inductive case's filter condition, to cause index-prefix matching to happen "one query step at a time." Which, if you think about it, is just "causing hundreds/thousands of individual queries"... just with all of those queries happening within a single tx, statement, query plan, table lock, and (hopefully!) page cursor. † Some RDBMSes do know how to push this window-node attribute back into the fetch node; the feature is called "index skip scans" or "loose index scans." (Before anyone asks: Postgres isn't one of them... yet. https://wiki.postgresql.org/wiki/Loose_indexscan https://wiki.postgresql.org/wiki/Loose_indexscan)
- anonygler 4y agoIt’s definitely ruby that’s slow. Your database may also be slow. I love ruby. But it’s slow. It’s a great choice for a lot of things.
- adenozine 4y agoGot some sources? Ruby3 was faster than python at some point, iirc
- Jensson 4y agoPython is slow.
- adenozine 4y agoIncredible insight, thanks
- wiseowise 4y ago> Ruby3 was faster than python at some point, iirc That’s the lowest bar for performance.
- drogus 4y agoI started working with Ruby almost two decades ago and I can confirm that, in fact, Ruby is slow. It might be fast enough to make it viable for most web apps, but compared to Go or Rust it will be much harder to write a fast app in Ruby. I've seen tweets and articles like this one (it's the databse not Ruby!) numerous times and it usually doesn't map to reality, especially in bigger applications. Of course you can make Rails to respond in milliseconds, but in real production apps I've usually seen 50-80% of the responses time to be spent in CPU (ie. Ruby). At the same time when the app grows and you add more middleware an empty response can easily take 5-10ms with a bit of a traffic (like: an empty controller action, no database queries etc). It's better with smaller frameworks like Sinatra, but still you can make the database much faster than the Ruby code making queries update: Maybe out of curiosity I'll make some benchmarks later, but even if you look at frameworks benchmarks you can see that for the same database queries there's an order of magnitude of difference between Ruby and Rust or Go frameworks: https://www.techempower.com/benchmarks/#section=data-r21&test=query https://www.techempower.com/benchmarks/#section=data-r21&tes... (look at latency)
- KingOfCoders 4y agoI do think this might have been true in the past, but SSDs have changed the game for databases. Databases became 10x faster over night but this hasn't sunk in to every developer yet. Also ORMs have created an environment, where bad queries are sent to the database for questionable gains (after saving time writing some initial code). After writing a nice Java ORM in the 90s myself (also wrote my own Ruby ORM framework called Ruby.RO before Rails became a thing), then using Hibernate/JPA extensively for a decade and later ORMs in Scala and TS. I now prefer plain SQL (with Go). Quite often you can write some CTE that is fast but would have been several calls by an ORM. I also now use CTE to read/update/deleate data in one query (but be careful b/c execution order).
- ngc248 4y agoTrue ... I always preferred to roll my own DAL instead of an ORM, because when you actually get into the weeds, the kinda optimizations you can do with your own queries will always be better than what an ORM can do.
- norman784 4y ago> I now prefer plain SQL I don't know if Go is capable of, but Rust has a library called SQLx that does check that your SQL query at compile time (it makes the compilation slower than normal, but it's a good trade off I think), if this could done in Go it will be amazing too.
- KingOfCoders 4y agoYes I was using SQLx when writing Rust code and I've enjoyed it a lot.
- heurisko 4y ago> I now prefer plain SQL I prefer plain SQL using an ORM. Usually using the ORM for insertion, SQL for extraction. The ones I have used are not mutually exclusive (doctrine, hibernate).
- suction 4y ago
- amalow 4y agoClassic whataboutism, same as python bottleneck discussions, lets just shift the blame! Never understanding people don't understand database design or how query engines work.
- CitizenKane 4y agoSo apparently we're still discussing this and you can find variations on this article stretching back a long time https://hn.algolia.com/?dateRange=all&page=0&prefix=false&query=ruby%20slow&sort=byPopularity&type=story https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu... Like everything engineering it really depends though, and Ruby is sadly quite slow. Twitter was rough before they replaced Rails. Most Ruby devops tools I've used have been substantially slower than tools built in other languages (e.g. Puppet vs Ansible). Ruby certainly still has value as a tool, but it should be understood to be one that isn't very performance focused.
- berkes 4y agoYou are right. And I tried to link to as many relevant previous articles in my article here. Still, I think it is good to keep sharing this. If only because writing a blog-post allows me to organize my thoughts on this ;) WRT the twitter story: correct. But that conflates Rails with Ruby and it uses a case that is quite exceptional. Ruby is only a part of why Rails is slow. Rails is mostly slow because of the heavy and poorly optimized reliance on the database (as I argue in the article) and because of it's immensly complicated layering and dataflow. A single DateTime from a database might pass through some thousands of classes and methods before being send off in a body of HTML. Ruby is a problem here, because tousands of times "something rather slow" makes it very slow. But the "thousands of layers" really is a problem in itself. Without rails it could easily be less than hundred and you'll still have a neat architecture and layering and stuff. The other thing is that no-one except twitter (and maybe three, four other companies) are operating on that kind of scale. At such scale, entirely different metrics start to matter than what we usually encounter on a typical 2000 MAU SAAS product.
- mrkeen 4y agoIf your database is slow and ruby isn't, simply rewrite your database in Ruby.
- berkes 4y agoBut that wasn't the point, really?