Rails api

Introduction

If you’re looking to speed up your Rails API with caching and pagination, you’re on the right path to creating faster, more scalable backend services. In today’s fast-moving web environment, performance is everything — slow APIs lead to poor user experience and lost users. In this guide, we’ll walk through practical techniques to boost your Rails API’s performance using smart pagination and efficient caching strategies.

Why Speed Matters in APIs

  • Faster response = better UX (User Experience)
  • Reduces server load and bandwidth
  • Helps in scalability when you have thousands of records
  • Google Core Web Vitals matter for SEO even for APIs powering web frontends

Part 1: Pagination – Stop Loading Everything

What is Pagination?

Pagination divides large data sets into smaller “pages”. Instead of sending 10,000 records, you send 20–50 per page.

Benefits

  • Reduces response size
  • Decreases memory usage
  • Improves performance on both client and server

Implementation in Rails API

Using kaminari gem:

# Gemfile
gem 'kaminari'
bundle install

In your controller:

def index
  @posts = Post.page(params[:page]).per(20)
  render json: @posts
end

Include metadata (optional but useful):

def index
  posts = Post.page(params[:page]).per(20)
  render json: {
    data: posts,
    meta: {
      current_page: posts.current_page,
      total_pages: posts.total_pages,
      total_count: posts.total_count
    }
  }
end

Part 2: Caching – Don’t Recompute What You Already Know

What is Caching?

Caching stores frequently accessed data so you can serve it faster without recalculating or re-querying it.

Types of Caching in Rails API

  1. Low-level caching (e.g., Rails.cache)
  2. HTTP Caching (using etag and last_modified)
  3. Fragment caching (mostly for views – less useful in APIs)
  4. Database query caching

Low-Level Caching Example:

def index
  cached_posts = Rails.cache.fetch("posts_page_#{params[:page]}", expires_in: 10.minutes) do
    Post.page(params[:page]).per(20).to_a
  end

  render json: cached_posts
end


You can cache based on filters too:
key = "posts_page_#{params[:page]}_category_#{params[:category_id]}"
Rails.cache.fetch(key, expires_in: 10.minutes) { ... }

HTTP Caching Example:

In controller:

def show
@post = Post.find(params[:id])
fresh_when(@post)
end

This sends an ETag and Last-Modified header so the browser or client can skip downloading if not modified.

Bonus Tips for API Performance

  • Use select to limit fields returned:
         Post.select(:id, :title).page(params[:page])
  • Avoid N+1 queries with includes:
         Post.includes(:comments).page(params[:page])
  • Use background jobs for heavy tasks

Conclusion

Improving your Rails API performance with pagination(Kaminari Documentation) and caching is one of the fastest ways to deliver a better product. With just a few lines of code, you’ll reduce server load, speed up responses, and make your users happier.

Looking for more Rails guides? Check out SaasTrail’s advanced Rails guides for practical tips and advanced development patterns.