100% found this document useful (3 votes)
75 views

Practical CockroachDB: Building Fault-Tolerant Distributed SQL Databases 1st Edition Rob Reid - Quickly download the ebook to start your content journey

The document promotes the ebook 'Practical CockroachDB: Building Fault-Tolerant Distributed SQL Databases' by Rob Reid, available for download at ebookmeta.com. It also lists several other recommended ebooks related to CockroachDB and various technical topics. The document includes links to purchase and download these ebooks instantly.

Uploaded by

pereexpapua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
75 views

Practical CockroachDB: Building Fault-Tolerant Distributed SQL Databases 1st Edition Rob Reid - Quickly download the ebook to start your content journey

The document promotes the ebook 'Practical CockroachDB: Building Fault-Tolerant Distributed SQL Databases' by Rob Reid, available for download at ebookmeta.com. It also lists several other recommended ebooks related to CockroachDB and various technical topics. The document includes links to purchase and download these ebooks instantly.

Uploaded by

pereexpapua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Read Anytime Anywhere Easy Ebook Downloads at ebookmeta.

com

Practical CockroachDB: Building Fault-Tolerant


Distributed SQL Databases 1st Edition Rob Reid

https://ebookmeta.com/product/practical-cockroachdb-
building-fault-tolerant-distributed-sql-databases-1st-
edition-rob-reid/

OR CLICK HERE

DOWLOAD EBOOK

Visit and Get More Ebook Downloads Instantly at https://ebookmeta.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

CockroachDB The Definitive Guide: Distributed Data at


Scale Guy Harrison

https://ebookmeta.com/product/cockroachdb-the-definitive-guide-
distributed-data-at-scale-guy-harrison/

ebookmeta.com

CockroachDB: The Definitive Guide: Distributed Data at


Scale 1st Edition Jesse Seldess

https://ebookmeta.com/product/cockroachdb-the-definitive-guide-
distributed-data-at-scale-1st-edition-jesse-seldess/

ebookmeta.com

Fault Tolerant Attitude Estimation for Small Satellites


1st Edition Chingiz Hajiyev & Halil Ersin Soken

https://ebookmeta.com/product/fault-tolerant-attitude-estimation-for-
small-satellites-1st-edition-chingiz-hajiyev-halil-ersin-soken/

ebookmeta.com

Lippincott Illustrated Reviews Pharmacology 7th edition


Karen Whalen

https://ebookmeta.com/product/lippincott-illustrated-reviews-
pharmacology-7th-edition-karen-whalen/

ebookmeta.com
What s So Controversial about Genetically Modified Food
1st Edition John T Lang

https://ebookmeta.com/product/what-s-so-controversial-about-
genetically-modified-food-1st-edition-john-t-lang/

ebookmeta.com

Her Daddy s Jewel MC Daddies Book 5 1st Edition Laylah


Roberts

https://ebookmeta.com/product/her-daddy-s-jewel-mc-daddies-book-5-1st-
edition-laylah-roberts-2/

ebookmeta.com

The Impact of Individual Expertise and Public Information


on Group Decision-Making 1st Edition Ulrich G. Strunz

https://ebookmeta.com/product/the-impact-of-individual-expertise-and-
public-information-on-group-decision-making-1st-edition-ulrich-g-
strunz/
ebookmeta.com

Cosplay The Builders Fans and Makers Who Bring Your


Favorite Stories to Life 1st Edition Andrew Liptak

https://ebookmeta.com/product/cosplay-the-builders-fans-and-makers-
who-bring-your-favorite-stories-to-life-1st-edition-andrew-liptak/

ebookmeta.com

Metaheuristic Algorithms in Industry 4.0 (Advances in


Metaheuristics) 1st Edition Pritesh Shah (Editor)

https://ebookmeta.com/product/metaheuristic-algorithms-in-
industry-4-0-advances-in-metaheuristics-1st-edition-pritesh-shah-
editor/
ebookmeta.com
C Language Specification ECMA 334 6th edition June 2022
Ecma

https://ebookmeta.com/product/c-language-specification-ecma-334-6th-
edition-june-2022-ecma/

ebookmeta.com
Practical CockroachDB

Building Fault-Tolerant Distributed

SQL Databases

Rob Reid

Practical CockroachDB: Building Fault-Tolerant Distributed


SQL Databases

Rob Reid

Liss, Hampshire, UK

ISBN-13 (pbk): 978-1-4842-8223-6

ISBN-13 (electronic): 978-1-4842-8224-3

https://doi.org/10.1007/978-1-4842-8224-3

Copyright © 2022 by Rob Reid

This work is subject to copyright. All rights are reserved by the


Publisher, whether the whole or part of the material is concerned,
specifically the rights of translation, reprinting, reuse of illustrations,
recitation, broadcasting, reproduction on microfilms or in any other
physical way, and transmission or information storage and retrieval,
electronic adaptation, computer software, or by similar or dissimilar
methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book.


Rather than use a trademark symbol with every occurrence of a
trademarked name, logo, or image we use the names, logos, and
images only in an editorial fashion and to the benefit of the
trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service
marks, and similar terms, even if they are not identified as such, is
not to be taken as an expression of opinion as to whether or not they
are subject to proprietary rights.

