Debian Codegraph @

README.md

raw symbols SCIP
1 [![Go Reference](https://pkg.go.dev/badge/github.com/scalalang2/golang-fifo.svg)](https://pkg.go.dev/github.com/scalalang2/golang-fifo)
2 [![Go Report Card](https://goreportcard.com/badge/github.com/scalalang2/golang-fifo)](https://goreportcard.com/report/github.com/scalalang2/golang-fifo)
3 ![MIT License](https://img.shields.io/badge/license-MIT-_red.svg)
4 [![Coverage Status](https://coveralls.io/repos/github/scalalang2/golang-fifo/badge.svg?branch=main)](https://coveralls.io/github/scalalang2/golang-fifo?branch=main)
5
6 <h1 align="center">golang-fifo</h1>
7
8 This is a modern cache implementation, **inspired** by the following papers, provides high efficiency.
9
10 - **SIEVE** | [SIEVE is Simpler than LRU: an Efficient Turn-Key Eviction Algorithm for Web Caches](https://junchengyang.com/publication/nsdi24-SIEVE.pdf) (NSDI'24)
11 - **S3-FIFO** | [FIFO queues are all you need for cache eviction](https://dl.acm.org/doi/10.1145/3600006.3613147) (SOSP'23)
12
13 This offers state-of-the-art efficiency and scalability compared to other LRU-based cache algorithms.
14
15 ## Basic Usage
16 ```go
17 import "github.com/scalalang2/golang-fifo/sieve"
18
19 size := 1e5
20 ttl := 0 // 0 means no expiration
21 cache := sieve.New[string, string](size, ttl)
22
23 // set value under hello
24 cache.Set("hello", "world")
25
26 // get value under hello
27 val, ok := cache.Get("hello")
28 if ok {
29 fmt.Printf("value: %s", val) // => "world"
30 }
31
32 // set more keys
33 for i := 0; i < 10; i++ {
34 cache.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i))
35 }
36
37 // get number of cache entries
38 fmt.Printf("len: %d", cache.Len()) // => 11
39
40 // remove value under hello
41 removed := cache.Remove("hello")
42 if removed {
43 fmt.Println("hello was removed")
44 }
45 ```
46
47 ## Expiry
48 ```go
49 import "github.com/scalalang2/golang-fifo/sieve"
50
51 size := 1e5
52 ttl := time.Second * 3600 // 1 hour
53 cache := sieve.New[string, string](size, ttl)
54
55 // this callback will be called when the element is expired
56 cahe.SetOnEvict(func(key string, value string) {
57 fmt.Printf("key: %s, value: %s was evicted", key, value)
58 })
59
60 // set value under hello
61 cache.Set("hello", "world")
62
63 // remove all cache entries and stop the eviction goroutine.
64 cache.Close()
65 ```
66
67 ## Benchmark Result
68 The benchmark result were obtained using [go-cache-benchmark](https://github.com/scalalang2/go-cache-benchmark)
69
70 ```
71 itemSize=500000, workloads=7500000, cacheSize=0.10%, zipf's alpha=0.99, concurrency=16
72
73 CACHE | HITRATE | QPS | HITS | MISSES
74 -----------------+---------+---------+---------+----------
75 sieve | 47.66% | 2508361 | 3574212 | 3925788
76 tinylfu | 47.37% | 2269542 | 3552921 | 3947079
77 s3-fifo | 47.17% | 1651619 | 3538121 | 3961879
78 slru | 46.49% | 2201350 | 3486476 | 4013524
79 s4lru | 46.09% | 2484266 | 3456682 | 4043318
80 two-queue | 45.49% | 1713502 | 3411800 | 4088200
81 clock | 37.34% | 2370417 | 2800750 | 4699250
82 lru-groupcache | 36.59% | 2206841 | 2743894 | 4756106
83 lru-hashicorp | 36.57% | 2055358 | 2743000 | 4757000
84 ```
85
86 **SIEVE** delivers both high hit rates and the highest QPS(queries per seconds) compared to other LRU-based caches.
87 Additionally, It approximately improves 30% for efficiency than a simple LRU cache.
88
89 Increasing efficiency means not only reducing cache misses,
90 but also reducing the demand for heavy operations such as backend database access, which lowers the mean latency.
91
92 While LRU promotes accessed objects to the head of the queue,
93 requiring a potentially slow lock acquisition,
94 SIEVE only needs to update a single bit upon a cache hit.
95 This update can be done with a significantly faster reader lock, leading to increased performance.
96
97 The real-world traces are also evaluated at [here](https://observablehq.com/@1a1a11a/sieve-miss-ratio-plots)
98
99 ## Appendix
100
101 <details>
102 <summary>Performance : golang-fifo</summary>
103
104 ```shell
105 goos: linux
106 goarch: amd64
107 pkg: github.com/scalalang2/golang-fifo
108 cpu: Intel(R) Core(TM) i5-10600KF CPU @ 4.10GHz
109 BenchmarkCache
110 BenchmarkCache/cache=sieve
111 BenchmarkCache/cache=sieve/t=int32
112 BenchmarkCache/cache=sieve/t=int32-12 2765682 393.8 ns/op 148 B/op 4 allocs/op
113 BenchmarkCache/cache=sieve/t=int32-12 3037669 388.1 ns/op 149 B/op 4 allocs/op
114 BenchmarkCache/cache=sieve/t=int32-12 3075998 395.0 ns/op 149 B/op 4 allocs/op
115 BenchmarkCache/cache=sieve/t=int32-12 2924646 392.0 ns/op 148 B/op 4 allocs/op
116 BenchmarkCache/cache=sieve/t=int32-12 2632326 409.3 ns/op 148 B/op 4 allocs/op
117 BenchmarkCache/cache=sieve/t=int32-12 2746551 463.5 ns/op 148 B/op 4 allocs/op
118 BenchmarkCache/cache=sieve/t=int32-12 3004071 401.0 ns/op 148 B/op 4 allocs/op
119 BenchmarkCache/cache=sieve/t=int32-12 2398981 456.0 ns/op 149 B/op 4 allocs/op
120 BenchmarkCache/cache=sieve/t=int32-12 2698939 422.9 ns/op 148 B/op 4 allocs/op
121 BenchmarkCache/cache=sieve/t=int32-12 2647030 392.1 ns/op 148 B/op 4 allocs/op
122 BenchmarkCache/cache=sieve/t=int64
123 BenchmarkCache/cache=sieve/t=int64-12 2532614 414.1 ns/op 158 B/op 4 allocs/op
124 BenchmarkCache/cache=sieve/t=int64-12 2825973 419.3 ns/op 158 B/op 4 allocs/op
125 BenchmarkCache/cache=sieve/t=int64-12 2693790 407.1 ns/op 158 B/op 4 allocs/op
126 BenchmarkCache/cache=sieve/t=int64-12 2882792 414.7 ns/op 157 B/op 4 allocs/op
127 BenchmarkCache/cache=sieve/t=int64-12 2903197 421.7 ns/op 157 B/op 4 allocs/op
128 BenchmarkCache/cache=sieve/t=int64-12 2876046 435.7 ns/op 157 B/op 4 allocs/op
129 BenchmarkCache/cache=sieve/t=int64-12 2846494 410.4 ns/op 157 B/op 4 allocs/op
130 BenchmarkCache/cache=sieve/t=int64-12 2455807 440.1 ns/op 158 B/op 4 allocs/op
131 BenchmarkCache/cache=sieve/t=int64-12 2774462 435.1 ns/op 158 B/op 4 allocs/op
132 BenchmarkCache/cache=sieve/t=int64-12 2833150 433.9 ns/op 157 B/op 4 allocs/op
133 BenchmarkCache/cache=sieve/t=string
134 BenchmarkCache/cache=sieve/t=string-12 2117859 546.9 ns/op 186 B/op 4 allocs/op
135 BenchmarkCache/cache=sieve/t=string-12 2079752 527.1 ns/op 186 B/op 4 allocs/op
136 BenchmarkCache/cache=sieve/t=string-12 2210930 530.8 ns/op 186 B/op 4 allocs/op
137 BenchmarkCache/cache=sieve/t=string-12 2122942 514.4 ns/op 186 B/op 4 allocs/op
138 BenchmarkCache/cache=sieve/t=string-12 2222488 553.6 ns/op 186 B/op 4 allocs/op
139 BenchmarkCache/cache=sieve/t=string-12 2260266 558.6 ns/op 186 B/op 4 allocs/op
140 BenchmarkCache/cache=sieve/t=string-12 2239196 567.1 ns/op 186 B/op 4 allocs/op
141 BenchmarkCache/cache=sieve/t=string-12 2064308 576.8 ns/op 186 B/op 4 allocs/op
142 BenchmarkCache/cache=sieve/t=string-12 1882754 569.9 ns/op 185 B/op 4 allocs/op
143 BenchmarkCache/cache=sieve/t=string-12 1917342 574.6 ns/op 185 B/op 4 allocs/op
144 BenchmarkCache/cache=sieve/t=composite
145 BenchmarkCache/cache=sieve/t=composite-12 1825063 707.0 ns/op 223 B/op 4 allocs/op
146 BenchmarkCache/cache=sieve/t=composite-12 1745775 660.1 ns/op 224 B/op 4 allocs/op
147 BenchmarkCache/cache=sieve/t=composite-12 1680552 678.1 ns/op 225 B/op 4 allocs/op
148 BenchmarkCache/cache=sieve/t=composite-12 1774438 690.1 ns/op 224 B/op 4 allocs/op
149 BenchmarkCache/cache=sieve/t=composite-12 1530580 731.1 ns/op 226 B/op 4 allocs/op
150 BenchmarkCache/cache=sieve/t=composite-12 1663950 761.7 ns/op 225 B/op 4 allocs/op
151 BenchmarkCache/cache=sieve/t=composite-12 1607760 678.4 ns/op 225 B/op 4 allocs/op
152 BenchmarkCache/cache=sieve/t=composite-12 1703283 784.4 ns/op 225 B/op 4 allocs/op
153 BenchmarkCache/cache=sieve/t=composite-12 1295089 864.6 ns/op 229 B/op 4 allocs/op
154 BenchmarkCache/cache=sieve/t=composite-12 1552182 769.9 ns/op 226 B/op 4 allocs/op
155 ```
156 </details>
157
158 <details>
159 <summary>Why LRU Cache is not good enough?</summary>
160
161 - LRU is often implemented with a doubly linked list and a hash table, requiring two pointers per cache entry,
162 which becomes large overhead when the object is small.
163 - It promotes objects to the head of the queue upon cache hit, which performs at least six random memory accesses
164 protected by lock, which limits the scalability.
165 </details>
166
167 <details>
168 <summary>Things to consider before adoption</summary>
169
170 - Both **S3-FIFO** and **SIEVE** have a O(n) time complexity for cache eviction,
171 which only occurs when all objects are hit the cache, which means that there is a perfect (100%) hit rate in the cache.
172 - **SIEVE** is not designed to be scan-resistant. Therefore, it's currently recommended for web cache workloads,
173 which typically follow a power-law distribution.
174 - **S3-FIFO** filters out one-hit-wonders early, It bears some resemblance to designing scan-resistant cache eviction algorithms.
175 - **SIEVE** scales well for read-intensive applications such as blogs and online shops, because it doesn't require to hold a writer lock on cache hit.
176 - The `golang-fifo` library aims to provide a straightforward and efficient cache implementation,
177 similar to [hashicorp-lru](https://github.com/hashicorp/golang-lru) and [groupcache](https://github.com/golang/groupcache).
178 Its goal is not to outperform highly specialized in-memory cache libraries (e.g. [bigcache](https://github.com/allegro/bigcache), [freecache](https://github.com/coocood/freecache) and etc).
179 </details>
180
181 <details>
182 <summary>Brief overview of SIEVE & S3-FIFO</summary>
183
184 Various workloads typically follows **Power law distribution (e.g. Zipf's law)** as shown in the following figure.
185
186 ![zipflaw_discovered_by_realworld](./docs/zipf_law_discovered_by_realworld_traces.png)
187
188 The analysis reveals that most requests are "one-hit-wonders", which means it's accessed only once.
189 Consequently, a cache eviction strategy should quickly remove most objects after insertion.
190
191 **S3-FIFO** and **SIEVE** achieves this goal with simplicity, efficiency, and scalability using simple FIFO queue only.
192
193 ![s3-fifo-is-powerful-algorithm](./docs/graphs_shows_s3_fifo_is_powerful.png)
194 </details>
195
196 ## Contribution
197 How to run unit test
198 ```bash
199 $ go test -v ./...
200 ```
201
202 How to run benchmark test
203 ```bash
204 $ ./bench.sh
205 ```