Share
Embed
Transcript
41 slides
-
About me Tomohiko Tanihata / @taniyarn 2 Software Engineer @enechain
https://taniyarn.dev -
ϚΠΫϩαʔϏεؒ௨৴ʹ͓͚ΔΩϟογϡ - enechainͰϚΠΫϩαʔϏεΞʔΩςΫνϟΛ࠾༻ - σʔλΞΫηεͰҰ෦ΩϟογϡΛར༻ - golang-lruΛ༻͍ͯ͠Δ 4 Microservice A
Microservice B Microservice B Microservice C Ωϟογϡར༻ ར༻݅ - සൟʹΞΫηε͞ΕΔ - มߋස͕͍ - σʔλͷҰ؏ੑ͕ݫີʹෆཁ -
LRU vs LFU LRUʢLeast Recently Usedʣ ࠷ۙΞΫηε͞Ε͍ͯͳ͍ΞΠςϜ͔Βআ 😁 ࣌ؒతہॴੑʹڧ͍ 😢
සൟʹ༻͞ΕΔ͕࠷ۙ༻͞Ε͍ͯͳ͍ΞΠςϜ͕আ LFUʢLeast Frequently Usedʣ සൟʹΞΫηε͞Ε͍ͯͳ͍ΞΠςϜ͔Βআ 😁 සൟʹΞΫηε͞ΕΔΞΠςϜΛอ࣋ɺߴසΞΫηεʹڧ͍ 😢 ظؒʹଟ͘ΞΫηε͞Ε͕ͨظతʹΘΕͳ͍ΞΠςϜ͕อ࣋ 6 -
LRU vs LFU 7 key ۙͷࢀর࣌ؒ ࢀরճ 1 10:00:00 5
2 11:00:00 3 3 12:00:00 1 4 13:00:00 2 5 14:00:00 1 Ճ আରʢLRUʣ আରʢLFUʣ -
golang-lruͷػೳҰཡ 9 - Simple LRUʢ ୯७ͳLRUΩϟογϡ - Expirable LRU ʢLRU
+ TTLʣ LRUʹՃ͑ͯɺΞΠςϜ͕ࢦఆ͞Εͨ࣌ؒܦաޙʹظݶΕʹͳΔػೳΛ࣋ͭ - TwoQueueCacheʢLRU + LFUʣ ࠷ۙ༻͞ΕͨΤϯτϦͱසൟʹ༻͞ΕΔΤϯτϦΛผʑʹ - Adaptive Replacement Cacheʢ LRUͱLFUͷ྆ํͷϝϦοτΛऔΓೖΕ -
- Simple LRUʢ ୯७ͳLRUΩϟογϡ - Expirable LRU ʢLRU + TTLʣ
LRUʹՃ͑ͯɺΞΠςϜ͕ࢦఆ͞Εͨ࣌ؒܦաޙʹظݶΕʹͳΔػೳΛ࣋ͭ - TwoQueueCacheʢLRU + LFUʣ ࠷ۙ༻͞ΕͨΤϯτϦͱසൟʹ༻͞ΕΔΤϯτϦΛผʑʹ - Adaptive Replacement Cacheʢ LRUͱLFUͷ྆ํͷϝϦοτΛऔΓೖΕ golang-lruͷػೳҰཡ 10 ͜Εͷ -
Simple LRUͷΞϧΰϦζϜ 12 prev value next prev value next prev
value next ํ॥ϦετΛར༻ͯ͠ॱ൪ཧ - ઌ಄ͷprevΛඌʹ࿈݁ - ඌͷnextΛઌ಄ʹ࿈݁ - ϦετͷඌΛࢀর͍ͨ࣌͠ʹઌ಄ͷprevΛݟΕΑ͍ Entry -
Simple LRUͷΞϧΰϦζϜ 13 Size 4ͷListʹEntry͕Ճ͞Ε͍ͯ͘έʔεΛߟ͑Δ root อଘEntry อଘEntry Size 4
ۭͷ߹root͚ͩଘࡏ rootࣗମEntry -
อଘEntry Simple LRUͷΞϧΰϦζϜ 14 entAΛՃ root entA อଘEntry - entA
entA.prev = root entA.next = root root.next = entA root.prev = entA Size 4 rootͷྡʹentAΛૠೖ -
อଘEntry entB Simple LRUͷΞϧΰϦζϜ 15 entBΛՃ root entA อଘEntry -
entA - entB Size 4 entB.prev = root entB.next = entA root.next = entB entA.prev = entB rootͱentAͷؒʹentBΛૠೖ -
อଘEntry entC entB Simple LRUͷΞϧΰϦζϜ 16 entCΛՃ root entA อଘEntry
- entA - entB - entC Size 4 entC.prev = root entC.next = entB root.next = entC entB.prev = entC rootͱentBͷؒʹentAΛૠೖ -
อଘEntry อଘEntry - entA - entB - entC - entD
entD entC entB Simple LRUͷΞϧΰϦζϜ 17 entDΛՃ root entA Size 4 entD.prev = root entD.next = entC root.next = entD entC.prev = entD rootͱentCͷؒʹentDΛૠೖ -
อଘEntry entE entD entC entB Simple LRUͷΞϧΰϦζϜ 18 entEΛՃ root
อଘEntry - entB - entC - entD - entE Size 4 entE.prev = root entE.next = entD root.next = entE entD.prev = entE rootͱentDͷؒʹentEΛૠೖ entA👋 🗑 -
อଘEntry entC entE entD entB Simple LRUͷΞϧΰϦζϜ 19 entCΛՃ root
อଘEntry - entB - entC - entD - entE Size 4 entE.next = entD entB.prev = entD entC.prev = root entC.next = entE root.next = entC entE.prev = entC rootͱentEͷؒʹentCΛҠಈ อଘEntryมΘΒͣ -
Simple LRUͷ෦࣮ΛಡΉ αϯϓϧίʔυ 20 func main() { l, _ :=
lru.New[int, any](128) for i := 0; i < 256; i++ { l.Add(i, nil) } if l.Len() != 128 { panic(fmt.Sprintf("bad len: %v", l.Len())) } } sizeҰகΛ֬ೝ -
Simple LRUͷ෦࣮ΛಡΉ LRU structΛݟͯΈΔ 21 // LRU implements a non-thread
safe fixed size LRU cache type LRU[K comparable, V any] struct { size int evictList *internal.LruList[K, V] items map[K]*internal.Entry[K, V] onEvict EvictCallback[K, V] } ํ॥Ϧετ KVΛอ࣋͢ΔMap -
Simple LRUͷ෦࣮ΛಡΉ AddΛݟͯΈΔ: طʹListʹitem͕ଘࡏ͍ͯ͠Δ߹ 22 func (c *LRU[K, V]) Add(key
K, value V) (evicted bool) { // Check for existing item if ent, ok := c.items[key]; ok { c.evictList.MoveToFront(ent) ent.Value = value return false } // Add new item … } Mapʹ͕͋Δ͔֬ೝ Listͷઌ಄ʹҠಈ -
Simple LRUͷ෦࣮ΛಡΉ AddΛݟͯΈΔ: Listʹitem͕ଘࡏ͠ͳ͍߹ 23 func (c *LRU[K, V]) Add(key
K, value V) (evicted bool) { // Check for existing item … // Add new item ent := c.evictList.PushFront(key, value) c.items[key] = ent // Verify size not exceeded if c.evictList.Length() > c.size { c.removeOldest() } return evict } Listͷઌ಄ʹૠೖ ListͷඌΛআ -
Expirable LRUͷΞϧΰϦζϜ Simple LRUʹTTLͷ੍ޚΛՃ - TTLʢTime To Liveʣ - Entryͷੜଘ࣌ؒʢ࣍ͷྫͰ3sʣ
- EntryTTLܦաޙʹBucket୯ҐͰআ - Bucketʢόέπʣ - আ͢ΔEntryΛཧ͢Δശ - BucketΛnݸ༻ҙ͢Δ߹ɺTTLͰnճআʢ࣍ͷྫͰ3ݸɻ΄Μ·100ݸʣ 26 -
Expirable LRUͷΞϧΰϦζϜ ʢA, B, B, CʣͷॱʹAdd͞Εͨ߹ 27 Next Cleanup Bucket
Add Bucket Time 0 2 1 0 2 TTL 4 5 1 0 2 A Entry B Buckets ܦա࣌ؒ0.0s B C TTL3s Bucket3ݸ ܦա࣌ؒ s -
Expirable LRUͷΞϧΰϦζϜ ʢA, B, B, CʣͷॱʹAdd͞Εͨ߹ 28 Next Cleanup Bucket
Add Bucket Time 0 2 1 0 2 TTL 4 5 1 0 2 A Entry B Buckets A B Delete 1 0 ܦա࣌ؒ1.0s B C A, B͕Bucket2ʹՃ ܦա࣌ؒ s -
Expirable LRUͷΞϧΰϦζϜ ʢA, B, B, CʣͷॱʹAdd͞Εͨ߹ 29 Next Cleanup Bucket
Add Bucket Time 0 2 1 0 2 TTL 4 5 1 0 2 A Entry B Buckets A B 1 0 Delete 2 1 ܦա࣌ؒ2.0s B C C BBucket0ʹҠಈ ܦա࣌ؒ s -
Expirable LRUͷΞϧΰϦζϜ ʢA, B, B, CʣͷॱʹAdd͞Εͨ߹ 30 Next Cleanup Bucket
Add Bucket Time 0 2 1 0 2 TTL 4 5 1 0 2 A Entry B Buckets 1 0 2 1 A ܦա࣌ؒ3.0s B C B C Bucket2ͷEntryͷTTL͕ܦա͢Δ·Ͱͭ ܦա࣌ؒ s -
Expirable LRUͷΞϧΰϦζϜ ʢA, B, B, CʣͷॱʹAdd͞Εͨ߹ 31 Next Cleanup Bucket
Add Bucket Time 0 2 1 0 2 TTL 4 5 1 0 2 A Entry B Buckets ܦա࣌ؒ3.4s 1 0 2 1 Delete B C B C Aআ 0.4sԆ 0 2 ܦա࣌ؒ s -
Expirable LRUͷΞϧΰϦζϜ ʢA, B, B, CʣͷॱʹAdd͞Εͨ߹ 32 Next Cleanup Bucket
Add Bucket Time 0 2 1 0 2 TTL 4 5 1 0 2 A Entry B Buckets ܦա࣌ؒ4.6s 1 0 2 1 Delete B C 0 2 B, Cআ ͍ํʹ߹Θͤͯ0.6s BTTL͑ͯ গ͠ͷ͚ؒͩ BucketʹΔ ܦա࣌ؒ s -
Expirable LRUͷ෦࣮ΛಡΉ αϯϓϧίʔυ 33 func main() { // make cache
with 10ms TTL and 5 max keys cache := expirable.NewLRU[string, string](5, nil, time.Millisecond*10) // set value under key1. cache.Add("key1", "val1") // wait for cache to expire time.Sleep(time.Millisecond * 12) // get value under key1 after key expiration r, ok = cache.Get("key1") fmt.Printf("value after expiration is found: %v, value: %q\n", ok, r) } TTLΛ͑ΔͷΛͭ Entry͕আ͞Ε͍ͯΔ -
Expirable LRUͷ෦࣮ΛಡΉ 34 LRU structΛݟͯΈΔ type LRU[K comparable, V any]
struct { size int evictList *internal.LruList[K, V] items map[K]*internal.Entry[K, V] onEvict EvictCallback[K, V] // expirable options mu sync.Mutex ttl time.Duration done chan struct{} // buckets for expiration buckets []bucket[K, V] // uint8 because it's number between 0 and numBuckets nextCleanupBucket uint8 } Simple LRUͱಉ͡ TTL͕ఆٛ Bucket͕ఆٛ -
Expirable LRUͷ෦࣮ΛಡΉ 35 AddΛݟͯΈΔ: طʹListʹitem͕ଘࡏ͍ͯ͠Δ߹ func (c *LRU[K, V]) Add(key
K, value V) (evicted bool) { c.mu.Lock() defer c.mu.Unlock() now := time.Now() // Check for existing item if ent, ok := c.items[key]; ok { c.evictList.MoveToFront(ent) c.removeFromBucket(ent) ent.Value = value ent.ExpiresAt = now.Add(c.ttl) c.addToBucket(ent) return false } // Add new item … } Mapʹ͕͋Δ͔֬ೝ BucketʹՃ Bucket͔Βআ -
Expirable LRUͷ෦࣮ΛಡΉ 36 AddΛݟͯΈΔ: Listʹitem͕ଘࡏ͠ͳ͍߹ func (c *LRU[K, V]) Add(key
K, value V) (evicted bool) { // Check for existing item … // Add new item ent := c.evictList.PushFrontExpirable(key, value, now.Add(c.ttl)) c.items[key] = ent c.addToBucket(ent) evict := c.size > 0 && c.evictList.Length() > c.size // Verify size not exceeded if evict { c.removeOldest() } return evict } Size͑ͯͨΒListͷඌΛআ Listͷઌ಄ʹૠೖ BucketʹՃ -
Expirable LRUͷ෦࣮ΛಡΉ 37 BucketsͰͷআΛͬͯΈΔ: NewLRUฤ func NewLRU(size int, onEvict EvictCallback,
ttl time.Duration) *LRU { … if res.ttl != noEvictionTTL { go func(done <-chan struct{}) { ticker := time.NewTicker(res.ttl / numBuckets) defer ticker.Stop() for { select { case <-done: return case <-ticker.C: res.deleteExpired() } } }(res.done) } return &res } TTL / numBucketsඵ͝ͱʹൃՐ goroutine্ཱͪ͛ͯtickerىಈ -
Expirable LRUͷ෦࣮ΛಡΉ 38 BucketsͰͷআΛͬͯΈΔ: deleteExpiredฤ func (c *LRU[K, V]) deleteExpired()
{ c.mu.Lock() bucketIdx := c.nextCleanupBucket timeToExpire := time.Until(c.buckets[bucketIdx].newestEntry) // wait for newest entry to expire before cleanup without holding lock if timeToExpire > 0 { c.mu.Unlock() time.Sleep(timeToExpire) c.mu.Lock() } for _, ent := range c.buckets[bucketIdx].entries { c.removeElement(ent) } c.nextCleanupBucket = (c.nextCleanupBucket + 1) % numBuckets c.mu.Unlock() } BucketΛۭʹ͢Δ BucketͷEntryͷੜଘظ͕ؒΕΔ·Ͱͭ -
Expirable LRUͷ෦࣮ΛಡΉ 39 GetΛݟͯΈΔ func (c *LRU[K, V]) Get(key K)
(value V, ok bool) { c.mu.Lock() defer c.mu.Unlock() var ent *internal.Entry[K, V] if ent, ok = c.items[key]; ok { // Expired item check if time.Now().After(ent.ExpiresAt) { return value, false } c.evictList.MoveToFront(ent) return ent.Value, true } return } Mapʹଘࡏͯ͠ TTL͑ͨͷฦ͞ͳ͍ -
·ͱΊ 40 - golang-lruͷSimple LRUͱExpirable LRUͷΞϧΰϦζϜͱ࣮Λશཧղͨ͠ - Simple LRUͰํ॥ϦετΛ༻͍ͯޮΑ͘ॱং੍ޚ͞Ε͍ͯͨ -
Expirable LRUͰ1ͭͷtickerͷgoroutine͚ͩͰݡ͘TTL͕ཧ͞Ε͍ͯͨ