typescript
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { Resource } from '@opentelemetry/resources';
import {

OpenTelemetry tracing for Node HTTP

observability tracing opentelemetry
by codesnips 3 tabs
typescript
import { Queue } from "bullmq";
import IORedis from "ioredis";

export const connection = new IORedis(process.env.REDIS_URL ?? "redis://localhost:6379", {
  maxRetriesPerRequest: null,
});

BullMQ worker with retries + dead-letter

node redis background-jobs
by codesnips 3 tabs
sql
CREATE TABLE outbox (
    id             BIGSERIAL PRIMARY KEY,
    aggregate_type TEXT        NOT NULL,
    aggregate_id   TEXT        NOT NULL,
    event_type     TEXT        NOT NULL,
    payload        JSONB       NOT NULL,

Transactional outbox in Node (DB write + event)

node postgres reliability
by codesnips 3 tabs
sql
CREATE TABLE posts (
    id           bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    author_id    bigint NOT NULL REFERENCES authors (id),
    title        text   NOT NULL,
    body         text   NOT NULL,
    published_at timestamptz NOT NULL DEFAULT now()

Cursor-based pagination with stable ordering

postgres performance pagination
by codesnips 4 tabs
typescript
import { readFileSync } from 'fs';
import { join } from 'path';
import type { Redis } from 'ioredis';
import { randomUUID } from 'crypto';

export interface LimitResult {

Rate limiting by IP + user (Express)

security express redis
by codesnips 4 tabs
typescript
import Redis from "ioredis";

export const redis = new Redis(process.env.REDIS_URL ?? "redis://localhost:6379");

export interface Codec<T> {
  encode(value: T): string;

Redis cache-aside for expensive reads

redis performance caching
by codesnips 3 tabs
typescript
import crypto from 'crypto';

interface VerifyOptions {
  rawBody: Buffer;
  signatureHeader: string | undefined;
  secret: string;

Webhook signature verification (timing-safe compare)

security webhooks hmac
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
typescript
import { Pool, PoolClient, Client, QueryResult, QueryResultRow } from 'pg';

const MAX_LIFETIME_MS = 30 * 60 * 1000;

export const pool = new Pool({
  connectionString: process.env.DATABASE_URL,

Postgres connection pooling with pg + max lifetime

node postgres connection-pooling
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
typescript
import { z } from 'zod';

export const createUserSchema = z
  .object({
    email: z.string().email(),
    name: z.string().min(1).max(120),

Runtime validation for request bodies (Zod)

typescript validation api
by codesnips 3 tabs
typescript
import pino, { Logger } from 'pino';
import { AsyncLocalStorage } from 'node:async_hooks';

export interface Store {
  requestId: string;
  logger: Logger;

Request ID + structured logging (Express + pino)

express logging observability
by codesnips 3 tabs