Database architecture
Database architecture decides how far an application can grow before it slows down. We design the schema around how the data is read and written, and we review slow queries with the query plan in hand. Schema changes ship as versioned migrations planned to run without stopping the application.
Database architecture: an overview
When an application gets slow, the cause is very often in the database: a missing index, a query that loads a whole table into memory, an ORM issuing one query per row of a list. They stay hidden while the tables are small. Good database architecture means designing the schema, the queries and the indexes so the application keeps behaving as it grows.
Data modeling comes first. We start from how the data is used: which screens read it, which jobs write it, which reports aggregate it, and what must stay consistent. That decides the tables, the keys, the constraints and where denormalization is acceptable.
Scaling tools get careful treatment. Read replicas, partitioning, caching layers and sharding all add moving parts and new ways to fail. They come in when a measurement shows they are needed: a query plan, a load test with k6, a monitoring graph of connection usage. Before that point, an index, a rewritten query or a connection pooler usually solves the problem with far less complexity.
What the service covers
Data modeling
We design tables, keys and constraints from how the data is read and written. Rules that must always hold, such as an invoice belonging to exactly one customer, are enforced by the database and not only by the code.
Query and index review
Slow pages are traced to the queries behind them, and each expensive query is read through its plan. Fixes range from a missing index to changing how the ORM loads related rows.
Zero-downtime migrations
Schema changes are versioned migrations in the repository, split into steps that run while the application is live. Each one is rehearsed in CI and in the test environment.
Multi-tenant data isolation
For SaaS products we choose between a tenant column, a schema per tenant or a database per tenant. Isolation is enforced in one place, by the database itself where it can be, rather than by a convention in the code.
Scaling when measured
Connection pooling, caching with Redis, read replicas and partitioning are introduced when a measurement shows the need. Each comes with the monitoring that shows whether it helped.
Backups and recovery
We review replication, backup frequency and retention against how much data you can afford to lose. The restore procedure is written down so someone other than its author can run it.
When it is the right call
- 01
Pages that were fast at launch now load slowly, and the database is the obvious suspect.
- 02
You are starting a product and want a data model that will not need rebuilding when a second type of customer arrives.
- 03
Every schema change is scheduled for the night because a migration once locked a table in production.
- 04
Nobody has tested restoring the backups, and you are not sure how much data you would lose in an incident.
What you get
Data modeling and schema design for PostgreSQL and MySQL
Query and index review based on real query plans
Versioned migrations planned to run without downtime
Replication, backups and restore tests
How we work on it
Step 1
Measure first
We enable slow query logging and statistics such as pg_stat_statements. We collect the queries that cost the most time in total, not only the slowest single runs.
Step 2
Read the plans
For each expensive query we read the EXPLAIN ANALYZE output and find the cause. The cause decides the fix, which is not always a new index.
Step 3
Fix and verify
Each fix is applied in the test environment first and measured again with the same queries. Only changes that show an improvement go to production.
Step 4
Plan migrations
For each schema change we write the expand and contract steps and the backfill, and decide what can be rolled back. The sequence is rehearsed on a database of realistic size.
Step 5
Secure the data
A recent backup is restored into a clean environment and the application is started against it. A backup counts only once it has been restored.
Zero-downtime database migrations
A migration that changes a column type, adds a NOT NULL constraint to a populated table or builds an index the default way can lock a large table long enough to take the application down. Safe migrations follow the expand and contract pattern: add the new structure alongside the old, deploy code that writes to both, backfill existing rows in batches, switch the reads, and remove the old structure in a later release.
In PostgreSQL that also means creating indexes concurrently, adding constraints as NOT VALID and validating them separately, and setting a lock timeout so a migration gives up instead of queueing behind a long transaction. Each migration also runs in CI against a real database container.
Indexes, query plans and read replicas when measured
An index is a trade: faster reads for slower writes and more storage. We add indexes for the queries that actually run, matched to their filters and sort order, and remove the ones that are never used. Composite and partial indexes often help more than a pile of single-column ones.
Read replicas spread read traffic but bring replication lag: a user saves a form, the next page reads from a replica and shows the old value. We introduce them when monitoring shows the primary is limited by read load that indexes and caching cannot reduce, and we route reads that must see fresh data to the primary. The decision, the measurement behind it and the routing rules go into an ADR.
Data modeling for multi-tenant SaaS products
A SaaS product has to keep each customer's data apart, and the model decides how hard that is. A tenant_id column on every table is the simplest to run and to migrate, and it relies on every query filtering correctly. A schema per tenant gives stronger separation and per-customer backups, at the cost of running every migration once per tenant. A database per tenant suits customers with contractual or regulatory requirements to keep their data physically separate.
Whatever the model, isolation should be enforced in one place. In PostgreSQL, row-level security can make the database refuse another tenant's rows even when a query forgets its filter. Access is recorded in an audit log, and tests check that a user of one tenant cannot read another tenant's records. Choosing the model early matters, because moving from shared tables to separate databases later means a data migration for every customer.
Methods and tools
- Monoliths and microservices
- Domain-driven design
- Architecture decision records
- C4 model
- pnpm workspaces
- Turborepo
- Nx
- PostgreSQL
- Kafka
How you can work with us
Fixed-scope project
The scope of the first version is written down, split into milestones and priced per milestone. It suits a product or a feature with a clear outline.
Dedicated team
A team works on your product over the long run, with its own planning and release cadence. It suits a product that keeps changing.
Team extension
One or more of our engineers join your team and work with your tools and routines. It suits a company that knows where it is going but lacks people or a specific skill.
Monthly support
A monthly retainer covers maintenance, updates and small changes, with an agreed response time. It suits a product in production that needs upkeep more than new features.
Questions about Database architecture
PostgreSQL, MySQL or MongoDB: which database should we use?
It depends on the data and on what your team already runs. PostgreSQL suits relational business data, with constraints between tables, transactions and JSON columns for the flexible parts. MySQL is a sound choice when your team or your hosting already relies on it, for example in a Laravel project. A document database fits when the data really is a set of independent documents with few relationships, which is rarer than it seems.
Can you speed up our database without changing the application?
Often, partly. Indexes, configuration, connection pooling and statistics can be changed without touching application code. The larger gains usually come from changing how the application queries, such as fixing an ORM that sends one query per row.
What drives the cost of a database review?
The number of databases and services involved, the size of the data, whether we have access to production metrics, and whether you want us to apply the fixes or only recommend them. A single PostgreSQL database behind one application is the simplest case. Data spread across several databases, with migrations still to plan, takes more work.
Can you migrate us from MySQL to PostgreSQL, or between hosting providers?
Yes. We map the schema and the features that differ between the two systems, such as types, collations and the behavior of auto-increment keys, and test the application against the new database in a container. The data is copied and then kept in sync until the switch, so the cut-over is a short, rehearsed step rather than a long outage.
Do we need a separate database for analytics and reports?
Not at first. Heavy reports can run on a read replica or on materialized views refreshed on a schedule, away from the main write path. A separate analytical store becomes worth it when reports scan large volumes, join data from several systems, or slow the production database despite those measures.
How do you handle Hebrew text in the database?
Text is stored as UTF-8, with collations set so Hebrew sorts and compares correctly. Search needs more than a LIKE query, because prefixes such as ה, ו and ב attach directly to Hebrew words. We use Meilisearch or PostgreSQL trigram indexes depending on the volume, and test them with real Hebrew queries, including mixed Hebrew and English product names.
In the same service area
Design, build and ship your project
Describe it in three lines. We reply with specific questions and a first take on how to proceed.