As I understand it, Couchbase stores documents as JSON.
On one of my API routes, I send in the response couchbase documents without making any changes to them. For each document, I call JSON.Stringify before sending it to the browser.
Is it possible to get the documents from couchbase as raw JSON, so that I could send them “as is” to the browser without needing to call JSON.stringify on each document ? In this way I could avoid the cost of deserializing and then reserializing the data on each call ?
There are different ways to store/retrieve data in couchbase. If you follow the common examples, yes, the data is strored and retrieved as JSON.
The SDKs can convert to/from JSON so there is no need for your application.
Also - de/serialization from/to JSON is fast - I haven’t found enough if a difference to make avoiding it worthwhile (also, n1ql won’t work on non-json documents)
Hi mreiche, thanks for your reply, but I wonder if you actually read the question before replying ?
Why would I use the java-sdk when my whole backend is using node JS ? The question is tagged clearly under “Node.js SDK” . You realize that it is not possible for me to switch my whole backend to Java.
what do you mean "de/serialization from/to JSON is fast " ? JSON compression and decompression has been shown to be the primary bottleneck in multiple scenarios. It is a useless performance hit on every document retrieval in a very common scenario, when we just want to return a document as is without making changes to it, for many GET routes in a REST API.
For reference, here is an article on how LinkedIn solved performance issues by optimizing JSON :
I recreated my question while trying to make it more clear what I’m trying to accomplish :
Edit: the original post didn’t mention query so I assumed KV. Query might different. I’ll follow up on your new/other post.
Hi mreiche, thanks for your reply, but I wonder if you actually read the question before replying ?
I didn’t. But at least I responded I work mainly on the Java SDK, so I tend to respond in terms of the Java SDK. Since all SDKs are supposed to implement the same functionality, whatever works in one SDK should work in all the others. And I did point you to transcoders, and if one does a google search. https://www.google.com/search?q=couchbase+sdk+node+transcoder, the first link shows example using RawStringTranscoder which sounds like what you are looking for https://docs.couchbase.com/nodejs-sdk/current/howtos/transcoders-nonjson.html#rawstringtranscoder. Since this is a free, friendly forum let’s not be too critical of what others do or don’t do. If you have mission-critical needs Customer Support can provide more detailed answers.
JSON compression and decompression has been shown to be the primary bottleneck in multiple scenarios
That sounds like a compression issue, not a JSON serialization/deserialization issue. The default compression is SNAPPY. You can disable compression. However - disabling compression will result in uncompressed data being sent to/from the server which may be 20x larger than compressed resulting in increased network times. Once again - google to the rescue - https://www.google.com/search?q=couchbase+nodejs+sdk+compression first result, the very last line
By default, compression is turned on in the SDK. You can override the default compression setting with enable_compression=off in the connection string.
That said, I’m not a nodejs developer and not that familiar with the couchbase nodejs sdk - so if you could show me how you are using it I might be able to give better answers.
Hello mreiche, my bad for not using the correct word : I’m really thinking about the automatic JSON deserialization when the couchbase.Scope.query method of the Couchbase nodeJS SDK is used. I am not concerned with compression at the moment. I will add code examples in the other topic to make it more clear what I’m trying to do.
I believe you could either use RawBinaryTranscoder or if that fails (I am not certain if it will check the document’s common flags to double-check it is a binary doc, which will fail in your case), then the defaulttranscoder with a customised no-op serializer, to accomplish it.