Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HardMaxCacheSize to local cache config #165

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions pkg/zcache/config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package zcache

import (
"time"

"github.com/allegro/bigcache/v3"
"github.com/go-redis/redis/v8"
"github.com/zondax/golem/pkg/logger"
"github.com/zondax/golem/pkg/metrics"
"time"
)

const hardMaxCacheSizeDefault = 512

type StatsMetrics struct {
Enable bool
UpdateInterval time.Duration
Expand Down Expand Up @@ -40,11 +43,12 @@ type RemoteConfig struct {
}

type LocalConfig struct {
Prefix string
Logger *logger.Logger
MetricServer metrics.TaskMetrics
StatsMetrics StatsMetrics
CleanupProcess CleanupProcess
Prefix string
Logger *logger.Logger
MetricServer metrics.TaskMetrics
StatsMetrics StatsMetrics
CleanupProcess CleanupProcess
HardMaxCacheSizeInMB int
}

func (c *RemoteConfig) ToRedisConfig() *redis.Options {
Expand All @@ -66,8 +70,14 @@ func (c *RemoteConfig) ToRedisConfig() *redis.Options {
}

func (c *LocalConfig) ToBigCacheConfig() bigcache.Config {
evictionTime := time.Duration(100*365*24) * time.Hour // 100 years
return bigcache.DefaultConfig(evictionTime)
config := bigcache.DefaultConfig(time.Duration(100*365*24) * time.Hour)

if c.HardMaxCacheSizeInMB <= 0 {
c.HardMaxCacheSizeInMB = hardMaxCacheSizeDefault
}
config.HardMaxCacheSize = c.HardMaxCacheSizeInMB

return config
}

type CombinedConfig struct {
Expand Down
32 changes: 30 additions & 2 deletions pkg/zcache/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ It's important to note that MetricServer is a mandatory configuration field in L
func main() {
config := zcache.LocalConfig{
// CleanupInterval is optional; if omitted, a default value of 12 hours will be used
CleanupProcess: CleanupProcess{
CleanupProcess: CleanupProcess{
Interval: 30 * time.Minute,
},
MetricServer: metricServer,
// HardMaxCacheSizeInMB is optional; if omitted, a default value of 512MB will be used
HardMaxCacheSizeInMB: 1024, // Set maximum cache size to 1GB
MetricServer: metricServer,
}

cache, err := zcache.NewLocalCache(&config)
Expand Down Expand Up @@ -182,3 +184,29 @@ func TestSomeFunctionWithMutex(t *testing.T) {
}
```

## Best Practices

### Memory Management
When using the local cache (BigCache), it's important to understand how memory is managed:

1. **Memory Growth**: BigCache allocates memory in chunks and doesn't immediately release memory when items are deleted or overwritten
2. **Memory Limit**: The `HardMaxCacheSizeInMB` parameter (default: 512MB) is crucial to prevent unbounded memory growth
3. **Actual Memory Usage**: The total memory consumption will be slightly higher than `HardMaxCacheSizeInMB` due to:
- Shard overhead (approximately 2×(64+32)×n bits per entry)
- Internal map structures
- Go runtime overhead


### Memory Monitoring
Monitor cache memory usage through the provided metrics:
- `local_cache_cleanup_item_count`
- `local_cache_cleanup_deleted_item_count`
- `local_cache_cleanup_errors`
- `local_cache_cleanup_last_run`

### Notes
- BigCache has a known memory leak issue (see [issue #369](https://github.com/allegro/bigcache/issues/369)). To mitigate this:
1. Always set `HardMaxCacheSizeInMB` (default: 512MB)
2. Monitor memory usage through provided metrics
3. Consider using shorter cleanup intervals in high-traffic scenarios
4. For critical production environments, consider using the Combined Cache with Redis as primary storage
Loading