view as markdownreport a mistake

Sandwich

type: source-page
updated: 2026-07-02
status: imported
namespace: software-architecture-metapatterns
Imported source page from Denys Poltorak's Architectural Metapatterns wiki. Source path: Extension metapatterns/Sandwich.md.

A diagram for Sandwich Architecture, in abstractness-subdomain-sharding coordinates.

Follow the line of least resistance. Divide where it is loosely coupled.

<ins>Structure:</ins> A layer of domain-level services between shared integration and data layers.

<ins>Type:</ins> System topology, implementation.

BenefitsDrawbacks
Supports medium to large codebasesAggressive optimization is impossible
Multiple specialized development teamsLow fault tolerance
There are many ways to evolve the system

<ins>References:</ins> None I know of.

A Sandwich is a (sub)system midway between Layers and Services. It emerges when a layered component outgrows its architecture and its middle (domain) layer – which tends to be both the largest and least cohesive – is divided into subdomains while the application and data layers remain intact. Another, less common, origin is a (sub)system of (Micro)Services which becomes so tightly coupled that it needs to be partially merged.

A Sandwich, named after the shape of its diagram, includes the following components:

Sandwiches often occur naturally without being recognized as a distinct architecture. In fact, I had to make up a name for this system topology.

Performance

As domain-level services rarely interact among themselves, the performance of a Sandwich is similar to that of Layers with the same kind of deployment (components of a Sandwich may or may not be colocated).

Control and data flow is identical in Sandwich and Layers.

Dependencies

The Sandwich’s integration layer depends on every service inside the Sandwich. The services themselves depend on the data layer.

The integration layer depends on every service. Every service depends on the data layer.

Having two shared layers provides three options for invoking the domain components:

Applicability

Sandwich <ins>fits</ins>:

Sandwich <ins>does not help</ins>:

Relations

Transitions between Layers, a Sandwich, a Service-Based Architecture, and Layered Services.

Sandwich:

A Cell encapsulates several services so that they can be treated as a single entity with the goal of reducing the number of top-level system components. Typically, interdependent and closely communicating services are clustered into a single Cell, predisposing the Cell’s internals for further merging to reduce the communication overhead and the amount of boilerplate code. Such a scenario often results in a Sandwich trapped inside a Cell.

Examples

Though most real-world Sandwiches stay beneath the radar as non-standard architectures, there are two well-documented and highly specialized occurrences of this topology in data-centric domains and a few less stringent generic cases:

Blackboard System

A Blackboard System includes a control which orchestrates knowledge sources which access a blackboard with shared data.

Some domains are complex and ill-structured: there is only a vague understanding of how the inputs relate to outputs, therefore you cannot write a single algorithm to solve it. If you are lucky to have a huge labeled dataset, you can attempt training a neural network. If there are not so many examples, you are in trouble.

Blackboard Systems \[POSA1, POSA4\] were invented half a century ago to tackle non-deterministic problems: speech or image recognition, X-ray crystallography of proteins, or even tracking enemy submarines or stealth aircraft. In these cases inputs are noisy and scarce and outputs can vary indefinitely, therefore one must go through trial and error proposing, refining, and comparing many possible solutions to arrive at something plausible.

The system consists of:

This architecture makes best use of every system layer:

Space-Based Architecture

Space-Based Architecture comprises the following layers: a messaging grid, a processing grid, scaled processing units, a data grid, a deployment manager, and a persistent database.

Space-Based Architecture \[SAP, FSA\] is an extremely elastic alternative to Microservices which works well for data-centric domains. Each processing unit – a domain-level service – is co-located with an in-memory replica of the entire system’s data, which makes both data access and creation of new instances of processing units blazingly fast. As is common for Sandwiches, processing units can be orchestrated by the processing grid or they can subscribe to changes in the shared data grid, this architecture being flexible enough to allow for choosing a communication paradigm on per request basis.

Aside from processing units, which contain the main business logic, Space-Based Architecture involves:

As Space-Based Architecture runs every component in a Mesh, it avoids the fault tolerance and database performance drawbacks inherent to Sandwich, trading them for possibility of write conflicts when multiple clients cause changes to the same piece of data simultaneously.

