Stable JSON stringify to avoid cache misses
See original GitHub issueIt looks like this lib is using JSON.stringify on requests. Per the elasticsearch query cache docs:
if the JSON changes — for instance if keys are output in a different order — then the cache key will not be recognised.
This means that if we make requests to the elasticsearch client with objects where the keys are not created in the exact same order, we can’t take advantage of the request cache. My unstanding is that these two would not be the same from the cache’s perspective:
await client.search({
index: 'twitter',
type: 'tweets',
body: {
query: {
match: {
body: 'elasticsearch'
}
}
}
})
vs
await client.search({
type: 'tweets',
index: 'twitter',
body: {
query: {
match: {
body: 'elasticsearch'
}
}
}
})
If we switch to any of these libs, we could make sure the request cache is hit more often. If it’s much slower, maybe we could make it a config option for the client (e.g. stableStringify: true
)?
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
store strings in stable memory in c++ - Stack Overflow
For now, I've settled on preallocating a cache size that is large enough to avoid any conceivable moving of the vector, but I...
Read more >Are you causing XSS vulnerabilities with JSON.stringify()?
Using JSON.stringify() can result in XSS vulnerabilities. Find out how and what to do to prevent this from happening!
Read more >How to Set Up Redis for Caching in Node.js - Better Stack
A good caching strategy will ensure that most requests will result in a cache hit. However, the occasional cache miss cannot be avoided, ......
Read more >Configuring Infinispan caches
If you configure a capability that is not compatible with a simple cache, Infinispan throws an exception. Simple cache configuration. XML. JSON.
Read more >Memoization Forget-Me-Bomb. Remember it? No! - ITNEXT
*Usually does not mean always. Lodash.memoize , by default, uses JSON.stringify to convert passed arguments into a string cache. Just because ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, that could happen, but in your example the cache would still be used since only the keys within the body are relevant.
If you want to try out one of these libs you can specify a custom serializer in the config like so:
Interested to hear what your experience is, if the potential overhead of a slower serializer is beneficial in the long run for potentially faster querying. I think it would be pretty surprising for the keys JSON bodies from a single application that are requesting the same thing to not always be sent in the same order, mostly because I expect that people won’t be copy/pasting the same query in different places, but rather each place they query sends unique queries and the function body produced by a single function probably creates a specific query the same way every time.
I can imagine how that might not be the case for contexture, so maybe an option like a custom serializer is more appropriate if it does slow down serialization in a meaningful way.
Sorry for the delay - I’ve opened PR #727 adding the relevant documentation.