reliability

python
from datetime import datetime
from app.extensions import db


class IdempotencyKey(db.Model):
    __tablename__ = "idempotency_keys"

Idempotency-Key Deduplication for POST Requests in a Flask Blueprint

flask idempotency postgres
by codesnips 3 tabs
ruby
module Middleware
  class RateLimiter
    def initialize(app, redis:, limit:, window:)
      @app = app
      @limiter = SlidingWindowLimiter.new(redis: redis, limit: limit, window: window)
      @limit = limit

Sliding-Window API Rate Limiting with a Rack Middleware and Redis in Rails

rails rack redis
by codesnips 3 tabs
python
from django.db import models
from django.utils import timezone


class WebhookEvent(models.Model):
    class Status(models.TextChoices):

Idempotent Stripe Webhook Handling in Django with a Unique Event-ID Constraint

django webhooks idempotency
by codesnips 3 tabs
sql
-- Step 1: add the column nullable, no default.
-- Catalog-only change in Postgres 11+, returns instantly.
ALTER TABLE orders
  ADD COLUMN currency text;

-- Optional: keep the lock attempt bounded so a long-running

SQL migration safety: add column nullable, backfill, then constrain

postgres migrations reliability
by codesnips 3 tabs
javascript
'use strict';

const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const crypto = require('crypto');

Stream a Multipart Upload Through Gzip to Disk with stream.pipeline

nodejs streams backpressure
by codesnips 2 tabs
typescript
import { Injectable } from '@nestjs/common';

type Completed = { status: 'completed'; statusCode: number; body: unknown; expiresAt: number };
type InFlight = { status: 'in-flight'; startedAt: number };
type Record = Completed | InFlight;

Idempotency-Key Interceptor in NestJS to Debounce Duplicate Form Submissions

nestjs idempotency interceptor
by codesnips 4 tabs
rust
use tokio::sync::mpsc::{self, Sender};
use tokio::sync::mpsc::error::TrySendError;

use crate::email::{EmailJob, EmailMessage};
use crate::worker::EmailWorker;

Retry-Aware Email Queue With a Tokio mpsc Worker and Exponential Backoff

rust tokio async
by codesnips 3 tabs
typescript
import express, { type Express, type Request, type Response } from 'express';

export function buildApp(isShuttingDown: () => boolean): Express {
  const app = express();
  app.disable('x-powered-by');

Graceful shutdown for Node HTTP servers

reliability nodejs express
by codesnips 3 tabs
python
import logging
from typing import Awaitable, Callable, List, Tuple

log = logging.getLogger("saga")

Compensation = Callable[[], Awaitable[None]]

Saga-Style Rollback With a Context-Managed Compensating Action Stack

saga rollback context-manager
by codesnips 3 tabs
ruby
Rails.application.config.middleware.insert_before(
  Rack::Runtime,
  Rack::Timeout,
  service_timeout: 15  # 15 seconds
)

Request timeout handling with Rack::Timeout

rails reliability performance
by Alex Kumar 2 tabs
ruby
class LeaderboardCache
  TOP_KEY = "leaderboard:top".freeze
  STATS_KEY = "leaderboard:stats".freeze

  def top_players
    Rails.cache.fetch(TOP_KEY, expires_in: 5.minutes, race_condition_ttl: 15.seconds) do

Cache Stampede Protection with race_condition_ttl

rails caching performance
by codesnips 3 tabs
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