While the advice and information in this book are believed to be true
and accurate at the date of publication, neither the authors nor the
editors nor the publisher can accept any legal responsibility for any
errors or omissions that may be made. The publisher makes no
warranty, express or implied, with respect to the material contained
herein.

Managing Director, Apress Media LLC: Welmoed Spahr

Acquisitions Editor: Jonathan Gennick

Development Editor: Laura Berendson

Coordinating Editor: Jill Balzano

Cover image photo by Maximalfocus on Unsplash

Distributed to the book trade worldwide by Springer


Science+Business Media LLC, 1 New York Plaza, Suite 4600, New
York, NY 10004. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.

com, or visit www.springeronline.com. Apress Media, LLC is a


California LLC and the sole member (owner) is Springer Science +
Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is
a Delaware corporation.

For information on translations, please e-mail


[email protected]; for reprint, paperback, or
audio rights, please e-mail [email protected].

Apress titles may be purchased in bulk for academic, corporate, or


promotional use. eBook versions and licenses are also available for
most titles. For more information, reference our Print and eBook Bulk
Sales web page at http://www.apress.com/bulk-sales.

Any source code or other supplementary material referenced by the


author in this book is available to readers on GitHub.

Printed on acid-free paper

For Emily, Ruby, and our little one on the way.

Thank you for giving me the space and patience

I needed to put this wonderful project together.

Table of Contents

About the Author


�������������������������������
�������������������������������
�������������������������������
�������� xi About the Technical Reviewer
�������������������������������
�������������������������������
����������������� xiii Acknowledgments
�������������������������������
�������������������������������
�������������������������������
������xv Introduction
�������������������������������
�������������������������������
�������������������������������
��������������xvii Chapter 1: The Reason for
CockroachDB
�������������������������������
�������������������������������
���� 1
What Is CockroachDB?
�������������������������������
�������������������������������
�������������������������������
��������������������� 1

CockroachDB’s Architecture
�������������������������������
�������������������������������
�������������������������������
������������� 2

What Does CockroachDB Solve?


�������������������������������
�������������������������������
�������������������������������
������ 2

Who Is CockroachDB For?


�������������������������������
�������������������������������
�������������������������������
���������������� 4

Chapter 2: Installing CockroachDB


�������������������������������
�������������������������������
������������� 5

Licensing
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 5
Free
�������������������������������
�������������������������������
�������������������������������
�������������������������������
������������� 5

Paid For
�������������������������������
�������������������������������
�������������������������������
�������������������������������
������� 6

CockroachDB Core
�������������������������������
�������������������������������
�������������������������������
���������������������� 6

Local Installation
�������������������������������
�������������������������������
�������������������������������
������������������������������ 6

Binary Install
�������������������������������
�������������������������������
�������������������������������
�������������������������������
7

Docker Install
�������������������������������
�������������������������������
�������������������������������
������������������������������ 8

Kubernetes Install
�������������������������������
�������������������������������
�������������������������������
����������������������� 9

Multinode
Clusters����������������������������
�������������������������������
�������������������������������
����������������������� 20

Multiregion Clusters
�������������������������������
�������������������������������
�������������������������������
����������������� 21

The demo Command


�������������������������������
�������������������������������
�������������������������������
���������������� 28

Table of ConTenTs

CockroachDB Serverless/Dedicated
�������������������������������
�������������������������������
����������������������������� 29
Creating a Cluster
�������������������������������
�������������������������������
�������������������������������
��������������������� 29

Connecting to Your Cluster


�������������������������������
�������������������������������
�������������������������������
������� 29

Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
������������� 30

Chapter 3: Concepts
�������������������������������
�������������������������������
�������������������������������
�� 31

Database Objects
�������������������������������
�������������������������������
�������������������������������
��������������������������� 31

Data Types
�������������������������������
�������������������������������
�������������������������������
�������������������������������
������� 33
UUID
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 33

ARRAY
�������������������������������
�������������������������������
�������������������������������
�������������������������������
�������� 34

BIT
�������������������������������
�������������������������������
�������������������������������
�������������������������������
������������� 37

BOOL
�������������������������������
�������������������������������
�������������������������������
�������������������������������
���������� 38

BYTES
�������������������������������
�������������������������������
�������������������������������
�������������������������������
�������� 39

DATE
�������������������������������
�������������������������������
�������������������������������
�������������������������������
���������� 39

ENUM
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��������� 40

DECIMAL
�������������������������������
�������������������������������
�������������������������������
�������������������������������
���� 41

FLOAT
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��������� 43

INET
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 44

INTERVAL
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 45

JSONB
�������������������������������
�������������������������������
�������������������������������
�������������������������������
������� 46

SERIAL
�������������������������������
�������������������������������
�������������������������������
�������������������������������
������� 48

STRING
�������������������������������
�������������������������������
�������������������������������
�������������������������������
������� 49

TIME/TIMETZ
�������������������������������
�������������������������������
�������������������������������
����������������������������� 50

TIMESTAMP/TIMESTAMPTZ
�������������������������������
�������������������������������
�������������������������������
������ 51
GEOMETRY
�������������������������������
�������������������������������
�������������������������������
�������������������������������
� 53

Functions
�������������������������������
�������������������������������
�������������������������������
�������������������������������
�������� 55

