August 28th, 2024

Scaling Rails and Postgres to Users at Microsoft: Lessons and Takeaways

Andrew Atkinson discusses scaling Ruby on Rails applications with PostgreSQL, emphasizing ActiveRecord optimization, Sidekiq for background jobs, and addressing challenges like high concurrency and seasonal traffic in educational software.

Read original articleLink Icon
CuriositySkepticismAdmiration
Scaling Rails and Postgres to Users at Microsoft: Lessons and Takeaways

Andrew Atkinson, a consultant at StepChange, discusses strategies for scaling Ruby on Rails applications using PostgreSQL to accommodate millions of users, drawing from his experience at Microsoft with the Flip app. He emphasizes the importance of optimizing ActiveRecord queries, enhancing database efficiency, and utilizing Sidekiq for background job processing. Atkinson highlights the challenges faced when scaling applications, particularly in educational software, which experiences seasonal traffic fluctuations. He notes that relational databases like PostgreSQL provide strong data guarantees but can struggle with high concurrency during peak usage, especially with frequent updates, such as view counts in a social media context. The interview underscores the need for developers to understand the implications of their design choices on database performance, particularly when using an Object-Relational Mapping (ORM) like ActiveRecord, which abstracts database interactions. Atkinson's insights aim to help developers navigate the complexities of scaling applications effectively.

- Andrew Atkinson shares insights on scaling Rails applications with PostgreSQL based on his experience at Microsoft.

- Key strategies include optimizing ActiveRecord queries and using Sidekiq for background processing.

- Challenges include managing high concurrency and frequent updates in relational databases.

- Understanding the balance between application development needs and database performance is crucial.

- Seasonal traffic patterns in educational software require careful planning for scalability.

AI: What people are saying
The comments reflect a range of opinions on scaling Ruby on Rails applications with PostgreSQL, highlighting various strategies and concerns.
  • Several commenters emphasize the importance of caching and optimizing read operations to improve performance.
  • There is a debate on the scalability of Rails compared to other frameworks, with some suggesting alternatives like Elixir/Phoenix for better performance.
  • Some users advocate for vertical scaling of PostgreSQL without the need for microservices or additional tools.
  • Concerns are raised about the inherent limitations of Ruby on Rails for handling high traffic and large user bases.
  • Recommendations for resources, such as documentation and books on scaling Rails and PostgreSQL, are shared among commenters.
Link Icon 13 comments
By @pajeets - 4 months
Postgres can be scaled vertically like Stackoverflow did. With cache on edge for popular reads if you absolutely must (but you most likely dont).

No need to microservice or sync read replicas even (unless you are making a game). No load balancers. Just up the RAM and CPU up to TB levels for heavy real world apps (99% of you wont ever run into this issue)

Seriously its so create scalable backend services with postgrest, rpc, triggers, v8, even queues now all in Postgres. You dont even need cloud. Even a mildly RAM'd VPS will do for most apps.

got rid of redis, kubernetes, rabbitmq, bunch of SaaS tools. I just do everything on Postgres and scale vertically.

One server. No serverless. No microservice or load handlers. It's sooo easy.

By @cdiamand - 4 months
I ran into some scaling challenges with Postgres a few years ago and had to dive into the docs.

While I was mostly living out of the "High Availability, Load Balancing, and Replication" chapter, I couldn't help but poke around and found the docs to be excellent in general. Highly recommend checking them out.

https://www.postgresql.org/docs/16/index.html

By @rubyfan - 4 months
15 years ago I worked on a couple of really high profile rails sites. We had millions of users with Rails and a single mysql instance (+memcached and nginx). Back then ruby was a bit slower than it is today but I’m certain some of the challenges you face at that scale are things people still do today…

1. try to make most things static-ish reads and cache generic stuff, e.g. most things became non-user specific HTML that got cached as SSI via nginx or memcached

2. move dynamic content to services to load after static-ish main content, e.g. comments, likes, etc. would be loaded via JSON after the page load

3. Move write operations to microservices, i.e. creating new content and changes to DB become mostly deferrable background operations

I guess the strategy was to do as much serving of content without dipping into ruby layer except for write or infrequent reads that would update cache.

By @teleforce - 4 months
Please check this excellent book by former Microsoft and Groupon engineer on scaling Rails and Postgres:

[1] High Performance PostgreSQL for Rails Reliable, Scalable, Maintainable Database Applications by Andrew Atkinson:

https://pragprog.com/titles/aapsql/high-performance-postgres...

By @giovannibonetti - 4 months
What a small world. Earlier today I got tagged in a PR [1] where Andrew became the maintainer of a Ruby gem related to database migrations. Good to know he is involved in multiple projects in this space.

[1] https://github.com/lfittl/activerecord-clean-db-structure/is...

By @benwilber0 - 4 months
Postgres can scale to millions of users, but Rails definitely can't. Unless you're prepared to spend a ton of money.
By @cies - 4 months
My experience scaling up Rails (mostly in size of codebase NOT in size of traffic) really made me love typesafe languages.

IDE smartness (auto complete, refactoring), compile error instead of runtime, clear APIs...

Kotlin is a pretty nice "Type-safe Ruby" to me nowadays.

By @neonsunset - 4 months
This desperately needs the Walmart treatment of JET.com’s teams past acquisition :)
By @jojobas - 4 months
What's Rails and Postgres? Do they mean ASP.NET and MS SQL Server?
By @djaouen - 4 months
I don't understand why you wouldn't just use Elixir/Phoenix if you need to scale?
By @datadeft - 4 months
Scaling a non-scalabe by default framework that should have been few services written in a performance first language at a billion+ USD company.

I am not sure why are we boliling the oceans for the sake of a language like Ruby and a framework like Rails. I love those to death but Amazons approach is much better (or it used to be): you can't make a service for 10.000+ users in anything else than: C++, Java (probably Rust as well nowadays).

For millions of users the CPU cost difference probably justifies the rewrite cost.