Reference Tables
A reference table is a table that is fully replicated across all shards instead of being divided into separate partitions. This allows every shard to have a local copy of the table, reducing network overhead and ensuring fast access to frequently used data.
Reference tables are useful when you need:
- Consistent access to shared data across all shards.
- Fast lookups without cross-shard queries.
- Integrity constraints on small tables used across different parts of an application.
Examples
Some typical use cases for reference tables include:
- eCommerce: Sales tax rates apply to all stores.
- Common codes: A table with country codes and region names.
- Roles: A system-wide list of roles and permissions.
- Finance: Exchange rates.
- Global settings: Settings or feature flags.
- SaaS: A table defining different subscription tiers, their pricing, and feature sets.
How to use it?
Creating a reference table in SPQR is just a metadata operation. You will need to create this table on the shards as well, either through the SPQR router or any other method you prefer.
To create a reference table, use the SPQR administrative console:
Then, create the table and insert data:
Verify replication:
Sequences
In some cases, it may be beneficial to avoid manually specifying a unique identifier (commonly the id
column) when creating new records. Instead, you can rely on the AUTO INCREMENT feature provided by the database.
For reference tables, this can be challenging because the id
values must remain synchronized across all shards. Fortunately, SPQR supports this functionality through a feature called sequences.
Here’s an example of how you can automatically generate id
values. To create a reference table with country codes and region names, follow these steps:
Then, create the table and insert data:
Verify replication:
Was this page helpful?