reliability

go
package events

import "encoding/json"

type Envelope struct {
  Type    string          `json:"type"`

Store unstructured JSON safely with json.RawMessage

go json events
by Leah Thompson 1 tab
ruby
module StatementTimeout
  extend ActiveSupport::Concern

  class TimeoutExceeded < StandardError; end

  def with_statement_timeout(milliseconds)

Guard Against Slow Queries with statement_timeout

rails postgres performance
by codesnips 3 tabs
go
package middleware

import (
  "net/http"

  "go.uber.org/zap"

Panic recovery middleware for HTTP servers

go http middleware
by Leah Thompson 1 tab
go
package jobs

import (
  "context"
  "math/rand"
  "time"

Robfig cron jobs with context cancellation and jitter

go cron background-jobs
by Leah Thompson 1 tab
go
package middleware

import (
  "log"
  "net/http"
  "runtime/debug"

Panic recovery middleware that fails closed and logs context

go http middleware
by Leah Thompson 1 tab
typescript
import { Queue } from "bullmq";
import { createHash } from "crypto";

export const connection = { host: "127.0.0.1", port: 6379 };

export interface ChargePayload {

BullMQ job idempotency via dedupe id

node redis background-jobs
by codesnips 3 tabs
go
package deps

import "sync"

type Lazy[T any] struct {
  once sync.Once

Lazy init of expensive clients with sync.Once

go concurrency sync
by Leah Thompson 1 tab
ruby
class AddSoftDeleteToDocuments < ActiveRecord::Migration[7.0]
  disable_ddl_transaction!

  def change
    add_column :documents, :marked_for_deletion_at, :datetime, null: true

Safer Time-Based Deletes with “mark then sweep”

rails reliability activerecord
by codesnips 4 tabs
javascript
const express = require('express');
const { enqueueEmail } = require('./emailQueue');

const router = express.Router();

router.post('/users/:id/welcome-email', async (req, res, next) => {

Reliable Background Email Jobs With BullMQ, Redis, and a Worker Process

bullmq redis background-jobs
by codesnips 3 tabs
sql
CREATE TABLE idempotency_keys (
    request_key         text        NOT NULL,
    endpoint            text        NOT NULL,
    request_fingerprint text        NOT NULL,
    status              text        NOT NULL DEFAULT 'in_progress'
                                    CHECK (status IN ('in_progress', 'completed')),

Idempotency keys for “create” endpoints

reliability postgres idempotency
by codesnips 3 tabs
python
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",

Custom DRF Throttle Class for a Public API With Sliding-Window Rate Limits

django drf rate-limiting
by codesnips 3 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