redis

ruby
class LastSeenTracker
  THROTTLE = 5.minutes
  PENDING_KEY = "pending:last_seen".freeze

  class << self
    def touch(user_id, at: Time.current)

Database “Last Seen” without Hot Row Updates

rails performance redis
by codesnips 3 tabs
php
<?php

use Illuminate\Support\Facades\Cache;

// Remember pattern - fetch from cache or execute closure
$posts = Cache::remember('posts.all', 3600, function () {

Laravel cache strategies for performance

laravel cache performance
by Carlos Mendez 4 tabs
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
php
<?php

declare(strict_types=1);

namespace App\Security;

PHP Login Rate Limiting with a Sliding-Window Throttle Middleware

php rate-limiting middleware
by codesnips 3 tabs
lua
-- KEYS[1] = bucket key
-- ARGV[1] = capacity, ARGV[2] = refill_rate (tokens/sec)
-- ARGV[3] = now_ms, ARGV[4] = requested tokens
local capacity    = tonumber(ARGV[1])
local refill_rate = tonumber(ARGV[2])
local now_ms      = tonumber(ARGV[3])

Token Bucket Rate Limiting in a Servlet Filter with Redis and Lua

rate-limiting token-bucket servlet
by codesnips 3 tabs
ruby
class SlidingWindowLimiter
  Result = Struct.new(:allowed, :count, :limit, :window_ms, keyword_init: true)

  SCRIPT = <<~LUA.freeze
    local key = KEYS[1]
    local now = tonumber(ARGV[1])

Rate Limiting with Redis + Increment Expiry

rails redis reliability
by codesnips 3 tabs
ruby
class DebouncedReindexJob
  include Sidekiq::Job

  sidekiq_options queue: :indexing, retry: 5

  DEBOUNCE_DELAY = 5 # seconds

Debouncing Sidekiq Jobs Per-Record With Redis So Rapid Updates Coalesce Into One Run

rails sidekiq redis
by codesnips 3 tabs
go
package health

import (
	"context"
	"sync"
	"time"

Concurrent Readiness Health Check Endpoint in Go with Timeouts

go health-check readiness
by codesnips 3 tabs