Geo-partitioned Data
�������������������������������
�������������������������������
�������������������������������
���������������������� 58

REGION BY ROW
�������������������������������
�������������������������������
�������������������������������
����������������������� 59

REGION BY TABLE
�������������������������������
�������������������������������
�������������������������������
��������������������� 63

vi

Table of ConTenTs
Chapter 4: Managing CockroachDB from the Command Line
�������������������������������
�� 67

The Cockroach Binary


�������������������������������
�������������������������������
�������������������������������
�������������������� 67

The start and start-single-node Commands


�������������������������������
�������������������������������
����������������� 68

The demo Command


�������������������������������
�������������������������������
�������������������������������
���������������������� 69

The cert Command


�������������������������������
�������������������������������
�������������������������������
������������������������� 70

The sql Command


�������������������������������
�������������������������������
�������������������������������
�������������������������� 71

The node Command


�������������������������������
�������������������������������
�������������������������������
����������������������� 73

The import Command


�������������������������������
�������������������������������
�������������������������������
�������������������� 78

The sqlfmt Command


�������������������������������
�������������������������������
�������������������������������
��������������������� 78

The workload Command


�������������������������������
�������������������������������
�������������������������������
���������������� 80

Chapter 5: Interacting with CockroachDB


�������������������������������
�������������������������������
83

Connecting to CockroachDB
�������������������������������
�������������������������������
�������������������������������
���������� 83

Connecting with
Tools�����������������������������
�������������������������������
�������������������������������
����������������� 83
Connecting Programmatically
�������������������������������
�������������������������������
�������������������������������
�� 88

Designing Databases
�������������������������������
�������������������������������
�������������������������������
��������������������� 96

Database
Design����������������������������
�������������������������������
�������������������������������
�������������������������� 96

Schema Design
�������������������������������
�������������������������������
�������������������������������
������������������������� 98

Table Design
�������������������������������
�������������������������������
�������������������������������
��������������������������� 101

View Design
�������������������������������
�������������������������������
�������������������������������
���������������������������� 109
Moving Data
�������������������������������
�������������������������������
�������������������������������
�������������������������������
�� 114

Exporting and Importing Data


�������������������������������
�������������������������������
�������������������������������
� 114

Watching for Database Changes


�������������������������������
�������������������������������
��������������������������� 116

Chapter 6: Data Privacy


�������������������������������
�������������������������������
�������������������������� 123

Global Regulations
�������������������������������
�������������������������������
�������������������������������
����������������������� 123

Location-Specific Considerations
�������������������������������
�������������������������������
�������������������������������
125

UK Company with UK and European Customers


�������������������������������
�������������������������������
���� 125

European Company with European and American Customers


�������������������������������
������������� 126

American Company with American Customers


�������������������������������
�������������������������������
����� 127

American Company with American and European Customers


�������������������������������
������������� 127

A Non-Chinese Company with Chinese Customers


�������������������������������
������������������������������
128

vii

Table of ConTenTs

Personally Identifiable Information


�������������������������������
�������������������������������
����������������������������� 129

Encryption
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����� 129
In Transit
�������������������������������
�������������������������������
�������������������������������
�������������������������������
�� 130

At Rest
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����� 133

Chapter 7: Deployment Topologies


�������������������������������
�������������������������������
��������� 139

Single-Region Topologies
�������������������������������
�������������������������������
�������������������������������
������������� 140

Development
�������������������������������
�������������������������������
�������������������������������
�������������������������� 140

Basic
Production��������������������������
�������������������������������
�������������������������������
�������������������������� 140
Multiregion Topologies
�������������������������������
�������������������������������
�������������������������������
����������������� 143

Regional
Tables����������������������������
�������������������������������
�������������������������������
�������������������������� 143

Global Tables
�������������������������������
�������������������������������
�������������������������������
�������������������������� 149

Follower Reads
�������������������������������
�������������������������������
�������������������������������
����������������������� 151

Follow-the-Workload
�������������������������������
�������������������������������
�������������������������������
�������������� 152

Antipatterns
�������������������������������
�������������������������������
�������������������������������
�������������������������������
�� 152
Summary
�������������������������������
�������������������������������
�������������������������������
�������������������������������
� 157

Chapter 8: Testing
�������������������������������
�������������������������������
�������������������������������
���� 159

Structural Testing
�������������������������������
�������������������������������
�������������������������������
������������������������� 161

Functional Testing
�������������������������������
�������������������������������
�������������������������������
������������������������ 165

Black Box Testing


�������������������������������
�������������������������������
�������������������������������
������������������� 165

White Box Testing


�������������������������������
�������������������������������
�������������������������������
������������������� 184
Nonfunctional Testing
�������������������������������
�������������������������������
�������������������������������
������������������ 194

Performance Testing
�������������������������������
�������������������������������
�������������������������������
��������������� 194

Resilience Testing
�������������������������������
�������������������������������
�������������������������������
������������������� 203

Chapter 9: Production
�������������������������������
�������������������������������
����������������������������� 207

Best Practices
�������������������������������
�������������������������������
�������������������������������
������������������������������
208

SELECT Performance
�������������������������������
�������������������������������
�������������������������������
�������������� 208
INSERT Performance
�������������������������������
�������������������������������
�������������������������������
�������������� 209

