diff --git a/apps/blog/src/components/PostCard.astro b/apps/blog/src/components/PostCard.astro index 195d2df..26ab0d5 100644 --- a/apps/blog/src/components/PostCard.astro +++ b/apps/blog/src/components/PostCard.astro @@ -22,7 +22,7 @@ const category = tags[0]
-
+
{cover ? ( {title} ) : ( diff --git a/apps/blog/src/components/TagFilter.tsx b/apps/blog/src/components/TagFilter.tsx index 4192f0f..fc050fd 100644 --- a/apps/blog/src/components/TagFilter.tsx +++ b/apps/blog/src/components/TagFilter.tsx @@ -84,7 +84,7 @@ export function TagFilter({ tags, totalCount, initialTags = [], locale = 'en' }:
{t('index.filterLabel')} {totalCount} {t('index.articleCount')} -
+
{tags.map((tag) => { const active = selectedTags.includes(tag.name) return ( diff --git a/apps/blog/src/content/posts/en/webhooks-keeping-systems-in-sync.mdx b/apps/blog/src/content/posts/en/webhooks-keeping-systems-in-sync.mdx new file mode 100644 index 0000000..a27862c --- /dev/null +++ b/apps/blog/src/content/posts/en/webhooks-keeping-systems-in-sync.mdx @@ -0,0 +1,179 @@ +--- +title: "A lost webhook is wrong data in production" +description: "When identity lives in the IAM and the business application keeps its own copy, everything rests on a notification. What a webhook guarantees, what happens when the application is down, how FerrisKey's delivery queue works, and what it costs." +short_description: "A webhook is not judged when everything works, but the day the business application is unreachable." +date: 2026-11-15 +tags: [webhooks, architecture, integration, reliability, ferriskey, iam] +status: published +author: leadcode_dev +--- + +Alice changes her last name. An administrator fixes her profile in the IAM, the screen confirms the save, and everyone moves on. But the HR application keeps its own `employees` table, with its own `lastname`. If nobody tells it the identity changed, it will keep issuing documents under the wrong name, with no error and no alert, until someone notices. + +There is nothing exotic about this problem. As soon as an identity system lives next to an application that keeps a copy of some of its fields, you have two sources for the same piece of information, and only one of them is right. The question is not whether they will drift apart, but how you will find out, and for how long. + +## Three ways to keep two databases in agreement + +Querying the IAM every time you need a value removes the copy, and with it the problem, but it ties your application's availability to the IAM's and adds a network call to every read. Polling keeps the copy but ties the freshness of the data to the period of the job. And sharing the database is a contract nobody can change anymore. + +A webhook goes the other way: the IAM tells you, at the moment the thing happens. Your application keeps its local, fast copy, and never has to ask whether something changed. + +There is a catch, and everything below follows from it. A webhook is a promise: "I will tell you". A promise can be broken, because the network drops, because your application restarts, because a deployment ran three minutes too long. A webhook without a failure strategy does not replace polling, it makes it invisible, which is worse. The work of a good webhook system happens almost entirely on the failure path. + +## The simple case: a name changes + +In FerrisKey, a webhook belongs to a realm, points to an endpoint and subscribes to specific events. For our HR application: + +```json title="POST /realms/acme/webhooks" +{ + "name": "HR sync", + "description": "Keeps the employees table aligned with identities", + "endpoint": "https://hr.acme.example/webhooks/ferriskey", + "headers": { "X-Webhook-Source": "ferriskey" }, + "subscribers": ["user.created", "user.updated", "user.deleted"] +} +``` + +A webhook can subscribe to 38 events, grouped by resource (users, clients, roles, realm, authentication…). The documentation states that in 0.10.0 the server only emits 33 of them: the other five accept a subscription, then stay silent. Check the [list of events](https://docs.ferriskey.rs/en/modules/webhooks/triggers) before relying on one. + +When the administrator edits Alice's profile, FerrisKey sends a POST whose body looks like this: + +```json title="user.updated (excerpt)" +{ + "event": "user.updated", + "timestamp": "2026-03-17T14:30:00.123456+00:00", + "resource_id": "01914b3c-1234-7f5a-b456-000000000001", + "data": { + "id": "01914b3c-5678-7f5a-b456-000000000002", + "username": "alice", + "firstname": "Alice", + "lastname": "Martin", + "email": "alice@example.com", + "updated_at": "2026-03-17T14:30:00.101234Z" + } +} +``` + +Two details of this format surprise people on first contact, and the documentation points them out. First, `resource_id` does not identify the user: for almost every event it is the realm id. The object concerned is read from `data.id`. Second, `data` changes shape depending on the event: the full user for `user.updated`, a bare id for `client.deleted`, `null` for `auth.reset_password`. + +:::callout{variant="warning"} +`user.updated` is also emitted when a role is removed from a user. In that case `data` is the role, not the user. A handler that reads `data.firstname` without checking what it received will overwrite names with `undefined`. Test for the presence of `data.username` before treating the event as a profile change. +::: + +On the application side, the handler fits in a few lines, provided you choose a write that survives bad days: + +```ts title="webhooks/ferriskey.ts" +const { event, data } = JSON.parse(rawBody.toString('utf8')) + +if (event === 'user.updated' && data.username) { + await db.query( + `INSERT INTO employees (iam_user_id, firstname, lastname, iam_updated_at) + VALUES ($1, $2, $3, $4) + ON CONFLICT (iam_user_id) DO UPDATE + SET firstname = EXCLUDED.firstname, + lastname = EXCLUDED.lastname, + iam_updated_at = EXCLUDED.iam_updated_at + WHERE employees.iam_updated_at < EXCLUDED.iam_updated_at`, + [data.id, data.firstname, data.lastname, data.updated_at], + ) +} + +res.writeHead(204).end() +``` + +The `WHERE employees.iam_updated_at < EXCLUDED.iam_updated_at` clause does three things at once. Replaying the same event twice changes nothing. An old event that arrives after a newer one is ignored. And if Alice's creation was missed, the first update creates the row (if your table can exist with just the profile). You did not write any recovery logic, you made the write insensitive to ordering and duplicates. For the rest of this article, that is the most useful property your side of the contract can have. + +Before touching `data`, this handler should also verify the signature. FerrisKey signs every attempt with an HMAC-SHA256 computed over `{timestamp}.{raw body}`, using the webhook's secret, and sends the result in `x-ferriskey-signature` (the details and a complete example are in the [payload documentation](https://docs.ferriskey.rs/en/modules/webhooks/payload#verifying-the-signature)). The timestamp is part of the signed string: someone who captures a request cannot replay it later by simply changing the header. A static header, like the `Authorization` you can also configure, proves that the caller knows a token, not that the body was left untouched. + +## What FerrisKey checks before calling anything + +From the IAM's point of view, a webhook is an outbound request to an address chosen by a user. That is exactly the shape of an SSRF attack, and the documentation draws the consequences: FerrisKey refuses to save an endpoint that is not `https`, that carries credentials in the URL, or that is a loopback, private or link-local address. The hostname is checked again each time it is resolved, at delivery time. For local development, an environment variable (`WEBHOOK_ALLOW_PRIVATE_ENDPOINTS=true`) lifts the restriction, and it must never be on in production. + +The rest of the delivery behavior is deliberately strict: FerrisKey waits 5 seconds to connect and 10 seconds in total, counts any `2xx` as a successful delivery, and does not follow redirects. A `301` from `http` to `https` is therefore recorded as a failure. That is not a defect, it is what makes the behavior predictable: the endpoint decides, through its status code, what happens to the delivery. + +## When the endpoint answers badly: automatic retries + +Not all errors are equal, and FerrisKey does not treat them the same way. A transport failure, a failed DNS resolution, a `5xx`, a `408` or a `429` are retried. An invalid configuration, a refused address, or any other HTTP status (a `400`, a `401`, a `404`) are not: the delivery goes straight to `failed`. A `503` during your deployment will be retried. A `401` because your endpoint rejected the signature will not, which is the behavior you want: retrying will not make a signature valid. + +Retries follow exponential backoff with jitter, governed by four values: + +| Setting | Default | Bounds | +|---|---|---| +| `max_attempts` | 5 | 1 to 20 | +| `base_delay_ms` | 500 ms | 100 ms to 60 s | +| `max_delay_ms` | 30 s | at least `base_delay_ms` | +| `max_total_delay_ms` | 120 s | at least `max_delay_ms` | + +The wait before each retry is drawn at random between 0 and a ceiling that doubles with each attempt. With the defaults, the ceilings are 1, 2, 4 and then 8 seconds. The jitter is not decorative: if a hundred deliveries fail together because your application goes down, they must not all come back in the same millisecond when it recovers. The price is that a retry can arrive almost immediately. + +These values are set field by field, at three levels: the webhook itself, otherwise the realm, otherwise the system default. A webhook can raise only `max_attempts` and inherit the delays. Reading a webhook returns both `retry_policy` (what it overrides) and `effective_retry_policy` (what will actually be applied), and the second one is the one to look at when you wonder what the worker will do. + +## When the business application is down + +This is where the difference between a webhook system and a `fetch` in a `try/catch` shows. Imagine the HR application is unavailable for an hour, on a Tuesday morning, because of a migration that goes wrong. During that time, three profiles are edited in the IAM. + +What happens, with the default settings: + +1. The administrator's action succeeds. It does not wait for the endpoint and does not fail if it is unreachable, which the documentation states explicitly. +2. FerrisKey recorded one delivery per subscribed webhook before trying to send anything. The first attempt fails, the retry is scheduled. +3. Five attempts or 120 seconds later, whichever comes first, the delivery goes to `failed`. +4. FerrisKey records a `webhook_delivery_exhausted` event in SeaWatch, with the delivery id, the last error code and the number of attempts. + +In other words, with the default configuration, the endpoint has about two minutes to come back. An hour of downtime is not recovered by retries. It is recovered by what comes after. + +You can lengthen the window, per webhook or per realm: + +```json title="PUT /realms/acme/webhooks/{webhook_id} (excerpt)" +{ + "retry_policy": { + "max_attempts": 20, + "max_delay_ms": 300000, + "max_total_delay_ms": 3600000 + } +} +``` + +But the limit of 20 attempts caps what automatic retrying can absorb. With a maximum delay of 5 minutes, the sum of the wait ceilings across those 19 retries comes to roughly an hour. Since each wait is drawn between 0 and its ceiling, the real window is, on average, closer to half of that. This is my own calculation from the documented rules, not a measurement, but the order of magnitude is enough: backoff is built to absorb a blip, not an outage. + +For an outage, there is manual replay. The `webhook_delivery_exhausted` event is the one to wire your alert to: it is the difference between "a delivery failed then recovered" and "this endpoint has been unreachable long enough that we gave up". Once the application is back: + +```text +POST /realms/acme/webhooks/{webhook_id}/deliveries/{delivery_id}/retry +``` + +The response is a `202`. A delivery that is still `pending` or `delivering` answers `409`: wait for it to finish. Replay re-sends the stored payload, unchanged, signed again with a new timestamp. It is not a fresh snapshot of the resource: the event described the world as it was, and replaying it says the same thing again. If Alice changed her name a second time in the meantime, the old event will arrive after the new one, in the order you replay. That is exactly the case the `updated_at` clause in the handler above absorbs without asking anyone for anything. + +When something does not work, the order of reading is simple. Is the webhook subscribed to anything (the console flags webhooks subscribed to nothing as "without event")? Is there a delivery for that event at all, and if not, is the trigger name exact? What does `last_status_code` say, which points at your endpoint, or `last_error_code`, which points at the path to reach it? Once fixed, replay rather than waiting for the next event to find out whether it works. + +## The queue: what it gives you, what it costs + +The behavior above rests on a `webhook_deliveries` table in PostgreSQL, introduced by FerrisKey's persisted delivery work ([#1340](https://github.com/ferriskey/ferriskey/pull/1340)). What follows comes from reading the code, not the documentation, and the constants quoted may change. + +When an event occurs, FerrisKey finds the subscribed webhooks and writes one `pending` row per webhook, with the full payload as JSONB. The first attempt leaves almost immediately, from an in-memory channel of 1024 slots, with at most 16 concurrent sends. If that channel is full, the delivery simply waits for the worker's next pass. The worker wakes up every 5 seconds, claims up to 8 due deliveries with `FOR UPDATE SKIP LOCKED` (so several FerrisKey instances do not fight over the same row), and processes them. A claimed delivery holds a 10-minute lease: if the instance holding it dies, the lease expires and the row goes back in the queue. An hourly sweep deletes finished deliveries older than 30 days. + +What this gives you: + +- Decoupling. The administrator's action depends neither on the latency nor on the availability of your endpoint. +- Surviving restarts. The state of a delivery is in the database, not in a process's memory. +- Observability. Every attempt leaves a queryable trace (HTTP status, error code, attempt count, next pass), and the console lets you open the payload that was sent. +- Replay. It is only possible because the payload was kept. + +What it costs, and this is the part to read before building on it: + +- At least once, not exactly once. A delivery can arrive twice: your endpoint processed the request but did not answer in time, a lease expired, you replayed by hand. The delivery id, sent in `x-ferriskey-delivery-id`, stays the same on every attempt and every replay. It is the natural deduplication key. `resource_id` and `timestamp` cannot play that role. +- No guaranteed order. Jitter, concurrent sends and retries mean an old event can arrive after a newer one. Nothing I read promises an order, and what the system does is consistent with that absence of promise. The way to protect yourself is to write it into the data (an `updated_at` to compare), not to hope for it from the infrastructure. +- Coarser retry latency than it looks. A retry scheduled "in one second" is picked up by a worker that passes every 5 seconds. The real delay is closer to the next pass. +- A write that is not in the same transaction as the action. According to the code, recording the delivery is a separate step, after the user update. If that write fails, FerrisKey logs it (`failed to record a webhook delivery; the event is lost`) and the action itself has succeeded. The case is rare, but it exists, and it weighs on the next section. +- Space in the database. One row per delivery per webhook, with the whole payload, for 30 days. + +## When a webhook is not the right tool + +I think a webhook, even a well-built one, is not enough for data whose accuracy is critical. The case of a delivery lost upstream, just described, is precisely the one no retry mechanism can repair: the event was never recorded, so it will never be replayed. What I recommend is a periodic, slow reconciliation job that compares your table with the IAM's API and fixes the gap. Webhooks provide freshness, reconciliation provides correctness. Each covers the other's weak point. + +And if you need strict ordering, exactly-once delivery, or a very high volume of events, that is a different problem: a message bus with per-key ordering guarantees answers it better than an HTTP POST, however good the retries are. + +## Going further + +The [FerrisKey webhooks documentation](https://docs.ferriskey.rs/en/modules/webhooks/overview) details the payload format, the error codes and the rotation of the signing secret. On that last point, a tip from the docs: rotation takes effect immediately, including for deliveries already in the queue. If your endpoint does not know the new secret yet, it will reject requests that FerrisKey will not retry. Rotate the secret when you already have somewhere to deploy it. + +The code is on [GitHub](https://github.com/ferriskey/ferriskey). diff --git a/apps/blog/src/content/posts/en/what-is-iam-introduction-to-ferriskey.mdx b/apps/blog/src/content/posts/en/what-is-iam-introduction-to-ferriskey.mdx index c9a3678..35b850c 100644 --- a/apps/blog/src/content/posts/en/what-is-iam-introduction-to-ferriskey.mdx +++ b/apps/blog/src/content/posts/en/what-is-iam-introduction-to-ferriskey.mdx @@ -37,7 +37,7 @@ IAM is the structured answer to that chaos. An IAM system is the centralized gatekeeper of your infrastructure. It plays several roles at once. -::::card-group{cols=3} +::::card-group{cols=2} :::card{label="Directory" icon="lucide:users"} It knows who exists. Every human user, every application, every machine service has a unique identity. One place, one source of truth. ::: diff --git a/apps/blog/src/content/posts/fr/webhooks-keeping-systems-in-sync.mdx b/apps/blog/src/content/posts/fr/webhooks-keeping-systems-in-sync.mdx new file mode 100644 index 0000000..a74708d --- /dev/null +++ b/apps/blog/src/content/posts/fr/webhooks-keeping-systems-in-sync.mdx @@ -0,0 +1,179 @@ +--- +title: "Un webhook perdu, c'est une donnée fausse en production" +description: "Quand l'identité vit dans l'IAM et que l'application métier garde sa propre copie, tout repose sur une notification. Ce que garantit un webhook, ce qui se passe quand l'application est down, comment fonctionne la file d'attente de FerrisKey, et ce que cela coûte." +short_description: "Un webhook ne se juge pas quand tout va bien, mais le jour où l'application métier est injoignable." +date: 2026-11-15 +tags: [webhooks, architecture, integration, reliability, ferriskey, iam] +status: published +author: leadcode_dev +--- + +Alice change de nom de famille. Un administrateur corrige son profil dans l'IAM, l'écran confirme l'enregistrement, et tout le monde passe à autre chose. Mais l'application RH garde sa propre table `employees`, avec son propre `lastname`. Si personne ne lui dit que l'identité a changé, elle continuera d'éditer des documents au mauvais nom, sans erreur, sans alerte, jusqu'à ce que quelqu'un le remarque. + +Ce problème n'a rien d'exotique. Dès qu'un système de gestion des identités cohabite avec une application qui garde une copie de certains champs, vous avez deux sources pour la même information, et une seule des deux est la bonne. La question n'est pas de savoir si elles divergeront, mais comment vous le saurez, et combien de temps cela durera. + +## Trois façons de garder deux bases d'accord + +Interroger l'IAM à chaque besoin supprime la copie, donc le problème, mais couple la disponibilité de votre application à celle de l'IAM et ajoute un appel réseau à chaque lecture. Interroger périodiquement (le polling) garde la copie mais fixe la fraîcheur de la donnée à la période du job. Et partager la base est un contrat que plus personne ne peut faire évoluer. + +Le webhook prend le chemin inverse : c'est l'IAM qui prévient, au moment où la chose se produit. Votre application garde sa copie locale, rapide, et ne l'interroge jamais pour savoir si quelque chose a changé. + +Il y a une contrepartie, et tout ce qui suit en découle. Un webhook est une promesse : « je vous préviens ». Une promesse peut ne pas être tenue, parce que le réseau coupe, parce que votre application redémarre, parce qu'un déploiement a duré trois minutes de trop. Un webhook sans stratégie d'échec ne remplace pas le polling, il le rend invisible, ce qui est pire. Le travail d'un bon système de webhooks se joue donc presque entièrement sur le chemin d'échec. + +## Le cas simple : un nom qui change + +Dans FerrisKey, un webhook appartient à un realm, pointe vers un endpoint et s'abonne à des événements précis. Pour notre application RH : + +```json title="POST /realms/acme/webhooks" +{ + "name": "Synchronisation RH", + "description": "Garde la table employees alignée sur les identités", + "endpoint": "https://rh.acme.example/webhooks/ferriskey", + "headers": { "X-Webhook-Source": "ferriskey" }, + "subscribers": ["user.created", "user.updated", "user.deleted"] +} +``` + +Il existe 38 événements auxquels un webhook peut s'abonner, regroupés par ressource (utilisateurs, clients, rôles, realm, authentification…). La documentation précise qu'en 0.10.0 le serveur n'en émet que 33 : les cinq autres acceptent un abonnement, puis restent silencieux. Vérifiez donc la [liste des événements](https://docs.ferriskey.rs/en/modules/webhooks/triggers) avant de compter sur l'un d'eux. + +Quand l'administrateur modifie le profil d'Alice, FerrisKey envoie un POST dont le corps ressemble à ceci : + +```json title="user.updated (extrait)" +{ + "event": "user.updated", + "timestamp": "2026-03-17T14:30:00.123456+00:00", + "resource_id": "01914b3c-1234-7f5a-b456-000000000001", + "data": { + "id": "01914b3c-5678-7f5a-b456-000000000002", + "username": "alice", + "firstname": "Alice", + "lastname": "Martin", + "email": "alice@example.com", + "updated_at": "2026-03-17T14:30:00.101234Z" + } +} +``` + +Deux détails de ce format surprennent au premier contact, et la documentation les signale. D'abord, `resource_id` n'identifie pas l'utilisateur : pour presque tous les événements, c'est l'identifiant du realm. L'objet concerné se lit dans `data.id`. Ensuite, `data` change de forme selon l'événement : l'utilisateur complet pour `user.updated`, un simple identifiant pour `client.deleted`, `null` pour `auth.reset_password`. + +:::callout{variant="warning"} +`user.updated` est aussi émis quand un rôle est retiré à un utilisateur. Dans ce cas, `data` est le rôle, pas l'utilisateur. Un handler qui lit `data.firstname` sans vérifier ce qu'il reçoit écrasera des noms par `undefined`. Testez la présence de `data.username` avant de traiter un changement de profil. +::: + +Côté application, le handler tient en quelques lignes, à condition de choisir une écriture qui supporte les mauvais jours : + +```ts title="webhooks/ferriskey.ts" +const { event, data } = JSON.parse(rawBody.toString('utf8')) + +if (event === 'user.updated' && data.username) { + await db.query( + `INSERT INTO employees (iam_user_id, firstname, lastname, iam_updated_at) + VALUES ($1, $2, $3, $4) + ON CONFLICT (iam_user_id) DO UPDATE + SET firstname = EXCLUDED.firstname, + lastname = EXCLUDED.lastname, + iam_updated_at = EXCLUDED.iam_updated_at + WHERE employees.iam_updated_at < EXCLUDED.iam_updated_at`, + [data.id, data.firstname, data.lastname, data.updated_at], + ) +} + +res.writeHead(204).end() +``` + +La clause `WHERE employees.iam_updated_at < EXCLUDED.iam_updated_at` fait trois choses à la fois. Rejouer deux fois le même événement ne change rien. Un événement ancien qui arrive après un plus récent est ignoré. Et si la création d'Alice a été manquée, la première mise à jour crée la ligne (si votre table peut se contenter du profil pour exister). Vous n'avez pas écrit de logique de reprise, vous avez rendu l'écriture insensible à l'ordre et aux doublons. Pour la suite de l'article, c'est la propriété la plus utile que puisse avoir votre côté du contrat. + +Avant de toucher à `data`, ce handler devrait aussi vérifier la signature. FerrisKey signe chaque tentative avec un HMAC-SHA256 calculé sur `{timestamp}.{corps brut}`, avec le secret du webhook, et envoie le résultat dans `x-ferriskey-signature` (le détail et un exemple complet sont dans la [documentation du format](https://docs.ferriskey.rs/en/modules/webhooks/payload#verifying-the-signature)). Le timestamp fait partie de la chaîne signée : quelqu'un qui capture une requête ne peut pas la rejouer plus tard en changeant simplement l'en-tête. Un en-tête statique, comme le `Authorization` que l'on peut aussi configurer, prouve que l'appelant connaît un jeton, pas que le corps n'a pas été modifié. + +## Ce que FerrisKey vérifie avant d'appeler quoi que ce soit + +Un webhook est, du point de vue de l'IAM, une requête sortante vers une adresse choisie par un utilisateur. C'est exactement la forme d'une attaque SSRF, et la documentation en tire les conséquences : FerrisKey refuse d'enregistrer un endpoint qui n'est pas en `https`, qui contient des identifiants dans l'URL, ou qui est une adresse en boucle locale, privée ou link-local. Le nom d'hôte est revérifié à chaque résolution, au moment de la livraison. Pour le développement local, une variable d'environnement (`WEBHOOK_ALLOW_PRIVATE_ENDPOINTS=true`) lève la restriction, et elle ne doit jamais être active en production. + +Le reste du comportement de livraison est volontairement strict : FerrisKey attend 5 secondes pour se connecter et 10 secondes au total, considère tout `2xx` comme une livraison réussie, et ne suit pas les redirections. Un `301` de `http` vers `https` est donc enregistré comme un échec. Ce n'est pas un défaut, c'est ce qui rend le comportement prévisible : l'endpoint décide, par son code de statut, du sort de la livraison. + +## Quand l'endpoint répond mal : le retry automatique + +Toutes les erreurs ne se valent pas, et FerrisKey ne les traite pas de la même façon. Une panne de transport, une résolution DNS qui échoue, un `5xx`, un `408` ou un `429` sont réessayés. Une configuration invalide, une adresse refusée, ou n'importe quel autre statut HTTP (un `400`, un `401`, un `404`) ne le sont pas : la livraison passe directement en `failed`. Un `503` pendant votre déploiement sera retenté. Un `401` parce que votre endpoint a rejeté la signature ne le sera pas, ce qui est le comportement que l'on veut : réessayer ne rendra pas une signature valide. + +Les réessais suivent un backoff exponentiel avec jitter, gouverné par quatre valeurs : + +| Réglage | Valeur par défaut | Bornes | +|---|---|---| +| `max_attempts` | 5 | 1 à 20 | +| `base_delay_ms` | 500 ms | 100 ms à 60 s | +| `max_delay_ms` | 30 s | au moins `base_delay_ms` | +| `max_total_delay_ms` | 120 s | au moins `max_delay_ms` | + +L'attente avant chaque réessai est tirée au hasard entre 0 et un plafond qui double à chaque tentative. Avec les valeurs par défaut, les plafonds sont de 1, 2, 4 puis 8 secondes. Le jitter n'est pas décoratif : si cent livraisons échouent ensemble parce que votre application tombe, elles ne doivent pas toutes revenir à la même milliseconde quand elle remonte. Son prix est qu'un réessai peut arriver presque immédiatement. + +Ces valeurs se règlent champ par champ, à trois niveaux : le webhook lui-même, sinon le realm, sinon la valeur système. Un webhook peut donc relever uniquement `max_attempts` et hériter des délais. Une lecture du webhook renvoie `retry_policy` (ce qu'il surcharge) et `effective_retry_policy` (ce qui sera réellement appliqué), et c'est le second qu'il faut regarder quand on se demande ce que fera le worker. + +## Quand l'application métier est down + +C'est ici que la différence entre un système de webhooks et un `fetch` dans un `try/catch` se voit. Imaginons que l'application RH soit indisponible pendant une heure, un mardi matin, pour une migration qui se passe mal. Pendant ce temps, trois profils sont modifiés dans l'IAM. + +Ce qui se passe, avec les réglages par défaut : + +1. L'action de l'administrateur réussit. Elle n'attend pas l'endpoint et n'échoue pas s'il est injoignable, c'est dit explicitement dans la documentation. +2. FerrisKey a enregistré une livraison par webhook abonné avant de tenter l'envoi. La première tentative échoue, le réessai est planifié. +3. Cinq tentatives ou 120 secondes plus tard, selon ce qui arrive en premier, la livraison passe en `failed`. +4. FerrisKey enregistre un événement `webhook_delivery_exhausted` dans SeaWatch, avec l'identifiant de la livraison, le dernier code d'erreur et le nombre de tentatives. + +Autrement dit, avec la configuration par défaut, l'endpoint dispose d'environ deux minutes pour revenir. Une heure d'indisponibilité n'est pas rattrapée par les retries. Elle est rattrapée par ce qui vient après. + +On peut allonger la fenêtre, par webhook ou par realm : + +```json title="PUT /realms/acme/webhooks/{webhook_id} (extrait)" +{ + "retry_policy": { + "max_attempts": 20, + "max_delay_ms": 300000, + "max_total_delay_ms": 3600000 + } +} +``` + +Mais la limite à 20 tentatives plafonne ce que le réessai automatique peut absorber. Avec un délai maximal de 5 minutes, la somme des plafonds d'attente de ces 19 réessais est d'à peu près une heure. Comme chaque attente est tirée entre 0 et son plafond, la fenêtre réelle est, en moyenne, plutôt de l'ordre de la moitié. C'est mon calcul à partir des règles de la documentation, pas une mesure, mais l'ordre de grandeur suffit : le backoff est fait pour absorber une coupure, pas une panne. + +Pour une panne, il y a le rejeu manuel. L'événement `webhook_delivery_exhausted` est celui sur lequel brancher votre alerte : c'est la différence entre « une livraison a échoué puis s'est rétablie » et « cet endpoint est injoignable depuis assez longtemps pour que nous ayons renoncé ». Quand l'application est de retour : + +```text +POST /realms/acme/webhooks/{webhook_id}/deliveries/{delivery_id}/retry +``` + +La réponse est un `202`. Une livraison encore `pending` ou `delivering` répond `409` : il faut attendre qu'elle ait fini. Le rejeu renvoie le payload stocké, inchangé, signé à nouveau avec un nouveau timestamp. Ce n'est pas un instantané frais de la ressource : l'événement décrit le monde tel qu'il était, et le rejouer redit la même chose. Si Alice a changé de nom une deuxième fois entre-temps, l'ancien événement arrivera après le nouveau, dans l'ordre où vous rejouez. C'est exactement le cas que la clause `updated_at` du handler plus haut absorbe sans rien demander à personne. + +Quand quelque chose ne marche pas, l'ordre de lecture est simple. Le webhook est-il abonné à quelque chose (la console signale « sans événement » les webhooks qui ne le sont à rien) ? Existe-t-il une livraison pour cet événement, et si non, le nom du déclencheur est-il exact ? Que dit `last_status_code`, qui pointe vers votre endpoint, ou `last_error_code`, qui pointe vers le chemin pour l'atteindre ? Une fois corrigé, rejouez plutôt que d'attendre le prochain événement pour savoir si ça marche. + +## La file d'attente : ce qu'elle apporte, ce qu'elle coûte + +Le comportement ci-dessus repose sur une table `webhook_deliveries` dans PostgreSQL, introduite par la fonctionnalité de livraison persistée de FerrisKey ([#1340](https://github.com/ferriskey/ferriskey/pull/1340)). Ce qui suit vient de la lecture du code et non de la documentation, et les constantes citées peuvent évoluer. + +Quand un événement survient, FerrisKey trouve les webhooks abonnés et écrit une ligne `pending` par webhook, avec le payload complet en JSONB. La première tentative part presque aussitôt, depuis un canal mémoire de 1024 places, avec au plus 16 envois simultanés. Si ce canal est plein, la livraison attend simplement le passage du worker. Ce worker, lui, se réveille toutes les 5 secondes, réclame jusqu'à 8 livraisons échues avec `FOR UPDATE SKIP LOCKED` (donc plusieurs instances de FerrisKey ne se disputent pas la même ligne), et les traite. Une livraison réclamée porte un bail de 10 minutes : si l'instance qui la détient meurt, le bail expire, et la ligne est remise dans la file. Un nettoyage horaire supprime les livraisons terminées de plus de 30 jours. + +Ce que cela apporte : + +- Le découplage. L'action de l'administrateur ne dépend ni de la latence ni de la disponibilité de votre endpoint. +- La survie aux redémarrages. L'état d'une livraison est en base, pas dans la mémoire d'un processus. +- L'observabilité. Chaque tentative laisse une trace consultable (statut HTTP, code d'erreur, nombre de tentatives, prochain passage), et la console permet d'ouvrir le payload envoyé. +- Le rejeu. Il n'est possible que parce que le payload a été conservé. + +Ce que cela coûte, et c'est la partie à lire avant de bâtir dessus : + +- Au moins une fois, pas exactement une fois. Une livraison peut arriver en double : votre endpoint a traité la requête mais n'a pas répondu à temps, un bail a expiré, vous avez rejoué à la main. L'identifiant de la livraison, envoyé dans `x-ferriskey-delivery-id`, reste le même à chaque tentative et à chaque rejeu. C'est la clé de déduplication naturelle. `resource_id` et `timestamp` ne peuvent pas jouer ce rôle. +- Pas d'ordre garanti. Jitter, envois concurrents et réessais font qu'un événement ancien peut arriver après un plus récent. Rien dans ce que j'ai lu ne promet un ordre, et ce que le système fait est cohérent avec cette absence de promesse. Le moyen de s'en protéger est de l'écrire dans la donnée (un `updated_at` à comparer), pas de l'espérer de l'infrastructure. +- Une latence de réessai plus grossière qu'il n'y paraît. Un réessai planifié « dans une seconde » est repris par un worker qui passe toutes les 5 secondes. Le délai réel est donc plutôt celui du prochain passage. +- Une écriture qui n'est pas dans la même transaction que l'action. D'après le code, l'enregistrement de la livraison est une étape séparée, après la mise à jour de l'utilisateur. Si cette écriture échoue, FerrisKey le journalise (`failed to record a webhook delivery; the event is lost`) et l'action, elle, a réussi. Le cas est rare, mais il existe, et il pèse sur la section suivante. +- De la place en base. Une ligne par livraison et par webhook, avec le payload entier, pendant 30 jours. + +## Quand un webhook n'est pas le bon outil + +Je pense qu'un webhook, même bien fait, ne suffit pas pour une donnée dont l'exactitude est critique. Le cas d'une livraison perdue en amont, qui vient d'être décrit, est précisément celui qu'aucun mécanisme de retry ne peut réparer : l'événement n'a jamais été enregistré, donc il ne sera jamais rejoué. Ce que je recommande, c'est un job de réconciliation périodique, lent, qui compare votre table à l'API de l'IAM et corrige l'écart. Les webhooks assurent la fraîcheur, la réconciliation assure la correction. Chacun couvre le point faible de l'autre. + +Et si vous avez besoin d'un ordre strict, d'une livraison exactement une fois, ou d'un très gros volume d'événements, c'est un autre problème : un bus de messages avec des garanties d'ordre par clé répond mieux à cette question qu'un POST HTTP, quelle que soit la qualité des retries. + +## Pour aller plus loin + +La [documentation des webhooks de FerrisKey](https://docs.ferriskey.rs/en/modules/webhooks/overview) détaille le format des payloads, les codes d'erreur et la rotation du secret de signature. Sur ce dernier point, un conseil tiré de la doc : la rotation prend effet immédiatement, y compris pour les livraisons déjà en file. Si votre endpoint ne connaît pas encore le nouveau secret, il rejettera des requêtes que FerrisKey ne réessaiera pas. Faites tourner le secret quand vous avez déjà de quoi le déployer. + +Le code est sur [GitHub](https://github.com/ferriskey/ferriskey). diff --git a/apps/blog/src/content/posts/fr/what-is-iam-introduction-to-ferriskey.mdx b/apps/blog/src/content/posts/fr/what-is-iam-introduction-to-ferriskey.mdx index bff45f5..d6fa8c3 100644 --- a/apps/blog/src/content/posts/fr/what-is-iam-introduction-to-ferriskey.mdx +++ b/apps/blog/src/content/posts/fr/what-is-iam-introduction-to-ferriskey.mdx @@ -37,7 +37,7 @@ L'IAM est la réponse structurée à ce chaos. Un système IAM est le gardien centralisé de votre infrastructure. Il joue plusieurs rôles à la fois. -::::card-group{cols=3} +::::card-group{cols=2} :::card{label="Annuaire" icon="lucide:users"} Il sait qui existe. Chaque utilisateur humain, chaque application, chaque service machine a une identité unique dans le système. Un seul endroit, une seule source de vérité. ::: diff --git a/apps/website/src/components/website-navbar.tsx b/apps/website/src/components/website-navbar.tsx index e7cf16b..ebafcd5 100644 --- a/apps/website/src/components/website-navbar.tsx +++ b/apps/website/src/components/website-navbar.tsx @@ -2,8 +2,8 @@ import { LocaleSwitcher, getAppLinks } from '@explainer/ui' import { Icon } from '@iconify/react' import { useEffect, useRef, useState } from 'react' import { useTranslations } from '../i18n/utils' -import { modules } from '../lib/modules' import { moduleSprites } from '../lib/module-sprites' +import { modules } from '../lib/modules' import { PixelSprite } from './pixel/pixel-sprite' function getClientLocale(): string { @@ -134,7 +134,6 @@ export function WebsiteNavbar({ appUrlOverrides }: WebsiteNavbarProps) { const links: Link[] = [ { label: t('nav.roadmap'), href: 'https://github.com/orgs/ferriskey/projects/1', external: true }, { label: t('nav.releaseNotes'), href: '/release-notes' }, - { label: t('nav.getStarted'), href: docsHref }, ...(blogHref ? [{ label: 'Blog', href: blogHref }] : []), ] const externalProps = (link: Link) => (link.external ? { target: '_blank', rel: 'noopener noreferrer' } : {})