concurrency

rust
use std::time::Duration;
use tokio_util::sync::CancellationToken;

pub struct Worker {
    id: usize,
    token: CancellationToken,

Graceful Task Shutdown in Tokio Using CancellationToken

tokio async cancellation
by codesnips 3 tabs
java
@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean(name = "orderExecutor")
    public ThreadPoolTaskExecutor orderExecutor() {

Debouncing Order Recalculations with Spring @Async and a Configured ThreadPoolTaskExecutor

spring-boot async thread-pool
by codesnips 3 tabs
sql
CREATE TYPE outbox_status AS ENUM ('pending', 'retry', 'processing', 'done', 'dead');

CREATE TABLE outbox_events (
    id           BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    dedupe_key   TEXT        NOT NULL,
    topic        TEXT        NOT NULL,

Atomic “Read + Mark Processed” with UPDATE … RETURNING

postgres concurrency reliability
by codesnips 3 tabs
go
package main

import (
	"context"
	"log"
	"net/http"

Graceful HTTP Server Shutdown in Go with SIGINT and Context Cancellation

go http graceful-shutdown
by codesnips 2 tabs
ruby
class AddSlugToPosts < ActiveRecord::Migration[7.1]
  def change
    add_column :posts, :slug, :string, null: false
    add_index :posts, :slug, unique: true
  end
end

Database-Backed Unique Slugs with Retry

rails postgres slugs
by codesnips 4 tabs
python
from django.db import transaction
from django.shortcuts import get_object_or_404
from django.http import JsonResponse
from .models import Product

Django select_for_update for database locking

django python database
by Priya Sharma 1 tab
lua
-- KEYS[1] = bucket key
-- ARGV[1] = capacity, ARGV[2] = refill_per_sec, ARGV[3] = now (float seconds)
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])

Redis-Backed Token Bucket Rate Limiter as FastAPI Middleware

fastapi redis rate-limiting
by codesnips 4 tabs
ruby
require "securerandom"

class RedisMutex
  class LockError < StandardError; end

  UNLOCK_SCRIPT = <<~LUA.freeze

Redis-Based Distributed Mutex (with TTL)

rails redis concurrency
by codesnips 2 tabs
go
package pricecache

import (
	"context"
	"sync"

Collapse Concurrent Identical Requests in Go with singleflight

go singleflight concurrency
by codesnips 3 tabs
ruby
class PresenceRegistry
  TTL = 30 # seconds a user counts as present without a heartbeat

  def initialize(room_id, redis: REDIS)
    @room_id = room_id
    @redis = redis

Action Cable Presence Tracking (Lightweight)

rails actioncable redis
by codesnips 3 tabs
php
<?php

namespace App\Jobs;

use App\Models\Invoice;
use App\Services\StripeGateway;

Preventing Double-Charges in Laravel with WithoutOverlapping Job Middleware

laravel queues background-jobs
by codesnips 3 tabs
ruby
class AddLockVersionToPosts < ActiveRecord::Migration[6.1]
  def change
    add_column :posts, :lock_version, :integer, default: 0, null: false
  end
end

Optimistic locking for concurrent updates

rails database concurrency
by Alex Kumar 2 tabs