HLD and LLD
- Lesson 37 of 40
- 8 min
- Beginner
- 8 steps
Designing a system means looking from two distances: the full map of parts and connections, and the code and tables of each part. Knowing which one you are at saves you from spending time on the wrong one.
- Zoom level
- whole system
- Parts
- services and stores
- Deliverable
- system map
- request
- response
- event
Step 1 of 8
Concept
One system, two distances
Picture a food delivery app. From afar it is three services, Orders, Couriers and Payments, each with its own data, behind an API gateway and telling each other about things with events.
Up close, each of those blocks is a whole program, with classes, tables and rules. The first level is called high-level design (HLD) and the second, low-level design (LLD).
Step 2 of 8
Technique
HLD: the map of the system
HLD answers which parts exist and how they connect: which services there are and which data each one owns, whether they talk through a call or an event, which kind of store holds each piece of data, and what happens when traffic grows or something fails.
It is the level every earlier lesson worked at. Its output is a diagram like this one, along with the reasons behind each decision.
What changed
- The unit of design is the whole system
Step 3 of 8
Trade-off
Every arrow is a decision
Placing an order should not wait for a courier to show up. So Orders publishes an OrderCreated event and Couriers handles it at its own pace, as you saw in Pub/Sub and events.
Payments needs transactions, so it uses a relational database. Locations are another story: in the example, 200,000 active couriers send their position every 5 s, which is 40,000 writes/s, and each one replaces the previous one. A fast key-value store suits that. The figures are example values.
What changed
- Orders → Couriers: an event instead of a call
- Payments in SQL, locations in key-value
Step 4 of 8
Solution
LLD: what is inside one block
Now we open the Orders service. LLD decides how one component is built: its modules or classes, the interfaces between them, its tables and indexes, its algorithms and how it handles errors.
Here a controller receives the HTTP requests, the order service applies the business rules, a repository talks to the database and a payment client wraps the calls to the Payments service.
What changed
- The rest of the system
- Inside Orders: controller, service, repository and payment client
Step 5 of 8
Technique
States, tables and odd cases
An order goes through several states and LLD fixes which changes are allowed: it can be cancelled while it is created or paid, but not once the kitchen has started.
It also defines the table (columns, types and an index on (user_id, created_at) to list «my orders» from newest to oldest) and the awkward cases: what if the call to Payments gets no answer, or the same order arrives twice?
What changed
- Order state machine
- Orders table and index
- Idempotency key on POST /orders
Step 6 of 8
Concept
Where they touch: contracts
The two levels meet at the edge of each block. HLD fixes what a component promises: its API and the events it publishes. LLD decides how it keeps that promise.
That split lets teams move without stepping on each other: Orders can rewrite everything inside it as long as POST /orders and OrderCreated keep meaning the same thing.
What changed
- The contract at the edge of the service
Step 7 of 8
Trade-off
The mistake of designing at the wrong level
In an interview you are asked to design this app. One person spends twenty minutes on class diagrams and never mentions the 40,000 location writes per second. Another draws a flawless map but cannot explain how an order avoids being charged twice.
Work top-down: requirements, rough numbers and the HLD; then go down to LLD only at the one or two points where the hard part is.
What changed
- Back to the whole system
- Deep dives: locations and double charges
Step 8 of 8
Summary
Recap
HLD: the whole system. It decides which services, stores and queues exist, who talks to whom and where each piece of data lives. It changes rarely and with agreement between teams.
LLD: one component from the inside. It decides classes, tables, indexes, states and error handling. It changes often, within one team.
Contracts (API and events) are the border: HLD fixes them and LLD keeps them.
Design top-down and go down to LLD only where a decision needs it. Object-oriented design and design patterns are a subject of their own.
← → step · R replay · Space pause
You will learn
- What high-level design decides (covered)
- What low-level design decides (covered)
- Contracts: the border between the two (covered)
- How to move between levels while designing (covered)