dine. a kitchen run by sagas Book a table
Log in with Google (coming soon)

Articles

Tiếng Việt · English

One way in for many services: the API gateway

October 14, 2026 · about 9 min read

The dine site calls six services: sign-in, orders, menu, payment, kitchen, reservations. Yet the browser knows exactly one address: api.dinesaga.app. Standing at that address is a small service called the gateway, the only way into the whole system. This article is about why it is needed, what it does, and why dine wrote its own instead of using an existing tool.

Without a gateway

It works perfectly well. Each service opens an address to the internet, and the browser calls each one directly. But you soon notice the shared chores being repeated everywhere:

A gateway gathers those shared chores in one place. The services behind it only have to deal with their own business.

The browser calls api.dinesaga.app; the gateway checks sign-in and CORS, then forwards the request by path: /auth to auth, /order to order, /product to product, /payment to payment, /kitchen to kitchen, /reservation to reservation. The services only open ports inside the server. Browser dinesaga.app 1 address server: services listen only internally; the internet sees only the gateway gateway checks sign-in CORS, request ID routes by path /auth/* auth /order/* order /product/* product /payment/* payment /kitchen/* kitchen /reservation/*reservation
The path decides where a request goes: /order/... to order, /payment/... to payment. The path is forwarded unchanged.

What dine's gateway does

Every request passes through the gateway in exactly this order:

  1. Attach a request ID. Each request gets its own ID, which is passed down to the service behind and returned to the browser. When something fails, searching for that ID finds the logs of both the gateway and the service.
  2. Strip forged headers. If the browser sends a header saying "I am user X" by itself, the gateway removes it. Without that, anyone could impersonate anyone else.
  3. CORS. Only the dinesaga.app page may call across. The browser asks first with an OPTIONS request, and the gateway answers it directly without forwarding it to a service.
  4. Pick the service by path. If no service matches, return 404.
  5. Check sign-in if that path requires it, for example creating an order or viewing your own orders. A missing or invalid token gets a 401. If it is valid, the gateway adds a header saying who the user is, and only then forwards the request.
  6. Forward and return the result. For live data streams (the home page updating every second), the gateway pushes each piece of data on immediately instead of batching it.

Sign-in: check in one place, trust everywhere

Of the six services behind the gateway, auth is the odd one out. In the figure at the top it is just a box like order or payment, but requests go through auth almost only once: at sign-in. After that, the gateway checks things by itself without asking auth again.

Two phases. Sign-in, once: the browser goes through the gateway to auth, auth checks with Google and signs a JWT with its private key, returned to the browser in the dine_token cookie. Every request after that: the browser sends the cookie, the gateway verifies the signature with the public key, attaches the user header and forwards to order; auth takes no part. 1 · SIGN-IN, ONCE Browser gateway auth holds private key Google verify signed JWT → cookie dine_token (back via gateway) 2 · EVERY REQUEST AFTER THAT Browser gateway public key order auth not asked cookie checks signature, expiry user: usr_... Verifying a signature is a local calculation: the gateway calls nobody.
Auth is the only place that signs tokens. The gateway only verifies the signature, so a request never needs an extra round trip to auth.

When you sign in with Google, auth checks with Google and then gives the browser a signed token (JWT), stored in the dine_token cookie. Only auth holds the key to sign. The gateway only holds the public key to verify that signature: a public key cannot sign new tokens, so even if it leaks, nobody can forge a token. Once verified, the gateway attaches "the user is usr_..." to the request.

The services behind it need to know nothing about tokens, keys or Google. order just reads that header to know whose order this is. For this to be safe there is one condition: nobody can go around the gateway. At dine, the services only open ports inside the server; from the internet, only the gateway is reachable. If a service accidentally opened a port to the outside, an attacker could call it directly, fill in the header "I am user X" themselves, and be done.

Not asking auth: what you gain, what you lose

What else a gateway can do

Because every request passes through one place, it is also the natural place for many other things. dine's gateway currently only does the jobs above, but it could add:

There are also things that should not live in the gateway: business logic, or calling several services and merging their data. Do that, and the gateway slowly turns into a giant service that knows everything, exactly what splitting into services was meant to avoid. dine merges data from several services in the browser; the gateway only forwards.

Can you write your own gateway?

You can, and dine did. But it is one choice out of three:

ApproachFits whenTrade-off
Nginx configurationAll you need is routing requests by path Very fast, very stable. But plain Nginx usually cannot verify a JWT by itself; you need an extra module or some Lua.
An off-the-shelf gateway
(Kong, Traefik, Envoy, a cloud provider's gateway)
You need many features, many teams share it Rate limiting, caching and plugins come built in. But it is one more large system to learn and operate.
Write your ownThe needs are few and clear, and you want to understand and control every line Flexible and lightweight. But you have to run into, and fix, every bug and every trap yourself.

dine's gateway is written in Go, using only the standard library, with a core of a few hundred lines. Go already ships a request forwarder (reverse proxy), so the hard part is not the forwarding but the small traps. A few that dine ran into:

This list is the reason to think twice before writing your own: most of those traps were hit and fixed long ago by off-the-shelf gateways. For dine, writing its own was still worth it, because the needs are small and every trap it hit was a lesson about HTTP that an off-the-shelf tool would have hidden.

The price

One small but important detail: when the monitoring system asks the gateway "are you alive?", the gateway answers only for itself, without asking the services behind it. A dead service must not get the gateway marked as dead, dragging down the healthy services with it.

In short: things still work without a gateway, but every service has to handle sign-in and CORS itself and open its own door to the internet. A gateway gathers those shared chores in one place and is the only way in. You can write your own, as long as you know up front that you are also taking on the traps that off-the-shelf tools would have handled for you.

See it for yourself

Open your browser's developer tools (F12), go to the Network tab, and reload the home page: every data request goes to api.dinesaga.app, differing only in the path. Each response has an X-Request-Id header, which is the ID the gateway attached for searching the logs.

Spotted a mistake, or have a question? Write to [email protected].

dine is a personal project about distributed systems, written in Go: orchestrated sagas, transactional outbox, Kafka. More articles.