The Distributed SQL Blog

Thoughts on distributed databases, open source and cloud native

Distributed SQL Tips and Tricks – March 6, 2020

Welcome to this week’s tips and tricks blog where we recap some distributed SQL questions from around the Internet. We’ll also review upcoming events, new documentation, and blogs that have been published since the last post. Got questions? Make sure to ask them on our YugabyteDB Slack channel, Forum, GitHub, or Stackoverflow. Ok, let’s dive right in:

How can I configure YugabyteDB for serialization isolation level?

YugabyteDB supports snapshot isolation and serializable isolation levels. By default we use snapshot isolation which maps to PostgreSQL’s repeatable read isolation level. There are several ways to change the isolation level:

1. Change the default isolation level at the cluster level for all databases by setting the --ysql_default_transaction_isolation yb-tserver gflag to SERIALIZABLE.

2. Change isolation level for the current session using the SET statement:

3. Change isolation level for the current transaction as part of the BEGIN TRANSACTION step:

4. Change isolation level for the current transaction using SET TRANSACTION statement:

Is it possible to create read-only users in YugabyteDB?

Yes. Read-only users are often used by applications where data manipulation is not required, for example reporting-type applications. Users with this type of custom permission can be created easily using the instructions below:

First create the user:

Next, let’s add the ability to connect to the database and the schema using the GRANT statement:

Now, we can grant read only access to all tables in the public schema via the SELECT privilege:

Finally, we can test the privileges of the new user in another shell to make sure the permissions are working as expected:

How does YugabyteDB recover data after a crash?

YugabyteDB uses DocDB for persistence. DocDB is a LSM storage engine forked from RocksDB. In a single node scenario, when the process restarts, YugabyteDB will read the Write Ahead Log (WAL) to return to the node to the state that it was in before it crashed.

Each DML statement is synchronously replicated using the Raft algorithm and each replica writes to its own local WAL.

When a node crashes, YugabyteDB picks a new leader tablet in just a couple of seconds. If the old node doesn’t come back online or it takes too long, tablet peers are replicated in other nodes of the cluster using a process called bootstrapping until the replication factor is achieved for each tablet.

Should I use auto-incrementing IDs in a distributed SQL database?

Auto-incremented IDs are not recommended in YugabyteDB because they can introduce a bottleneck and may lower write concurrency by creating hotspots. Since auto-increment IDs are generated by using a single row in a system table that maintains the ID, the tablet hosting the row becomes a hotspot. Furthermore, auto-incremented IDs are logically close together and can end up writing to a set of few tablets (if table uses range sharding), thereby increasing contention and load on those tablets. 

A better way to get similar results is to use uuid columns. They do not introduce a single point of bottleneck, can be generated in the server or client and are uniformly distributed across tablets when inserting new rows.

In the example below, we first create the extension pgcrypto which enables automatically generating uuid values with the gen_random_uuid() function and use it as the default value:

However, if your application still requires auto-incrementing IDs, then you can create those using serial (4 byte integer) or bigserial (8 byte integer) columns.

Serial columns use sequences for generating the IDs. To alleviate the hotspot problem that sequences cause, we recommend configuring a cache for sequences that will pre-allocate a set of auto-incrementing IDs and store them in memory for faster access.

New Blogs, Tutorials, and Videos

New Blogs

New Videos

Upcoming Events

We Are Hiring!

Yugabyte is growing fast and we’d like you to help us keep the momentum going! Check out our currently open positions:

Our team consists of domain experts from leading software companies such as Facebook, Oracle, Nutanix, Google, and LinkedIn. We have come a long way in a short time but we cannot rest on our past accomplishments. We need your ideas and skills to make us better at every function that is necessary to create the next great software company. All while having tons of fun and blazing new trails!

Get Started

Ready to start exploring YugabyteDB features? Getting up and running locally on your laptop is fast. Visit our quickstart page to get started.

High performance distributed SQL by Yugabyte

What’s Next?

  • Compare YugabyteDB in depth to databases like CockroachDB, Google Cloud Spanner and MongoDB.
  • Get started with YugabyteDB on macOS, Linux, Docker and Kubernetes.
  • Contact us to learn more about licensing, pricing or to schedule a technical overview.

Related Posts