concurrency

python
import uuid
from django.db import models


class Cart(models.Model):
    OPEN = "open"

Prevent Duplicate Order Submissions in Django with select_for_update Row Locking

django postgres concurrency
by codesnips 3 tabs
javascript
class TokenBucket {
  constructor(capacity, refillPerSecond) {
    this.capacity = capacity;
    this.refillPerMs = refillPerSecond / 1000;
    this.tokens = capacity;
    this.lastRefill = Date.now();

Token Bucket Rate Limiter Middleware for Express with Per-Key Buckets

nodejs express rate-limiting
by codesnips 4 tabs
go
package worker

import (
	"context"
	"errors"
	"log"

Fan-Out Worker Pool With Context Cancellation and Graceful Shutdown in Go

go concurrency goroutines
by codesnips 2 tabs
sql
CREATE TABLE daily_metrics (
    id          BIGGENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    tenant_id   BIGINT      NOT NULL,
    metric      TEXT        NOT NULL,
    day         DATE        NOT NULL,
    count       BIGINT      NOT NULL DEFAULT 0,

SQL upsert for counters (ON CONFLICT DO UPDATE)

postgres sql concurrency
by codesnips 3 tabs
python
import asyncio
import time
from dataclasses import dataclass
from typing import Any, Dict

In-Memory TTL Cache for FastAPI Endpoints With a Cache-Key Builder Dependency

fastapi caching ttl
by codesnips 3 tabs
rust
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;
use tokio::time::sleep;

#[derive(Debug)]

Rate-Limiting Outbound HTTP with a Token Bucket in Rust (Tokio)

rust tokio rate-limiting
by codesnips 3 tabs
php
<?php

namespace App\Http\Controllers;

use App\Models\Wallet;
use App\Services\WalletService;

Debit a Wallet Balance Safely with Row Locking in Laravel

laravel eloquent transactions
by codesnips 4 tabs
go
package pipeline

import "context"

func generate(ctx context.Context, nums ...int) <-chan int {
	out := make(chan int)

Fan-Out/Fan-In Pipeline With Channels and Context Cancellation in Go

go concurrency channels
by codesnips 3 tabs
ruby
class CounterFlushJob
  include Sidekiq::Job
  sidekiq_options queue: :counters, retry: 3

  def perform
    CounterBuffer.flush_all.each do |field, delta|

Coalesced Counter Cache with Redis Buffering and Nightly Reconciliation in Rails

rails counter-cache redis
by codesnips 4 tabs
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
python
import time
import uuid
from dataclasses import dataclass

import redis

Sliding-Window Per-User Rate Limiting With Redis and a Flask Decorator

rate-limiting redis flask
by codesnips 3 tabs
rust
use async_trait::async_trait;
use serde::Serialize;

#[derive(Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Status {

Aggregating Subsystem Health Checks Behind an Axum /healthz Endpoint

axum tokio health-check
by codesnips 3 tabs