Debian Codegraph @

README.md

raw
1 [![Build Status](https://github.com/VictoriaMetrics/fastcache/workflows/main/badge.svg)](https://github.com/VictoriaMetrics/fastcache/actions)
2 [![GoDoc](https://godoc.org/github.com/VictoriaMetrics/fastcache?status.svg)](http://godoc.org/github.com/VictoriaMetrics/fastcache)
3 [![Go Report](https://goreportcard.com/badge/github.com/VictoriaMetrics/fastcache)](https://goreportcard.com/report/github.com/VictoriaMetrics/fastcache)
4 [![codecov](https://codecov.io/gh/VictoriaMetrics/fastcache/branch/master/graph/badge.svg)](https://codecov.io/gh/VictoriaMetrics/fastcache)
5
6 # fastcache - fast thread-safe inmemory cache for big number of entries in Go
7
8 ### Features
9
10 * Fast. Performance scales on multi-core CPUs. See benchmark results below.
11 * Thread-safe. Concurrent goroutines may read and write into a single
12 cache instance.
13 * The fastcache is designed for storing big number of entries without
14 [GC overhead](https://syslog.ravelin.com/further-dangers-of-large-heaps-in-go-7a267b57d487).
15 * Fastcache automatically evicts old entries when reaching the maximum cache size
16 set on its creation.
17 * [Simple API](http://godoc.org/github.com/VictoriaMetrics/fastcache).
18 * Simple source code.
19 * Cache may be [saved to file](https://godoc.org/github.com/VictoriaMetrics/fastcache#Cache.SaveToFile)
20 and [loaded from file](https://godoc.org/github.com/VictoriaMetrics/fastcache#LoadFromFile).
21 * Works on [Google AppEngine](https://cloud.google.com/appengine/docs/go/).
22
23
24 ### Benchmarks
25
26 `Fastcache` performance is compared with [BigCache](https://github.com/allegro/bigcache), standard Go map
27 and [sync.Map](https://golang.org/pkg/sync/#Map).
28
29 ```
30 GOMAXPROCS=4 go test github.com/VictoriaMetrics/fastcache -bench='Set|Get' -benchtime=10s
31 goos: linux
32 goarch: amd64
33 pkg: github.com/VictoriaMetrics/fastcache
34 BenchmarkBigCacheSet-4 2000 10566656 ns/op 6.20 MB/s 4660369 B/op 6 allocs/op
35 BenchmarkBigCacheGet-4 2000 6902694 ns/op 9.49 MB/s 684169 B/op 131076 allocs/op
36 BenchmarkBigCacheSetGet-4 1000 17579118 ns/op 7.46 MB/s 5046744 B/op 131083 allocs/op
37 BenchmarkCacheSet-4 5000 3808874 ns/op 17.21 MB/s 1142 B/op 2 allocs/op
38 BenchmarkCacheGet-4 5000 3293849 ns/op 19.90 MB/s 1140 B/op 2 allocs/op
39 BenchmarkCacheSetGet-4 2000 8456061 ns/op 15.50 MB/s 2857 B/op 5 allocs/op
40 BenchmarkStdMapSet-4 2000 10559382 ns/op 6.21 MB/s 268413 B/op 65537 allocs/op
41 BenchmarkStdMapGet-4 5000 2687404 ns/op 24.39 MB/s 2558 B/op 13 allocs/op
42 BenchmarkStdMapSetGet-4 100 154641257 ns/op 0.85 MB/s 387405 B/op 65558 allocs/op
43 BenchmarkSyncMapSet-4 500 24703219 ns/op 2.65 MB/s 3426543 B/op 262411 allocs/op
44 BenchmarkSyncMapGet-4 5000 2265892 ns/op 28.92 MB/s 2545 B/op 79 allocs/op
45 BenchmarkSyncMapSetGet-4 1000 14595535 ns/op 8.98 MB/s 3417190 B/op 262277 allocs/op
46 ```
47
48 `MB/s` column here actually means `millions of operations per second`.
49 As you can see, `fastcache` is faster than the `BigCache` in all the cases.
50 `fastcache` is faster than the standard Go map and `sync.Map` on workloads
51 with inserts.
52
53
54 ### Limitations
55
56 * Keys and values must be byte slices. Other types must be marshaled before
57 storing them in the cache.
58 * Big entries with sizes exceeding 64KB must be stored via [distinct API](http://godoc.org/github.com/VictoriaMetrics/fastcache#Cache.SetBig).
59 * There is no cache expiration. Entries are evicted from the cache only
60 on cache size overflow. Entry deadline may be stored inside the value in order
61 to implement cache expiration.
62
63
64 ### Architecture details
65
66 The cache uses ideas from [BigCache](https://github.com/allegro/bigcache):
67
68 * The cache consists of many buckets, each with its own lock.
69 This helps scaling the performance on multi-core CPUs, since multiple
70 CPUs may concurrently access distinct buckets.
71 * Each bucket consists of a `hash(key) -> (key, value) position` map
72 and 64KB-sized byte slices (chunks) holding encoded `(key, value)` entries.
73 Each bucket contains only `O(chunksCount)` pointers. For instance, 64GB cache
74 would contain ~1M pointers, while similarly-sized `map[string][]byte`
75 would contain ~1B pointers for short keys and values. This would lead to
76 [huge GC overhead](https://syslog.ravelin.com/further-dangers-of-large-heaps-in-go-7a267b57d487).
77
78 64KB-sized chunks reduce memory fragmentation and the total memory usage comparing
79 to a single big chunk per bucket.
80 Chunks are allocated off-heap if possible. This reduces total memory usage because
81 GC collects unused memory more frequently without the need in `GOGC` tweaking.
82
83
84 ### Users
85
86 * `Fastcache` has been extracted from [VictoriaMetrics](https://github.com/VictoriaMetrics/VictoriaMetrics) sources.
87 See [this article](https://medium.com/devopslinks/victoriametrics-creating-the-best-remote-storage-for-prometheus-5d92d66787ac)
88 for more info about `VictoriaMetrics`.
89
90
91 ### FAQ
92
93 #### What is the difference between `fastcache` and other similar caches like [BigCache](https://github.com/allegro/bigcache) or [FreeCache](https://github.com/coocood/freecache)?
94
95 * `Fastcache` is faster. See benchmark results above.
96 * `Fastcache` uses less memory due to lower heap fragmentation. This allows
97 saving many GBs of memory on multi-GB caches.
98 * `Fastcache` API [is simpler](http://godoc.org/github.com/VictoriaMetrics/fastcache).
99 The API is designed to be used in zero-allocation mode.
100
101
102 #### Why `fastcache` doesn't support cache expiration?
103
104 Because we don't need cache expiration in [VictoriaMetrics](https://github.com/VictoriaMetrics/VictoriaMetrics).
105 Cached entries inside `VictoriaMetrics` never expire. They are automatically evicted on cache size overflow.
106
107 It is easy to implement cache expiration on top of `fastcache` by caching values
108 with marshaled deadlines and verifying deadlines after reading these values
109 from the cache.
110
111
112 #### Why `fastcache` doesn't support advanced features such as [thundering herd protection](https://en.wikipedia.org/wiki/Thundering_herd_problem) or callbacks on entries' eviction?
113
114 Because these features would complicate the code and would make it slower.
115 `Fastcache` source code is simple - just copy-paste it and implement the feature you want
116 on top of it.