Abstractions in System Design 00:00:01 One call crosses a boundary A caller asks for user one hundred twenty-three. Should it also need to know whether that user's record lives in a cache, a relational table or a different storage service tomorrow? get_user("123"). The client knows one method and nothing about storage. The service checks what the business cares about, such as whether the request is valid. The service calls the UserRepository interface. It does not know that this one has a cache. Nothing is stored yet. The wrapped repository runs a SELECT by id. The user goes into the cache for 300 seconds. The answer travels back up. The client gets a User and never learns how. Define what a user lookup returns, what a missing user means and how failures are signalled. An interface with a short name can still have a complicated consistency promise. 00:00:54 The caller repeats the lookup The same call again. The interface is the same, the work underneath is not. The client repeats get_user("123"). The cache has the user, so the database is not asked. The result is the same as before, and it came back sooner. The second call may take a different internal route yet produce the same public shape. A faster result is a benefit; silently weakening a promised freshness guarantee is a contract change. 00:01:23 Replace storage behind the interface Change which implementation is built at start-up, then look at what really changed. use_redis is off. The cache is a dictionary inside the program. use_redis is on. Only the wiring differs: the service and the repository are the same code. Redis stops answering. A dictionary could never do that, so a new kind of failure reached the repository. The contract says a cache call may fail. The repository treats the failure as a miss and asks the database. Keeping a method signature stable is insufficient if a replacement changes accepted writes, ordering or failure behavior. Verify callers' real expectations against both implementations. 00:02:07 Write and invalidate A write must not leave an old copy behind the interface. The service saves user 123 with a new email. The new row is written. The cache still holds the old copy for a moment. The repository removes the cached user. The next read misses and fetches the new row. Updating the authority and refreshing a cached copy are separate events. The service boundary can hide their mechanics but must still tell consumers what they may observe between them. 00:02:35 Choose an interface callers can implement Go from a god interface to small ones that say what callers need. One big interface has everything. Two callers use very little of it. Run a database query, get a cursor, and flush the connection pool all expose how the data is stored. Those operations leave the application-facing interface. A reader and a writer. Each caller depends only on what it uses. Prefer an operation tied to a business question over exposing the storage engine's private mechanics. A later backend should not have to mimic an unrelated database quirk simply to satisfy clients. 00:03:10 Test against a small substitute The same service runs against a fake repository. The service uses the real repository and PostgreSQL. The test hands the service a fake that keeps users in a dictionary. No database is started. The same contract tests run against the fake and the real one, so they cannot drift apart. A fake can exercise caller behavior quickly. It cannot prove that a real store handles concurrent writes, recovery and latency as promised; include contract and integration checks where those guarantees matter. A good abstraction hides details callers need not depend on and exposes the behavior they must depend on, including what happens to a stale read, a failed write or a missing user.