Databases: recap and quiz
The module in five sentences
Section titled “The module in five sentences”- Choosing a data model. A data model decides which questions are cheap, so choose it from the access pattern, and expect no model to meet both a transaction across records and a write load spread by key.
- Storage engines. A B-tree changes pages in place, an LSM-tree appends and merges, and a write-ahead log makes a commit survive a crash, so a commit is acknowledged once the log is flushed.
- Indexing and query plans. An index turns a scan that grows with the table into a lookup that grows with its logarithm, at the price of work on every write, and the planner uses it only when the query keeps a small share of the table.
- Normalisation and denormalisation. Normalisation keeps each fact once, and denormalisation copies it where it is read, trading read cost for write work and copies that can disagree, while some values that look like copies are different facts.
- Connection pools. A pool is a queue with as many servers as connections, so slow queries or leaked connections push it to overload, a larger pool is not a faster database, and the connections of every instance must fit the database’s limit.
How each one breaks
Section titled “How each one breaks”| Idea | What breaks | What you see | What helps |
|---|---|---|---|
| Data model | A question the model was not chosen for | The application does the database’s work: a second index kept by hand, or a scan | Write the access pattern down first, and keep a second store for a second kind of question |
| Storage engine | A commit acknowledged before its log is flushed, or fsync turned off | The last commits lost, and at worst a corrupt database | Keep the defaults for data that matters, and check that the storage honours flushes |
| Index | The planner ignores it, or there are too many | A scan that grows with the table, or slow writes | Read the plan on data of the real size, index for the queries, drop unused indexes |
| Copies | A copy is missed or goes stale | Two values for one fact | Give every copy an owner, decide how stale it may be, and keep different facts in the row |
| Connection pool | Slower queries, a leak, or too many instances | Waiting, then timeouts, or refused connections | Find the slow query, return every connection, and budget pools against the limit |
A checklist for the data layer
Section titled “A checklist for the data layer”Use these when you design the data layer, and when you review someone else’s. Each question points back to the lesson that answers it.
- What are the three or four reads and writes that matter most? Write down how often each happens, by which key, and what must never go wrong. (Choosing a data model)
- Does one change touch several records, and are next month’s questions unknown? If both, the relational model is built for it. If you chose another, say which cost you accepted. (Choosing a data model)
- What does a commit promise? Know what the database flushes before it answers, and what a power failure can lose under the settings you run. (Storage engines)
- Which queries filter, sort or join, and is there an index for each? Read the plan on data of the real size, and compare the estimates with
EXPLAIN ANALYZE. (Indexing and query plans) - How many indexes does each write pay for? Keep the smallest set that serves the access patterns. (Indexing and query plans)
- For each copy of a fact, who updates it, and how stale may it be? Check that a value that should not change with the original is stored as its own fact. (Normalisation and denormalisation)
- How many connections can the fleet open, and what is the database’s limit? Multiply the pool size by the instances, including the instances an autoscaler may add. (Connection pools)
- What happens to the pool when queries get twice as slow? Compute the pool’s capacity, its size divided by the query time, and compare it with the demand. (Connection pools)
- Did you measure it? Every figure in these lessons is arithmetic on stated assumptions or taken from a documentation page. Your system’s own numbers are the ones that decide.
Check your understanding
Section titled “Check your understanding”Seven questions cover the whole module. Every option is explained, whether it is right or wrong.
Databases: module quiz
This quiz needs JavaScript to check your answers.
Where this goes next
Section titled “Where this goes next”The first lesson showed that no option meets both a transaction across records and a write load spread by key. The next three modules are about paying for that: how a database keeps several steps all-or-nothing while other sessions run, how it keeps copies of its data on other machines, and how it splits its data across machines. When you meet them, ask this module’s questions first: what is the access pattern, what does a commit promise, and where are the copies.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.