UPDATE Performance
�������������������������������
�������������������������������
�������������������������������
������������� 214

Cluster Maintenance
�������������������������������
�������������������������������
�������������������������������
�������������������� 220

Moving a Cluster
�������������������������������
�������������������������������
�������������������������������
�������������������������� 227

viii

Table of ConTenTs

Backing Up and Restoring Data


�������������������������������
�������������������������������
�������������������������������
��� 231

Full Backups
�������������������������������
�������������������������������
�������������������������������
��������������������������� 233

Incremental Backups
�������������������������������
�������������������������������
�������������������������������
�������������� 236

Encrypted Backups
�������������������������������
�������������������������������
�������������������������������
����������������� 237

Locality-Aware Backups
�������������������������������
�������������������������������
�������������������������������
��������� 240

Scheduled Backups
�������������������������������
�������������������������������
�������������������������������
���������������� 241

Cluster Design
�������������������������������
�������������������������������
�������������������������������
������������������������������
243

Cluster Sizing
�������������������������������
�������������������������������
�������������������������������
������������������������� 243

Node Sizing
�������������������������������
�������������������������������
�������������������������������
���������������������������� 243

Monitoring
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����� 244

Index
�������������������������������
�������������������������������
�������������������������������
������������������������ 249

ix

About the Author

Rob Reid is a software developer from London, England. In his


career, he has written back-end, front-end, and messaging software
for the police, travel, finance, commodity, sports betting, telecom,
retail, and aerospace industries. He is an avid user of CockroachDB
and has worked with the Cockroach Labs team in recent years to
promote the database and embed it into development teams in the
United States and the UK.

xi

About the Technical Reviewer


Fernando Ipar has been working on and with open source
databases since 2000, focusing on performance, scaling, and high
availability. He currently works as a Database Reliability Engineer at
Life360. Before that, he has worked at Perceptyx, Pythian, and
Percona, among other places. When not working, Fernando enjoys
going to plant nurseries with his wife and playing music with their
children while being a good service employee for the family’s cat.

xiii

Acknowledgments

I’m incredibly grateful to the following people. Their contributions to


this book have been invaluable to me.

Kai Niemi (Solutions Engineer (EMEA) at Cockroach Labs) – I met Kai


when he

was a customer of Cockroach Labs and have witnessed him transition


from being a CockroachDB expert at one company to an expert the
global CockroachDB community can be grateful to have.

Daniel Holt (Director, Sales Engineering, International (EMEA and


APAC), at

Cockroach Labs) – I worked very closely with Daniel from the


moment he joined Cockroach Labs and have often marvelled at his
comprehensive knowledge of the database.

Katarina Vetrakova (Privacy Programme Manager at GoCardless) –


Katarina is quite possibly the most enthusiastic data privacy specialist
you could ever hope to meet. She’s completely dedicated to the art,
and since working with her at Lush, her passion and knowledge have
been inspiring to me.

Jonathan Gennick (Assistant Editorial Director of Databases at


Apress) – I’d like to thank Jonathan Gennick for approaching me to
write this book. Without him, this amazing (and terrifying)
opportunity wouldn’t have found me. He has been amazing
throughout the process of writing this book, and his patient
knowledge sharing allowed this first-time author to really find his feet
and enjoyment in writing.

The Cockroach Labs team – The Cockroach Labs team is among the
smartest people I’ve ever met. They’re incredibly dedicated to their
database and its customers and are a big reason for my affection
toward CockroachDB. I’d like to thank the following people from
Cockroach Labs (past and present) for their help, inspiration,
hospitality, and friendship: Jim Walker, Jeff Miller, Carolyn Parrish,
Jordan Lewis, Bram Gruneir, Kai Niemi, Daniel Holt, Glenn Fawcett,
Tim Veil, Jessica Edwards, Dan Kelly, Lakshmi Kannan, Spencer
Kimball, Peter Mattis, Ben Darnell, Nate Stewart, Jesse Seldess, Andy
Woods, Meagan Goldman, Megan Mueller, Andrew Deally, Isaac
Wong, Vincent Giacomazza, Maria Toft, Tom Hannon, Mikael Austin,
Eric Goldstein, Amruta Ranade, Armen Kopoyan, Robert Lee, Charles
Sutton, Kevin Maro, James Weitzman, and anyone I’ve failed to
mention.

xv

Introduction

Every so often, the technology community is blessed with truly


disruptive technology.

We’ve seen the likes of Kubernetes for orchestration, Kafka for


streaming, gRPC for Remote Procedure Call, and Terraform for
infrastructure. CockroachDB does what these technologies have done
for their respective use cases; it’s a game changer for data.

I first discovered CockroachDB in 2016, where I used it to create


rapid prototypes during company Hackathons at my then employer. It
immediately felt familiar and as if it had been designed for a
developer to build reliable and scalable software without an army of
database specialists to help them.

In this book, I’ll share my excitement for this database and the
experience I’ve gained from using it for many different use cases.

Who Should Read This Book

This book is for developers, database specialists, and enterprise


owners. So whether you’re in a position of convincing people to use
CockroachDB or you’re looking for a tool to complement your
enterprise tech stack, there’s something in this book for you.

You don’t need to have existing knowledge of CockroachDB, as we’ll


