Cliente–Servidor

Dos niveles

Escrito por Darjus Kazakevic · revisado el

Clients ask and one server decides. The base under almost every other style.

In client-server, clients (browsers, mobile apps, other programs) send requests over a network to a server. The server owns the data and enforces the rules. The web works this way: a browser sends an HTTP request and a server answers. Most other styles in this category, from monolith to microservices, describe only the server side.

Naming the style makes three facts visible. There is a network hop, so latency and failure are normal. There is one authority, which makes consistency simple. And the client is untrusted: anyone can send a hand-written request, so the server must check everything.

En esencia — Clients call a central server that owns the data and the rules; design for the network hop, enforce every rule on the server, and plan how it scales.

Cuándo recurrir a él

Recurre a él cuando

  • One party must hold the true data and enforce the rules
  • Clients cannot be trusted: browsers, mobile apps, user scripts
  • Many clients need the same shared, up-to-date data

Reconsidéralo cuando

  • No single party can or should be the authority
  • Nodes must keep working and syncing with no server reachable
  • Nodes exchange heavy data directly, and a server would only relay it

The parts

Client
Starts every exchange, shows data and collects input. It runs on hardware you do not control, so its code can be changed or replaced.
Server
Waits for requests, owns the data and applies the rules. Many clients share it, so it is the single source of truth.
Request and response
Each exchange is one request and one response, usually over HTTP. The network hop has a cost you can estimate.
Stateless server
If the server keeps no session in its memory, any copy can answer any request. Then you scale by adding copies behind a load balancer.
Thin and thick clients
A thin client, like a server-rendered page, does little alone. A thick client, like a mobile app, caches data and may work offline, but must sync back.

Reglas de diseño

Enforce every rule on the server
A client-side check gives fast feedback, not protection. Assume every request was hand-written, because one day it will be. A rule checked only in the client is not enforced.
Treat the network hop as normal
Every call can be slow or fail. Decide early what the client does while it waits: show cached data, queue the action, or block with a clear message.
Keep the server stateless
Store sessions and files outside the process, in Redis, S3 or the database. Then any server copy can answer any request, and you can add or replace copies freely.
Change the API only by adding
Old mobile app versions stay installed for months. Add fields instead of changing them, and keep old endpoints until their traffic is close to zero.

Errores habituales

  • Validating only in the client, so a crafted request skips the rule
  • Keeping sessions in server memory, so a restart logs everyone out
  • A chatty client that makes 30 calls to render one screen
  • Removing an API field that old app versions still read