sidekiq

ruby
class CreateTopSellersMv < ActiveRecord::Migration[7.0]
  def up
    execute <<~SQL
      CREATE MATERIALIZED VIEW top_sellers AS
        SELECT p.id            AS product_id,
               p.name          AS product_name,

Cache-Friendly “Top N” with Materialized View Refresh

rails postgres performance
by codesnips 4 tabs
ruby
class CreateDeadJobs < ActiveRecord::Migration[7.1]
  def change
    create_table :dead_jobs do |t|
      t.string  :jid, null: false
      t.string  :queue, null: false
      t.string  :klass, null: false

Background Job Dead Letter Queue (DLQ) Table

rails reliability background-jobs
by codesnips 4 tabs
ruby
require "timeout"

module HealthCheck
  class Probe
    Result = Struct.new(:name, :status, :latency_ms, :critical, :error, keyword_init: true) do
      def healthy?

Health Check Endpoint with Dependency Probes

rails reliability health-check
by codesnips 3 tabs
ruby
class CreateProcessedEvents < ActiveRecord::Migration[7.1]
  def change
    create_table :processed_events do |t|
      t.string :event_key, null: false
      t.string :job_class, null: false
      t.jsonb :metadata, null: false, default: {}

Idempotent Job with Advisory Lock

rails postgres reliability
by codesnips 3 tabs
ruby
class ReindexCheckpoint < ApplicationRecord
  enum status: { idle: 0, running: 1, done: 2, failed: 3 }

  validates :index_name, presence: true, uniqueness: true

  def self.for(index_name)

Safer Background Reindex: slice batches + checkpoints

rails reliability elasticsearch
by codesnips 4 tabs
ruby
class CreateEmailDeliveries < ActiveRecord::Migration[7.1]
  def change
    create_table :email_deliveries do |t|
      t.string :dedupe_key, null: false
      t.string :mailer, null: false
      t.string :action, null: false

Transactional Email “Send Once” with Delivered Marker

rails reliability activerecord
by codesnips 4 tabs
yaml
:concurrency: 10
:queues:
  - [critical, 4]
  - [default, 2]
  - [low, 1]

Background jobs with Sidekiq and reliable queues

rails sidekiq background-jobs
by Alex Kumar 2 tabs
ruby
class NotifyFollowersJob
  include Sidekiq::Job

  sidekiq_options queue: :notifications, retry: 5

  def perform(post_id, actor_id)

Safer Background Job Arguments (Serialize IDs only)

rails reliability sidekiq
by codesnips 3 tabs
ruby
class Document < ApplicationRecord
  belongs_to :account

  validates :source_url, presence: true

  before_save :normalize_checksum

Prevent Long Transactions with after_save_commit for Heavy Work

rails activerecord background-jobs
by codesnips 3 tabs
ruby
class Post < ApplicationRecord
  has_many :comments, dependent: :destroy

  # comments_count is maintained by counter_cache on Comment#belongs_to

  scope :stale_comment_counts, lambda {

Counter Cache Repair Job (Consistency Tooling)

rails activerecord counter-cache
by codesnips 3 tabs
ruby
require "sidekiq/api"

class QueueDepthGuard
  class QueueSaturated < StandardError
    attr_reader :queue, :depth

Background Job Backpressure with Queue Depth Guard

rails reliability sidekiq
by codesnips 3 tabs
ruby
class RegistrationsController < ApplicationController
  def new
    @user = User.new
  end

  def create

Send a Welcome Email After Signup Using a Sidekiq Background Job in Rails

ruby rails sidekiq
by codesnips 4 tabs