concurrency

ruby
class CreateWebhookEvents < ActiveRecord::Migration[7.1]
  def change
    create_table :webhook_events do |t|
      t.string :event_id, null: false
      t.string :source, null: false, default: "stripe"
      t.string :event_type, null: false

Idempotent Stripe Webhook Processing with a Unique Event Key in Rails

rails postgres webhooks
by codesnips 4 tabs
ruby
class CreateOutboxEvents < ActiveRecord::Migration[7.1]
  def change
    create_table :outbox_events do |t|
      t.string :event_type, null: false
      t.string :aggregate_type, null: false
      t.string :aggregate_id, null: false

Transactional Outbox for Reliable Event Publishing

rails background-jobs reliability
by codesnips 4 tabs
typescript
import { Prisma, PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

type TxClient = Prisma.TransactionClient;

Prisma transaction with retries for serialization errors

prisma postgres concurrency
by codesnips 3 tabs
rust
use std::collections::HashMap;
use std::sync::Mutex;

pub type AccountId = u64;
pub type Cents = i64;

Atomic Double-Entry Ledger Transfers With Rust Locking and Poison Recovery

ledger concurrency double-entry
by codesnips 3 tabs
typescript
import { EventEmitter } from "events";

export interface Job<T> {
  id: string;
  payload: T;
  attempts: number;

Typed In-Memory Job Queue With a Concurrency-Limited Worker Pool

typescript job-queue concurrency
by codesnips 3 tabs
ruby
class LastSeenTracker
  THROTTLE = 5.minutes
  PENDING_KEY = "pending:last_seen".freeze

  class << self
    def touch(user_id, at: Time.current)

Database “Last Seen” without Hot Row Updates

rails performance redis
by codesnips 3 tabs
ruby
class CreateReservations < ActiveRecord::Migration[7.1]
  def up
    enable_extension "btree_gist" unless extension_enabled?("btree_gist")

    create_table :reservations do |t|
      t.references :room, null: false, foreign_key: true

DB-Level “no overlapping ranges” with exclusion constraint

rails postgres exclusion-constraint
by codesnips 3 tabs
go
package bank

import "errors"

var ErrVersionConflict = errors.New("optimistic lock: version conflict")
var ErrInsufficientFunds = errors.New("insufficient funds")

Optimistic Locking in Go With a Version Column on UPDATE

optimistic-locking concurrency postgres
by codesnips 3 tabs
python
import queue
import time
from dataclasses import dataclass, field, replace
from typing import Any, Dict, Tuple

In-Process Threaded Background Job Queue for Sending Emails Without Redis

background-jobs threading queue
by codesnips 4 tabs
go
package ratelimit

import (
	"sync"
	"time"
)

Token Bucket Rate-Limiting Middleware for a Go net/http API

go net-http middleware
by codesnips 4 tabs
go
package watch

import (
	"sync"
	"time"
)

Debounce Filesystem Change Events Before Triggering a Rebuild in Go

fsnotify debounce filewatcher
by codesnips 3 tabs
ruby
module RequestStore
  def self.store
    Thread.current[:request_store] ||= {}
  end

  def self.fetch(key)

Hot Path Memoization (within request only)

rails performance memoization
by codesnips 4 tabs