Skip to content
This repository was archived by the owner on Nov 12, 2024. It is now read-only.

Commit 6ef972e

Browse files
1 parent d20d377 commit 6ef972e

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

docs/sharding/keyspaces.md

+38-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,52 @@
11
---
22
title: 'What is a keyspace'
3-
subtitle: 'Learn about what a keyspace is and how to modify them on your PlanetScale database.'
4-
date: '2024-09-20'
3+
subtitle: 'Learn about what a keyspace is in Vitess and how to modify them on your PlanetScale database.'
4+
date: '2024-10-25'
55
---
66

7-
A keyspace is a logical database that maps directly to your database in PlanetScale. Once you begin sharding, you will create a new keyspace, or keyspaces, where your shards will exist. These sharded keyspaces map to new logical databases in your PlanetScale database.
7+
In Vitess, a keyspace is a logical database that maps to one or many MySQL instances. Keyspaces are used to group MySQL instances, typically to shard certain tables in your database cluster. While a keyspace may contain several MySQL instances across many shards, to your application, it will appear as a single database.
88

9-
The following diagram depicts a PlanetScale database with 2 keyspaces: one unsharded and one sharded. The unsharded keyspace has the default 1 primary and 2 replicas. The sharded keyspace contains two shards, each with 1 primary and 2 replicas.
9+
PlanetScale is built on top of Vitess, so every PlanetScale database cluster has one or more keyspaces.
10+
11+
## Unsharded keyspaces
12+
13+
If you only have a single, unsharded keyspace in your database cluster, then your keyspace maps directly to your database cluster in PlanetScale. For example, if you have an unsharded PlanetScale database named `gymtracker` with the default primary and 2 replicas, you likely only have a single keyspace. You can reference this keyspace in the same way you normally would your database:
14+
15+
```sql
16+
use gymtracker;
17+
show tables;
18+
```
1019

11-
When a new request comes in, they first go through our global edge gateway layer, and then hit the VTGates, which will forward the incoming query to the correct keyspace / shard.
20+
Unless your database cluster is sharded or you have created multiple unsharded keyspaces (uncommon), you don't necessarily need to understand the concept of a keyspace. Keyspaces become important once you start to consider sharding.
21+
22+
## Sharded keyspaces
23+
24+
As your database grows, you may wish to shard some tables in your database cluster. To do this, you will create a new keyspace and add the shards to the sharded keyspace.
25+
26+
The following diagram depicts a PlanetScale database with 2 keyspaces: one unsharded and one sharded. The unsharded keyspace has the default 1 primary and 2 replicas. The sharded keyspace contains two shards, each with 1 primary and 2 replicas.
1227

1328
![Keyspace diagram](/assets/docs/sharding/keyspace-diagram.png)
1429

30+
When a new request comes in, it first goes through our Global Edge Network layer. This Edge layer manages the connection and sends it to the correct Vitess cluster in PlanetScale. From here, the VTGates will parse the incoming query and determine the correct keyspace and shard to route it to.
31+
32+
If you want to directly target a keyspace, you can do that with the `use` syntax, but this time, specify the keyspace name. For example, if we created a sharded keyspace for `gymtracker`, we may call it `gymtracker_sharded`. You can directly target the keyspace with the following:
33+
34+
```sql
35+
use gymtracker_sharded;
36+
show tables;
37+
```
38+
39+
## Routing queries in your application
40+
41+
One of the many perks of Vitess/PlanetScale is that you can distribute your data across across hundreds of MySQL instances without complicating your application code. As mentioned earlier, all of these keyspaces and shards appear as a single MySQL instance to your application. Once you add multiple keyspaces, you no longer need to specify a database name in your application's database connection configuration code. Our Global Edge Network will correctly route you to the correct Vitess cluster, and the VTGates in your cluster will handle routing the request to the correct keyspace and shard.
42+
43+
Some frameworks and ORMs require a database name is specified. In these scenarios, you can set the database name to `@primary`, and your requests will be automatically routed to the correct keyspace/shard. Alternatively, if you have specific queries that you wish to send to replicas, you can use `@replica`.
44+
45+
## Modifying keyspaces in PlanetScale
46+
1547
Having 1 unsharded keyspace and 1 sharded is a typical setup for a database that needs sharding. With our [Cluster configuration panel](/docs/concepts/cluster-configuration), you are able to customize the number of shards in the sharded keyspace. You can also adjust the instance size for each primary and replica, and you can add additional replicas beyond the default of two if needed.
1648

17-
To get a better sense of this, click on your [Cluster configuration](/docs/concepts/cluster-configuration) tab in your dashboard. If you have an existing unsharded database, you'll see that database listed there as an unsharded keyspace. If you click "New keyspace", you're able to configure a brand new keyspace here.
49+
To get a better sense of this, or to configure your keyspaces, click on your [Cluster configuration](/docs/concepts/cluster-configuration) tab in your dashboard. If you have an existing unsharded database, you'll see that database listed there as an unsharded keyspace. If you click "New keyspace", you're able to configure a brand new keyspace here.
1850

1951
The most common use case for creating a new keyspace is to shard one or multiple tables.
2052

docs/sharding/vindexes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Queries are more performant when performed on a single shard. In other words, cr
113113

114114
Secondary indexes are optional, but may be created to help speed up lookups for queries in your workload.
115115

116-
Consider the case where we want to look at sequences of the log that happened for all user at a specific gym.
116+
Consider the case where we want to look at sequences of the log that happened for all users at a specific gym.
117117
Such a query would look something like this:
118118

119119
```sql

0 commit comments

Comments
 (0)