performance

erb
<%# Fragment caching - caches rendered HTML %>
<% cache @product do %>
  <h1><%= @product.name %></h1>
  <p><%= @product.description %></p>
  <p>Price: $<%= @product.price %></p>
<% end %>

Rails caching strategies for performance

ruby rails caching
by Sarah Mitchell 4 tabs
ruby
class CreateCustomerRevenueSummaries < ActiveRecord::Migration[7.0]
  def change
    create_view :customer_revenue_summaries, materialized: true, version: 1

    add_index :customer_revenue_summaries,
              :customer_id,

Database Views for Read Models

rails postgres performance
by codesnips 4 tabs
sql
-- Create basic index
CREATE INDEX idx_users_email ON users(email);

-- Unique index (enforces uniqueness)
CREATE UNIQUE INDEX idx_users_username ON users(username);

Database indexing strategies for performance

database indexing performance
by Maria Garcia 2 tabs
sql
CREATE TABLE subscriptions (
    id                  BIGGENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    customer_id         BIGINT       NOT NULL,
    plan_code           TEXT         NOT NULL,
    status              TEXT         NOT NULL DEFAULT 'active',
    current_period_end  TIMESTAMPTZ  NOT NULL,

Partial index for “active” rows in Postgres

postgres performance sql
by codesnips 3 tabs
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["sentinel"]
  static values = {
    url: String

Infinite scroll with Turbo Frames and Intersection Observer

hotwire turbo stimulus
by Jordan Lee 3 tabs
typescript
import { Link } from 'react-router-dom'
import { useQueryClient } from '@tanstack/react-query'
import api from '@/services/api'
import { PostId } from '@/types'

interface PostLinkProps {

Prefetching data on hover for instant navigation

react performance prefetching
by Maya Patel 1 tab
typescript
import React from 'react'

interface SkeletonProps {
  width?: string | number
  height?: string | number
  className?: string

Skeleton screens for better perceived performance

react ux performance
by Maya Patel 3 tabs
ruby
module KeysetPaginatable
  extend ActiveSupport::Concern

  class_methods do
    def keyset_page(cursor: nil, limit: 20, direction: :desc)
      limit = limit.to_i.clamp(1, 100)

Keyset Cursor Pagination for ActiveRecord Instead of OFFSET

ruby rails activerecord
by codesnips 4 tabs
ruby
Rails.application.config.middleware.insert_before(
  Rack::Runtime,
  Rack::Timeout,
  service_timeout: 15  # 15 seconds
)

Request timeout handling with Rack::Timeout

rails reliability performance
by Alex Kumar 2 tabs
go
package feed

import (
	"encoding/base64"
	"encoding/json"
	"time"

Cursor-Based Keyset Pagination for a Postgres Feed API in Go

go postgres pagination
by codesnips 3 tabs
ruby
class LeaderboardCache
  TOP_KEY = "leaderboard:top".freeze
  STATS_KEY = "leaderboard:stats".freeze

  def top_players
    Rails.cache.fetch(TOP_KEY, expires_in: 5.minutes, race_condition_ttl: 15.seconds) do

Cache Stampede Protection with race_condition_ttl

rails caching performance
by codesnips 3 tabs
python
from django.db import models
from django.contrib.postgres.indexes import GinIndex


class Post(models.Model):
    title = models.CharField(max_length=200, db_index=True)

Django database index strategies

django python database
by Priya Sharma 1 tab