The Pattern Atlas: Design Patterns at the Code, Application and System Level
One AI support feature seen at three zoom levels: the system page (queue, retry, cache-aside, cascade), the application page (ports, aggregate, guardrails) and the code page (strategy, adapter, repository, DI). 27 patterns on one map, and the bill each one sends.
What you will learn
- Describe any pattern by its name, problem, shape and cost
- Place a pattern on the code, application or system page by asking what it arranges
- Map 27 patterns of an AI support app onto one atlas
- Decide when a pattern earns its place and explain it in an interview
Before you start
- Basic Python classes and functions
- What an API and a queue are
Picture a plain support app with an AI assistant. A customer writes a ticket, and the assistant drafts a reply for the support agent. Three engineers describe this one feature three ways:
- "The request goes on a queue and a worker calls the model."
- "The service checks the ticket and then calls the model through a port."
- "Each model provider gets its own small adapter."
All three are right. They're looking at different zoom levels, and each level has its own named shapes. This lesson builds one map of them, a pattern atlas.
What a pattern is
A pattern has four parts. Take Adapter:
| Part | Adapter |
|---|---|
| Name | Adapter |
| Problem that keeps coming back | Three model providers, each with a different request format |
| Shape of the answer | One interface for the app, one small wrapper per provider |
| Cost | One more layer to keep up to date, and only the features every provider shares |
People forget the last row. If you can't say the cost, you don't really know the pattern yet.
Why bother with the name? Without it you spend a long sentence in a design review describing the wrapper idea. With it you say one word, "adapter", and everyone sees the same shape. It works like the roundabout sign on a road map: one small symbol, and every driver knows the junction and the rule. A pattern name is shared vocabulary that keeps design talk short and exact.
Three pages of the atlas
Like a real atlas, patterns sit on different pages, and one question finds the page: what does this pattern arrange?
The system page
Open the system page for our feature. The ticket arrives at the API, and the API doesn't wait for the model. It puts a job on a message queue and answers right away. A worker picks up the job. If a model call times out, it waits a bit longer each time and tries again: retry with backoff. Before calling anything, the worker checks the cache. Same question asked before? The saved draft comes back: cache-aside. On a miss, a router sends the ticket to a cheap model first and to a strong model only when it has to: a model cascade. Every label here joins processes over a network.
The application page
Zoom into the API box. On the outside sits the HTTP edge, inside it a service, and in the middle the domain, the business rules. That's a layered or hexagonal picture. The domain talks to the outside world only through ports, one for the database and one for the model. The ticket and its messages change together as one unit, an aggregate. And around the model call, input is checked on the way in and the draft is checked on the way out: a guardrail sandwich. All of this lives inside one app.
The code page
Now zoom into the model port. A small registry maps a provider name to the object that serves it; ask for "cheap" and you get the cheap provider. That's a factory, and each provider is a strategy. Each provider object is an adapter around that vendor's format. A thin wrapper times every call without touching the call: a decorator. On the database side, a repository hides how tickets are saved. And the service never builds any of this itself; it's handed in from outside, which is dependency injection.
The one-page atlas
| Page | Patterns | Count |
|---|---|---|
| Code | strategy, factory, adapter, facade, decorator, proxy, repository, unit of work, dependency injection | 9 |
| Application | layered, hexagonal, clean, modular monolith, entity and value object, aggregate, CQRS, guardrail sandwich, evaluator-optimizer, agent harness | 10 |
| System | microservices, message queue, retry with backoff, durable workflow, cache-aside, router, fallback chain, model cascade | 8 |
That's 9 + 10 + 8 = 27 patterns. Under the map sit two ways to write decisions down, ADRs and C4 diagrams. They aren't patterns; they record the ones you chose (see ADRs and C4 diagrams).
Same idea, three zooms, three bills
Some ideas show up on every page. Take "one door, many things behind it":
| Page | Shape | What you swap | Cost |
|---|---|---|---|
| Code | Strategy | One function for another | A function call |
| Application | Port | The database or the model provider | An interface and a test double |
| System | Router | Whole model services | A network hop that can be slow, or fail |
Same idea, but the higher the zoom, the bigger the bill.
Every pattern sends a bill
Put example numbers on the model cascade. Without it, all 100 tickets go to the strong model: 100 strong calls. With it, all 100 go to the cheap model first, 30 come back unsure, and only those 30 go to the strong model. Strong calls drop from 100 to 30.
But total calls go up to 100 + 30 = 130, and those 30 hard tickets wait for two calls instead of one. There's also a threshold to tune and two models to watch. That bill is worth paying only when the strong model is the expensive part. The full numbers are in LLM model routing.
When a pattern earns its place
Say the app talks to one model provider and someone suggests an adapter per provider. Run three checks:
- Is the problem here now? Not really. One provider, so the extra layer buys nothing yet.
- Is the cost smaller than the problem it solves?
- Does the team know the name, so the shape stays readable?
A pattern earns its place when its problem shows up, not before.
Saying it in an interview
Use five slots: name, level, problem, cost, and why it's fine here.
I'd use a model cascade, at the system level. Most tickets are simple, and the strong model is slow on all of them. The cost is that hard tickets make two calls and the threshold needs tuning. That's fine here because 70 of 100 tickets never reach the strong model.
The last slot is what tells the interviewer you've weighed it.
Recap
- A pattern is a name, a problem, a shape and a cost.
- Find its page by asking what it arranges: functions and classes, modules inside one app, or processes over a network.
- The same idea can appear on every page, and the bill grows as you zoom out.
- Add a pattern when its problem shows up.
Try it yourself
Pick a feature in a project you know. Name one pattern on each of the three pages, and for each one write the cost in a single line.
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
When the main model goes down, the backup has to be allowed to take the data, able to read the whole request and tested before the bad day. A simulated four-model chain for an EU customer: a 6,000-token request lands on mid-eu, flagged degraded; a 10,000-token request is served by nobody, on purpose.
Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
A bulk upload buries the chat. In a small simulation, one shared queue grows to 199 waiting and the middle chat answer takes 80.2 s. A bound plus a chat lane brings it to 0.35 s while 178 bulk jobs are told to retry in 10 s, and cancelling abandoned calls saves 21,000 wasted tokens.
Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
Six GPU replicas, two at 95% and four at 30%: round robin counts requests, and requests aren't equal. In a small simulation, least outstanding requests cuts p95 from 6.52 s to 3.05 s, prefix affinity lifts cache hits to 95% but pushes p95 to 11.45 s, a real health check takes errors from 48 to 2, and draining cuts 0 answers instead of 4.
A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
A queue doesn't lose a document, but it can hand one out twice. In a small simulation, a worker crash makes document two go from 3 to 6 chunks and the index holds 11 when the documents hold 8. An idempotent consumer brings it back to 8 with the same two deliveries, and a limit of three tries moves a poison file to a dead letter queue.
