Abstractions in system design
easy 22 minAn abstraction hides how something works behind a small, stable interface, so callers only need to know what it does.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When to bring it up
| You hear | Say | Because |
|---|---|---|
| We may change the database later | Put storage behind a repository | Callers stay unchanged |
| Several payment providers | One interface with an adapter for each | Rules stay in one place |
| Two teams build in parallel | Agree the contract first | Each side can fake the other |
| It should be easy to test | Inject dependencies | Tests use fakes |
| It looks like a local call | Name the leak: time and failure | The network is still there |
Options
Compare your moves
The product may move from one database to another next year. How do you prepare?
BreaksCall the database everywhere
- How
- Each endpoint builds its own queries.
- Limit
- The move touches every endpoint, and no one knows the full list of queries.
- Choose when
- A throwaway script.
WorksA generic layer over every database
- How
- Support only the operations all databases share.
- Limit
- Loses transactions and indexes you need, so people go around it.
- Choose when
- Very simple key and value needs.
StrongRepositories in business terms
- How
- Small interfaces such as find order and save order, one implementation, contract tests and a list of the guarantees used.
- Limit
- Needs discipline, and data still has to be migrated.
- Choose when
- The decision is likely to change and several callers need the data.