hotkey

package
v0.0.0-...-411d5f7 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ShardKeyName

func ShardKeyName(key string, shardIndex int) string

ShardKeyName returns the shard key name for a given key and shard index.

Types

type CountMinSketch

type CountMinSketch struct {
	// contains filtered or unexported fields
}

CountMinSketch is a probabilistic data structure for frequency estimation. It uses d independent hash functions and a 2D array of counters to estimate the frequency of elements in a stream with bounded error.

func NewCountMinSketch

func NewCountMinSketch(width, depth uint32) *CountMinSketch

NewCountMinSketch creates a new Count-Min Sketch. width (w) controls accuracy — larger = more accurate. depth (d) controls confidence — more rows = lower false positive rate. Recommended: d=4, w=4096 (16KB memory).

func (*CountMinSketch) Decay

func (cms *CountMinSketch) Decay()

Decay halves all counters. This is used periodically to handle changing access patterns — old hot keys gradually cool down.

func (*CountMinSketch) Estimate

func (cms *CountMinSketch) Estimate(key string) uint64

Estimate returns the estimated frequency of the given key. The estimate is always >= actual count (never underestimates).

func (*CountMinSketch) Increment

func (cms *CountMinSketch) Increment(key string)

Increment adds 1 to the frequency estimate for the given key.

func (*CountMinSketch) Reset

func (cms *CountMinSketch) Reset()

Reset zeroes all counters.

func (*CountMinSketch) Total

func (cms *CountMinSketch) Total() uint64

Total returns the total number of increments.

type Detector

type Detector struct {
	// contains filtered or unexported fields
}

Detector uses a Count-Min Sketch to detect hot keys in real-time.

func NewDetector

func NewDetector(hotThreshold uint64, decayPeriod time.Duration) *Detector

NewDetector creates a new hot key detector. hotThreshold: minimum estimated frequency to consider a key "hot". decayPeriod: how often to halve all counters (e.g., 60 seconds).

func (*Detector) GetFrequency

func (d *Detector) GetFrequency(key string) uint64

GetFrequency returns the estimated access frequency for a key.

func (*Detector) IsHot

func (d *Detector) IsHot(key string) bool

IsHot checks if a key is currently hot without recording an access.

func (*Detector) RecordAccess

func (d *Detector) RecordAccess(key string) bool

RecordAccess records an access to a key and returns whether the key is hot.

func (*Detector) Start

func (d *Detector) Start()

Start begins the background decay goroutine.

func (*Detector) Stop

func (d *Detector) Stop()

Stop stops the background decay goroutine.

type MitigationStrategy

type MitigationStrategy int

MitigationStrategy defines how to handle a hot key.

const (
	// StrategyNone means no mitigation is applied.
	StrategyNone MitigationStrategy = iota
	// StrategyKeySplit splits the hot key across multiple sharded keys.
	StrategyKeySplit
	// StrategyReadReplica adds extra read replicas for the hot key.
	StrategyReadReplica
)

func (MitigationStrategy) String

func (s MitigationStrategy) String() string

String returns the strategy name.

type Mitigator

type Mitigator struct {
	// contains filtered or unexported fields
}

Mitigator handles hot key mitigation strategies.

func NewMitigator

func NewMitigator(s *store.Store, detector *Detector, shardCount int) *Mitigator

NewMitigator creates a new hot key mitigator. shardCount is the number of shards to split hot keys into (e.g., 4).

func (*Mitigator) CheckAndMitigate

func (m *Mitigator) CheckAndMitigate(key string) bool

CheckAndMitigate checks if a key is hot and applies mitigation if needed. Returns true if the key is hot (caller can decide to hint the client).

func (*Mitigator) GetMitigatedKeys

func (m *Mitigator) GetMitigatedKeys() map[string]MitigationStrategy

GetMitigatedKeys returns a snapshot of all mitigated keys and their strategies.

func (*Mitigator) GetShardedKey

func (m *Mitigator) GetShardedKey(key string, clientID string) string

GetShardedKey returns the shard key for a hot key based on a client identifier. For non-hot keys, returns the original key.

func (*Mitigator) IsMitigated

func (m *Mitigator) IsMitigated(key string) bool

IsMitigated returns true if a key has active mitigation.

func (*Mitigator) MitigationCount

func (m *Mitigator) MitigationCount() uint64

MitigationCount returns the total number of mitigations applied.

func (*Mitigator) WriteToShards

func (m *Mitigator) WriteToShards(key string, value []byte, ttl time.Duration)

WriteToShards writes a value to all shards of a hot key. This should be called instead of a normal Set for hot keys.

Jump to

Keyboard shortcuts

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