thanos-io / thanos

Highly available Prometheus setup with long term storage capabilities. A CNCF Incubating project.
https://thanos.io
Apache License 2.0
13.09k stars 2.1k forks source link

inmemory cache: research other cache libraries for better performance under high concurrency #6762

Open yeya24 opened 1 year ago

yeya24 commented 1 year ago

Is your proposal related to a problem?

Currently, we are mainly using https://github.com/hashicorp/golang-lru simplelru library for inmemory cache. It is not thread safe, so we added a lock for the whole cache https://github.com/thanos-io/thanos/blob/main/pkg/store/cache/inmemory.go#L36.

This impacts the performance a lot under high concurrency situation as each get and set operation, we need to hold the lock.

In the ideally case, we should use a thread safe cache implementation which bucketizes the whole cache into multiple buckets to improve performance under high concurrency.

Describe the solution you'd like

https://github.com/dgraph-io/ristretto might be a good candidate to check.

Akhil-2001 commented 1 year ago

@yeya24 This seems quite interesting. We could implement ristretto as another module and introduce a new case to select it as our indexCache here. That would give us a starting point to compare the performance of the two in-memory caches. I'd love to take this up unless we need to explore other concurrent hashmap/cache options.

yeya24 commented 1 year ago

@Akhil-2001 hey thanks for the interests.

Actually I already implemented the code to use Ristretto and currently doing tests with it. So I will take this up, sorry about this.

Akhil-2001 commented 1 year ago

@yeya24 sure, np! Let me know if there are other issues or prospective features which you might need help with.