reliability

ruby
class CreateIdempotencyKeys < ActiveRecord::Migration[7.1]
  def change
    create_table :idempotency_keys do |t|
      t.string :key, null: false
      t.string :request_path, null: false
      t.datetime :locked_at

Idempotent Form Submissions in Rails with an Idempotency-Key Column and before_action Guard

rails idempotency postgres
by codesnips 4 tabs
ruby
module ConnectionHealth
  extend ActiveSupport::Concern

  def with_fresh_connection
    conn = ActiveRecord::Base.connection
    conn.verify! # pings and reconnects if the socket is dead

Keep DB Connections Healthy in Long Jobs

rails activerecord background-jobs
by codesnips 3 tabs
ruby
class Cart < ApplicationRecord
  TTL = 30.minutes

  has_many :line_items, dependent: :destroy

  enum status: { active: 0, expired: 1, checked_out: 2 }

Expiring Idle Shopping Carts with a TTL Check and a Sweeper Job in Rails

rails background-jobs sidekiq
by codesnips 3 tabs
typescript
import type { Redis } from "ioredis";

export interface StoredResponse {
  status: "pending" | "completed";
  fingerprint: string;
  httpStatus?: number;

Idempotent POST Requests in Express with a Redis-Backed Middleware

express redis idempotency
by codesnips 3 tabs
php
<?php

namespace App\Http\Controllers;

use App\Jobs\SendNewsletterChunk;
use App\Models\NewsletterSend;

Chunk-Process a Large Newsletter Send with Laravel Job Batching and Progress Tracking

laravel queues job-batching
by codesnips 3 tabs
ruby
module DefensiveDeserialization
  extend ActiveSupport::Concern

  MissingRecord = Struct.new(:gid) do
    def missing?
      true

Defensive Deserialization for ActiveJob

rails activejob reliability
by codesnips 3 tabs
typescript
export interface AnalyticsEvent {
  name: string;
  props?: Record<string, unknown>;
  ts: number;
}

Batching Analytics Events With Interval Flush and Backpressure in TypeScript

analytics batching queue
by codesnips 3 tabs
php
<?php

namespace App\Providers;

use App\Events\OrderPlaced;
use App\Listeners\DecrementInventory;

Fan Out an Order Placed Domain Event to Multiple Queued Laravel Listeners

laravel events queues
by codesnips 4 tabs
javascript
const SHUTDOWN_TIMEOUT_MS = 10_000;

function registerFatalHandlers({ logger, onFatal, exitCode = 1 }) {
  let shuttingDown = false;

  async function handleFatal(kind, error) {

Graceful Node.js Shutdown on uncaughtException and unhandledRejection

node reliability graceful-shutdown
by codesnips 3 tabs
python
import uuid
from django.db import models
from django.utils import timezone


class OutboxManager(models.Manager):

Transactional Outbox with Timer-Based Flushing and Exponential Backoff in Django

django outbox postgres
by codesnips 3 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Batching Laravel Queue Jobs to Roll Up Daily Order Totals With a Completion Callback

laravel queues background-jobs
by codesnips 3 tabs
ruby
module BoundedFanOut
  Result = Struct.new(:value, :error) do
    def ok?
      error.nil?
    end
  end

Parallelize Independent External Calls (in a bounded way)

concurrency threads http
by codesnips 3 tabs