Skip to main content

Your first Route: an Upstream, a key and a limit in ten minutes

Create an Upstream, a Route, a Consumer with a key, key-auth and limit-count on the Route; 401, 200 and 429 as records in Traffic Explorer, each save audited.

By Efsun Balcı Getting started

What you will have at the end

On the Sandbox Environment from Add your first Environment: an Upstream with one node, the orders service; a Route, orders-api, that serves /orders/* from it; a Consumer, mobile_app, with an API key; and two Plugins on the Route, key-auth and limit-count, so that a request without the key is refused with 401, a request with it is answered 200, and the sixth within a minute is refused with 429. The three answers are records in Traffic Explorer with gateway or the backend’s address in the Upstream node column, each save is an audit record with the before and the after, and the Raw JSON tab shows the Route as the engine stores it. The other articles of this knowledge base call this Route “the Route from the first article”.

Prerequisites

  • The Environment from the previous article, with its engine and gateway address, and a log store connected under Observability as in Traffic Explorer: connect your log store; the previous article left the log store out, and the records below need it; the gateway here answers at 127.0.0.1:9381.
  • A backend the gateway can reach. The lab’s orders service, orders-svc:8080, answers GET /orders/<id> with a JSON order.
  • A role that may manage Upstreams, Routes and Consumers. The screenshots below were taken as nora.becker.

The Upstream

Open Upstreams in the Environment’s navigation and select New upstream. Name orders-backend. Under Targets, keep Nodes, a fixed list of hosts, ports and weights, and Add entry: Host orders-svc, Port 8080, Weight 1. The section’s own help says what the other two columns do: weight 0 keeps a node in the list without sending it traffic, and a lower priority is used only when every node of a higher one is down.

The Targets section of the new-Upstream form: Nodes selected, one entry with Host orders-svc, Port 8080, Weight 1 and Priority empty, and the Add entry button One node is enough to start. A second one later makes it a pool, with the health checks of a later article.

Select Create upstream. The Changes panel on the right had shown the object that reaches the engine: name, description and the nodes list. Everything else, balancing, health checks, timeouts, keeps the engine’s defaults, and the Upstream page says so, card by card.

The Route

Open Routes, New route. Name orders-api. Under Matching, Paths /orders/*, where the trailing * matches everything under the path, and tick GET and POST under Methods; left unticked, a Route matches any method.

The Matching section of the new-Route form: Paths holding /orders/*, Methods with GET and POST ticked and the others clear, Hosts and Client addresses empty, Priority empty A request is routed here when every rule holds: the path, and one of the two methods.

Under Target, choose Upstream and pick orders-backend from the list. The other choices are what they say: nodes written on the Route itself and shared with nothing, a Service whose Upstream and Plugins the Route inherits, or no backend at all for a Route that serves its Plugins alone.

The Target section with Upstream selected, the list showing orders-backend · 00000000000000000017 with an Open upstream link beside it, and the Websocket switch off The Route sends to the shared Upstream. Open upstream is the link back.

Select Create route. The Route page shows its address under Matching, http://127.0.0.1:9381/orders/, built from the Gateway URL of the Environment, and a request already works, because nothing guards it yet:

### GET /orders/ord-10421 right after the save, no Plugin on the Route
HTTP/1.1 200 OK

The Consumer and its key

Open Consumers, New consumer, Username mobile_app; the username is the identity of the Consumer and cannot change later. Create consumer, then the Credentials tab, New credential: Credential ID mobile-app-key, Plugin key-auth, and under it Key, the API key the client will send. The note under the field is worth a second look: a credential whose key is empty authenticates any request that sends the header with an empty value.

The Authentication section of the new-credential form: Plugin key-auth selected and the Key field filled and masked, with the note about an empty key under it The key is typed once. After the save it is shown as dots, to roles that manage credentials.

key-auth on the Route

Back on the Route, the Plugins tab, Add plugin, key-auth. Its five settings can all stay at their defaults: the key is read from the header the Header field shows in grey, apikey, or from the Query parameter when the header is absent; Hide credentials keeps the key from reaching the backend; Realm is sent back in WWW-Authenticate on a refusal; Anonymous consumer names a Consumer that a request without a credential passes as, and the note says why to leave it empty.

The key-auth form: Header and Query empty with their grey defaults, Hide credentials off, Realm empty, Anonymous consumer empty with its note Nothing to type. The Plugin’s presence is the rule: no credential, no request.

Select Add plugin.

limit-count on the Route

Add plugin again, limit-count. Under Basics, One limit for everything here: Count 5 and Time window 60 seconds, a small budget so the refusal is easy to reach by hand. Key type stays var and Key is consumer_name, so the engine counts under the name of the authenticated Consumer and every Consumer gets a window of its own. Under Behavior, Policy stays local, counted in the memory of each gateway node; the Redis policies share the count across nodes, and Rate limiting per Consumer sets one up. Rejected code 429. Show limit quota header on, so every answer says how much of the budget is left.

The limit-count form: One limit for everything here selected with Count 5 and Time window 60 s, Key type var, Key consumer_name, and the note under Key about a variable the request does not carry Five per minute per Consumer. The note under Key says what happens when the variable resolves to nothing: the count falls back to the client address.

Select Add plugin. The Plugins tab now draws the request path: what runs, in which order, for a request to this Route.

The Plugins tab of orders-api: the Request path rail with the request /orders/* GET POST, Global rules with prometheus and elasticsearch-logger first, then the Route's two Plugins by priority, key-auth at 2500 above limit-count at 1002, then the Upstream; beside it the table Plugins on this route with both Enabled Authentication at 2500 runs before the limit at 1002, which is why the limit can count by Consumer name. The two global rules above them are the Environment’s own logging and metrics.

What a client sees

### GET /orders/ord-10421 without a key
HTTP/1.1 401 Unauthorized
WWW-Authenticate: apikey realm="key"
{"message":"Missing API key in request"}
### the same request with the key
HTTP/1.1 200 OK
X-RateLimit-Limit: 5
X-RateLimit-Remaining: 4
X-RateLimit-Reset: 60
{"id":"ord-10421","customer":"cust-8831","status":"open","total":249.90,"currency":"EUR",…}
### four more with the key, status only
200 200 200 200
### the sixth within the minute
HTTP/1.1 429 Too Many Requests
X-RateLimit-Limit: 5
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 59.79
### a wrong key
HTTP/1.1 401 Unauthorized
{"message":"Invalid API key in request"}

The three headers are the quota headers the switch turned on: the limit, what is left, and the seconds until the window resets.

What Traffic Explorer shows

Every request is a record. The refusals carry gateway in the Upstream node column, because the gateway answered them itself; the answered requests carry the backend’s address; and the requests that were authenticated carry the Consumer’s name beside the Route.

Traffic Explorer in Sandbox, 10 records: the 401 of the wrong key answered by gateway, the 429 answered by gateway with orders-api · mobile_app in the Route column, five 200 records with the backend's address and the Consumer's name, the 401 without a key, a 404 for / with no route matched, and the 200 from before the Plugins without a Consumer Ten records: the eight requests above, the 200 from before the Plugins and a probe for / that matched no Route. The row from before the Plugins has no Consumer, because nothing had identified the caller yet.

The Route’s Analytics tab counts the same requests by status class from Prometheus. Right after the first batch it read zero; a minute later came a second batch of twenty keyed requests, all of them refused with 429 because the budget was spent, and one more without a key, refused with 401. Then it reads this:

The Requests card of the orders-api Analytics tab: 22 requests in the last 24 hours, by status class 2xx 0, 3xx 0, 4xx 22 at 100 %, 5xx 0 Twenty-two, all 4xx. Prometheus’s own counters for this Route: the 429 series first scraped at 1 and then at 21, the 401 series at 2 and then at 3, so the increases are 20 and 1. The card reads 22 for those 21 because this Prometheus gives estimates, as Connect Prometheus explains. The 200 from before the Plugins, the five keyed 200s, the first two 401s and the first 429 were each the first sample of their series and are not counted, as the Server errors card of A failing backend out of rotation showed. Traffic Explorer holds every request; Analytics is the rate.

What the audit log keeps

Each save is a record in the Environment’s audit log: upstream.created, route.created, consumer.created, credential.created and two route.updated, one per Plugin. The last one shows the Route before and after, with the limit-count block added beside the key-auth block that was already there.

The Change section of the audit record of the second Route update: the before side with the key-auth block alone, the after side with limit-count added, key consumer_name, count 5, time_window 60 and rejected_code 429 The Plugin as the engine received it. The credential’s record, not shown, carries the key as four dots.

The same Route as the engine stores it

The Raw JSON tab shows the whole object the engine holds, validated against the gateway’s schema as you type, and it is the one place to read the Route without the forms: name, paths, methods, the two Plugin blocks and the Upstream’s id.

The Raw JSON tab of orders-api: the full engine object, name, id, uris, methods, desc, the plugins block with key-auth and limit-count, create_time, upstream_id and update_time, with a Format button Every form in this article wrote into this document. A field no form draws can be edited here and is checked the same way.

What to do next

Written by
Efsun Balcı
Engineering intern, G8
Was this helpful?
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.