Designing schemas and queries for one member cluster
In a database cluster group for Aurora, each member cluster stays a standard Aurora DB cluster, so schema design inside it works as usual. For more information, see Best practices with Amazon Aurora MySQL. Through the routing layer, each statement and transaction runs on one member cluster, and no constraint applies across member clusters. Treat the collocation as the design boundary.
Designing statements that route to one member cluster
A statement that resolves to one member cluster is the fastest way to read and the only way to write. Follow these practices:
-
Keep rows that change together on one member cluster. A write across member clusters can't be atomic.
-
Give every statement on a scaleout table a routing key predicate. Use equality or
INon literals that your application computes. Under aCOLUMNrouting function withVALUES IN RANGEcollocations on an integer or temporal key, a range inside one collocation also routes. A range that spans collocations returns error1235. Any other range, a pattern, null test, inequality, function on the key column, or key inside a subquery or view doesn't resolve. A subquery or a common table expression over a scaleout table routes only when its own routing key predicate resolves it to the same member cluster as the rest of the statement. On a table with a database-level or table-level mapping, any predicate routes. -
Batch multi-row writes by member cluster. The routing layer rejects a multi-row
INSERTwhose rows resolve to more than one member cluster, and anUPDATEorDELETEmust resolve to one member cluster through its routing key. Group the rows by member cluster in your application. UnderMOD,PREFIX, andCOLUMN, your application can compute the collocation of each row from the routing function. UnderHASH, it can't, so send one row in eachINSERT. -
Read session-state functions directly after the statement that they report on. On Aurora MySQL, the routing layer sends
LAST_INSERT_ID(),FOUND_ROWS(), andROW_COUNT()to the member cluster that served that statement.
For keeping transactions on one member cluster and retrying them, see Running queries and transactions through the routing layer.
Shaping tables around the routing key
Inside a collocation, normalize as on one cluster. A related set of tables routes to one member cluster when both of the following are true: every table carries the root key, such as tenant_id, and every scaleout table in the set inherits the root table's function with USING ROUTING FUNCTION OF.
The following shapes don't stay on one member cluster:
-
A child keyed by its parent's ID. A team routed by
dept_idor a project routed byteam_idspreads one root's rows over member clusters. Key every level by the root. -
The same key column under a different routing function. Mismatched functions send rows with the same key value to different member clusters, and the routing layer doesn't detect it, so give tables that share collocations the same routing function.
-
A scaleout table joined with a table that has a database-level or table-level mapping. On Aurora MySQL, the routing layer refuses a statement that names both, with error 1235. Read each table with its own statement, and combine the results in your application.
Keep routing and join keys in typed columns, not inside JSON, because a JSON column can't be a routing key.
Declare integer IDs and routing keys as BIGINT NOT NULL, with the same type on the routing function parameter and on the key column in every member table. For the reasons, see Properties of a good routing key.
On Aurora MySQL, routed writes don't use the member cluster's sql_mode parameter. A routed session runs with the sql_mode routing endpoint parameter, whose default is an empty value, unless the session sets its own. Set a strict mode, such as STRICT_TRANS_TABLES, in the routing endpoint parameter or in each session, so that a member cluster refuses a write that would truncate or clamp a key instead of storing a different value. For more information, see Supported system variables.
Join patterns
A join resolves only when every table that it names resolves to the same member cluster. You meet this rule with placement, not with a rewrite of the query. Joined scaleout tables must share the routing key and routing function. Then a literal routing key on one table is enough, inside or outside a transaction. When the keys are on different member clusters, the routing layer rejects the join, with error 1235 on Aurora MySQL. When the tables can't share a key, choose one of the following patterns.
| Pattern | Use when | Cost |
|---|---|---|
| Same routing key | Parent and child carry the key and share one routing function | The key column on every table |
| Propagate the key | The child carries only its parent's ID, such as order_items.order_id |
A new column, a backfill, and the key passed on every write |
| Reference table | A small, read-mostly table that everything joins to, such as postal codes | A separate read. Query routing reaches only the one mapped copy. To keep a copy on each member cluster, reach each copy through a pinned session or a direct connection, and keep the copies the same from your application. |
| Per-key queries | A report or list over many keys | One query for each member cluster, with a routing key or against that member cluster's endpoint, combined in the application |
Before you route the application, run EXPLAIN on every critical join at the routing endpoint to see the member cluster that the routing layer selects.
IDs, sequences, and AUTO_INCREMENT
A database cluster group for Aurora has no global sequence. On Aurora MySQL, each member cluster generates AUTO_INCREMENT values independently, so these values can repeat between member clusters. To get a value that's unique across the group, generate it in your application. Use that value as the primary key of a table whose rows span member clusters, not an identity column that a sequence fills. For a scaleout table, the routing layer never fills a default, sequence, or generated column in the routing key, so include the routing key value in every insert.
Generated AUTO_INCREMENT values behave as follows:
-
Database-level and table-level mappings. A mapped table is on one member cluster, so its
AUTO_INCREMENTvalues stay unique within the table, andLAST_INSERT_ID()works as on a single cluster. -
Scaleout tables. Each member table keeps its own counter, so values can repeat across member clusters. A data movement job carries the counter with the table. The member cluster can't generate the routing key, even in an
AUTO_INCREMENTcolumn.
Route a UUID as a character key under HASH, because binary types don't work as routing keys and Aurora MySQL doesn't accept a UUID type.
Uniqueness, triggers, and routines
Each member cluster enforces primary key, unique, and check constraints on its own rows only. The group has no global unique constraints or global secondary indexes. In a scaleout table, a unique key holds across the group only when it includes every routing key column, such as UNIQUE KEY (tenant_id, email). Generate a value that must be unique across the group in your application.
Triggers, views, stored routines, and events run on the member cluster that holds them and see only its data. Create them on each member cluster that needs them, through a member cluster endpoint or a pinned session. A routed write to a scaleout table fires the triggers of the member table that it reaches.
Foreign keys
A foreign key is a standard engine constraint, checked only against the rows on the child's member cluster. The routing layer doesn't check foreign keys. The following table shows where a foreign key holds.
| Where the parent and child are | Does the key hold? |
|---|---|
| One collocation, database-level or table-level mappings | Yes, as on one cluster |
| Member tables of scaleout tables on one routing function, in one collocation | Yes, with children OF the root and a composite key such as (tenant_id, order_id) |
| Two collocations on one member cluster | Only until a move separates the collocations |
| Two member clusters | Never |
Keep each set of tables linked by foreign keys in one collocation. After a mapping or routing function change, check for orphan rows, because the change moves no rows. On Aurora MySQL, a move's precheck refuses a table that has a foreign key or that a foreign key references, even when the related tables move together. Drop the foreign keys before the move. A trigger on a table with a table-level mapping also blocks the precheck. For the steps, see Moving data between member clusters.
Work that spans member clusters
The routing layer refuses a join, UNION, subquery, or transaction whose objects are on two member clusters. It doesn't gather the rows. The following table shows what to do instead. For queries and transactions that span databases, Aurora DSQL is a complementary option.
| The work | Do this instead |
|---|---|
| A join of tables that are always queried together | Collocate them with one of the preceding join patterns |
| A join of tables that can't share a key | Join in the application, with one keyed statement for each side |
| A report, count, or aggregate over many keys | Run one query for each member cluster and combine the results in the application |
| A transaction over two keys | Put the rows that must change together on one member cluster. Or write to each member cluster in a separate transaction, and detect and reconcile a failure on one of them in your application. |
| A join or transaction over mapped tables in two collocations that currently share a member cluster | Reshape it before you move either collocation, because it fails after a move separates them. On a scaleout table, a statement whose key values fall in two collocations is refused with error 1235, No member cluster could be resolved for this query, even while both collocations share a member cluster |
To find statements that cross collocations before you route an application, see Sending traffic through the routing layer.
Related pages
The following pages cover related tasks and concepts.