

# Describing existing data partitions with data mappings

If your application or a proxy already splits data across several Aurora DB clusters, you can bring that application into a database cluster group for Aurora without moving data between clusters. Member clusters must run Aurora MySQL version 8.4.900, and you can't upgrade an existing cluster to it, so you restore a snapshot of each cluster onto version 8.4.900 and add the restored cluster. For instructions on restoring a snapshot and adding a cluster, see [Add member clusters](databaseclustergroups-gs-add-members.md). A restored cluster is a new DB cluster with its own endpoints. When you enable routing, data mappings describe the split that you already have, so no data moves.

Your directory or proxy keeps working against the original clusters until you point it at the member clusters, and every member cluster stays directly reachable through its own endpoints. You route applications one at a time, you can point each one back, and you change your layout only when you choose. To take a cluster out of the group, see [Removing a member cluster](databaseclustergroups-member-clusters.md#databaseclustergroups-removing-members).

## Clusters placed by a directory in the application
<a name="databaseclustergroups-existing-split-directory"></a>

In this layout, a directory tells the application which cluster holds each tenant, and the application connects there directly. If you enable routing, data mappings describe the same placement as the directory, and you point directory entries at the routing endpoint one at a time.

**To manage clusters placed by a directory from one group**

1. With the AWS CLI or the Amazon RDS API, restore a snapshot of each existing cluster onto version 8.4.900, and add each restored cluster to the group, as described in [Add member clusters](databaseclustergroups-gs-add-members.md). The restored clusters have new endpoints, so the directory keeps pointing at the original clusters until you change it. Writes made after the snapshot don't reach the restored member cluster, so stop writes or copy them before you switch. If you don't use routing, you can continue at [Managing a database cluster group for Aurora](databaseclustergroups-managing.md).

1. Over a direct connection to each member cluster, check that each application router user has its same-named member cluster user, with grants. For more information, see [Router users and routing layer privileges](databaseclustergroups-router-users.md).

1. At the routing endpoint, export the data mappings, as described in [Exporting the mappings before and after a change](databaseclustergroups-changing-mappings.md#databaseclustergroups-export-mappings).

1. Generate mapping rows from the directory with a script, and write them at the routing endpoint, outside a transaction. Data mapping changes aren't transactional, so a load that spans several statements can stop part way: read the mappings back after each statement. The tables after this procedure show the rows for each layout.

   One statement can add, change, or remove at most 1,000 data mappings. A larger `INSERT` writes nothing and returns error 1235. For more information about the limit, see [Routing metadata and naming limits](databaseclustergroups-limitations.md#databaseclustergroups-limitations-routing-metadata-limits). Split a larger load into several statements.

1. Verify through the routing endpoint while every request still uses its direct connection. For example, compare one tenant's row counts read both ways.

1. In the directory, change the endpoint to the routing endpoint for one member cluster's tenants at a time, reads before writes, as described in [Sending traffic through the routing layer](databaseclustergroups-routing-traffic.md).

If each tenant has its own database, make each directory entry one database-level mapping, with one collocation for each tenant, so that each tenant can move on its own later. When a database or table has the same name on every cluster, the name alone can't tell the routing layer where a tenant is. Give each copy a unique router name, each in its own collocation. Use the following mapping rows.


| Directory entry | Mapping row | 
| --- | --- | 
| Tenant 1 on the first cluster | Database tenant\_0001 on member-a, collocation cg\_t0001 | 
| Tenant 2 on the second cluster | Database tenant\_0002 on member-b, collocation cg\_t0002 | 


| Copy | Mapping | Router name | 
| --- | --- | --- | 
| Database app on member-a, member-b, and member-c | Database-level, collocations cg\_s1, cg\_s2, cg\_s3 | app\_s1, app\_s2, app\_s3 | 
| Table app.orders on each member cluster | Table-level, each copy in its own collocation | app.orders\_m1, app.orders\_m2, app.orders\_m3 | 

If every directory entry uses the routing endpoint, the application can drop the directory lookup. You can then rebalance by moving a collocation with data movement. For more information, see [Moving data between member clusters](databaseclustergroups-data-movement.md).

If you give same-named copies unique router names, keep the directory, and change its entries to the routing endpoint and the router name. The application still does its own lookup, and the SQL changes only in the name that it uses. A job moves a whole copy with its collocation, and the router name stays the same. To let the routing layer choose the member cluster instead of the directory, see [Replacing your placement with a routing function](#databaseclustergroups-existing-split-second-pass).

## Clusters behind a proxy that splits data
<a name="databaseclustergroups-existing-split-proxy"></a>

In this layout, a proxy sends each statement to one of several backend clusters, by query rules or by a split key and lookup tables. The backends must be Aurora DB clusters of the group's engine to join a group as they are. To load data from another MySQL-compatible database into Aurora MySQL, see [Migrating data to an Amazon Aurora MySQL DB cluster](AuroraMySQL.Migrating.md).

You can add the backend clusters as they are and keep the proxy in front of them. To route some applications through the routing endpoint instead, inventory the proxy's configuration: every rule, split key, placement rule, lookup table, sequence, and reference table. Also list the statements that the proxy sends to more than one backend. Then sort each item into the following tables. Before you route an application, change it for the items that have no equivalent.


| In the proxy | In a database cluster group for Aurora | 
| --- | --- | 
| A rule that sends a database, a schema, or one table to a backend cluster | A database-level mapping or a table-level mapping, on that member cluster | 
| A set of tables split by a split key | A scaleout table for each table, routed by that key through one shared routing function | 
| Hash or modulo placement, placement by the leading segments of a key up to a delimiter, or placement by key ranges or value lists | The HASH, MOD, PREFIX, or COLUMN routing function that Aurora provides | 
| Placement by your own logic, such as a lookup table that maps each key to a backend | Database-level or table-level data mappings that record the placement that your lookup chooses, or session pinning to the member cluster that holds the key | 
| Tables split by the same key, kept on the same backend cluster | Tables routed by the same routing function, in the same collocations | 
| A backend cluster's share of the data that you move to rebalance | A collocation that you move | 
| Connection pooling in the proxy | The routing endpoint, with pools sized for each member cluster | 

For the routing functions, see [Routing functions](databaseclustergroups-scaleout-tables.md#databaseclustergroups-routing-function-availability).


| No equivalent in a group | Do this instead | 
| --- | --- | 
| A lookup that routes by a column other than the split key | Read an index table first, then read by key. | 
| Scatter queries, and joins or transactions across backend clusters | Each statement and transaction runs on one member cluster. Fan out statements for each key in the application and merge the results, or run the report outside the group. For distributed queries and transactions in one logical database, Aurora DSQL is a complementary option. | 
| Reference tables that the proxy copies to every backend cluster | Cache or denormalize them, or keep them in their own database. | 
| Read and write splitting to replicas | Routed statements run on each member cluster's writer. To read from replicas, connect directly to the member cluster's reader endpoint or instance endpoints. | 
| Sequences that the proxy manages | Generate the values in your application. A sequence or AUTO\_INCREMENT column counts within one member cluster, so its values can repeat between member clusters. | 

For more information about these patterns, see [Designing schemas and queries for one member cluster](databaseclustergroups-schema-design.md). Then add the backend clusters as they are, write the mappings for the items that map directly, and use the routing endpoint alongside the proxy. Move one application, or one backend's share of traffic, to the routing endpoint at a time, reads before writes. To go back, point the application at the proxy endpoint again. The proxy still reaches the member clusters at their cluster endpoints, with the same data.

## Replacing your placement with a routing function
<a name="databaseclustergroups-existing-split-second-pass"></a>

To drop the directory or the proxy's key placement and let the routing layer place each key, you can convert the tables in a later, separate step. A routing function maps each routing key value to a collocation. It is the `MOD`, `HASH`, `PREFIX`, or `COLUMN` function that Aurora provides.

If a routing function selects the same member cluster that your directory or proxy selects today, the existing tables can become the member tables of a scaleout table, and the data stays where it is. `MOD` reproduces a modulo placement on the same key. `PREFIX` reproduces a placement by the leading segments of a key, up to a delimiter. It can't cut a key at a fixed number of characters. `COLUMN` reproduces a placement by lists of key values, when each collocation lists the values that your directory sends to one cluster. `COLUMN` also reproduces a placement by key ranges, but each collocation holds one range and one member table. So an existing table stays in place only if it holds one contiguous range. `HASH` uses the routing layer's own hash, so it doesn't reproduce a hash that your proxy computes. If no routing function reproduces your placement, convert the tables to new member tables, as described at the end of this section.

First prepare the schema, on the clusters as they are, well before you route any table:
+ Read statement digests on the production instances, and run the queries that scan tables on an Aurora clone, never on the production writer. For more information, see [Choosing a routing key and routing function](databaseclustergroups-routing-key.md).
+ Add the key as a non-nullable column of one type and collation to every table that a tenant owns, including child tables that carry only their parent's ID. Shared data carries no tenant key and stays at database level.
+ Have the application write the key on every new row. Backfill existing rows in keyed batches, then make the key `NOT NULL` and include it in every unique constraint. A unique constraint holds across a scaleout table only when it includes every routing key column.
+ Make your schema lint fail any new owned table that lacks the key, carries it as nullable, or gives it another type.

To convert the tables to new member tables, do the following:

1. Create scaleout tables over new member tables, with one routing function shared by the tenant's tables. Child tables use `USING ROUTING FUNCTION OF` the parent. For more information, see [Creating and loading a scaleout table](databaseclustergroups-create-scaleout-table.md).

1. Backfill the new tables from the copies that have their own router names, batched by key.

1. Route reads to the new tables first, then writes, one member cluster at a time.

1. Keep the per-copy router names in use until counts and checksums match, as described in [Sending traffic through the routing layer](databaseclustergroups-routing-traffic.md).

Going back by changing the directory entry or the connection setting back works before you move any data. The group copies no data back, and nothing changes on the member clusters. After a data move, going back means moving the collocation back, which copies the data again. After you convert a table to a scaleout table, merging the member tables back into one table is a copy that you run.

## Related pages
<a name="databaseclustergroups-existing-split-related"></a>

The following pages cover related tasks and concepts.
+ [Adding and removing member clusters](databaseclustergroups-member-clusters.md)
+ [Using database-level and table-level mappings](databaseclustergroups-mapping-levels.md)
+ [Designing schemas and queries for one member cluster](databaseclustergroups-schema-design.md)
+ [Sending traffic through the routing layer](databaseclustergroups-routing-traffic.md)