Service-Based Architecture

A Sandwich-like topology with user interface, a layer of domain services, and a shared database.

Service-Based Architecture \[FSA but not DEDS\] is the most pragmatic and loosely defined of topologies based on Services (hence the name). In a basic Service-Based Architecture the subdomain services are integrated by a User Interface layer, usually a Frontend, and there is a single Shared Database. However, as there are no written rules for Service-Based Architecture, multiple databases or finer-grained GUIs may be used for programmers’ convenience, disintegrating the Sandwich topology for the sake of less coupled Layered Services.

A Sandwich-like topology with shared user interface and database is gradually transformed into layered services.

Nanoservices

Nanoservices form a Sandwich-shaped architecture. The upper layer is an API Gateway for an orchestrated system or a gateway for pipelined Nanoservices. The lower layer is a shared datastore.

Nanoservices is another loosely defined architecture built of single-purpose functions (FaaS) individually deployed to the cloud. It is highly elastic and relies on a cloud provider for operational support, saving costs for small businesses.

As a single nanoservice is too small to implement a use case, there must be an integration layer that receives client or user requests, runs several nasoservices to execute it, and returns a response. The integration layer may be a Frontend, API Gateway, an Event Mediator for orchestrated Nanoservices, or an ordinary Gateway that initiates a pipelined scenario and receives its response from a choreographed system.

As each nanoservice is stateless, for the system to be stateful there must be a data store. And as each nanoservice can do only one thing (either read, write, or search), the data store must be shared to be of any use.

Please note how a fine-grained decomposition of business logic necessarily results in a Sandwich architecture.

Command Query Responsibility Segregation (CQRS)

A large read and smaller write models between a user interface and database.

Command Query Responsibility Segregation (CQRS) is a principle which calls for separation of components that process commands (requests that change the system’s data) and queries (requests that analyze the data). The subdivision necessarily happens at the domain (model) level, resulting in separate Write Model (Command Model) and Read Model (Query Model, Thin Read Layer). In the simplest case the data layer is kept monolithic, as shown in the diagram above.

CQRS helps decouple the logic-heavy object-oriented code which maintains data consistency and business constraints during editing data records from the search and aggregation code that needs direct database access to run complex SQL queries. If that is not enough, it is possible to trade complexity for performance by employing specialized databases: OLTP for use with the Write Model and OLAP for the Read Model, as described in the corresponding section of the Layered Services chapter:

The single database of a Sandwich-like CQRS with a shared database is subdivided into OLTP and OLAP databases, forming Layered Services.

(inexact) Replicated Load-Balanced Services, Lambdas

A load balancer connects a new client to a free instance of a stateless backend that accesses a database shared among all the backend instances.

Replicating a system’s domain tier along the sharding axis also results in a Sandwich-like topology, albeit with altered properties: while the original Sandwich’s subdivision of the codebase along the subdomain axis allows for multiteam development and easy integration of new functionality, replication along the sharding axis only makes it simple to add or remove instances of the middle tier as the system load changes.

Replicated Load-Balanced Services \[DDS\] is the general name for running multiple instances of a stateless service that share a database and run under a Load Balancer. Lambdas is a synonym for a similar setup from cloud computing providers (see also Nanoservices).

Evolutions

The components of a Sandwich provide plenty of ways to alter the system, with unique evolutions detailed in Appendix E:

Primary evolutions

Some evolutions involve the system’s domain logic or its topology:

One of the domain-level services is removed and another one is added.

One domain-level service is split in half while two other services are merged together.

The entire domain layer is merged, resulting in Layers.

The integration and data layers are divided into subdomains, producing Three-Layered Services.

Evolutions of the data layer

If the data layer becomes a performance bottleneck, it can be split as a Shared Repository:

Evolutions of the application layer

If the integration layer contains use cases and becomes cumbersome, it should be subdivided following the evolutions of Orchestrator:

Summary

Sandwich is a pragmatic architecture midway between Layers and Services. It combines simplicity and flexibility, avoiding unnecessary effort for the present while retaining many paths to evolve in the future.

External links