AlgoViz

Kong

medium 27 min

Kong Gateway is an API gateway built on NGINX and Lua.

See it run

One request through the plugins, their order, which setting wins, and what a limit really counts.

Operations

One request

Router, plugins by priority, proxy. Then the same without a key.

Step 1 of 6

Match a route

Our systemPartner appGET /ordersRouterkey-authrate-limitingProxyOrders

The router finds the route with the highest priority that fits. It belongs to the service Orders.

Step 1: Match a route

def handle(req):    route = router.match(req)            # highest priority    if not route: return 404    for plugin in by_priority(route):        answer = plugin.access(req)        if answer: return answer         # 401, 403, 429 ...    resp = proxy(route.service, req)    return response_phases(resp)         # transform, log
Work per request
Each plugin once
Where state lives
Config in memory

A plugin that answers early saves all the work behind it.

In the real world

The same kong doing real jobs. Pick one and step through it.

Hybrid mode: traffic and configuration fail separately.

Step 1 of 4

Normal

Our systemAppsControl planepushingPostgreSQLData plane 1version 8Data plane 2version 8Orders

The control plane reads the database and pushes configuration to the data planes.

Step 1: Normal

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • The Admin API on the internet

    critical

    Who reaches it can change every route and read every key. By default it listens on 127.0.0.1 only. Keep it so.

    Wrong
    admin_listen = 0.0.0.0:8001
    Right
    admin_listen = 127.0.0.1:8001# reach it over SSH or a private network
  • A way around the gateway

    critical

    If services answer calls that did not pass Kong, keys and limits protect nothing.

    Wrong
    # orders listens on a public address
    Right
    # network rule: only Kong reaches orders# or mutual TLS between Kong and orders
  • Reading a local limit as a total

    major

    With the policy local every node counts alone. Three nodes let through up to three times the limit.

    Wrong
    config: { minute: 100 }   # policy: local
    Right
    config:  minute: 100  policy: redis  redis: { host: redis.internal }
  • API keys in the address

    major

    key-auth also reads the query string by default, and addresses end up in logs and histories.

    Wrong
    GET /orders?apikey=...
    Right
    config:  key_names: [apikey]  key_in_query: false  hide_credentials: true
  • Pulling :latest and expecting 3.16

    minor

    The open source image ends at the 3.9 line. Newer minor versions are Enterprise packages that need a licence.

    Wrong
    image: kong:latest
    Right
    image: kong:3.9   # open source, patches only# or kong/kong-gateway with a licence

Interview tips

  • How to explain it

    • A proxy with plug-in places.
    • Service, route, consumer, plugin.
    • The services stay simple.
  • Where state lives

    • Config: database, file or control plane.
    • Counters: node, database or Redis.
    • Requests never need the database.
  • Reach for it when

    • Partners call your APIs.
    • Every API needs keys and limits.
    • Several teams share one door.
  • Before production

    • Admin API private.
    • Decide open or closed on Redis loss.
    • Check edition and end of support.