Share the experience of the CockroachDB team using Go.


Go was chosen to implement CockroachDB 7 years ago (November 3, 2015). Now look back at the original choice.

The first question many developers ask us is what is our experience writing distributed databases in the garbage-collected language Go. JVM garbage collection is notoriously expensive (resource-hungry), so aren't we taking a performance risk by building CockroachDB in Go?

In fact, when you're building high-performance distributed systems, you have only a handful of languages ​​to choose from, with C++, Java, and Go at the top of the list. Known performance issues with Java make it unattractive. And, while many of us have spent our careers developing in C++, the effort required to build our own library complicates the already daunting task of writing a distributed database. Next on the list is Go.

While Go is a brand new language for almost every developer on the project (including the founders), its support for libraries, interfaces, and tooling makes it the right choice for CockroachDB.

Perhaps the strongest indicator that Go is a good fit: a lack of prior exposure to the language is not a barrier for contributors. We now have 67 contributors working on the project, and CockroachDB has gone from an empty Github project to 125,000 lines of non-generated Go code, and a handful of C++ and .proto files. Admittedly, managing code complexity is influenced by language choice, which is especially important in an open source environment.

As of July 2022, CockroachDB now has 579 GitHub contributors [1] , 89.6% of which are written in Go, along with a small amount of TypeScript, Starlark and Yacc, and a few other languages.

It's hard to quantify the productivity impact of Go on C++ or even Java. Go is designed to scale to large codebases, emphasizing simplicity and orthogonality of functionality. Enforced code style, easy import and automatic import management, wide variety of linters, simple (and minimal) programming idioms... all these properties of Go are important for clean, understandable code.

Compared to Java, we appreciate the tight focus on implementation rather than OOP and abstraction: interfaces can be added when needed, not as an initial step, which is usually not required. Compared to C++, we appreciate automatic memory management and there are few more than one ways to accomplish something, such as using static and one-time initializers. We make good use of channels for synchronization, although we will note that there are tricks to using them effectively.

At the time, it remained to be seen how all this Go code would perform, with core functionality still being built in CockroachDB, so a lot of profiling was yet to come. However, in our past experience we ported a large system from Java to Go, which greatly reduced its memory footprint and garbage collection overhead.

Seven years on, with a lot of extra core functionality and a lot of profiling, we're still very happy with Go.

Want to learn more about how Go garbage collection works in CockroachDB? Garbage collection in Go can cause applications to pause. Fortunately, Go also provides many manual tweaks to control what ends up on top of the garbage heap.

How CockroachDB writes a large and complex Go application [2] In this hour-long deep (deep) dive video, Cockroach Labs CTO and co-founder Ben Darnell discusses how CockroachDB optimizes memory usage to mitigate Garbage collection related issues and improved channel usage to avoid deadlocks.

References

[1]

579 GitHub contributors: https://github.com/cockroachdb/cockroach

[2]

How CockroachDB writes a large and complex Go application: https://www.cockroachlabs.com/community/tech-talks/challenges-writing-massive-complex-go-application/


Recommended in the past

picture

My name is polarisxu. I graduated from Peking University with a master's degree. I have worked in well-known Internet companies such as 360 and have more than 10 years of experience in technology development and architecture! In 2012, I came into contact with the Go language and created the Go language Chinese website! Author of " Go Language Programming Journey ", open source book " Go Language Standard Library ", etc.

Insist on exporting technologies (including Go, Rust and other technologies), workplace experience and entrepreneurial insights! Welcome to follow "polarisxu" to grow together! Also welcome to add my WeChat friends to communicate: gopherstudio