Runtime Configuration With Free Polling¶
Some settings change more often than you deploy: the page size of a list, the text of a banner, the URL of a partner's API, the number of retries before giving up. Baking them into the build means a release for every change. Reading them from a configuration service means a request on every start, and usually a poll to notice changes.
The poll is where configuration services get expensive or slow. This post shows how to make it free.
One configuration per application and environment¶
An AppConfiguration holds the settings of one application in one
environment, under an id you choose, <application>_<environment>:
curl -X POST https://apis.objectsgrid.com/appconfigurations \
-H 'X-ObjectsGrid-AccessPoint: EU' -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
-d '{"id": "web-shop_production", "application": "web-shop", "environment": "production",
"settings": {"apiBaseUrl": "https://api.example.com", "pageSize": 20, "banner": {"enabled": false}}}'
settings is any JSON object up to 256 KB: nested objects, lists, numbers. The environment names are yours. A
second configuration with the same id is refused with 409, so there's never a doubt which one an app reads.
Polling that costs nothing¶
HTTP has had the answer since 1997: conditional requests. Every read returns the object's version as its
ETag:
Poll with that ETag in If-None-Match:
curl -i https://apis.objectsgrid.com/appconfigurations/web-shop_production \
-H 'X-ObjectsGrid-AccessPoint: EU' -H "Authorization: Bearer $TOKEN" \
-H 'If-None-Match: "1764233402419837"'
While nothing changed, the answer is 304 Not Modified, with no body, and ObjectsGrid doesn't bill a 304. When
something changed, it's 200 with the new settings and the new ETag. So an app that polls every minute pays for
the changes, not for the minutes.
let config, etag;
async function refresh() {
const response = await fetch(`${API}/appconfigurations/web-shop_production`, {
headers: { ...headers, ...(etag ? { "If-None-Match": etag } : {}) },
});
if (response.status === 304) return; // unchanged, and free
config = (await response.json()).settings;
etag = response.headers.get("ETag");
}
await refresh();
setInterval(refresh, 60_000);
Every object answers 304 this way, not only configurations: an app can poll a price list or an enum just as
cheaply. Servers can skip polling altogether and subscribe to the AppConfigurationUpdated and
AppConfigurationPatched webhook events.
Roll back by version¶
Every write keeps the previous version, so a bad change is one PUT away from undone:
curl "https://apis.objectsgrid.com/appconfigurations/web-shop_production?version=1764233402419837" \
-H 'X-ObjectsGrid-AccessPoint: EU' -H "Authorization: Bearer $TOKEN" > previous.json
Send previous.json back with PUT, leaving out its meta, and every app picks it up on its next poll.
meta.lastUpdatedBy on each version says who made the change.
Who may change production¶
A configuration is an object like any other, so your access policy decides who reads and changes it. One rule keeps production in a few hands:
deny if {
input.objectType == "AppConfiguration"
input.action in {"create", "update", "patch", "delete"}
input.newObject.environment == "production"
not has_role("release-manager")
}
A browser or mobile app can read its configuration with a public API key: a new
Organization Account's default policy lets anonymous callers read the AppConfigurations tagged visibility =
public, and no others.
Not for secrets¶
Settings are returned to every caller your policy lets read them, and travel through search and webhooks. A database password or an API key doesn't belong there: keep it in a secret store, and let a setting name where to find it.
For flags that turn features on for some users, see feature flags every client agrees on. The configuration and feature flags guide has the details.