concurrency

ruby
class NotifyFollowersJob
  include Sidekiq::Job

  sidekiq_options queue: :notifications, retry: 5

  def perform(post_id, actor_id)

Safer Background Job Arguments (Serialize IDs only)

rails reliability sidekiq
by codesnips 3 tabs
rust
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread;

fn main() {
    let counter = Arc::new(AtomicUsize::new(0));

Atomic operations for lock-free concurrency

rust concurrency atomics
by Marcus Chen 1 tab
ruby
class AddUniqueIndexToInventorySnapshots < ActiveRecord::Migration[7.1]
  disable_ddl_transaction!

  def change
    add_index :inventory_snapshots,
              [:warehouse_id, :sku],

Bulk Upsert with insert_all + Unique Index

rails activerecord postgres
by codesnips 3 tabs
go
package cache

import (
  "context"
  "sync"
  "time"

Singleflight cache fill to prevent thundering herd

go cache concurrency
by Leah Thompson 1 tab
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
sql
ALTER TABLE documents ADD COLUMN version BIGINT NOT NULL DEFAULT 0;

Optimistic locking with a version column

go postgres sql
by Leah Thompson 2 tabs
ruby
class Document < ApplicationRecord
  belongs_to :account

  validates :source_url, presence: true

  before_save :normalize_checksum

Prevent Long Transactions with after_save_commit for Heavy Work

rails activerecord background-jobs
by codesnips 3 tabs
ruby
module ConditionalGet
  extend ActiveSupport::Concern

  private

  def render_conditional(resource, extra: nil)

ETag + Conditional GET for JSON API

rails performance http-caching
by codesnips 2 tabs
java
package com.example.demo.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

CompletableFuture for async programming

java async completablefuture
by David Kumar 1 tab
rust
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::{Arc, RwLock};

pub struct Memoizer<K, V> {
    store: RwLock<HashMap<K, Arc<V>>>,

Thread-Safe Memoization in Rust with RwLock and OnceCell Sharding

rust concurrency memoization
by codesnips 3 tabs
go
package workpool

import (
	"context"
	"sync"
)

Bounded Worker Pool Processing Jobs from a Buffered Channel in Go

go concurrency worker-pool
by codesnips 3 tabs
ruby
class CreateStripeEvents < ActiveRecord::Migration[7.1]
  def change
    create_table :stripe_events do |t|
      t.string :stripe_event_id, null: false
      t.string :event_type, null: false
      t.string :status, null: false, default: "received"

Idempotent Stripe Webhook Processing in Rails with a Durable Event Log

rails stripe webhooks
by codesnips 4 tabs