start with the basics and quickly ramp up to real-world examples. Any
experience with relational databases (especially Postgres) will be
beneficial but is not required.

Navigating This Book

This book is both an introduction and a reference to CockroachDB. It


starts with the

“why” of CockroachDB – why it was created and what problems it


solves. It then dives into the “what” – the database’s data types,
constructs, and fundamentals. Finally, it covers the “how” – how you
can use what you’ve learned to solve real-world scaling, safety, and
performance challenges.

xvii

InTroduCTIon

The book aims to remain practical, so it hovers above the database’s


internal details.

To continue your journey, I recommend reading the excellent


documentation and blog posts available on the Cockroach Labs
website: www.cockroachlabs.com.

Using Code Examples

Code examples can be found in GitHub and are arranged in chapters


to help you find what you’re looking for as you make your way
through the book.

https://github.com/codingconcepts/practical- cockroachdb

Code is self-contained to a chapter, meaning you won’t have to read


the whole book to get something working. In some cases, code
examples are split across adjoining code blocks, but this will be
highlighted.

I execute all of the code examples against version v21.1.7 of


CockroachDB, which is the current stable version of the database at
the time of writing.

Contacts

CockroachDB

[email protected]

53 W 23rd Street

8th Floor

New York, NY

10010

www.cockroachlabs.com

https://forum.cockroachlabs.com

Rob Reid
[email protected]

https://robreid.io

https://twitter.com/robreid_io

https://github.com/codingconcepts

www.linkedin.com/in/rob- reid

xviii

CHAPTER 1

The Reason

for CockroachDB

Databases are a critical part of almost every stateful system.


However, correctly running them can be challenging, especially where
price, availability, and shifting international data privacy regulations
are concerned. CockroachDB makes navigating this tricky landscape
not only easier but enjoyable.

In this chapter, we’ll explore the why of CockroachDB – why it came


to be, and why you might want to consider using it for your data.

What Is CockroachDB?

CockroachDB is a cloud-native Relational Database Management


System (RDBMS). It falls into the class of “NewSQL” or “DistSQL”
(Distributed SQL) databases, which aim to provide the scalability of
NoSQL databases while giving users the experience and features of a
traditional SQL database. It is wire-compatible with Postgres, which
means you can connect to a CockroachDB cluster with most Postgres
tools and drivers. To find out which drivers are available, visit
www.cockroachlabs.com/docs/stable/install-
client-drivers.html.

On paper, CockroachDB is “CP,” which, in terms of the CAP Theorem,


means it favors data Consistency over Availability in the face of
network Partitions. In reality, however, CockroachDB – as its name
suggests – has been built with availability in mind as well.

In practice, if you were to lose the majority of replicas in a


CockroachDB cluster, rather than compromising the integrity of your
data, requests will block until the nodes have recovered. Sizing your
cluster to ensure CockroachDB can continue to function in the event
of lost nodes is, therefore, an essential constituent to availability.

© Rob Reid 2022

R. Reid, Practical CockroachDB, https://doi.org/10.1007/978-1-4842-


8224-3_1

Chapter 1 the reason for CoCkroaChDB

Cockroach Labs – the company behind CockroachDB – was founded


in 2015 by

Spencer Kimball, Peter Mattis, and Ben Darnell. Inspired by Google’s


Spanner database, the team sought to bring Spanner’s level of
scalability to the masses, removing Spanner’s dependency on atomic
clocks and writing in the performant, modern, and increasingly
ubiquitous programming language, Go.

It gives you the freedom to create multiregion databases without the


need for a database department. Importantly, it’s also a safety net,
making it harder to create that multiregion database incorrectly.

CockroachDB’s Architecture
CockroachDB uses the Raft Consensus algorithm to give users a
multiactive system, which means every node is the same. There are
no special “active,” “leader,” or “write”

nodes. Every node can receive a portion of read and write requests
for different shards (or “ranges”) of data, helping CockroachDB to
scale horizontally.

Under the hood, CockroachDB is a distributed key/value store. But


thanks to its layered architecture of SQL > Transactions >
Distribution > Replication > Storage, you can interact with data as
richly as you would in any other RDBMS.

What Does CockroachDB Solve?

Creating a new database is relatively easy, regardless of the


technology. You download a binary, pull a Docker image, or start a
subscription, and away you go. On the other hand, you typically
install a traditional database on a single machine that will need to be
vertically scaled, sharded, or have read replicas added when
outgrown. All of these needs pose complexity, availability, and
consistency challenges.

With CockroachDB, the challenges of complexity and reliability shift to


the database.

It takes care of distribution, scaling, and replication, leaving you to


focus on your data.

Generally speaking, if a cluster requires more capacity, you simply


add a node to that cluster. CockroachDB will automatically rebalance
the data evenly across all nodes.

Figure 1-1 depicts a cluster of three nodes, each with a capacity of


100GB and each with a 40GB share of 120GB total data.

2
Chapter 1 the reason for CoCkroaChDB

Figure 1-1. A three-node cluster

In Figure 1-2, you’ll see that when we add a node, data in the cluster
will be rebalanced across all four nodes, resulting in shares of 30GB
per node of the 120GB total.

3
Chapter 1 the reason for CoCkroaChDB

Figure 1-2. The same cluster, rebalanced across four nodes

Who Is CockroachDB For?

