AlgoViz

Abstractions in system design

easy 22 min

An 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 hearSayBecause
We may change the database laterPut storage behind a repositoryCallers stay unchanged
Several payment providersOne interface with an adapter for eachRules stay in one place
Two teams build in parallelAgree the contract firstEach side can fake the other
It should be easy to testInject dependenciesTests use fakes
It looks like a local callName the leak: time and failureThe 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.