Slice Boundaries

Slice Boundaries

Two slices coexisting in the same codebase need rules for what they can share and how they communicate. Get this wrong and VSA collapses back into layered architecture — just nested one folder deeper.

What can a slice depend on?

Target Allowed?
Another slice's domain types No — tight coupling
Another slice's domain events Yes — events are the contract
Another slice's use cases No — use cases are slice-internal
Another slice's HTTP API No — internal calls bypass the API
Shared kernel (shared/) Yes, sparingly
True infrastructure (logging, DB, HTTP) Yes

Cross-slice communication

Three legal channels:

  1. Domain events. Slice A publishes; Slice B subscribes. Slice B reacts after Slice A's transaction commits.
  2. IDs. Slice A holds Slice B's entity ID (a UUID, an int). When it needs data, it goes through Slice B's public interface (use case or — for read models — a query service).
  3. Shared kernel. Truly shared concepts (Money, UserId, EventId) live in shared/. Frozen as much as possible — every team depends on it.

What goes in the shared kernel

Almost nothing. Specifically:

What does not belong:

Detecting a leak

Symptoms that slice boundaries have rotted:

Anti-patterns

How to test boundary discipline

Add a static import check (via import-linter or similar). Encode the rule: "Slice A must not import from slice_b.domain, only from slice_b.events or shared/." CI fails when a developer accidentally couples slices.

The hardest rule

Two slices may share a database, but they should never share a write transaction. Cross-slice writes are events, not joins. Cross-slice reads are explicit queries with their own DTOs, not JOIN aliases.