CockroachDB is an excellent choice for anyone needing a scalable,


reliable, and distributed Online Transaction Processing (OLTP)
database. Built with modern, cloud-based infrastructure and
workloads in mind, it will be at home in the cloud, on-premises, or a
hybrid of the two.

It’s also a great choice if you’re unsure of your database storage


requirements. You can start small, expanding your cluster node by
node as your data grows. CockroachDB
will ensure that your data is distributed evenly and safely across the
growing cluster.

If you are familiar with RDBMSs, CockroachDB will feel familiar to


you. For Postgres users, you may even find that CockroachDB can be
a drop-in replacement for your database.

As you’ll see shortly, CockroachDB can be installed with minimal fuss


whether you want to create a local development database or a
distributed production database.

CockroachDB is trusted to run critical, globally distributed workflows


by some of the world’s largest tech, finance, travel, retail, telecoms,
video streaming, and gaming companies. And its position on
https://db-engines.com/en/ranking continues to improve. It is now
more popular than Google’s Spanner, the database that inspired it.

CHAPTER 2

Installing CockroachDB

One of CockroachDB’s many strengths is the ease with which you can
install it. In this chapter, we’ll explore CockroachDB’s licensing model
and the various ways you can install it.

Licensing

We’ll start with a tour of CockroachDB’s licensing options to


familiarize ourselves with what each option provides. There are both
free and paid-for models, and your choice of model will depend on
your requirements and the features available in each.

Free

There are two free options available. One is an on-premises


installation of the CockroachDB Core functionality, and the other is a
cloud offering:

• CockroachDB Core – Build and elastically scale a distributed

database with many of the most critical features as standard; a good

option if you don’t need some of CockroachDB’s more advanced

features. You would need to install this option onto either cloud-

based or on-premises hardware.

• CockroachDB Serverless (Free Tier) – Sign up and create a


free

CockroachDB cluster in the cloud. At the time of writing, this cloud

option provides similar but slightly more limited functionality to

CockroachDB Core, limiting users to a multitenant, single-node

machine with 1 vCPU and 5GB of storage.

© Rob Reid 2022

R. Reid, Practical CockroachDB, https://doi.org/10.1007/978-1-4842-


8224-3_2

Chapter 2 InstallIng CoCkroaChDB

Paid For

To unlock everything that CockroachDB has to offer (and to support


Cockroach Labs in continuing to build the database into the future),
the following paid-for models are available:
• CockroachDB Self-Hosted – The complete CockroachDB offering
for

cloud-based or on-premises installations. Sporting advanced features

such as geo and archival partitioning, incremental backups,


encryption

at rest, change data capture, and direct support from Cockroach


Labs.

• CockroachDB Dedicated – The complete CockroachDB offering in

the cloud. In addition to CockroachDB Enterprise, it provides SLAs

and cloud-specific options like VPC peering.

A decision to use any of the aforementioned licenses will depend on


your

requirements. In each of the following installation options, I’ll provide


a recap of the licensing model to help you decide which best matches
your needs.

CockroachDB Core

Beginning in version 19.2, CockroachDB Core is subject to multiple


licenses. Some functionality retains the original Apache 2.0 license,
meaning it is fully Open Source. Other features are subject to a
Cockroach Community License, which protects Cockroach Labs from
companies using their code to build products that do not benefit
Cockroach Labs.

In short, if you plan on using CockroachDB Core’s free features to


power your own databases and do not intend to sell CockroachDB as
a service to others, CockroachDB

Core is a good choice for you.


Local Installation

In this section, I’ll show you how to install CockroachDB on your local
machine. For each installation method, we’ll run CockroachDB with
insecure configuration for brevity for each installation method, which
means no authentication or encryption. This is acceptable for a
local development database but not for anything else. Once we’ve
installed and tested local insecure deployments, we’ll move on to
some real-world secure implementations.

Chapter 2 InstallIng CoCkroaChDB

Binary Install

You can install the cockroach binary in several ways, depending on


your

operating system.

For Linux users, run the following commands to get started:

$ curl https://binaries.cockroachdb.com/cockroach-v21.1.7.linux-
amd64.tgz

| tar -xz

$ sudo cp cockroach-v21.1.7.linux-amd64/cockroach /usr/local/bin/

For Mac users, run the following command to get started:

$ brew install cockroachdb/tap/cockroach

For Windows 10 users, run the following commands to get started:

$ curl -o cockroach-v21.1.7.windows-6.2-amd64.zip
https://binaries.cockroachdb.com/cockroach-v21.1.7.windows-6.2-
amd64.zip

$ powershell.exe -NoP -NonI -Command "Expand-Archive


'.\cockroach-

v21.1.7.windows-6.2-amd64.zip' '.'"

Either change directory (cd) into the cockroach-v21.1.7.windows-6.2-


amd64

directory and invoke the cockroach executable when you need it, or
add its containing directory to your PATH and invoke the cockroach
executable from any directory.

The cockroach executable is now available, and you can start a local
instance of CockroachDB using the following command:

$ cockroach start-single-node --insecure --listen-addr=localhost

We’ve just started a single node, insecure cluster, listening on


localhost with the default 26527 port for SQL connections and the
8080 port for HTTP connections.

To test that CockroachDB is up and running, use the cockroach sql


