Part 6 of 8 · 3 min read
Splitting the database
Find the data seams, then cut one case at a time.
BM calls the database the mother of all tangled dependencies. Page 7 said services should not share one. So the database must be split too.
Step 1: see which code touches which tables. Move the data-access code into the same context packages as the rest. A tool that draws the table links also shows database-level rules, such as a foreign key between two tables that the code does not show.
Step 2: cut each tie. BM gives four cases.
Foreign keys across contexts. Finance has a ledger table, and catalog has a line item table. A monthly report reaches into line items for album titles, and a foreign key links the two. Fix in two parts. First, stop finance from reading the catalog table directly. Offer the titles through a call on the catalog package, which later becomes a call over the network. Second, drop the foreign key. The report now needs two lookups instead of one. BM's answer to speed worries is to ask how fast the system needs to be, and how fast it is now. The lost rule must be handled in the services. If an order holds catalog ids and an item is removed, what should happen? That is a business question, not a technical one.
Shared static data. Country codes stored in a table that every part reads. Options: copy the table to each context, treat the data as code (a configuration file or enumeration), or, at the extreme, make a service for it. BM prefers configuration files or code for simple cases, since a file is easier to push out than a live table.
Shared mutable data. Finance writes payments and refunds, and the warehouse writes dispatch records, in one generic customer record table. BM sees a missing concept: the domain idea of Customer exists in the database but not in the code. Make it concrete. First a Customer package that others call through an interface. Later a customer service.
Shared tables. Catalog stores name and price, the warehouse stores stock counts, all in one line item table. Two ideas are mixed. Split the table in two, for example a stock list and a catalog entry table.
Step 3: stage the break. BM recommends two moves, not one. First split the schema but keep the application together. Then split the application. With separate schemas but one application, the team can still revert or adjust without any consumers noticing. Only when happy with the data split are the services separated.
Database refactoring
A database refactoring is a small change to a database design that keeps its meaning, such as splitting a table in two or dropping a link between tables.
Example: a line item table becomes a stock list and a catalog entry table, with the same data and no change in what the shop can do.
A worked example (made up). A single table customer_record has 12 columns. Finance writes 4 columns, the warehouse writes 3, and the website reads all 12. The team creates a Customer package with an interface for the three kinds of access. Finance and the warehouse stop writing the table directly and call it. Only then does the table move to its own schema, and finally the Customer package becomes a service.
Check yourself: 1. What does a table-drawing tool show that the code does not? 2. Why does BM split the schema before splitting the application? 3. What missing concept did the shared customer table reveal?