Architectural Patterns
Architectural Patterns: Layered, MVC and Pipe-and-Filter
When you design a new system, you rarely invent its overall shape from scratch. An architectural pattern is a named, proven arrangement of components and connectors for a recurring structural problem, together with the constraints that make the arrangement work. Three patterns appear in almost every software design course and in most production code: layered architecture, model–view–controller (MVC) and pipe-and-filter. For each one, notice which dependencies are allowed. Those constraints are what deliver the pattern's benefits.
Layered architecture
A layered system groups responsibilities into tiers ordered from the most user-facing to the most infrastructural. A typical business application has four:
- Presentation: screens or an API endpoint that accepts input.
- Application: use cases that coordinate work and transactions.
- Domain: business rules and entities.
- Data access: code that reads and writes the database.
Under strict layering, each layer may call only the layer directly beneath it and never a layer above. The payoff is replaceability: you can move from one database to another by rewriting only the data-access layer, because nothing above it depends on SQL. The cost is indirection, since every request crosses every layer and the per-layer overheads add up. Relaxed layering lets a layer call any layer below it, trading some isolation for speed and less pass-through code.
Model–view–controller
MVC splits an interactive application into three roles. The model holds the data and the rules that change it. The view renders the model's state. The controller interprets user input, such as a click or a form submission, and turns it into operations on the model. The model never depends on a particular view; views learn of changes by acting as an observer of the model or by being refreshed by the controller.
Because the model knows nothing about displays, you can attach several views to one model.
Example: a fitness app shows the same workout history as a list, a weekly chart and a calendar. Recording a new run updates the model once, and all three views redraw from it. Model logic can also be unit tested with no user interface at all.
Pipe-and-filter
A pipe-and-filter system breaks processing into filters, independent components that each read a stream of data, transform it and write the result. Pipes connect the filters and carry data one way. Filters share no state and do not know their neighbours, so you can reorder, reuse or replace them. Compilers (lexing, then parsing, then code generation) and chains of Unix commands follow this pattern.
When the filters run concurrently on a stream of items, each item still visits every filter, but different filters work on different items at the same time. If filter i runs as one instance and needs time ti per item, and pipe transfer time is negligible:
The slowest filter, the bottleneck, sets the rate of the whole chain. Running n identical instances of a filter raises its rate to n/ti when items are independent, but replication does not shorten any one item's latency.
Example: filters taking 4 ms, 10 ms and 6 ms per item give a latency of 20 ms per item, while throughput is set by the 10 ms filter: 1/(0.010 s)=100 items per second.
Pattern | Allowed dependencies | Main benefit | Typical cost |
|---|---|---|---|
Layered | Each layer calls downward only | Replace one layer without touching others | Every request crosses every layer |
MVC | View and controller depend on the model; the model depends on neither | Several synchronized views of one model | Extra classes and wiring for simple screens |
Pipe-and-filter | Filters connect only through pipes | Reorder, reuse and parallelize stages | Poor fit for interactive, stateful work |
What to remember
- A pattern is defined by its constraints on dependencies, not by its box diagram.
- Strict layering allows calls only to the layer directly below; upward calls break it.
- In MVC the model is independent of every view, so one model can feed many views.
- A pipeline runs at the rate of its slowest filter, and an item's latency is the sum of the filter times.
- Replicating a filter raises throughput but leaves latency unchanged.
Example: Finding the bottleneck in an image-processing pipeline
A photo-sharing service processes every uploaded image through a pipe-and-filter chain. Each filter runs as a separate process on its own core and handles one image at a time. The measured service times per image are:
Filter | Time per image (ms) |
|---|---|
Decode | 8 |
Resize | 20 |
Watermark | 5 |
Compress | 12 |
Assume pipe transfer time is negligible, images are independent of one another, and a replicated filter splits its images evenly between identical instances.
a) With one instance of each filter, find the maximum throughput in images per second and the latency of a single image.
Convert each service time to a maximum rate with ri=1/ti. Decode manages 1/(0.008 s), which is ________; Resize manages 1/(0.020 s)=50 images/s; Watermark manages 1/(0.005 s)=200 images/s; and Compress manages 1/(0.012 s), which is ________.
The filters work on different images at the same time, so the chain runs at the rate of its slowest filter. Resize is the bottleneck:
A single image still visits every filter in turn, so its latency is the sum of the service times:
The chain sustains 50 images per second, and each image takes 45 ms from Decode to the end of Compress.
b) At peak, uploads arrive at 120 images per second. The team first runs three Resize instances and leaves the other filters alone. Find the new throughput and the filter that now limits it.
Three identical Resize instances share the images evenly, so Resize now manages 3/(0.020 s), which is ________. The other rates are unchanged: Decode 125, Watermark 200 and Compress 83.3 images/s.
Compress is now the bottleneck, and the chain sustains 83.3 images per second, still short of the 120 images per second target. Removing one bottleneck exposes the next slowest filter.
c) Find the smallest number of instances of each filter that sustains 120 images per second. Then give the resulting throughput and the latency of one image.
A filter with ni instances manages ni/ti, so it keeps up when ni≥120 s−1×ti. Round each requirement up to a whole instance:
- Decode: 120×0.008=0.96, so 1 instance.
- Resize: 120×0.020 = ________, so 3 instances.
- Watermark: 120×0.005=0.6, so 1 instance.
- Compress: 120×0.012 = ________, so 2 instances, which together manage ________.
With 1, 3, 1 and 2 instances the rates are 125, 150, 200 and 166.7 images/s:
Decode is now the bottleneck, but its 125 images/s exceeds the 120 images/s target. Each image still passes through exactly one instance of each filter, so its latency stays 8+20+5+12=45 ms.
The minimum configuration is 1 Decode, 3 Resize, 1 Watermark and 2 Compress instances, giving 125 images per second with a latency of 45 ms per image.
An online shop uses a strictly layered architecture with presentation, application, domain and data-access layers above a relational database. A customer submits a form to change their delivery address. Arrange the steps in the order the request is handled until the new address is stored.
Loading ordering steps…
A team is building a desktop budgeting application. The same budget data must appear at once as an editable grid, a pie chart and a monthly summary, and an edit made in any one of these displays must appear immediately in the other two. Which architectural pattern best fits the structure of the user interface?