by facetube · 8y ago · 0 comments
Map/reduce views ("secondary indexes") are currently built by serializing JSON down a pipe to an external process called couchjs that links against a seven year old version of Mozilla Spidermonkey (1.8.5, released on March 31st 2011, and the only version it currently works with). This external process then pipes the map results back, again as JSON. Forget zerocopy; we're serializing and deserializing twice per document. Almost all of this "external view server" (the only kind AFAICT) is actually written in Javascript on that seven year old engine. Nothing is streaming; your map function is sandbox-evaled in that process, the results of a single call are stuffed into a Javascript array via .push, then stringified and printed on standard output as a single line. The entire internal "view server" protocol is based on a pipe of single-line JSON commands and data payloads. Performance (and likely security, due to age of the engine) in this subsystem is extremely poor IME.
You can write map/reduce views in Erlang itself as an alternative, but that engine is disabled by default due to security concerns – it's "not sandboxed". Performance in this area also appeared to be proportional to the code size of the map function, which sounded a lot like it was being serialized down a pipe with no caching.
If you make a change to a single view in a design document, the database system will rebuild all indexes in that design document unconditionally, even if the other view code hasn't changed. The only way to prevent this is to store a single view per design document, and manage those documents yourself.
Once you've endured your multi-day index rebuild on a couple million documents, you'll also have to issue an extra "cleanup" command to delete the old (now essentially useless) indexes. It's a full rebuild, so you need 2x the space at a minimum to pull it off.
The data itself is stored in a single file per shard, opened in O_APPEND mode, effectively serializing all writes on the shard. You also will not see kswapd active, ever, because the OS VMM is not being used effectively – no shared memory for IPC, and no use of mmap AFAICT.
Server-side changes feed filters appear to be extremely slow when compared to just filtering the results in the client.
Authorization: limiting read access will be difficult at best. Transactional behavior: no, aside from a single bulk write feature (make sure to use "all or nothing" mode though).
Logging: signal to noise ratio is poor. Giant stack traces for minor events. You may even see strings emitted as UTF-32 arrays of integers in base 10. Hope you brought an ASCII table at least.
Documentation: The following passage from section 5.2.5 of the CouchDB v2.1.1 manual just about says it all: "Views with the JavaScript query server are extremely slow to generate when there are a non-trivial number of documents to process. The generation process won’t even saturate a single CPU let alone your I/O. The cause is the latency involved in the CouchDB server and separate couchjs query server, dramatically indicating how important it is to take latency out of your implementation." I can't say anything nice here about that last sentence, so I'm just going to keep my mouth shut.
But worst of all: every time it starts up, it tells me "it's time to relax". Nothing about this is relaxing.
(These are my own private individually-held views, and do not reflect the views of any employer or organization)
Except CouchDB entire REST API, where nearly every endpoint streams result row by row, which is great.
> Performance (and likely security, due to age of the engine) in this subsystem is extremely poor
As of security: wrong guess. Go compare number of critical CouchDB CVE for last, say, 5 years, with any other DB you want.
As for query server: indexing is slow for the reasons you pointed out. Requesting persisted index is ok. Taking in account CouchDB persists everything, it‘s not in-memory DB (which is again great), I‘d say it‘s even fast.
> Performance in this (Erlang) area also appeared to be proportional to the code size of the map function
Performance in this area depends much more on how branchy is the json doc being processed. It‘s not about CouchDB itself, it‘s about json parser used. There might be a situation when you have better perf using JS views.
> Nothing about this is relaxing.
Indeed. But then it just works, for years, with zero maintenance even for replications – the result I‘ve never even nearly achieved with any other DB I used.
I'm talking about the internals of couchjs.
> As of security: wrong guess. Go compare number of critical CouchDB CVE
I'm talking about Spidermonkey 1.8.5, which is seven years old. Multiple CVEs have been reported including a full RCE, which was patched in later versions. If you believe these are patched in libmozjs185, I'd love to see a link to the source package showing where the fix was applied. Because I can't find it.
Further: https://www.cvedetails.com/vulnerability-list/vendor_id-45/p.... Another full RCE in CouchDB.
> Performance in this area depends much more on how branchy is the json doc being processed
Citation needed. There's an issue specifically about this in the CouchDB issue tracker if you'd like to read more.
> As for query server: indexing is slow for the reasons you pointed out.
Right. It is not performant. That's my point.
Curious what kind of database sizes you're working with too; you won't see this stuff with a few hundred documents.
There may be no material security issue. But the age of the software does not instill confidence, and newer engines are likely far more performant.
I get the impression the author(s) of this DB were still at the stage of needing to brow-beat the infrastructure into submission, or somesuch :S
It's depressing.
Or you can use a query language mostly similar to what MongoDB uses http://docs.couchdb.org/en/2.0.0/api/database/find.html
> If you make a change to a single view in a design document, the database system will rebuild all indexes in that design document unconditionally,
That's a feature. If they don't usually change together put them in a separate design documents.
> and manage those documents yourself.
It's a document database. So managing documents yourself is kind of what you do. Isn't it?
> he data itself is stored in a single file per shard, opened in O_APPEND mode, effectively serializing all writes on the shard.
So have more shards? A lot of databases use append only mode. There is a reason for that. Having uses the db for a good number of years, it had never lost data. Even during power outages or brutal restarts. That's more than I can say about other databases.
> no shared memory for IPC,
Shared memory IPC doesn't magically solve problems but it is a minefield of bugs.
> use of mmap AFAICT.
Large blocks of allocated memory use malloc instead of brk so memory allocation uses mmap. For file IO mmap isn't a panacea either. If data is not there, reads will still go to the disk. If it uses regular reads through data should end up in the page cache
> kswapd active,
Hmm, seeing kswapd being active is not something I look forward on a production server. From what I remember I have seen page cache utilized correctly and that's what should matter. Not kswapd.
> Transactional behavior: no, aside from a single bulk write feature
bulk docks is not a transactional feature. The database is not CP database so it doesn't have transactional feature.
> But worst of all: every time it starts up, it tells me "it's time to relax". Nothing about this is relaxing.
Yeah that is silly never liked that. From what I remember it was a DB that was built when MySQL ruled the open source DB world and many of the current DBs were not there. It was actually pretty relaxing being able to insert regular json object in it, index them, have master to master replication (which most dbs still don't support), have a nice built in web dashboard to inspect and query documents.
Yes, provided you started on 2.0, as I specifically stated in a reply.
> That's a feature. If they don't usually change together put them in a separate design documents.
Unnecessary computation and I/O is not a feature. They should hash the views individually. A database that makes me do extra work to keep it from doing unnecessary work is not helpful.
> So managing documents yourself is kind of what you do. Isn't it?
This doesn't have anything to do with the unnecessary index rebuilds. CouchDB has all of the information it needs to avoid this unnecessary computation and I/O.
> So have more shards
More shards spending time serializing and deserializing JSON instead of doing useful work. Throwing shards at inefficient code might help, but it doesn't address the underlying problem.
> Shared memory IPC doesn't magically solve problems but it is a minefield of bugs.
Lots of database systems manage to use it just fine. You probably use one of them. And it does solve a problem: it avoids serializing and deserializing all of your data down a pipe.
> Large blocks of allocated memory use malloc instead of brk so memory allocation uses mmap.
Yes, with MAP_ANONYMOUS. I'm not talking about anonymous mappings.
> Hmm, seeing kswapd being active is not something I look forward on a production server.
For swap? Sure. For file-backed mmap pageout guided by madvise? I actually do. It means the VMM is being used for its intended purpose.
> bulk docks is not a transactional feature
It was an example of the one way to achieve an atomic write of multiple documents, which comes in very handy for any nontrivial application.
I'd honestly be fine with them just deprecating the entire external view server protocol – but as it stands now, it's a supported feature, the performance documentation in 5.2.5 of the manual is nonsensical, and it performs very very poorly due to excessive serialization/deserialization and inefficient IPC.
Isn’t it supposed to be mainly REST based and have a goal of being able to sync multiple nodes with expected connection loss in between?
I’ve never needed that on a project but it seems like it would work for some use cases. I hear good things about PouchDB.
Couch has always been good at this use case where systems on both ends may have changed in the disconnect time.
We use transactions under the hood actually writing data to indexeddb
I can understand it working the other way around, e.g. an application initially seems good, but opinion of it drops when finding out it's written in COBOL.
I can understand if the CouchDB authors agreed with criticism of their application, and explained them as being inherent to the language. AFAIK they haven't. EDIT: Ah, looks like the author wrote CouchDB!
I can understand if Erlang and its proponents held CouchDB up as a shining example of their philosophy. AFAIK they don't (e.g. it doesn't appear on https://www.erlang.org/community ).