Documentation
¶
Index ¶
- func ShardKeyName(key string, shardIndex int) string
- type CountMinSketch
- type Detector
- type MitigationStrategy
- type Mitigator
- func (m *Mitigator) CheckAndMitigate(key string) bool
- func (m *Mitigator) GetMitigatedKeys() map[string]MitigationStrategy
- func (m *Mitigator) GetShardedKey(key string, clientID string) string
- func (m *Mitigator) IsMitigated(key string) bool
- func (m *Mitigator) MitigationCount() uint64
- func (m *Mitigator) WriteToShards(key string, value []byte, ttl time.Duration)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ShardKeyName ¶
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) 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 ¶
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 ¶
GetFrequency returns the estimated access frequency for a key.
func (*Detector) RecordAccess ¶
RecordAccess records an access to a key and returns whether the key is hot.
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 ¶
NewMitigator creates a new hot key mitigator. shardCount is the number of shards to split hot keys into (e.g., 4).
func (*Mitigator) CheckAndMitigate ¶
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 ¶
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 ¶
IsMitigated returns true if a key has active mitigation.
func (*Mitigator) MitigationCount ¶
MitigationCount returns the total number of mitigations applied.