reliability

ruby
class AddOptimisticLockingToDocuments < ActiveRecord::Migration[7.1]
  def change
    add_column :documents, :lock_version, :integer, null: false, default: 0
    add_index :documents, :updated_at
  end
end

Optimistic Locking for Collaborative Edits

rails activerecord concurrency
by codesnips 3 tabs
go
package consumers

import (
  "context"

  "github.com/jackc/pgx/v5/pgxpool"

Idempotent event consumer with processed-events table

go postgres messaging
by Leah Thompson 1 tab
go
package main

import (
  "time"

  "google.golang.org/grpc"

Graceful gRPC shutdown with fallback to Stop

go grpc shutdown
by Leah Thompson 1 tab
go
package ingest

import (
  "bufio"
  "bytes"
  "io"

Avoid bufio.Scanner token limits by switching to Reader

go io logs
by Leah Thompson 1 tab
ruby
module RetryableTransaction
  module_function

  RETRIABLE = [ActiveRecord::Deadlocked, ActiveRecord::LockWaitTimeout].freeze

  def run(max_retries: 3, base_delay: 0.05, &block)

Deadlock-Aware Retry Wrapper

rails postgres reliability
by codesnips 3 tabs
ruby
module Idempotency
  extend ActiveSupport::Concern

  included do
    before_action :check_idempotency_key, only: [:create, :update]
    after_action :store_idempotent_response, only: [:create, :update]

Request deduplication with idempotency keys

rails api reliability
by Alex Kumar 1 tab
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
ruby
require "faraday"
require "faraday/retry"

module Http
  class RetryableError < StandardError; end
  class CircuitOpenError < StandardError; end

HTTP Timeouts + Retries Wrapper (Faraday)

rails http reliability
by codesnips 3 tabs
go
package deps

import (
  "context"
  "net/http"
  "time"

Circuit breaker around flaky dependencies

go reliability circuit-breaker
by Leah Thompson 1 tab
go
package cache

import (
  "context"
  "sync/atomic"
  "time"

Context-aware cache refresh with atomic.Pointer (Go 1.19+)

go concurrency cache
by Leah Thompson 1 tab
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
go
package cache

import (
  "context"
  "sync"
  "time"

Singleflight cache fill to prevent thundering herd

go cache concurrency
by Leah Thompson 1 tab