David Henner - Level up performance with simple coding changes - Rails World 2024

Learn practical strategies for improving Rails app performance through optimized caching, database queries, monitoring, and coding patterns with David Henner at Rails World 2024.

Key takeaways
  • Add proper observability and monitoring to identify performance bottlenecks - tools like Datadog traces help pinpoint issues

  • Leverage proper caching strategies:

    • Use read_multi instead of multiple individual cache reads
    • Consider ActiveSupport memory store for frequently accessed data
    • Implement ETags and 304 responses to reduce unnecessary data transfer
  • Optimize database queries:

    • Use pluck instead of instantiating full ActiveRecord objects when possible
    • Replace array operations with more efficient data structures (Set vs Array.unique)
    • Use hashes for lookups instead of array iterations
    • Be careful with forced indexes as they may become problematic over time
  • Implement proper data lifecycle management:

    • Plan for data archiving from the start
    • Consider sharding for large datasets
    • Have a clear data retention/removal strategy
  • Write performance-focused tests:

    • Test for specific numbers of SQL queries
    • Add assertions for memoization behavior
    • Verify cache hits/misses
  • Optimize code patterns:

    • Use flat_map instead of map.flatten
    • Properly memoize shared data on singleton/global objects
    • Process/sanitize data on input rather than output
    • Use blocks for custom performance tracing
  • Small optimizations add up:

    • Even 1ms improvements matter at scale (billions of requests)
    • Focus on high-traffic endpoints first
    • Consider infrastructure costs alongside latency