Client/Server
A role split: clients initiate, servers wait and respond. Most of the internet.
Level: beginner
What is it?
The client-server model divides responsibilities between clients that request a service and servers that listen for requests and return responses. The roles are logical; a single machine or process can act in both roles for different interactions.
Why does it matter?
This model allows a service to centralize shared state, authorization, and business logic while many clients use it. It introduces network latency, partial failure, and scaling requirements at the server.
Mental model
A client initiates a protocol exchange; the server listens at an endpoint and processes requests. The network can fail even if either side is healthy, so timeouts, idempotency, and retry rules are part of the design.
How it works
- Servers commonly bind to a port and listen for incoming connections or datagrams.
- A client timeout means no result arrived in time, not necessarily that the server did no work.
- Servers need capacity limits, authentication, authorization, input validation, and observability.
- Stateless request handlers can scale horizontally more easily, while shared state needs coordination or external storage.
- Connection pooling and batching reduce setup costs, but pools need limits and timeouts.
Steps
- The client resolves a service name and connects to the server endpoint.
- It sends a request under an agreed protocol.
- The server authenticates and validates the request, performs permitted work, and sends a response.
- The client handles success, errors, retries, and timeouts according to the operation's semantics.
Real-world usage
- Web applications, database clients, APIs, and internal services.
- Designing clear request/response APIs and server capacity limits.
Common mistakes
- Retrying a non-idempotent request without deduplication after a timeout.
- Assuming a successful TCP connection means the application service is healthy.
- Trusting client-side validation as a replacement for server-side checks.
Interview questions
What is one benefit and one cost of client-server architecture?
It centralizes service behavior and shared state; it also introduces network dependency, latency, and server capacity/failure concerns.
Why should clients use idempotency keys for some retries?
If a response is lost after a successful operation, the key lets the server recognize a retry and avoid performing the same logical action twice.