in ,

allegro / bigcache, Hacker News

allegro / bigcache, Hacker News


                    

        

Fast, concurrent, evicting in-memory cache written to keep big number of entries without impact on performance. BigCache keeps entries on heap but omits GC for them. To achieve that operations on bytes arrays take place, therefore entries (de) serialization in front of the cache will be needed in most use cases.

Requires Go 1. 11 or newer.

importgithub.com/allegro/bigcachecache, (_) ******************************:=bigcache. ********************* (NewBigCache) ******************** (bigcache.) ******************** (DefaultConfig) ********************11* time.Minute)) cache.Set(

********************** (my-unique-key) ********************** (byte) ****************** (********************** value

)entry, (_) ******************************:=cache. ************** (Get) ************** (fmt.Println( (string(entry))************************** Custom initialization

When cache load can be predicted in advance then it is better to use custom initialization because additional memory allocation can be avoided in that way.

import**************** (log“**************************************** github.com/allegro/bigcache)config(*******************:=bigcache.) ****************** (Config

)( number of shards (must be a power of 2)Shards:2016( time after which entry can be evictedLifeWindow:* time.Minute, ( Interval between removing expired entries (clean up).
(**************** (If set to(**************** (Setting toCleanWindow:  (5) ******************** (time.) ******************* Minute, ( rps * lifeWindow, used only in initial memory allocation
MaxEntriesInWindow:127511*

*************************************************************************************************************************

,

(

max entry size in bytes, used only in initial memory allocation
MaxEntrySize:, ( prints information about additional memory allocationVerbose:  true, ( cache will not allocate more memory than this limit, value in MB

( if value is reached then the oldest entries can be overridden for the new ones( 0 value means no size limitHardMaxCacheSize:050212, ( callback fired when the oldest entry is removed because of its expiration time or no space left( for the new entry, or because delete was called. A bitmask representing the reason will be returned.( Default value is nil which means no callback and it prevents from unwrapping the oldest entry.OnRemove:nil, ( OnRemoveWithReason is a callback fired when the oldest entry is removed because of its expiration time or no space left( for the new entry, or because delete was called. A constant representing the reason will be passed through.( Default value is nil which means no callback and it prevents from unwrapping the oldest entry.( Ignored if OnRemove is specified.OnRemoveWithReason:nil, }cache, (initErr**********************:=bigcache. ********************* NewBigCache(config)ifinitErr!=nillog.Fatal(initErr ) } cache.Set(

********************** (my-unique-key) ********************** (byte) ****************** (********************** value

) if**************** (entry, (err) :=cache.Get(my-unique-key; err==nil{ fmt.Println(string(entry)) }**************************** (LifeWindow) &

          **************************

****************

Three caches were compared: bigcache, (freecacheand map. Benchmark tests were made using an i7 – (K CPU @ 4.) ****************************************************************************************************************************************************** (GHz with) *********************************************************************************************************************************** (GB of RAM on Ubuntu) ************************************************************************************************************************************************************************************************************************************************************** (LTS) 5.2.****************************************************************************************************************************** – – – generic).

Benchmarks source code can be found here

Writes and reads

go version go version go1. linux / amd (************************************************************************************************************************** gotest-bench=. -benchmem -benchtime=4s. / … -timeout m goos: linux goarch: amd pkg: github.com/allegro/bigcache/v2/caches_bench BenchmarkMapSet-8 (****************************************************************************************** (ns / op) ********************************************************************************************************** B / op 3 allocs / op BenchmarkConcurrentMapSet-8 (************************************************************************************ (ns / op) ************************************************************************************************ B / op 8 allocs / op BenchmarkFreeCacheSet-8 (**************************************************************************************** (ns / op) ************************************************************************************************** (B / op 2 allocs / op BenchmarkBigCacheSet-8 (****************************************************************************************** (ns / op) **************************************************************************************************** B / op 2 allocs / op BenchmarkMapGet-8 34222654 (ns / op) B / op 1 allocs / op BenchmarkConcurrentMapGet-8 (****************************************************************************************** (ns / op) B / op 2 allocs / op BenchmarkFreeCacheGet-8 ****************************************************************** (ns / op) ************************************************************************************************************** B / op 2 allocs / op BenchmarkBigCacheGet-8 (****************************************************************************************** (ns / op) ********************************************************************************************************** B / op 4 allocs / op BenchmarkBigCacheSetParallel-8 (********************************************************************************************** ns / op B / op 3 allocs / op BenchmarkFreeCacheSetParallel-8 (************************************************************************************** (ns / op) ********************************************************************************************** B / op 3 allocs / op BenchmarkConcurrentMapSetParallel-8 (********************************************************************************************** ns / op

************************************************************************************ B / op 6 allocs / op BenchmarkBigCacheGetParallel-8 (********************************************************************************************************************. 1 ns / op B / op 4 allocs / op BenchmarkFreeCacheGetParallel-8 (************************************************************************************************ ns / op B / op 3 allocs / op BenchmarkConcurrentMapGetParallel-8 (********************************************************************************************** ns / op B / op 2 allocs / op PASS ok github.com/allegro/bigcache/v2/caches_bench ************************************************************************ (s) ********************

Writes and reads in bigcache are faster than in freecache. Writes to map are the slowest.

GC pause time

go version go version go1. linux / amd (************************************************************************************************************************** go run caches_gc_overhead_comparison.go Number of entries: GC pause

forbigcache: 1. ms GC pauseforfreecache: 5. 01575879 ms GC pauseformap: 9. (ms)

Memory usage

You may encounter system memory reporting what appears to be an exponential increase, however this is expected behavior. Go runtime allocates memory in chunks or ‘spans’ and will inform the OS when they are no longer required by changing their state to ‘idle’. The ‘spans’ will remain part of the process resource usage until the OS needs to repurpose the address. Further reading available

How it works

BigCache relies on optimization presented in 1.5 version of Go ( (issue -) **********************************************************************

). This optimization states that if map without pointers in keys and values ​​is used then GC will omit its content. Therefore BigCache usesmap [uint64] uintkeys are hashed and values ​​are offsets of entries.

Entries are kept in bytes array, to omit GC again. Bytes array size can grow to gigabytes without impact on performance because GC will only see single pointer to it.

Collisions

BigCache does not handle collisions. When new item is inserted and it's hash collides with previously stored item, new item overwrites previously stored value.

Bigcache vs Freecache

Both caches provide the same core features but they reduce GC overhead in different ways. Bigcache relies on

Results from benchmark tests are presented above. One of the advantage of bigcache over freecache is that you don’t need to know the size of the cache in advance, because when bigcache is full, It can allocate additional memory for new entries instead of overwriting existing ones as freecache does currently. However hard max size in bigcache also can be set, checkHardMaxCacheSize.

This package also includes an easily deployable HTTP implementation of BigCache, which can be found in the (server) ************ (package.)

Bigcache genesis is described in allegro.tech blog post: Writing a very fast cache service in Go

BigCache is released under the Apache 2.0 license (see********  ************************************************ (**************************************************