redislock

package
v0.13.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package redislock provides distributed locking using Redis.

This implementation uses a naive "single instance" Redis distributed locking algorithm as described in https://redis.io/docs/latest/develop/use/patterns/distributed-locks/

We use the simple SET NX PX pattern which has edge cases where multiple nodes may acquire the lock under extreme circumstances (see the Redis documentation for details). We accept these edge cases because our primary use cases can tolerate occasional races:

  1. Infrastructure provisioning - infrequent, cloud providers handle concurrent attempts gracefully
  2. Migration coordination - prevents multiple nodes from running migrations simultaneously at startup; if a race occurs, the migration runner re-checks "already applied" status after acquiring the lock
  3. Initialization tasks - typically have "already exists" checks after acquiring lock

In the worst case, if an operation fails due to a lock race, the node will fail its health check and another node can take over. This is acceptable for startup-time operations that run infrequently.

Do NOT use this for high-frequency locking or cases where duplicate execution would cause data corruption. For those cases, consider Redlock or a proper distributed lock.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lock

type Lock interface {
	AttemptLock(ctx context.Context) (bool, error)
	Unlock(ctx context.Context) (bool, error)
}

Lock defines the interface for distributed locking

func New

func New(client redis.Cmdable, opts ...Option) Lock

New creates a new Redis-based distributed lock

type Option

type Option func(*redisLock)

Option configures a redisLock

func WithKey

func WithKey(key string) Option

WithKey sets a custom key for the lock

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL sets a custom TTL for the lock

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL