Blob: references/abstraction-and-dependencies.md
Abstraction and dependencies
Compare the responsibilities you will maintain, the capability you obtain, and the behavior that must survive. Line count helps estimate one cost; it cannot tell whether a dependency removes a protocol implementation or merely wraps the same coordination problem.
During a refactor of vaskel, a browser chat and calling application, the question included whether sections of its workspace really needed to load together. Removing that coupling could simplify the behavior even if representing independent loading states took more code. Rachel's shorthand was:
again, line count doesn't tell the whole story
Reuse machinery at a useful seam
Specter connects nodes through an overlay network and uses Chord, a distributed hash table, to locate the node responsible for a key. Its nodes need remote calls for membership and storage operations. The original RPC implementation owned request/reply envelopes, response dispatch, error conversion, and per-call stream handling. Replacing it with Twirp, which generates service clients and handlers from protocol definitions, moved those responsibilities into an established HTTP-based RPC system. Generated source grew while the custom protocol surface shrank; adding an operation could build on code generation and ordinary HTTP machinery.
The network still needed an adapter: these HTTP requests travel over streams opened by Specter's overlay transport. The generated clients start with a placeholder URL, http://chord, while each call selects a peer in its context. A request hook makes that peer visible to HTTP connection pooling:
r.URL.Host = peer.GetAddress() // needed to force dial instead of using http://chord as keyThe custom dialer opens a stream to the selected peer when HTTP needs a connection. Without the host change, the pool can reuse a connection associated with the placeholder instead of dialing the newly selected destination. Implementing a dialer alone is insufficient because a pool hit bypasses it. The useful seam was standard RPC above custom transport, with destination identity preserved across both. Generator versions and local marshaling adaptations still needed maintenance.
The PowerDNS backend used a similar division for DNS. PowerDNS answers authoritative queries and handles capabilities such as live DNSSEC signing, wildcard records, and record-type processing. A custom HTTP backend translates its requests into Chord-backed storage, where a zone's records are stored together. This bought a substantial DNS implementation while retaining control over storage. The backend also reused the actual chord.KV interface and its memory implementation, keeping integration tests tied to the storage contract the application would use.
The transaction adapter shows how much can remain at that seam. The management tool pdnsutil replaces a record set, looks it up to display the new value, and only then commits. Its lookup carries no transaction ID. Reading only committed storage made that display appear empty; the backend could not use the request to identify a special read belonging to the writer. The adopted adapter therefore lets zone reads prefer the active transaction's in-memory working copy.
That choice affects ordinary DNS lookups handled by the backend as well as management reads: both can observe uncommitted changes. Commit persists the working copy and invalidates the cached zone; abort discards it. A per-zone lease limits writers, but it does not distinguish readers or make staged answers committed. If readers must see only committed records, that requirement changes the integration problem even with only one writer. Delegating DNS machinery did not delegate this visibility decision.
Count what remains after consolidation
Dermail's email system used background workers to process queued jobs. It replaced Bull's Redis-backed queue with a queue library using the RethinkDB deployment it already operated, explicitly seeking fewer infrastructure components. The migration still needed a queue-master process, scheduling intervals, and a policy for retaining completed jobs. A later worker change compared processing and retry counts in an attempt to suppress duplicate work.
Retiring Redis removed a service to operate; it did not remove the distinction between scheduling a job, retrying it, and recognizing work already processed. That can still be a worthwhile trade when operating the extra service is costly. If the intended simplification is to stop owning delivery and replay behavior, moving queue records into an existing database does not accomplish it by itself.
Let real substitutes justify an interface
The service b hosts files, text pastes, and shortened links. It needs small stored values such as metadata as well as potentially large file bodies, and supports Redis, SQLite, file, S3, and encrypted-storage implementations or wrappers. These were real substitutes used by the application. Shared tests for identifier conflicts, retrieval, expiration, and deletion gave the interfaces a behavioral purpose beyond matching method names.
A redundant Save operation was removed because SaveTTL with a zero lifetime already represented nonexpiring storage. The byte-oriented and stream-oriented interfaces remained separate: retrieving a small metadata value as []byte is useful, while forcing a file upload or download through that shape would require materializing the whole body. Streams also carry closure obligations that byte slices do not. Deletion was composed as a separate capability for consumers that needed it.
The simplification removed a duplicate expression of the same behavior while preserving differences that affected consumers. A single implementation with no meaningful substitute would offer less justification for these interfaces; a workload requiring incremental I/O would offer strong justification for keeping the streaming distinction.
More structure can buy the intended capability
bland is a collaborative document application. Its editor migration deliberately accepted churn to obtain maintained, permissively licensed building blocks, collaborative editing support, and room for block-level interactions. Tiptap supplied the editor and extension mechanisms, including reusable collaboration and task-list support. Local controllers and extensions owned block dragging, image behavior, slash commands, and the way those capabilities appeared in the product.
That boundary increased local UI responsibility but gave the application control over the intended editing experience. It was also a development-stage decision: existing data and schema compatibility could be discarded. Once users rely on persisted documents, migration and rendering compatibility become part of the cost of making the same replacement.
Composer mixes live audio/video feeds into an outgoing program. Its browser studio lets an operator arrange and preview scenes before switching them live, with each production keeping its own sources, scenes, and outputs. The API-first design makes that studio one client of the media system. Provider integrations own the details of opening and closing media with services such as Cloudflare Realtime and LiveKit; the engine owns composition. A saved connection holds provider settings and credentials reusable across productions. A source selects the particular live feed to use within a production.
Those objects have different lifetimes. For Realtime, an app ID and secret select and authorize access to a provider application, while a publisher's session ID and chosen audio/video track names identify a current feed. LiveKit selections use a room and participant. Separating connection from selection allows several sources using the same protocol, each keyed by its own source ID, instead of one fixed input slot per provider. Discovery then becomes useful without being universal: Realtime can list tracks for a supplied session, while LiveKit can browse rooms and participants. The extra structure represents actual variation in identity, lifetime, and provider capability.
Use existing development mechanisms too
The same boundary appears in development tooling. bland's build configuration needed to read an AI-mode setting from environment files. Node's parseEnv could parse the file syntax, while the application retained the choice of which environment-specific or fallback file took precedence. Reusing the parser removed syntax-handling responsibility without asking the standard library to decide application policy.
For Cloudflare types, bland already generated declarations from its Worker configuration, including the platform bindings available to the application. Adding a separately maintained direct Workers Types dependency would introduce another owner for that contract. A package could still need those types transitively; the application's source of truth and the dependency graph are different questions.
Repeated Tiptap peer-version conflicts prompted a proposal for a new updater script. After Rachel questioned the need for it, the proposed remedy became coordinating the related package versions through the existing npm update mechanism. That was considered sufficient for the task, and no extra updater script was created.
These cases turn on what the existing mechanism actually owns. A parser can own syntax without owning precedence; generated platform types can own the application contract without eliminating transitive packages. An adapter earns its place when there is a real semantic difference left to translate. A replacement earns its cost when it removes a responsibility or supplies a capability the present boundary cannot support.
See representation and API craft for interface shape and change and validation for the consequences of replacing a foundation.