Design patterns
Proven answers to problems that keep coming back, from classic object-oriented design to cloud systems and AI products. One page per pattern, with diagrams.
A pattern is a named, reusable solution to a design problem. It gives a team a shared vocabulary: "put a circuit breaker in front of payments" says a lot in five words.
Patterns are tools, not goals. Reach for one when you feel the problem it solves, not because it's in the book. Every page here says when not to use the pattern and what to choose instead.
Design patterns (GoF)
The 23 classic object-oriented patterns from the Gang of Four, each with a class diagram, a sequence diagram and a TypeScript example.
Create whole families of related objects through one interface, so code never mixes parts from different families.
Assemble a complex object step by step with readable calls, check the rules once at the end, and get back a finished, valid object.
Let a base class run a workflow but leave one step, creating an object, to subclasses that decide which class it is.
Create new objects by copying a configured example, instead of building each one from scratch.
Make sure a class has exactly one instance and give the whole program one way to reach it. Useful, and the most overused pattern.
Wrap an existing class whose interface doesn't fit, so it works with code that expects a different interface.
Split something that varies in two ways into two separate class hierarchies, joined by a reference, so each side can grow on its own.
Build tree structures where single items and groups share one interface, so code can treat a whole group exactly like one item.
Wrap an object in another object with the same interface to add behaviour, such as caching, logging or retries, without changing the original.
Give a complicated subsystem one simple front door, so most callers can use it without knowing how its parts fit together.
Share the common, unchanging part of many similar objects instead of copying it into each one, to save memory at scale.
Put a stand-in with the same interface in front of an object to control access to it: check permissions, create it lazily, or reach it remotely.
Pass a request along a chain of handlers; each one either handles it or hands it to the next, so the sender doesn't need to know who deals with it.
Turn a request into an object with everything needed to run it, so you can queue it, log it, retry it or undo it.
Represent each rule of a small language as a class, build sentences as trees of those objects, and evaluate the tree to get an answer.
Walk through the items of a collection one at a time without exposing how the collection is stored, whether it's an array, a tree or a paginated API.
Make objects talk through one coordinator instead of directly to each other, so the rules about who affects whom live in one place.
Capture an object's state in a snapshot so you can restore it later, without exposing the object's internals to whoever keeps the snapshots.
Let objects subscribe to another object's changes, so it can notify all of them without knowing who they are.
Let an object change its behaviour when its internal state changes, with one class per state instead of status checks in every method.
Put each variant of an algorithm in its own class behind one interface, so callers can swap behaviour without changing their code.
Write the skeleton of an algorithm once in a base class, and let subclasses fill in only the steps that differ.
Add new operations to a fixed set of classes without changing them, by putting each operation in its own visitor object.
Cloud patterns
Patterns for distributed systems in the cloud: staying up when parts fail, moving messages, scaling data and shaping the edge.
Give each dependency or workload its own limited pool of resources, so one slow part can't use up everything and sink the whole service.
Stop calling a dependency that keeps failing, fail fast with a fallback, and probe now and then to see if it has recovered.
Expose an endpoint that reports whether an instance can do its job, so load balancers, orchestrators and monitors can route around problems.
Automatically try a failed call again after a growing, randomised delay, because many cloud failures are brief and fix themselves.
Cap how many requests each client can make in a time window, and reject the excess politely, to protect your service and keep it fair.
Put a translation layer between a new system and a legacy or external one, so the old system's model and quirks don't leak into the new design.
Let several identical workers read from the same queue, each message going to exactly one of them, so processing scales out simply by adding workers.
Publish an event once to a topic and let any number of independent services subscribe to it, without the publisher knowing who they are.
Put a queue between a spiky source of work and the service that does it, so bursts are absorbed and processed at a steady, safe rate.
Run a business transaction across several services as a series of local steps, each with a compensating action that undoes it if a later step fails.
Save a change and the event announcing it in the same database transaction, then publish the event separately, so you never lose or invent events.
Check a fast cache first, load from the database only on a miss, and store the result in the cache for next time.
Use one model to change data and a separate, read-optimised model to query it, so each side can be designed and scaled for its own job.
Store every change as an immutable event in an append-only log, and derive the current state by replaying them, instead of overwriting rows.
Precompute the data a screen or report needs into a ready-made table, and keep it updated, instead of joining and aggregating on every request.
Split one big data set across several databases by a key, such as customer or region, so data and load can grow beyond what one server can handle.
Put a local helper between an app and the remote services it calls, to handle retries, timeouts, routing and credentials on the app's behalf.
Give clients one entry point that routes requests to the right backend service and handles shared concerns like authentication and rate limits.
Give each kind of client, such as web, mobile or partners, its own thin backend that shapes data exactly for that client.
Let a group of identical instances pick one leader to do work that must happen exactly once, and automatically pick a new one if it fails.
Run supporting features such as proxying, logging or config sync in a separate container next to the app, sharing its lifecycle but not its code.
Serve files that are the same for everyone, such as scripts, styles, images and prerendered pages, from object storage behind a CDN instead of from app servers.
Replace a legacy system piece by piece behind a routing layer, sending more features to the new system over time until the old one can be switched off.
Hand clients a short-lived, narrowly scoped token so they can upload or download files directly from storage, without streaming through your servers.
Architecture & AI patterns
How to structure a whole application, and how to build reliable systems around language models.
Structure a system around events: components announce what happened, and other components react, instead of calling each other directly.
Put the business logic at the centre with no dependencies on frameworks or databases; everything else plugs in through ports and adapters.
Organise an application into horizontal layers (presentation, business logic, data access) where each layer only talks to the one below it.
Build a system as small, independently deployable services, each owning one business capability and its own data, run by one team.
One deployable application split internally into modules by business capability, each with its own code, data and public interface.
Let a language model work in a loop: decide the next action, call a tool, look at the result, and repeat until the goal is reached.
One model call produces a draft, another evaluates it against clear criteria, and the draft is revised until it passes or a limit is reached.
Check what goes into and comes out of a language model with separate, independent rules, so unsafe input and bad output are stopped before they cause harm.
Break a task into fixed steps, each a focused model call whose output feeds the next, with ordinary code checking results in between.
Before asking a language model to answer, look up the most relevant passages from your own documents and give them to the model as context.