Configuring per-route options
Page last updated:
By default, communication between Gorouter and backends is configured through the general settings at the platform level.
This topic describes how to specify per-route Gorouter options scoped at the application level. This greater granularity lets developers tailor optimal routing behavior for applications’ unique load profiles or other requirements.
Gorouter supports the following per-route option, described in the section below:
loadbalancing
: Configures the load balancing algorithm used by Gorouter for this particular route.- Settings:
round-robin
,least-connection
.
- Settings:
Configure Gorouter’s Load Balancing Algorithm
The per-route option loadbalancing
allows configuring the load balancing algorithm, which defines how the load is distributed between Gorouters and backends.
This option supports two settings for load balancing:
round-robin
distributes the load evenly across all available backendsleast-connection
directs traffic to the backend with the fewest active connections at any given time, optimizing resource utilization
Configure Load Balancing in an App Manifest
To configure per-route load balancing for an application that has not yet been pushed:
In the application manifest, include a
route
definition with anoptions: loadbalancing
attribute set toround-robin
orleast-connection
. For example:--- applications: - name: MY-APP routes: - route: MY-HOST.EXAMPLE.COM options: loadbalancing: least-connection
Where
MY-APP
is the name of your app andMY-HOST.EXAMPLE.COM
is the route you want to map to your app.Push the app with the manifest:
cf push -f manifest.yml
Change Load Balancing Algorithm of an Existing Route
To change the per-route loadbalancing
option of an existing route, you can use the cli command, update-route
.
For example, to change an app route’s algorithm from least-connection
to round-robin
, you can run the update-route
command:
```console
cf update-route EXAMPLE.COM --host MY-HOST --option loadbalancing=round-robin
```
Alternatively, it is also possible to update the per-route load balancing option via the /v3/routes
API.
Run the PATCH
request to the targeted API endpoint:
```console
cf curl /v3/routes/GUID -X PATCH -H "Content-type: application/json" \
-d '{
"options": {
"loadbalancing": "round-robin"
}
}'
```
Where `GUID` is the unique identifier for the route.
Create a Route with a specific Load Balancing Algorithm
To create a route with a per-route loadbalancing
option, you can use the cli command create-route
.
For example:
```console
cf create-route EXAMPLE.COM --host MY-HOST --option loadbalancing=round-robin
```
Alternatively, it is also possible to create a route with a per-route load balancing option via the /v3/routes
API:
```console
cf curl /v3/routes -X POST -H "Content-type: application/json" \
-d '{
"host": "MY-HOST",
"path": "MY-PATH",
...
"options": {
"loadbalancing": "round-robin"
}
}'
```
Map a Route to an Existing App with specific Load Balancing Algorithm
To create and map a new route to an existing application with the per-route loadbalancing
option, you can use the cli command map-route
.
For example:
```console
cf map-route MY-APP EXAMPLE.COM --hostname MY-HOST --option loadbalancing=round-robin
```
The command map-route
supports the --option
flag only for new routes.
To update an existing route, the command update-route
must be used as described before.
Retrieve Route Options
To read route options, you can query the route using the route
command:
```console
cf route EXAMPLE.COM --hostname MY-HOST
```
The response lists the chosen loadbalancing
algorithm option, e.g. least-connection
:
```console
options: {loadbalancing=least-connection}
```
Alternatively, you can query the routes
API endpoint for a route:
```console
cf curl /v3/routes/?hosts=MY-HOST
```
Where `MY-HOST` is the host attribute of the route. The response lists the chosen `loadbalancing` algorithm option as well:
```console
"options": {"loadbalancing": "least-connection"}
```
To retrieve all the routes with the corresponding options in a space of an organization, you can use the routes
command.