News for hackers. Stuff that matters.
Re:How We Built a Vectorized SQL Engine by saagarjha on Monday November 11, 2019 @05:33PM (#21509633)
> A Datum now has a field for each possible type it may contain, rather than having separate interface implementations for each type. There is an additional enum field that serves as a type marker, so that when we do need to, we can inspect a type of a Datum without doing any expensive type assertions. This type uses extra memory due to having a field for each type, even though only one of them will be used at a time.

> The Go templating engine allows us to write a code template that, with a bit of work, we can trick our editor into treating as a regular Go file. We have to use the templating engine because the version of Go we are currently using does not have support for generic types.

Go’s lack of an expressive type system continues to disappoint :(

Re:How We Built a Vectorized SQL Engine by echelon on Monday November 11, 2019 @05:56PM (#21509816)
I'm shocked that they chose a garbage-collected language to implement a database with. Golang is great for building servers, but this is domain with potentially much greater resource constraints.

How do they deal with GC pause? I'm sure that they have an answer, but they'd have so much more latitude if they had the facility to reason about this from the ground up. They've sort of painted themselves into a corner now.

C, C++, or Rust would have been a better match and given me more faith in their product.

Re:How We Built a Vectorized SQL Engine by ken on Monday November 11, 2019 @06:36PM (#21510096)
A database is a server. Your malloc isn't deterministic, either.

According to the Go Wikipedia article, as of 2017:

> Garbage collection pauses should be significantly shorter than they were in Go 1.7, usually under 100 microseconds and often as low as 10 microseconds.

According to [1], "A trivial SELECT can take in the order of 0.1ms to execute server-side", i.e., 100 microseconds. Any I/O will of course significantly increase that.

This sounds like an entirely reasonable price to pay. Writing high-level functionality in C in 2019 would not give me much faith in their product.

[1]: https://www.2ndquadrant.com/en/blog/postgresql-latency-pipel...

Re:How We Built a Vectorized SQL Engine by jandrewrogers on Monday November 11, 2019 @07:33PM (#21510397)
Throughput optimization in database engines often relies on operation latency being visible and predictable to the execution scheduler. Disk I/O has this property, a GC does not, so they are not comparable in terms of their impact on throughput. An important class of schedule-based architectural optimizations are rendered ineffective if you are running a GC in the background.

This only matters to databases that actually implement these optimizations. If a database does not then a GC has a much smaller impact. But it also means that, all other things being equal, the database will never be competitive with a non-GC design since those optimizations can provide an integer factor improvement in performance.

Also, many database engines aren't doing malloc() at runtime. 100 microseconds of CPU time is an integer factor larger than many atomic database operations in a fast database engine. An operation taking an order of magnitude longer than the scheduler would expect based on runtime state has real adverse consequences.

Re:How We Built a Vectorized SQL Engine by endymi0n on Tuesday November 12, 2019 @03:06AM (#21512393)
This is slightly missing the point. Lots of successful large scale databases have been written in garbage collected languages, including for example Cassandra and ElasticSearch. They're completely fine.

What's important to notice though is that what really matters about latency and predictability is _relative_ performance.

Now there is a category of databases that are built for speed of response first, some of which have some pretty spaced out architecture under the hood. Aerospike comes to my mind, together with ScyllaDB or Redis maybe. They trade off this speed with massive compromises in complexity, consistency and scalability.

GC would be disastrous for these, so they're all written in C(++).

The main premise of CockroachDB is geographic replication, durability and scalability while maintaining full consistence and serialization.

In order just to keep these promises, a large part of the design of Cockroach is that the DB needs to keep _waiting_ most of the time of any request until all geo replicated shards are consistent. We're talking dozens to hundreds of milliseconds here. And even if that wasn't the case, a full GC of 100 microseconds probably isn't even noticeable compared with the weight of complex SQL query planning and execution.

You may stink a lot of valid points about Go, but this isn't one of them.

Re:How We Built a Vectorized SQL Engine by stubish on Tuesday November 12, 2019 @04:40AM (#21512820)
Cassandra has huge problems with JVM garbage collection, and it still isn't rid of them in the decades it has been in development. The reason for the scary warnings if your partition sizes exceed 100MB is garbage collection. The reason you can't tune it to use all your RAM on a modern box is garbage collection. Picking JVM implementations and tuning mostly seems to revolve around garbage collection and its trade offs. A common symptom of poorly maintained clusters is second long garbage collection pauses.

The trick with Go seems to be that programs don't seem to generate much garbage to collect, so I don't know if there is much use comparing with Java apps. But garbage collection overhead is certainly a major issue to be wary of for any application needing consistently low response times.

Re:How We Built a Vectorized SQL Engine by mumblemumble on Monday November 11, 2019 @11:48PM (#21511604)
So, before we theorize that Cockroach can't possibly be fast enough to meet anyone's needs, perhaps we should look at some performance figures?

I haven't ever touched it myself, but, if the benchmarks that Cockroach Labs posts on their blog are to be believed, it's pretty respectable for what it is.

Re:How We Built a Vectorized SQL Engine by jandrewrogers on Tuesday November 12, 2019 @01:45AM (#21512045)
You are misrepresenting what was written. Most databases don't need to be optimal to be useful, merely adequate, and few attempt to be optimal in any kind of absolute sense. I still use PostgreSQL regularly and it unambiguously falls into this "far from optimal" category.

However, as data velocity and volume grow, optimization of absolute performance and hardware efficiency has an increasingly large impact on the cost of operating a database and the kinds of applications you can economically run. Databases that sacrifice major optimizations at an architectural level will have no chance to be competitive with databases that do not over the long term as average data volume and velocity grows. At the scale of database operations many companies are using today, these optimizations literally save them tens of millions of dollars per year on infrastructure.

CockroachDB produces what looks like a fine product and has every right to make whatever technical decisions they wish. That the architecture has made substantial performance sacrifices is not theoretical though, and they don't pretend to operate in markets that require any kind of absolute operational efficiency.

Re:How We Built a Vectorized SQL Engine by mumblemumble on Tuesday November 12, 2019 @11:00AM (#21515093)
This seems to me, though, like a spot where the application of Knuth's law is critical. It's not enough to demonstrate that X is slower than it could be, you have to demonstrate that it is that, and that this is also having an appreciable impact on the overall behavior of the system.

So, given that CockroachDB is a distributed system, I'd want to start by seeing some measurements to demonstrate that GC overhead is having a bigger impact than, say, network latency.

Re:How We Built a Vectorized SQL Engine by aloknnikhil on Monday November 11, 2019 @07:14PM (#21510292)
For me, the issue with GC never has been about how long it runs for. It's been about the unpredictability of when it can kick in. Granted a malloc can be unpredictable too (albeit very very rarely), but at least you know a malloc is the only time your process could stall waiting for memory.

Databases make extensive use of scratch space for undo logs and redo logs and especially while processing JOINs. It's tremendous pressure on the GC that results in frequent performance dips. With C/C++/Rust, you'd pipeline the query stages to malloc/free that space outside the hot path to minimize query response times.

Re:How We Built a Vectorized SQL Engine by irfansharif on Monday November 11, 2019 @08:54PM (#21510828)
I'll repeat what was mentioned elsewhere. The GC pauses in Go are in the order of microseconds. We've done a ton of work keeping our memory usage under tight bounds and keeping a close eye on exactly how the go GC processes interplay with query execution threads (what processors are consumed, GC pressure buildup, where/when the stalls are introduced, etc.) Consistently we've found that given you're invariably dealing with network latencies (it's a distributed db after all), the microsecond GC pauses are almost entirely dwarfed to matter (I'm not excluding p99s here).
Re:How We Built a Vectorized SQL Engine by tom_mellior on Tuesday November 12, 2019 @04:03AM (#21512677)
> With C/C++/Rust, you'd pipeline the query stages to malloc/free that space outside the hot path to minimize query response times.

I don't understand why you seem to assert that GC'd languages would force you to put allocations on the hot path. Surely in Go you can also allocate that space elsewhere?

Re:How We Built a Vectorized SQL Engine by aloknnikhil on Tuesday November 12, 2019 @08:28AM (#21513873)
I was primarily referring to the free on the hot path. GCd languages don't force any allocation paradigms. But due to the unpredictability of the GC, you could be in the middle of a query execution and the GC can kick in. With C/C++/Rust, you're free to maintain your own a allocator that could have a buffer pool of pages that can be recycled and freed when the database is idle. Under a large load, the micro spikes in latency can have an avalanche effect that leads to a build up of queries. I've dealt with customer cases where the MySQL/InnoDB lock manager caused connection spikes due to a brief (< 10 us) global write lock taken while the system was under load.
Re:How We Built a Vectorized SQL Engine by tom_mellior on Tuesday November 12, 2019 @11:31AM (#21515364)
> But due to the unpredictability of the GC, you could be in the middle of a query execution and the GC can kick in.

Yes, that is a possibility. Typically there are two ways to deal with this in a GC'd language: (a) You know that GC is only triggered by certain operations that might allocate, and you avoid those operations in your critical hot section, or (b) you know how to disable the GC temporarily, while you are in that critical hot section. For Go the latter is apparently done by calling this function: https://golang.org/pkg/runtime/debug/#SetGCPercent

I'm not a Go developer myself, but I imagine people developing such low-latency software in Go know about this.

Re:How We Built a Vectorized SQL Engine by paulddraper on Monday November 11, 2019 @11:10PM (#21511449)
C may not be an attractive choice in 2019, but C++ and Rust certainly are.

Especially if you're working at the level where you want/need vectorization.

Re:How We Built a Vectorized SQL Engine by jordanlewis on Monday November 11, 2019 @06:33PM (#21510077)
We wrote a (brief) blog post about this decision way back in 2015 that you might be interested in: https://www.cockroachlabs.com/blog/why-go-was-the-right-choi...

We're generally pretty happy with our choice to use Go for the database, even when it causes us pain like the kind you see in this post. Rust wasn't really ready when we started working on CockroachDB, and we think that we wouldn't have been able to make progress as quickly as we did if we had used C(++).

Re:How We Built a Vectorized SQL Engine by fouc on Monday November 11, 2019 @11:10PM (#21511447)
How hard would it be to port the code to Rust? (Not trying to make it sound like a "Rewrite in Rust" thing)
Re:How We Built a Vectorized SQL Engine by mjibson on Tuesday November 12, 2019 @01:12AM (#21511909)
Super mega ultra hard. It would take so much time for us to learn rust, port everything including all tooling, and fix new bugs we introduce that we wouldn't add any new features (but lots of new bugs!) for like 2-4 years and the company would die.
Re:How We Built a Vectorized SQL Engine by roberson87 on Tuesday November 12, 2019 @03:05AM (#21512383)
Thanks for your input. I have no exp at all on db internals, but could you expand your thoughts on the possibility of moving critical (storage layer?) parts to Rust and leave all the networking stuff/rest in Go? Similar to what TiDb have done?
Re:How We Built a Vectorized SQL Engine by ryanworl on Monday November 11, 2019 @06:35PM (#21510089)
Writing a program in C++ or Rust will not just make it use memory more efficiently than Go with the GC for free. Many Rust programs do a lot of heap allocations, and lots of people in C++ liberally use reference counted pointers which both require heap allocation and atomics.

It is possible to write Go programs which efficiently use memory using object pooling just as you would in C. Go additionally can stack allocate many variables that would require a heap allocation in Java, where the GC is a much bigger issue.

Re:How We Built a Vectorized SQL Engine by timClicks on Tuesday November 12, 2019 @03:12AM (#21512422)
When you say that Rust uses lots of allocations, are you referring to the heavy use of Vec<_>? My understanding is that almost everything that can be allocated on the stack, is. Boxed types and trait objects are available, but they're opt-in features rather than the default.
Re:How We Built a Vectorized SQL Engine by weberc2 on Monday November 11, 2019 @06:54PM (#21510188)
Go’s GC pauses are on a microsecond scale. If you absolutely need to minimize latency at all costs, I’d be more worried that the Go compiler doesn’t optimize as aggressively as C/C++/Rust.
Re:How We Built a Vectorized SQL Engine by snark42 on Monday November 11, 2019 @07:22PM (#21510337)
InfluxDB is written in Golang as well. It's rather performant and scales out nicely, given my experience I don't think I'd consider Golang a big concern.
Re:How We Built a Vectorized SQL Engine by valyala on Tuesday November 12, 2019 @03:19PM (#21517707)
There is VictoriaMetrics, which is also written in Go and shows much higher performance numbers than InfluxDB, especially on high cardinality data. See [1], [2] and [3].

[1] https://medium.com/@valyala/measuring-vertical-scalability-f...

[2] https://medium.com/@valyala/insert-benchmarks-with-inch-infl...

[3] https://medium.com/@valyala/high-cardinality-tsdb-benchmarks...

Re:How We Built a Vectorized SQL Engine by jordanlewis on Monday November 11, 2019 @06:35PM (#21510091)
For the record, the first approach was just used for simplicity in the blog post. The production system works differently (and doesn't have to waste memory per-datum) - we have typed containers, each of which have a primitive slice within.

https://github.com/cockroachdb/cockroach/blob/master/pkg/col...