Agent Wranglers

Data and databases playbook

Rules for data you can trust. Constraints in the database, fast queries, safe transactions and migrations, tested backups and personal data handled with care.

  • Required 21
  • Recommended 38
  • Optional 1

A data and databases playbook for any app that stores information. It covers schema design, queries and indexes, transactions, migrations, backups, personal data, day-to-day operation, database security and caches. The last chapter covers multi-tenancy, from the isolation model to deleting a tenant. Product analytics and A/B tests have their own product analytics and experimentation playbook, and search has the search playbook. Events and queues live in the integration and events playbook.

The examples use PostgreSQL, MySQL and SQLite. Where document stores such as MongoDB work differently, the rule says so.

Data outlives code. A bug in a page is fixed with the next deploy, but a bad row can stay wrong for years. So these rules put as much of the checking as possible inside the database itself.

Chapters

  1. Schema design

    Let the database guard its own data. Use constraints, exact types and stable keys, so no bug in any app can store a row that makes no sense.

    • Recommended 8
  2. Queries and indexes

    Ask the database only for what you need, in one round trip where you can. Index for the queries you run, and read the plan before you guess.

    • Recommended 5
  3. Transactions and concurrency

    Make each change all or nothing, and safe when two requests arrive at once. Retry conflicts, and stage imported files before they touch live tables.

    • Required 2
    • Recommended 3
  4. Migrations

    Change the schema only through numbered migrations in version control. Run them in the deploy, test them on a copy, and keep old and new code working.

    • Required 3
    • Recommended 3
  5. Backups and recovery

    Keep point-in-time recovery, drill failover and prove each backup restores. Backup encryption lives in database security, schedules and targets in operations.

    • Required 1
    • Recommended 2
  6. Personal data

    Store as little personal data as the job needs, delete it on a schedule and on request, and keep it out of test copies. Know who read it and when.

    • Required 5
    • Recommended 1
  7. Operations

    Keep the database healthy day to day. Pool connections, set time limits, watch slow queries and growth, check for corruption and let maintenance run.

    • Required 2
    • Recommended 5
  8. Database security

    Pass values as parameters, give the app a narrow login, close the paths from SQL to the server, and encrypt disks, backups and keys.

    • Required 3
    • Recommended 3
  9. Caches and copies

    Treat every cache, replica and search index as a copy that can be stale or lost. Say how fresh it must be, and keep the source of truth in one place.

    • Recommended 3
  10. Multi-tenancy

    Pick how far apart customers' data lives, carry the tenant into every job, file and log, and restore, export or delete one tenant alone.

    • Required 5
    • Recommended 5
    • Optional 1

All 60 rules on one page

This is a lot to check. An Expert can check it for you.

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data">Agent Wranglers data and databases playbook</a>. In code you write, add a comment next to it with the same link.