command to enter the CockroachDB SQL shell. Note that the --host
argument can be omitted, as the default value assumes a locally
running node using the default port:

$ cockroach sql --insecure

# Welcome to the CockroachDB SQL shell.

# All statements must be terminated by a semicolon.

7
Another Random Scribd Document
with Unrelated Content
IN A GARDEN
Gushing from the mouths of stone men
To spread at ease under the sky
In granite-lipped basins,
Where iris dabble their feet
And rustle to a passing wind,
The water fills the garden with its rushing,
In the midst of the quiet of close-clipped lawns.

Damp smell the ferns in tunnels of stone,


Where trickle and plash the fountains,
Marble fountains, yellowed with much water.

Splashing down moss-tarnished steps


It falls, the water;
And the air is throbbing with it;
With its gurgling and running;
With its leaping, and deep, cool murmur.

And I wished for night and you.


I wanted to see you in the swimming-pool,
White and shining in the silver-flecked water.
While the moon rode over the garden,
High in the arch of night,
And the scent of the lilacs was heavy with stillness.

Night and the water, and you in your whiteness, bathing!

Amy Lowell
POSTLUDE
Now that I have cooled to you
Let there be gold of tarnished masonry,
Temples soothed by the sun to ruin
That sleep utterly.
Give me hand for the dances,
Ripples at Philæ, in and out,
And lips, my Lesbian,
Wall flowers that once were flame.

Your hair is my Carthage


And my arms the bow
And our words arrows
To shoot the stars,
Who from that misty sea
Swarm to destroy us.
But you’re there beside me
Oh, how shall I defy you
Who wound me in the night
With breasts shining
Like Venus and like Mars?
The night that is shouting Jason
When the loud eaves rattle
As with waves above me
Blue at the prow of my desire!
O prayers in the dark!
O incense to Poseidon!
Calm in Atlantis.

William Carlos Williams


I HEAR AN ARMY

I hear an army charging upon the land,


And the thunder of horses plunging; foam about their knees:
Arrogant, in black armour, behind them stand,
Disdaining the rains, with fluttering whips, the Charioteers.

They cry into the night their battle name:


I moan in sleep when I hear afar their whirling laughter.
They cleave the gloom of dreams, a blinding flame,
Clanging, clanging upon the heart as upon an anvil.

They come shaking in triumph their long grey hair:


They come out of the sea and run shouting by the shore.
My heart, have you no wisdom thus to despair?
My love, my love, my love, why have you left me alone?

James Joyce
ΔΏΡΙΑ

Be in me as the eternal moods


of the bleak wind, and not
As transient things are—
gaiety of flowers.
Have me in the strong loneliness
of sunless cliffs
And of grey waters.
Let the gods speak softly of us
In days hereafter,
The shadowy flowers of Orcus
Remember Thee.

Ezra Pound
THE RETURN

See, they return; ah, see the tentative


Movements, and the slow feet,
The trouble in the pace and the uncertain
Wavering!

See, they return, one, and by one,


With fear, as half-awakened;
As if the snow should hesitate
And murmur in the wind
and half turn back;
These were the “Wing’d-with-Awe,”
Inviolable.

Gods of the winged shoe!


With them the silver hounds
sniffing the trace of air!
Haie! Haie!
These were the swift to harry;
These the keen-scented;
These were the souls of blood.

Slow on the leash,


pallid the leash-men!

Ezra Pound
AFTER CH’U YUAN

I will get me to the wood


Where the gods walk garlanded in wisteria,
By the silver-blue flood move others with ivory cars.
There come forth many maidens
to gather grapes for the leopards, my friend.
For there are leopards drawing the cars.

I will walk in the glade,


I will come out of the new thicket
and accost the procession of maidens.

Ezra Pound
LIU CH’E

The rustling of the silk is discontinued,


Dust drifts over the courtyard,
There is no sound of footfall, and the leaves
Scurry into heaps and lie still,
And she the rejoicer of the heart is beneath them:

A wet leaf that clings to the threshold.

Ezra Pound.
FAN-PIECE FOR HER IMPERIAL LORD

O fan of white silk,


clear as frost on the grass-blade,
You also are laid aside.

Ezra Pound
TS’AI CHI’H

The petals fall in the fountain,


the orange coloured rose-leaves,
Their ochre clings to the stone.
Ezra Pound.
IN THE LITTLE OLD MARKET-PLACE

(To the Memory of A. V.)


It rains, it rains,
From gutters and drains
And gargoyles and gables:
It drips from the tables
That tell us the tolls upon grains,
Oxen, asses, sheep, turkeys and fowls
Set into the rain-soaked wall
Of the old Town Hall.

The mountains being so tall


And forcing the town on the river,
The market’s so small
That, with the wet cobbles, dark arches and all,
The owls
(For in dark rainy weather the owls fly out
Well before four), so the owls
In the gloom
Have too little room
And brush by the saint on the fountain
In veering about.

The poor saint on the fountain!


Supported by plaques of the giver
To whom we’re beholden;
His name was de Sales
And his wife’s name von Mangel.

(Now is he a saint or archangel?)


He stands on a dragon
On a ball, on a column
Gazing up at the vines on the mountain:
And his falchion is golden
And his wings are all golden.
He bears golden scales
And in spite of the coils of his dragon, without hint of alarm or
invective
Looks up at the mists on the mountain
Looks up at the mists on the mountain.

