News KrakenD CE v2.7 released with better rate-limiting and security options

Doing housekeeping on your configuration file

by Albert Lombarte

May 17, 2019

4 min read
post image

Starting your krakend.json configuration from scratch and writing every attribute in a blank editor might be a difficult and time consuming task, and that’s why we have the KrakenDesigner.

KrakenDesigner is an aid tool to generate KrakenD configurations for the uninitiated audience. It produces a verbose output of the configuration file and explicitly writes unneeded attributes to let you see that a specific configuration option exists. These attributes I am mentioning are not necessary because they contain default values and the server is not going to alter its behavior.

Maybe you just copied fragments from here and there, and now you have a working configuration. In any case, after tinkering with the designer or any other way, you might have a file full of default values that have no practical impact in the configuration. For instance, the designer lets you see that a "method": "GET" might be converted into a "method":"PUT", but when not stated, a GET is assumed. With this in mind, it’s time to delete unnecessary settings and enjoy a cleaner and more optimized file.

Settings you can safely delete from your configuration

If your configuration presents any of the following attributes with the exact value we are showing, it’s safe to delete them.

"output_encoding": "json"

By default KrakenD outputs all its endpoints in json format, no matter what the client asks for. If this is enough for you, remove this line. More information on output encoding

"concurrent_calls": 1

The concurrent requests technique improves the response times and decreases error rates by requesting the same information multiple times, taking the fastest successful response and canceling the pending ones.

As this is an aggressive option that does not fit all infrastructures, KrakenD takes the conservative option: one call to the endpoint, one request per backend.

Want real user experience improvement? Set it to 3. Concurrent calls docs.

"disable_host_sanitize": false

The host sanitization is automatically performed when connecting to a backend. A case where you don’t want the host sanitized is when your backends are found using Service Discovery. Not using SD? Out!

"sd": "static"

SD stands for service discovery, and when the value is static it means that the hostnames of your backends are resolved using the internal machine settings, using its DNS settings, resolv.conf configuration or any other internal system that could lead you to ping the name without additional help. A host like http(s)://your.dns is a typical static resolution.

"encoding": "json"

As it happens with the output encoding to return data to the user, when connecting to the backends KrakenD assumes that their response will be in JSON format. Unless they are returning other formats like XML or RSS, or you are proxying to them, you don’t need this attribute in the configuration.

"extra_config": {}

The additional configuration of the different plugins, middlewares, and other surrounding components, it’s necessary only when you want to provide specific settings for them. An empty extra_config without declaring a namespace, it’s just noise.

"port": 8080

We went through a deep brainstorming session to find the most original port in the Internet era, and we decided that our listening port would be 8080. Usually, the gateway will live behind a balancer that exposes to the world whatever port you like, so it doesn’t matter what port KrakenD is listening unless is conflicting with something else. If 8080 is fine for you, remove the line.

Summary of attributes with default values

These are the no-brainer deletions:

"output_encoding": "json"
"concurrent_calls": 1
"disable_host_sanitize": false
"sd": "static",
"encoding": "json",
"extra_config": {},
"port": 8080,

Send them to hell now!

Any flag set to false

All boolean flags on KrakenD are set to false by default. Meaning that you can delete any entry that sets a false value.

Some other refactoring you can do

Well, refactoring is too much of a word for this, but at least we would like to reduce the size of the file and optimize it for better legibility.

When all your backends use the same hosts (you have a single API), or a majority of backends use the same host list, place the host: [] key in the root of your file to use it as the default for all backends. Then delete it from the backend declarations and add it only when you need to overwrite them.

Do not assume that this technique works for any other piece of the configuration. Placing attributes in the root of the file won’t set them as default values, but you can put other keys at the root of the file like timeout or cache_ttl.

Now apply all this in your configuration file and spot the differences!

Split the configuration file into several files

For large configurations, you might also want to have a general KrakenD configuration file that includes many other templates. This can be done using the Flexible Configuration!

 Stay up to date with KrakenD releases and important updates