question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stable JSON stringify to avoid cache misses

See original GitHub issue

It 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:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
spalgercommented, Sep 1, 2018

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:

import DefaultJsonSerializer from 'elasticsearch/src/lib/serializers/json';
import jsonStableStringify from 'json-stable-stringify';

class CustomSerializer extends DefaultJsonSerializer {
  serialize(val, replacer, space) {
    return jsonStableStringify(val, { replacer, space })
  }
}

CustomSerializer.prototype.serialize.contentType = 'application/json';

const client = new elasticsearch.Client({
  serializer: CustomSerializer
})

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.

0reactions
daedalus28commented, Nov 12, 2018

Sorry for the delay - I’ve opened PR #727 adding the relevant documentation.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found