(Now what saint or archangel


Stands winged on a dragon,
Bearing golden scales and a broad bladed sword all golden?
Alas, my knowledge
Of all the saints of the college,
Of all these glimmering, olden
Sacred and misty stories
Of angels and saints and old glories . . .
Is sadly defective.)
The poor saint on the fountain . . .

On top of his column


Gazes up sad and solemn.
But is it towards the top of the mountain
Where the spindrifty haze is
That he gazes?
Or is it into the casement
Where the girl sits sewing?
There’s no knowing.

Hear it rain!
And from eight leaden pipes in the ball he stands on
That has eight leaden and copper bands on,
There gurgle and drain
Eight driblets of water down into the basin.

And he stands on his dragon


And the girl sits sewing
High, very high in her casement
And before her are many geraniums in a parket
All growing and blowing
In box upon box
From the gables right down to the basement
With frescoes and carvings and paint . . .

Th i t!
The poor saint!
It rains and it rains,
In the market there isn’t an ox,
And in all the emplacement
For waggons there isn’t a waggon,
Not a stall for a grape or a raisin,
Not a soul in the market
Save the saint on his dragon
With the rain dribbling down in the basin,
And the maiden that sews in the casement.

They are still and alone,


Mutterseelens alone,
And the rain dribbles down from his heels and his crown,
From wet stone to wet stone.
It’s grey as at dawn,
And the owls, grey and fawn,
Call from the little town hall
With its arch in the wall,
Where the fire-hooks are stored.

From behind the flowers of her casement


That’s all gay with the carvings and paint,
The maiden gives a great yawn,
But the poor saint—
No doubt he’s as bored!
Stands still on his column
Uplifting his sword
With never the ease of a yawn
From wet dawn to wet dawn . . .

Ford Madox Hueffer


SCENTED LEAVES FROM A CHINESE JAR
THE BITTER PURPLE WILLOWS
Meditating on the glory of illustrious lineage I lifted up my eyes and
beheld the bitter purple willows growing round the tombs of the
exalted Mings.
THE GOLD FISH
Like a breath from hoarded musk,
Like the golden fins that move
Where the tank’s green shadows part—
Living flames out of the dusk—
Are the lightning throbs of love
In the passionate lover’s heart.
THE INTOXICATED POET
A poet, having taken the bridle off his tongue, spoke thus: “More
fragrant than the heliotrope, which blooms all the year round, better
than vermilion letters on tablets of sendal, are thy kisses, thou shy
one!”
THE JONQUILS
I have heard that a certain princess, when she found that she had
been married by a demon, wove a wreath of jonquils and sent it to
the lover of former days.
THE MERMAID
The sailor boy who leant over the side of the Junk of Many Pearls,
and combed the green tresses of the sea with his ivory fingers,
believing that he had heard the voice of a mermaid, cast his body
down between the waves.
THE MIDDLE KINGDOM
The emperors of fourteen dynasties, clad in robes of yellow silk
embroidered with the Dragon, wearing gold diadems set with pearls
and rubies, and seated on thrones of incomparable ivory, have ruled
over the Middle Kingdom for four thousand years.
THE MILKY WAY
My mother taught me that every night a procession of junks carrying
lanterns moves silently across the sky, and the water sprinkled from
their paddles falls to the earth in the form of dew. I no longer
believe that the stars are junks carrying lanterns, no longer that the
dew is shaken from their oars.
THE SEA-SHELL
To the passionate lover, whose sighs come back to him on every
breeze, all the world is like a murmuring sea-shell.
THE SWALLOW TOWER
Amid a landscape flickering with poplars, and netted by a silver
stream, the Swallow Tower stands in the haunts of the sun. The
winds out of the four quarters of heaven come to sigh around it, the
clouds forsake the zenith to bathe it with continuous kisses. Against
its sun-worn walls a sea of orchards breaks in white foam; and from
the battlements the birds that flit below are seen like fishes in a
green moat. The windows of the Tower stand open day and night;
the winged Guests come when they please, and hold communication
with the unknown Keeper of the Tower.
Allen Upward
THE ROSE

I remember a day when I stood on the sea shore at Nice, holding a


scarlet rose in my hands.
The calm sea, caressed by the sun, was brightly garmented in blue,
veiled in gold, and violet, verging on silver.
Gently the waves lapped the shore, and scattering into pearls,
emeralds and opals, hastened towards my feet with a monotonous,
rhythmical sound, like the prolonged note of a single harp-string.
High in the clear, blue-golden sky hung the great, burning disc of the
sun.
White seagulls hovered above the waves, now barely touching them
with their snow-white breasts, now rising anew into the heights, like
butterflies over the green meadows . . .
Far in the east, a ship, trailing its smoke, glided slowly from sight as
though it had foundered in the waste.
I threw the rose into the sea, and watched it, caught in the wave,
receding, red on the snow-white foam, paler on the emerald wave.
And the sea continued to return it to me, again and again, at last no
longer a flower, but strewn petals on restless water.
So with the heart, and with all proud things. In the end nothing
remains but a handful of petals of what was once a proud flower . . .
John Cournos after K. Tetmaier

You might also like