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.


Three diagrams side by side: four stacked layers joined by downward arrows, a controller box with arrows to model and view boxes and an arrow from view to model, and three filter boxes joined left to right by arrows.


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:

  1. Presentation: screens or an API endpoint that accepts input.
  2. Application: use cases that coordinate work and transactions.
  3. Domain: business rules and entities.
  4. 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 ii runs as one instance and needs time tit_i per item, and pipe transfer time is negligible:


throughput=min⁡i1ti,latency=∑iti\text{throughput} = \min_i \frac{1}{t_i}, \qquad \text{latency} = \sum_i t_i


The slowest filter, the bottleneck, sets the rate of the whole chain. Running nn identical instances of a filter raises its rate to n/tin/t_i 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)=1001/(0.010\ \text{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/tir_i = 1/t_i. Decode manages 1/(0.008 s)1/(0.008\ \text{s}), which is ________; Resize manages 1/(0.020 s)=501/(0.020\ \text{s}) = 50 images/s; Watermark manages 1/(0.005 s)=2001/(0.005\ \text{s}) = 200 images/s; and Compress manages 1/(0.012 s)1/(0.012\ \text{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:


throughput=min⁡(125, 50, 200, 83.3)=50 images/s\text{throughput} = \min(125,\ 50,\ 200,\ 83.3) = 50\ \text{images/s}


A single image still visits every filter in turn, so its latency is the sum of the service times:


latency=8+20+5+12=45 ms\text{latency} = 8 + 20 + 5 + 12 = 45\ \text{ms}


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)3/(0.020\ \text{s}), which is ________. The other rates are unchanged: Decode 125, Watermark 200 and Compress 83.3 images/s.


throughput=min⁡(125, 150, 200, 83.3)=83.3 images/s\text{throughput} = \min(125,\ 150,\ 200,\ 83.3) = 83.3\ \text{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 nin_i instances manages ni/tin_i / t_i, so it keeps up when ni≥120 s−1×tin_i \ge 120\ \text{s}^{-1} \times t_i. Round each requirement up to a whole instance:

  • Decode: 120×0.008=0.96120 \times 0.008 = 0.96, so 1 instance.
  • Resize: 120×0.020120 \times 0.020 = ________, so 3 instances.
  • Watermark: 120×0.005=0.6120 \times 0.005 = 0.6, so 1 instance.
  • Compress: 120×0.012120 \times 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:


throughput=min⁡(125, 150, 200, 166.7)=125 images/s\text{throughput} = \min(125,\ 150,\ 200,\ 166.7) = 125\ \text{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 ms8 + 20 + 5 + 12 = 45\ \text{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.



Match each element of a common architectural pattern to the description of its responsibility. Each description fits exactly one element.

Match each term
A.

Renders current application state for the user and contains no business rules

B.

Rule that each tier may call only the tier directly beneath it

C.

Interprets user input and turns it into operations on application state

D.

Holds application data and the rules that change it, with no knowledge of any display

E.

Independent stage that reads an input stream, transforms it and writes an output stream

F.

One-way connector that carries data from one processing stage to the next

Pipe

View

Filter

Strict layering

Controller

Model

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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…

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice