iso-explainer 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +110 -2
  3. package/README.pt-BR.md +88 -0
  4. package/bin/iso-explainer.mjs +75 -0
  5. package/package.json +37 -4
  6. package/skills/iso-explainer/SKILL.md +92 -0
  7. package/skills/iso-explainer/assets/fonts/OFL.txt +93 -0
  8. package/skills/iso-explainer/assets/fonts/jetbrains-mono-latin-400-normal.woff2 +0 -0
  9. package/skills/iso-explainer/assets/fonts/jetbrains-mono-latin-500-normal.woff2 +0 -0
  10. package/skills/iso-explainer/assets/fonts/jetbrains-mono-latin-600-normal.woff2 +0 -0
  11. package/skills/iso-explainer/assets/fonts/jetbrains-mono-latin-700-normal.woff2 +0 -0
  12. package/skills/iso-explainer/assets/fonts/jetbrains-mono-latin-800-normal.woff2 +0 -0
  13. package/skills/iso-explainer/assets/runtime/LUCIDE-LICENSE.txt +43 -0
  14. package/skills/iso-explainer/assets/runtime/app.js +687 -0
  15. package/skills/iso-explainer/assets/runtime/engine.js +1177 -0
  16. package/skills/iso-explainer/assets/runtime/icons.js +205 -0
  17. package/skills/iso-explainer/assets/runtime/styles.css +447 -0
  18. package/skills/iso-explainer/assets/runtime/template.html +29 -0
  19. package/skills/iso-explainer/assets/templates/starter.course.json +134 -0
  20. package/skills/iso-explainer/examples/arquitetura-pedidos.docs.json +1743 -0
  21. package/skills/iso-explainer/examples/engenharia-na-pratica.course.json +2074 -0
  22. package/skills/iso-explainer/examples/figures/token-bucket.html +98 -0
  23. package/skills/iso-explainer/examples/historia-pedidos.story.json +1872 -0
  24. package/skills/iso-explainer/references/architecture-mode.md +82 -0
  25. package/skills/iso-explainer/references/design-system.md +58 -0
  26. package/skills/iso-explainer/references/engine.md +64 -0
  27. package/skills/iso-explainer/references/entities.md +70 -0
  28. package/skills/iso-explainer/references/flows.md +67 -0
  29. package/skills/iso-explainer/references/inside.md +64 -0
  30. package/skills/iso-explainer/references/layout.md +122 -0
  31. package/skills/iso-explainer/references/schema.md +157 -0
  32. package/skills/iso-explainer/references/story-mode.md +72 -0
  33. package/skills/iso-explainer/references/storytelling.md +56 -0
  34. package/skills/iso-explainer/scripts/build.mjs +80 -0
  35. package/skills/iso-explainer/scripts/harvest.mjs +240 -0
  36. package/skills/iso-explainer/scripts/lib/resolve.mjs +251 -0
  37. package/skills/iso-explainer/scripts/snapshot.mjs +97 -0
  38. package/skills/iso-explainer/scripts/validate.mjs +41 -0
@@ -0,0 +1,2074 @@
1
+ {
2
+ "meta": {
3
+ "id": "engenharia-na-pratica",
4
+ "title": "Engenharia na Prática",
5
+ "description": "Um curso interativo: cada ideia é um diagrama animado que você percorre passo a passo — escala, cache, filas, retries e idempotência.",
6
+ "locale": "pt-BR",
7
+ "currency": "USD",
8
+ "brand": {
9
+ "name": "Engenharia na Prática",
10
+ "highlight": "Prática"
11
+ },
12
+ "tabs": [
13
+ {
14
+ "label": "Aprender",
15
+ "icon": "book-open",
16
+ "active": true
17
+ },
18
+ {
19
+ "label": "Exercícios",
20
+ "icon": "flask-conical"
21
+ },
22
+ {
23
+ "label": "Resumos",
24
+ "icon": "layout-grid"
25
+ }
26
+ ]
27
+ },
28
+ "metricDefs": {
29
+ "ack": {
30
+ "label": "Resposta ao webhook",
31
+ "icon": "timer",
32
+ "format": "ms",
33
+ "good": 500,
34
+ "warn": 3000
35
+ },
36
+ "dups": {
37
+ "label": "Duplicadas",
38
+ "icon": "copy",
39
+ "format": "number",
40
+ "good": 0,
41
+ "warn": 5
42
+ },
43
+ "lost": {
44
+ "label": "Perdidas",
45
+ "icon": "alert",
46
+ "format": "percent",
47
+ "good": 0,
48
+ "warn": 1
49
+ },
50
+ "depth": {
51
+ "label": "Na fila",
52
+ "icon": "inbox",
53
+ "format": "number",
54
+ "good": 50,
55
+ "warn": 500
56
+ }
57
+ },
58
+ "glossary": {
59
+ "load balancer": "Componente que recebe todas as requisições e as distribui entre vários servidores iguais.",
60
+ "health check": "Requisição periódica do balanceador para saber se um servidor ainda responde.",
61
+ "cache-aside": "Padrão em que a aplicação consulta o cache primeiro e, num miss, lê do banco e grava no cache.",
62
+ "idempotente": "Operação que pode ser executada várias vezes com o mesmo resultado de executá-la uma vez.",
63
+ "DLQ": "Dead-letter queue: fila separada para mensagens que falharam vezes demais."
64
+ },
65
+ "modules": [
66
+ {
67
+ "id": "escala",
68
+ "title": "Fundamentos de escala",
69
+ "color": "green",
70
+ "lessons": [
71
+ "escalando-api"
72
+ ]
73
+ },
74
+ {
75
+ "id": "fluxos",
76
+ "title": "Fluxos de trabalho",
77
+ "color": "orange",
78
+ "lessons": [
79
+ "webhook-fila",
80
+ {
81
+ "id": "pipeline-cicd",
82
+ "title": "Pipeline de CI/CD"
83
+ }
84
+ ]
85
+ }
86
+ ],
87
+ "lessons": [
88
+ {
89
+ "id": "escalando-api",
90
+ "title": "Escalando uma API",
91
+ "summary": "De um servidor sobrecarregado a uma arquitetura que aguenta o triplo do tráfego — uma peça por vez.",
92
+ "minutes": 9,
93
+ "level": "beginner",
94
+ "metrics": [
95
+ "latency",
96
+ "throughput",
97
+ "errors",
98
+ "cost",
99
+ "hitRate"
100
+ ],
101
+ "legend": [
102
+ "req",
103
+ "res",
104
+ "write",
105
+ "fail"
106
+ ],
107
+ "goals": [
108
+ {
109
+ "text": "Por que um servidor só tem um teto",
110
+ "step": 1
111
+ },
112
+ {
113
+ "text": "Escala vertical e seus limites",
114
+ "step": 3
115
+ },
116
+ {
117
+ "text": "Balanceador + servidores sem estado",
118
+ "step": 4
119
+ },
120
+ {
121
+ "text": "Cache-aside para aliviar o banco",
122
+ "step": 6
123
+ },
124
+ {
125
+ "text": "Réplicas de leitura",
126
+ "step": 7
127
+ }
128
+ ],
129
+ "defs": {
130
+ "entities": {
131
+ "users": {
132
+ "type": "crowd",
133
+ "x0": -19,
134
+ "x1": -10,
135
+ "y0": 1.5,
136
+ "y1": 10.5,
137
+ "count": 5,
138
+ "label": "usuários"
139
+ },
140
+ "api": {
141
+ "type": "block",
142
+ "x": 6,
143
+ "y": 6,
144
+ "color": "blue",
145
+ "glyph": "server",
146
+ "label": [
147
+ "API"
148
+ ],
149
+ "sub": "2 vCPU",
150
+ "cpu": 0.35
151
+ },
152
+ "db": {
153
+ "type": "db",
154
+ "x": 17,
155
+ "y": 6,
156
+ "label": "postgres",
157
+ "cpu": 0.3
158
+ },
159
+ "lb": {
160
+ "type": "block",
161
+ "x": -4,
162
+ "y": 6,
163
+ "color": "orange",
164
+ "glyph": "balancer",
165
+ "label": [
166
+ "Load",
167
+ "Balancer"
168
+ ],
169
+ "pills": "below",
170
+ "inside": "lb-rodizio"
171
+ },
172
+ "apiA": {
173
+ "type": "block",
174
+ "x": 6,
175
+ "y": -2,
176
+ "s": 3.6,
177
+ "color": "blue",
178
+ "glyph": "server",
179
+ "label": [
180
+ "API 1"
181
+ ],
182
+ "cpu": 0.4
183
+ },
184
+ "apiB": {
185
+ "type": "block",
186
+ "x": 6,
187
+ "y": 6,
188
+ "s": 3.6,
189
+ "color": "blue",
190
+ "glyph": "server",
191
+ "label": [
192
+ "API 2"
193
+ ],
194
+ "cpu": 0.4
195
+ },
196
+ "apiC": {
197
+ "type": "block",
198
+ "x": 6,
199
+ "y": 14,
200
+ "s": 3.6,
201
+ "color": "blue",
202
+ "glyph": "server",
203
+ "label": [
204
+ "API 3"
205
+ ],
206
+ "cpu": 0.4
207
+ },
208
+ "tier": {
209
+ "type": "zone",
210
+ "x0": 3,
211
+ "x1": 9,
212
+ "y0": -6.5,
213
+ "y1": 17.5,
214
+ "label": "camada de API"
215
+ },
216
+ "cache": {
217
+ "type": "block",
218
+ "x": 17,
219
+ "y": -5,
220
+ "s": 3.4,
221
+ "h": 38,
222
+ "color": "yellow",
223
+ "glyph": "zap",
224
+ "label": [
225
+ "Redis"
226
+ ],
227
+ "sub": "cache",
228
+ "inside": "cache-aside"
229
+ },
230
+ "replica": {
231
+ "type": "db",
232
+ "x": 17,
233
+ "y": 16,
234
+ "color": "teal",
235
+ "label": "réplica"
236
+ }
237
+ },
238
+ "links": {
239
+ "u": {
240
+ "from": "users",
241
+ "to": "api",
242
+ "label": "HTTPS"
243
+ },
244
+ "q": {
245
+ "from": "api",
246
+ "to": "db",
247
+ "label": "SQL",
248
+ "heads": "both"
249
+ },
250
+ "ul": {
251
+ "from": "users",
252
+ "to": "lb",
253
+ "label": "HTTPS"
254
+ },
255
+ "a": {
256
+ "from": "lb",
257
+ "to": "apiA",
258
+ "pts": [
259
+ "lb",
260
+ [
261
+ 1,
262
+ 6
263
+ ],
264
+ [
265
+ 1,
266
+ -2
267
+ ],
268
+ "apiA"
269
+ ]
270
+ },
271
+ "b": {
272
+ "from": "lb",
273
+ "to": "apiB"
274
+ },
275
+ "c": {
276
+ "from": "lb",
277
+ "to": "apiC",
278
+ "pts": [
279
+ "lb",
280
+ [
281
+ 1,
282
+ 6
283
+ ],
284
+ [
285
+ 1,
286
+ 14
287
+ ],
288
+ "apiC"
289
+ ]
290
+ },
291
+ "tq": {
292
+ "from": "tier",
293
+ "to": "db",
294
+ "label": "SQL",
295
+ "heads": "both"
296
+ }
297
+ }
298
+ },
299
+ "steps": [
300
+ {
301
+ "short": "Um servidor",
302
+ "chapter": "problem",
303
+ "title": "Uma API, um servidor",
304
+ "body": [
305
+ "Todo produto começa assim: os usuários chamam uma <span class=\"term\">API</span> que roda num único servidor, e ela conversa com um banco <code>postgres</code>.",
306
+ "Com 300 requisições por segundo tudo vai bem: respostas em ~120 ms e a CPU folgada."
307
+ ],
308
+ "callout": {
309
+ "kind": "insight",
310
+ "text": "Antes de escalar, saiba onde está o gargalo. Aqui ainda não há nenhum."
311
+ },
312
+ "metrics": {
313
+ "latency": 120,
314
+ "throughput": 300,
315
+ "errors": 0.1,
316
+ "cost": 120
317
+ },
318
+ "scene": {
319
+ "entities": {
320
+ "users": "users",
321
+ "api": "api",
322
+ "db": "db"
323
+ },
324
+ "links": {
325
+ "u": "u",
326
+ "q": "q"
327
+ },
328
+ "flows": [
329
+ {
330
+ "route": [
331
+ "u",
332
+ "q"
333
+ ],
334
+ "bounce": true,
335
+ "rate": 1.4
336
+ }
337
+ ]
338
+ }
339
+ },
340
+ {
341
+ "short": "Pico",
342
+ "chapter": "problem",
343
+ "title": "O tráfego triplicou",
344
+ "body": [
345
+ "Uma campanha deu certo e chegam <b>900 req/s</b>. O servidor só dá conta de ~320: as requisições fazem fila na CPU, a latência explode e parte delas estoura o timeout.",
346
+ "Observe os pacotes ficando lentos e alguns falhando antes de chegar."
347
+ ],
348
+ "callout": {
349
+ "kind": "warn",
350
+ "text": "Latência alta e erros ao mesmo tempo quase sempre indicam saturação, não bug."
351
+ },
352
+ "changes": [
353
+ "~ Tráfego 300 → 900 req/s",
354
+ "~ CPU 35% → 97%",
355
+ "+ Timeouts: ~6% das requisições"
356
+ ],
357
+ "metrics": {
358
+ "latency": 1400,
359
+ "throughput": 320,
360
+ "errors": 6
361
+ },
362
+ "scene": {
363
+ "entities": {
364
+ "users": {
365
+ "count": 18
366
+ },
367
+ "api": {
368
+ "cpu": 0.97,
369
+ "heat": 1,
370
+ "tags": [
371
+ {
372
+ "text": "fila na CPU",
373
+ "tone": "red",
374
+ "icon": "alert"
375
+ }
376
+ ]
377
+ }
378
+ },
379
+ "links": {
380
+ "u": {
381
+ "fail": 0.25
382
+ }
383
+ },
384
+ "flows": [
385
+ {
386
+ "route": [
387
+ "u",
388
+ "q"
389
+ ],
390
+ "bounce": true,
391
+ "rate": 4.5,
392
+ "kind": "slow"
393
+ }
394
+ ]
395
+ }
396
+ },
397
+ {
398
+ "short": "Vertical",
399
+ "chapter": "technique",
400
+ "title": "Escalar verticalmente",
401
+ "body": [
402
+ "O caminho mais rápido: trocar a máquina por uma maior. De 2 para 16 vCPUs, sem mudar uma linha de código.",
403
+ "A latência volta ao normal e os erros somem. Mas a conta mensal foi de US$ 120 para US$ 960."
404
+ ],
405
+ "callout": {
406
+ "kind": "tip",
407
+ "text": "Escalar verticalmente é ótimo como primeiro passo: simples, reversível e sem mudanças na aplicação."
408
+ },
409
+ "changes": [
410
+ "~ API: 2 → 16 vCPU",
411
+ "~ CPU 97% → 55%",
412
+ "~ Custo 8×"
413
+ ],
414
+ "metrics": {
415
+ "latency": 180,
416
+ "throughput": 900,
417
+ "errors": 0.2,
418
+ "cost": 960
419
+ },
420
+ "scene": {
421
+ "entities": {
422
+ "api": {
423
+ "s": 5,
424
+ "h": 56,
425
+ "sub": "16 vCPU",
426
+ "cpu": 0.55,
427
+ "heat": 0,
428
+ "tags": [
429
+ {
430
+ "text": "2 → 16 vCPU",
431
+ "tone": "amber",
432
+ "icon": "cpu"
433
+ }
434
+ ]
435
+ }
436
+ },
437
+ "links": {
438
+ "u": {
439
+ "fail": 0
440
+ }
441
+ },
442
+ "flows": [
443
+ {
444
+ "route": [
445
+ "u",
446
+ "q"
447
+ ],
448
+ "bounce": true,
449
+ "rate": 4
450
+ }
451
+ ]
452
+ }
453
+ },
454
+ {
455
+ "short": "O teto",
456
+ "chapter": "tradeoff",
457
+ "title": "O teto (e o risco) do vertical",
458
+ "body": [
459
+ "Duas coisas não mudaram. O preço cresce mais rápido que a capacidade, e existe um maior servidor que dá para comprar.",
460
+ "Pior: continua sendo <b>uma</b> máquina. Numa atualização do sistema operacional, ela reinicia — e a API inteira cai junto."
461
+ ],
462
+ "callout": {
463
+ "kind": "warn",
464
+ "text": "Um único servidor é um ponto único de falha, não importa o tamanho."
465
+ },
466
+ "changes": [
467
+ "- API fora do ar (reboot)",
468
+ "~ Erros: 100%"
469
+ ],
470
+ "metrics": {
471
+ "latency": null,
472
+ "throughput": 0,
473
+ "errors": 100
474
+ },
475
+ "scene": {
476
+ "entities": {
477
+ "api": {
478
+ "dead": 1,
479
+ "cpu": -1,
480
+ "tags": [
481
+ {
482
+ "text": "DOWN",
483
+ "tone": "red",
484
+ "icon": "alert"
485
+ }
486
+ ]
487
+ },
488
+ "card": {
489
+ "type": "panel",
490
+ "title": "Escala vertical",
491
+ "icon": "scale",
492
+ "lines": [
493
+ "**custo** cresce mais rápido que a capacidade",
494
+ "! um servidor = um ponto único de falha"
495
+ ],
496
+ "bars": [
497
+ {
498
+ "label": "2 vCPU",
499
+ "value": 0.08,
500
+ "note": "$120",
501
+ "tone": "green"
502
+ },
503
+ {
504
+ "label": "16 vCPU",
505
+ "value": 0.3,
506
+ "note": "$960",
507
+ "tone": "amber"
508
+ },
509
+ {
510
+ "label": "64 vCPU",
511
+ "value": 1,
512
+ "note": "$4.8k",
513
+ "tone": "red"
514
+ }
515
+ ]
516
+ }
517
+ },
518
+ "links": {
519
+ "u": {
520
+ "fail": 1
521
+ },
522
+ "q": {
523
+ "style": "off"
524
+ }
525
+ },
526
+ "flows": [
527
+ {
528
+ "route": [
529
+ "u"
530
+ ],
531
+ "rate": 3
532
+ }
533
+ ]
534
+ }
535
+ },
536
+ {
537
+ "short": "Balancear",
538
+ "chapter": "technique",
539
+ "title": "Vários servidores atrás de um balanceador",
540
+ "body": [
541
+ "Em vez de uma máquina enorme, três pequenas e iguais. Um <span class=\"term\">load balancer</span> recebe todo o tráfego e reparte entre elas em rodízio.",
542
+ "Isso só funciona porque a API é <b>sem estado</b>: a sessão e os dados ficam no banco, então qualquer servidor atende qualquer usuário."
543
+ ],
544
+ "callout": {
545
+ "kind": "insight",
546
+ "text": "Escala horizontal troca uma máquina grande por várias comuns — e ganha tolerância a falhas de brinde."
547
+ },
548
+ "changes": [
549
+ "+ Load balancer",
550
+ "+ 3 servidores de 4 vCPU",
551
+ "~ Custo US$ 960 → US$ 400"
552
+ ],
553
+ "metrics": {
554
+ "latency": 140,
555
+ "throughput": 900,
556
+ "errors": 0.1,
557
+ "cost": 400
558
+ },
559
+ "scene": {
560
+ "entities": {
561
+ "api": null,
562
+ "card": null,
563
+ "users": {
564
+ "count": 18
565
+ },
566
+ "lb": "lb",
567
+ "tier": "tier",
568
+ "apiA": "apiA",
569
+ "apiB": "apiB",
570
+ "apiC": "apiC"
571
+ },
572
+ "links": {
573
+ "u": null,
574
+ "q": null,
575
+ "ul": "ul",
576
+ "a": "a",
577
+ "b": "b",
578
+ "c": "c",
579
+ "tq": "tq"
580
+ },
581
+ "flows": [
582
+ {
583
+ "route": [
584
+ "ul",
585
+ [
586
+ "a",
587
+ "b",
588
+ "c"
589
+ ],
590
+ "tq"
591
+ ],
592
+ "bounce": true,
593
+ "rate": 4.2
594
+ }
595
+ ]
596
+ }
597
+ },
598
+ {
599
+ "short": "Falha",
600
+ "chapter": "solution",
601
+ "title": "Um servidor cai — e ninguém percebe",
602
+ "body": [
603
+ "A API 2 trava. O balanceador faz um <span class=\"term\">health check</span> a cada poucos segundos; quando ela para de responder, sai do rodízio.",
604
+ "As outras duas absorvem o tráfego. Os usuários nem notam."
605
+ ],
606
+ "callout": {
607
+ "kind": "tip",
608
+ "text": "Dimensione com folga (N+1): se um servidor cair, os restantes precisam aguentar o pico."
609
+ },
610
+ "changes": [
611
+ "- API 2 fora do rodízio",
612
+ "~ CPU das outras 40% → 62%"
613
+ ],
614
+ "metrics": {
615
+ "latency": 165,
616
+ "errors": 0.3
617
+ },
618
+ "scene": {
619
+ "entities": {
620
+ "apiB": {
621
+ "dead": 1,
622
+ "cpu": -1,
623
+ "tags": [
624
+ {
625
+ "text": "DOWN",
626
+ "tone": "red",
627
+ "icon": "alert"
628
+ }
629
+ ]
630
+ },
631
+ "apiA": {
632
+ "cpu": 0.62
633
+ },
634
+ "apiC": {
635
+ "cpu": 0.62
636
+ }
637
+ },
638
+ "links": {
639
+ "b": {
640
+ "style": "off"
641
+ }
642
+ },
643
+ "flows": [
644
+ {
645
+ "route": [
646
+ "ul",
647
+ [
648
+ "a",
649
+ "c"
650
+ ],
651
+ "tq"
652
+ ],
653
+ "bounce": true,
654
+ "rate": 4.2
655
+ }
656
+ ]
657
+ }
658
+ },
659
+ {
660
+ "short": "Cache",
661
+ "chapter": "technique",
662
+ "title": "Um cache na frente do banco",
663
+ "body": [
664
+ "Com a API escalada, o gargalo migrou para o banco: 85% das leituras pedem os mesmos produtos.",
665
+ "No padrão <span class=\"term\">cache-aside</span>, a API consulta o Redis primeiro. Num acerto, responde em ~2 ms; num erro, lê do banco e guarda o resultado."
666
+ ],
667
+ "callout": {
668
+ "kind": "insight",
669
+ "text": "Cache é uma troca: velocidade em troca de dados possivelmente desatualizados. Defina um TTL."
670
+ },
671
+ "changes": [
672
+ "+ Redis (cache-aside)",
673
+ "~ API 2 de volta",
674
+ "~ Leituras no banco −85%"
675
+ ],
676
+ "metrics": {
677
+ "latency": 60,
678
+ "errors": 0.1,
679
+ "cost": 450,
680
+ "hitRate": 85
681
+ },
682
+ "scene": {
683
+ "entities": {
684
+ "apiB": {
685
+ "dead": 0,
686
+ "cpu": -1,
687
+ "tags": []
688
+ },
689
+ "apiA": {
690
+ "cpu": -1
691
+ },
692
+ "apiC": {
693
+ "cpu": -1
694
+ },
695
+ "db": {
696
+ "cpu": -1
697
+ },
698
+ "cache": "cache"
699
+ },
700
+ "links": {
701
+ "b": {
702
+ "style": "solid"
703
+ },
704
+ "k": {
705
+ "from": "tier",
706
+ "to": "cache",
707
+ "via": "y",
708
+ "label": "GET produto",
709
+ "heads": "both"
710
+ }
711
+ },
712
+ "flows": [
713
+ {
714
+ "routes": [
715
+ {
716
+ "path": [
717
+ "ul",
718
+ "a",
719
+ "k"
720
+ ],
721
+ "weight": 6
722
+ },
723
+ {
724
+ "path": [
725
+ "ul",
726
+ "b",
727
+ "k"
728
+ ],
729
+ "weight": 6
730
+ },
731
+ {
732
+ "path": [
733
+ "ul",
734
+ "c",
735
+ "k"
736
+ ],
737
+ "weight": 6
738
+ },
739
+ {
740
+ "path": [
741
+ "ul",
742
+ "b",
743
+ "tq"
744
+ ],
745
+ "weight": 3
746
+ }
747
+ ],
748
+ "pick": "weights",
749
+ "bounce": true,
750
+ "rate": 4.2
751
+ }
752
+ ]
753
+ }
754
+ },
755
+ {
756
+ "short": "Réplica",
757
+ "chapter": "solution",
758
+ "title": "Réplicas para as leituras",
759
+ "body": [
760
+ "As escritas continuam indo para o banco principal, que copia cada mudança para uma <b>réplica</b>. As leituras que escapam do cache vão para ela.",
761
+ "O principal fica livre para escrever; se ele cair, a réplica pode ser promovida."
762
+ ],
763
+ "callout": {
764
+ "kind": "warn",
765
+ "text": "A réplica fica alguns milissegundos atrás. Logo após uma escrita, leia do principal."
766
+ },
767
+ "changes": [
768
+ "+ Réplica de leitura",
769
+ "~ Escritas → principal, leituras → réplica"
770
+ ],
771
+ "metrics": {
772
+ "latency": 55,
773
+ "cost": 520
774
+ },
775
+ "scene": {
776
+ "entities": {
777
+ "replica": "replica",
778
+ "db": {
779
+ "label": "principal"
780
+ }
781
+ },
782
+ "links": {
783
+ "tq": {
784
+ "label": "escritas"
785
+ },
786
+ "r": {
787
+ "from": "tier",
788
+ "to": "replica",
789
+ "via": "y",
790
+ "label": "leituras",
791
+ "heads": "both"
792
+ },
793
+ "rep": {
794
+ "from": "db",
795
+ "to": "replica",
796
+ "style": "dashed",
797
+ "label": "replicação"
798
+ }
799
+ },
800
+ "flows": [
801
+ {
802
+ "routes": [
803
+ {
804
+ "path": [
805
+ "ul",
806
+ "a",
807
+ "k"
808
+ ]
809
+ },
810
+ {
811
+ "path": [
812
+ "ul",
813
+ "b",
814
+ "k"
815
+ ]
816
+ },
817
+ {
818
+ "path": [
819
+ "ul",
820
+ "c",
821
+ "k"
822
+ ]
823
+ },
824
+ {
825
+ "path": [
826
+ "ul",
827
+ "b",
828
+ "r"
829
+ ]
830
+ }
831
+ ],
832
+ "bounce": true,
833
+ "rate": 3.6
834
+ },
835
+ {
836
+ "route": [
837
+ "ul",
838
+ "a",
839
+ "tq"
840
+ ],
841
+ "kind": "write",
842
+ "rate": 0.8
843
+ },
844
+ {
845
+ "route": [
846
+ "rep"
847
+ ],
848
+ "kind": "write",
849
+ "rate": 0.8
850
+ }
851
+ ]
852
+ }
853
+ },
854
+ {
855
+ "short": "Resumo",
856
+ "chapter": "recap",
857
+ "title": "O que construímos",
858
+ "body": [
859
+ "Saímos de um servidor saturado para uma arquitetura em camadas, cada peça resolvendo um gargalo medido.",
860
+ "<ul><li>vertical primeiro, enquanto for barato</li><li>horizontal + balanceador para capacidade e falhas</li><li>cache para leituras repetidas</li><li>réplicas para o resto das leituras</li></ul>"
861
+ ],
862
+ "scene": {
863
+ "entities": {
864
+ "card": {
865
+ "type": "panel",
866
+ "title": "Onde foi o tempo",
867
+ "icon": "timer",
868
+ "lines": [
869
+ "servidor saturado 1400 ms",
870
+ "16 vCPU 180 ms",
871
+ "3 APIs + balanceador 140 ms",
872
+ "= + cache + réplica 55 ms",
873
+ "# custo final: US$ 520/mês"
874
+ ]
875
+ }
876
+ }
877
+ }
878
+ }
879
+ ]
880
+ },
881
+ {
882
+ "id": "webhook-fila",
883
+ "title": "Do webhook ao worker",
884
+ "summary": "Mensagens chegam por webhook e precisam de IA para responder. Como não perder nenhuma — nem responder duas vezes.",
885
+ "minutes": 10,
886
+ "level": "intermediate",
887
+ "metrics": [
888
+ "ack",
889
+ "depth",
890
+ "dups",
891
+ "lost"
892
+ ],
893
+ "legend": [
894
+ "msg",
895
+ "res",
896
+ "retry",
897
+ "poison",
898
+ "fail"
899
+ ],
900
+ "goals": [
901
+ {
902
+ "text": "Por que processar dentro do webhook dá errado",
903
+ "step": 0
904
+ },
905
+ {
906
+ "text": "Confirmar rápido e enfileirar",
907
+ "step": 1
908
+ },
909
+ {
910
+ "text": "Vários workers consumindo a mesma fila",
911
+ "step": 2
912
+ },
913
+ {
914
+ "text": "Reentrega e idempotência",
915
+ "step": 4
916
+ },
917
+ {
918
+ "text": "Dead-letter queue para mensagens envenenadas",
919
+ "step": 5
920
+ }
921
+ ],
922
+ "defs": {
923
+ "entities": {
924
+ "phone": {
925
+ "type": "device",
926
+ "form": "phone",
927
+ "x": -12,
928
+ "y": 0,
929
+ "label": "cliente"
930
+ },
931
+ "prov": {
932
+ "type": "block",
933
+ "x": -3,
934
+ "y": 0,
935
+ "s": 3.6,
936
+ "color": "rose",
937
+ "glyph": "message-circle",
938
+ "label": [
939
+ "Provedor"
940
+ ],
941
+ "sub": "WhatsApp API"
942
+ },
943
+ "hook": {
944
+ "type": "block",
945
+ "x": 6,
946
+ "y": 0,
947
+ "s": 3.6,
948
+ "color": "blue",
949
+ "glyph": "webhook",
950
+ "label": [
951
+ "Webhook"
952
+ ],
953
+ "inside": "webhook-rapido"
954
+ },
955
+ "ai": {
956
+ "type": "block",
957
+ "x": 6,
958
+ "y": 27,
959
+ "s": 3.6,
960
+ "color": "gray",
961
+ "glyph": "brain",
962
+ "label": [
963
+ "LLM"
964
+ ],
965
+ "sub": "≈ 6 s"
966
+ },
967
+ "broker": {
968
+ "type": "block",
969
+ "x": 6,
970
+ "y": 9,
971
+ "s": 3.4,
972
+ "h": 34,
973
+ "color": "yellow",
974
+ "glyph": "inbox",
975
+ "label": [
976
+ "Fila"
977
+ ]
978
+ },
979
+ "w1": {
980
+ "type": "block",
981
+ "x": -1,
982
+ "y": 18,
983
+ "s": 3.2,
984
+ "h": 38,
985
+ "color": "purple",
986
+ "glyph": "bot",
987
+ "label": [
988
+ "worker"
989
+ ]
990
+ },
991
+ "w2": {
992
+ "type": "block",
993
+ "x": 6,
994
+ "y": 18,
995
+ "s": 3.2,
996
+ "h": 38,
997
+ "color": "purple",
998
+ "glyph": "bot",
999
+ "label": [
1000
+ "worker"
1001
+ ]
1002
+ },
1003
+ "w3": {
1004
+ "type": "block",
1005
+ "x": 13,
1006
+ "y": 18,
1007
+ "s": 3.2,
1008
+ "h": 38,
1009
+ "color": "purple",
1010
+ "glyph": "bot",
1011
+ "label": [
1012
+ "worker"
1013
+ ]
1014
+ },
1015
+ "keys": {
1016
+ "type": "db",
1017
+ "x": 21,
1018
+ "y": 14,
1019
+ "r": 1.5,
1020
+ "h": 32,
1021
+ "color": "green",
1022
+ "glyph": "key-round",
1023
+ "label": "processadas"
1024
+ },
1025
+ "dlq": {
1026
+ "type": "block",
1027
+ "x": 16,
1028
+ "y": 9,
1029
+ "s": 3.2,
1030
+ "h": 34,
1031
+ "color": "rose",
1032
+ "glyph": "archive",
1033
+ "label": [
1034
+ "DLQ"
1035
+ ]
1036
+ }
1037
+ },
1038
+ "links": {
1039
+ "p": {
1040
+ "from": "phone",
1041
+ "to": "prov"
1042
+ },
1043
+ "h": {
1044
+ "from": "prov",
1045
+ "to": "hook",
1046
+ "label": "POST /webhook"
1047
+ },
1048
+ "x": {
1049
+ "from": "hook",
1050
+ "to": "ai",
1051
+ "label": "gera resposta"
1052
+ },
1053
+ "e": {
1054
+ "from": "hook",
1055
+ "to": "broker",
1056
+ "label": "enfileira"
1057
+ },
1058
+ "c1": {
1059
+ "from": "broker",
1060
+ "to": "w1",
1061
+ "pts": [
1062
+ "broker",
1063
+ [
1064
+ 6,
1065
+ 13.5
1066
+ ],
1067
+ [
1068
+ -1,
1069
+ 13.5
1070
+ ],
1071
+ "w1"
1072
+ ]
1073
+ },
1074
+ "c2": {
1075
+ "from": "broker",
1076
+ "to": "w2"
1077
+ },
1078
+ "c3": {
1079
+ "from": "broker",
1080
+ "to": "w3",
1081
+ "pts": [
1082
+ "broker",
1083
+ [
1084
+ 6,
1085
+ 13.5
1086
+ ],
1087
+ [
1088
+ 13,
1089
+ 13.5
1090
+ ],
1091
+ "w3"
1092
+ ]
1093
+ },
1094
+ "a1": {
1095
+ "from": "w1",
1096
+ "to": "ai",
1097
+ "pts": [
1098
+ "w1",
1099
+ [
1100
+ -1,
1101
+ 22.5
1102
+ ],
1103
+ [
1104
+ 6,
1105
+ 22.5
1106
+ ],
1107
+ "ai"
1108
+ ]
1109
+ },
1110
+ "a2": {
1111
+ "from": "w2",
1112
+ "to": "ai"
1113
+ },
1114
+ "a3": {
1115
+ "from": "w3",
1116
+ "to": "ai",
1117
+ "pts": [
1118
+ "w3",
1119
+ [
1120
+ 13,
1121
+ 22.5
1122
+ ],
1123
+ [
1124
+ 6,
1125
+ 22.5
1126
+ ],
1127
+ "ai"
1128
+ ]
1129
+ }
1130
+ }
1131
+ },
1132
+ "steps": [
1133
+ {
1134
+ "short": "Tudo junto",
1135
+ "chapter": "problem",
1136
+ "title": "O webhook que faz tudo",
1137
+ "body": [
1138
+ "Um cliente manda mensagem; o provedor chama nosso <code>POST /webhook</code>. O handler chama o LLM, espera ~6 s pela resposta e só então devolve <code>200</code>.",
1139
+ "Mas o provedor desiste depois de 5 s e <b>reenvia</b> a mesma mensagem. Resultado: respostas duplicadas e, nos picos, mensagens perdidas."
1140
+ ],
1141
+ "callout": {
1142
+ "kind": "warn",
1143
+ "text": "Webhooks têm timeout curto. Trabalho lento dentro do handler é o erro mais comum nesses fluxos."
1144
+ },
1145
+ "metrics": {
1146
+ "ack": 6200,
1147
+ "depth": null,
1148
+ "dups": 14,
1149
+ "lost": 3
1150
+ },
1151
+ "scene": {
1152
+ "entities": {
1153
+ "phone": "phone",
1154
+ "prov": "prov",
1155
+ "hook": {
1156
+ "use": "hook",
1157
+ "cpu": 0.9,
1158
+ "heat": 0.8
1159
+ },
1160
+ "ai": "ai"
1161
+ },
1162
+ "links": {
1163
+ "p": "p",
1164
+ "h": {
1165
+ "use": "h",
1166
+ "fail": 0.3
1167
+ },
1168
+ "x": "x"
1169
+ },
1170
+ "flows": [
1171
+ {
1172
+ "route": [
1173
+ "p",
1174
+ "h",
1175
+ "x"
1176
+ ],
1177
+ "kind": "msg",
1178
+ "bounce": true,
1179
+ "hold": 1.2,
1180
+ "rate": 1.2
1181
+ }
1182
+ ]
1183
+ }
1184
+ },
1185
+ {
1186
+ "short": "Enfileirar",
1187
+ "chapter": "technique",
1188
+ "title": "Responda rápido, processe depois",
1189
+ "body": [
1190
+ "O webhook passa a fazer só duas coisas: grava a mensagem numa <b>fila</b> e devolve <code>200</code> em ~40 ms.",
1191
+ "O provedor fica satisfeito e não reenvia. O trabalho pesado agora pode esperar sua vez na fila."
1192
+ ],
1193
+ "callout": {
1194
+ "kind": "insight",
1195
+ "text": "Separe “aceitar o trabalho” de “fazer o trabalho”. A fila é o contrato entre os dois."
1196
+ },
1197
+ "changes": [
1198
+ "+ Fila",
1199
+ "~ Resposta ao webhook 6,2 s → 40 ms",
1200
+ "- Chamada direta ao LLM"
1201
+ ],
1202
+ "metrics": {
1203
+ "ack": 40,
1204
+ "depth": 120,
1205
+ "dups": 0,
1206
+ "lost": 0
1207
+ },
1208
+ "scene": {
1209
+ "entities": {
1210
+ "hook": {
1211
+ "cpu": 0.15,
1212
+ "heat": 0
1213
+ },
1214
+ "broker": "broker",
1215
+ "backlog": {
1216
+ "type": "queue",
1217
+ "x": 6,
1218
+ "y": 6.2,
1219
+ "n": 4,
1220
+ "dir": [
1221
+ 0,
1222
+ -0.9
1223
+ ],
1224
+ "gap": 0.9
1225
+ }
1226
+ },
1227
+ "links": {
1228
+ "x": null,
1229
+ "h": {
1230
+ "fail": 0
1231
+ },
1232
+ "e": "e"
1233
+ },
1234
+ "flows": [
1235
+ {
1236
+ "route": [
1237
+ "p",
1238
+ "h"
1239
+ ],
1240
+ "kind": "msg",
1241
+ "bounce": true,
1242
+ "rate": 1.6
1243
+ },
1244
+ {
1245
+ "route": [
1246
+ "e"
1247
+ ],
1248
+ "kind": "msg",
1249
+ "rate": 1.6
1250
+ }
1251
+ ]
1252
+ }
1253
+ },
1254
+ {
1255
+ "short": "Workers",
1256
+ "chapter": "technique",
1257
+ "title": "Workers consomem a fila",
1258
+ "body": [
1259
+ "Três workers puxam mensagens da fila, chamam o LLM e enviam a resposta. Cada mensagem vai para <b>um</b> só worker.",
1260
+ "Precisa de mais vazão? Suba mais workers — o webhook nem fica sabendo."
1261
+ ],
1262
+ "callout": {
1263
+ "kind": "tip",
1264
+ "text": "Consumidores concorrentes escalam horizontalmente, como servidores atrás de um balanceador."
1265
+ },
1266
+ "changes": [
1267
+ "+ 3 workers",
1268
+ "~ Fila 120 → 8 mensagens"
1269
+ ],
1270
+ "metrics": {
1271
+ "depth": 8
1272
+ },
1273
+ "scene": {
1274
+ "entities": {
1275
+ "backlog": {
1276
+ "n": 1.5
1277
+ },
1278
+ "w1": "w1",
1279
+ "w2": "w2",
1280
+ "w3": "w3",
1281
+ "ai": "ai"
1282
+ },
1283
+ "links": {
1284
+ "c1": "c1",
1285
+ "c2": "c2",
1286
+ "c3": "c3",
1287
+ "a1": "a1",
1288
+ "a2": "a2",
1289
+ "a3": "a3"
1290
+ },
1291
+ "flows": [
1292
+ {
1293
+ "route": [
1294
+ "p",
1295
+ "h"
1296
+ ],
1297
+ "kind": "msg",
1298
+ "bounce": true,
1299
+ "rate": 1.6
1300
+ },
1301
+ {
1302
+ "route": [
1303
+ "e"
1304
+ ],
1305
+ "kind": "msg",
1306
+ "rate": 1.6
1307
+ },
1308
+ {
1309
+ "routes": [
1310
+ {
1311
+ "path": [
1312
+ "c1",
1313
+ "a1"
1314
+ ]
1315
+ },
1316
+ {
1317
+ "path": [
1318
+ "c2",
1319
+ "a2"
1320
+ ]
1321
+ },
1322
+ {
1323
+ "path": [
1324
+ "c3",
1325
+ "a3"
1326
+ ]
1327
+ }
1328
+ ],
1329
+ "kind": "msg",
1330
+ "bounce": true,
1331
+ "hold": 0.6,
1332
+ "rate": 1.6
1333
+ }
1334
+ ]
1335
+ }
1336
+ },
1337
+ {
1338
+ "short": "Queda",
1339
+ "chapter": "problem",
1340
+ "title": "Um worker morre no meio",
1341
+ "body": [
1342
+ "O worker 2 é reiniciado enquanto esperava o LLM. A mensagem que ele segurava não foi confirmada (<code>ack</code>), então a fila a <b>reentrega</b> depois do prazo de visibilidade.",
1343
+ "Ninguém perde a mensagem — mas o LLM já tinha respondido uma vez. O cliente recebe a resposta em dobro."
1344
+ ],
1345
+ "callout": {
1346
+ "kind": "warn",
1347
+ "text": "Filas garantem entrega “pelo menos uma vez”. Duplicatas não são bug da fila: são parte do contrato."
1348
+ },
1349
+ "changes": [
1350
+ "- Worker 2 reiniciado",
1351
+ "+ Reentrega após timeout",
1352
+ "~ Duplicadas 0 → 3"
1353
+ ],
1354
+ "metrics": {
1355
+ "dups": 3,
1356
+ "depth": 14
1357
+ },
1358
+ "scene": {
1359
+ "entities": {
1360
+ "w2": {
1361
+ "dead": 1,
1362
+ "tags": [
1363
+ {
1364
+ "text": "reiniciando",
1365
+ "tone": "red",
1366
+ "icon": "rotate-ccw"
1367
+ }
1368
+ ]
1369
+ },
1370
+ "backlog": {
1371
+ "n": 2.5
1372
+ }
1373
+ },
1374
+ "links": {
1375
+ "c2": {
1376
+ "style": "off"
1377
+ },
1378
+ "a2": {
1379
+ "style": "off"
1380
+ }
1381
+ },
1382
+ "flows": [
1383
+ {
1384
+ "route": [
1385
+ "p",
1386
+ "h"
1387
+ ],
1388
+ "kind": "msg",
1389
+ "bounce": true,
1390
+ "rate": 1.6
1391
+ },
1392
+ {
1393
+ "route": [
1394
+ "e"
1395
+ ],
1396
+ "kind": "msg",
1397
+ "rate": 1.6
1398
+ },
1399
+ {
1400
+ "routes": [
1401
+ {
1402
+ "path": [
1403
+ "c1",
1404
+ "a1"
1405
+ ],
1406
+ "kind": "msg"
1407
+ },
1408
+ {
1409
+ "path": [
1410
+ "c3",
1411
+ "a3"
1412
+ ],
1413
+ "kind": "retry"
1414
+ }
1415
+ ],
1416
+ "bounce": true,
1417
+ "hold": 0.6,
1418
+ "rate": 1.6
1419
+ }
1420
+ ]
1421
+ }
1422
+ },
1423
+ {
1424
+ "short": "Idempotência",
1425
+ "chapter": "solution",
1426
+ "title": "Torne o processamento idempotente",
1427
+ "body": [
1428
+ "Cada mensagem traz um <code>message_id</code>. Antes de responder, o worker grava esse id numa tabela com chave única; se o id já existe, ele confirma e descarta.",
1429
+ "Agora reentregas são inofensivas: o processamento é <span class=\"term\">idempotente</span>."
1430
+ ],
1431
+ "callout": {
1432
+ "kind": "insight",
1433
+ "text": "Entrega pelo menos uma vez + processamento idempotente = efeito exatamente uma vez."
1434
+ },
1435
+ "changes": [
1436
+ "+ Tabela de ids processados",
1437
+ "~ Worker 2 de volta",
1438
+ "~ Duplicadas 3 → 0"
1439
+ ],
1440
+ "metrics": {
1441
+ "dups": 0,
1442
+ "depth": 6
1443
+ },
1444
+ "scene": {
1445
+ "entities": {
1446
+ "w2": {
1447
+ "dead": 0,
1448
+ "tags": []
1449
+ },
1450
+ "backlog": {
1451
+ "n": 1
1452
+ },
1453
+ "keys": "keys"
1454
+ },
1455
+ "links": {
1456
+ "c2": {
1457
+ "style": "solid"
1458
+ },
1459
+ "a2": {
1460
+ "style": "solid"
1461
+ },
1462
+ "kv": {
1463
+ "from": "w3",
1464
+ "to": "keys",
1465
+ "label": "INSERT id",
1466
+ "heads": "both"
1467
+ }
1468
+ },
1469
+ "flows": [
1470
+ {
1471
+ "route": [
1472
+ "p",
1473
+ "h"
1474
+ ],
1475
+ "kind": "msg",
1476
+ "bounce": true,
1477
+ "rate": 1.6
1478
+ },
1479
+ {
1480
+ "route": [
1481
+ "e"
1482
+ ],
1483
+ "kind": "msg",
1484
+ "rate": 1.6
1485
+ },
1486
+ {
1487
+ "routes": [
1488
+ {
1489
+ "path": [
1490
+ "c1",
1491
+ "a1"
1492
+ ]
1493
+ },
1494
+ {
1495
+ "path": [
1496
+ "c2",
1497
+ "a2"
1498
+ ]
1499
+ },
1500
+ {
1501
+ "path": [
1502
+ "c3",
1503
+ "kv"
1504
+ ]
1505
+ }
1506
+ ],
1507
+ "kind": "msg",
1508
+ "bounce": true,
1509
+ "hold": 0.4,
1510
+ "rate": 1.8
1511
+ }
1512
+ ]
1513
+ }
1514
+ },
1515
+ {
1516
+ "short": "DLQ",
1517
+ "chapter": "solution",
1518
+ "title": "Mensagens envenenadas vão para a DLQ",
1519
+ "body": [
1520
+ "Algumas mensagens nunca vão dar certo: um áudio corrompido, um payload malformado. Reprocessá-las para sempre trava a fila.",
1521
+ "Depois de 3 tentativas, a mensagem vai para uma <span class=\"term\">DLQ</span>. Ali ela fica para inspeção humana, sem bloquear as outras."
1522
+ ],
1523
+ "callout": {
1524
+ "kind": "tip",
1525
+ "text": "Monitore o tamanho da DLQ: ela crescendo é o alarme mais barato que existe."
1526
+ },
1527
+ "changes": [
1528
+ "+ Dead-letter queue",
1529
+ "+ Limite de 3 tentativas"
1530
+ ],
1531
+ "metrics": {
1532
+ "lost": 0
1533
+ },
1534
+ "scene": {
1535
+ "entities": {
1536
+ "dlq": "dlq"
1537
+ },
1538
+ "links": {
1539
+ "d": {
1540
+ "from": "broker",
1541
+ "to": "dlq",
1542
+ "label": "após 3 falhas",
1543
+ "labelTone": "red",
1544
+ "style": "dashed"
1545
+ }
1546
+ },
1547
+ "flows": [
1548
+ {
1549
+ "route": [
1550
+ "p",
1551
+ "h"
1552
+ ],
1553
+ "kind": "msg",
1554
+ "bounce": true,
1555
+ "rate": 1.6
1556
+ },
1557
+ {
1558
+ "route": [
1559
+ "e"
1560
+ ],
1561
+ "pattern": [
1562
+ "msg",
1563
+ "msg",
1564
+ "msg",
1565
+ "poison"
1566
+ ],
1567
+ "rate": 1.6
1568
+ },
1569
+ {
1570
+ "routes": [
1571
+ {
1572
+ "path": [
1573
+ "c1",
1574
+ "a1"
1575
+ ]
1576
+ },
1577
+ {
1578
+ "path": [
1579
+ "c2",
1580
+ "a2"
1581
+ ]
1582
+ },
1583
+ {
1584
+ "path": [
1585
+ "c3",
1586
+ "kv"
1587
+ ]
1588
+ }
1589
+ ],
1590
+ "kind": "msg",
1591
+ "bounce": true,
1592
+ "hold": 0.4,
1593
+ "rate": 1.6
1594
+ },
1595
+ {
1596
+ "route": [
1597
+ "d"
1598
+ ],
1599
+ "kind": "poison",
1600
+ "rate": 0.35
1601
+ }
1602
+ ]
1603
+ }
1604
+ },
1605
+ {
1606
+ "short": "Resumo",
1607
+ "chapter": "recap",
1608
+ "title": "O fluxo completo",
1609
+ "body": [
1610
+ "O webhook só aceita e enfileira. Workers idempotentes fazem o trabalho lento, e o que não tem conserto vai para a DLQ.",
1611
+ "O mesmo desenho serve para pagamentos, e-mails, transcodificação de vídeo — qualquer trabalho lento disparado por um evento."
1612
+ ],
1613
+ "scene": {
1614
+ "entities": {
1615
+ "card": {
1616
+ "type": "panel",
1617
+ "title": "Checklist do fluxo",
1618
+ "icon": "list-checks",
1619
+ "lines": [
1620
+ "= webhook responde em < 1 s",
1621
+ "= fila entre aceitar e processar",
1622
+ "= workers escalam sozinhos",
1623
+ "= id único torna tudo idempotente",
1624
+ "= DLQ + alerta no tamanho"
1625
+ ]
1626
+ }
1627
+ }
1628
+ }
1629
+ }
1630
+ ]
1631
+ }
1632
+ ],
1633
+ "insides": {
1634
+ "lb-rodizio": {
1635
+ "title": "O balanceador por dentro",
1636
+ "summary": "Rodízio e health checks.",
1637
+ "legend": [
1638
+ "req",
1639
+ "ping"
1640
+ ],
1641
+ "defs": {
1642
+ "entities": {
1643
+ "lb": {
1644
+ "type": "block",
1645
+ "x": -8,
1646
+ "y": 0,
1647
+ "s": 3.4,
1648
+ "h": 40,
1649
+ "color": "orange",
1650
+ "glyph": "balancer",
1651
+ "label": [
1652
+ "LB"
1653
+ ]
1654
+ },
1655
+ "a": {
1656
+ "type": "block",
1657
+ "x": 4,
1658
+ "y": -7,
1659
+ "s": 3,
1660
+ "h": 34,
1661
+ "color": "blue",
1662
+ "glyph": "server",
1663
+ "label": [
1664
+ "API 1"
1665
+ ]
1666
+ },
1667
+ "b": {
1668
+ "type": "block",
1669
+ "x": 4,
1670
+ "y": 0,
1671
+ "s": 3,
1672
+ "h": 34,
1673
+ "color": "blue",
1674
+ "glyph": "server",
1675
+ "label": [
1676
+ "API 2"
1677
+ ]
1678
+ },
1679
+ "c": {
1680
+ "type": "block",
1681
+ "x": 4,
1682
+ "y": 7,
1683
+ "s": 3,
1684
+ "h": 34,
1685
+ "color": "blue",
1686
+ "glyph": "server",
1687
+ "label": [
1688
+ "API 3"
1689
+ ]
1690
+ }
1691
+ },
1692
+ "links": {
1693
+ "la": {
1694
+ "from": "lb",
1695
+ "to": "a",
1696
+ "pts": [
1697
+ "lb",
1698
+ [
1699
+ -2,
1700
+ 0
1701
+ ],
1702
+ [
1703
+ -2,
1704
+ -7
1705
+ ],
1706
+ "a"
1707
+ ]
1708
+ },
1709
+ "lb2": {
1710
+ "from": "lb",
1711
+ "to": "b"
1712
+ },
1713
+ "lc": {
1714
+ "from": "lb",
1715
+ "to": "c",
1716
+ "pts": [
1717
+ "lb",
1718
+ [
1719
+ -2,
1720
+ 0
1721
+ ],
1722
+ [
1723
+ -2,
1724
+ 7
1725
+ ],
1726
+ "c"
1727
+ ]
1728
+ }
1729
+ }
1730
+ },
1731
+ "steps": [
1732
+ {
1733
+ "short": "Rodízio",
1734
+ "chapter": "technique",
1735
+ "title": "Um para cada, em ordem",
1736
+ "body": [
1737
+ "O balanceador mantém a lista de servidores saudáveis e entrega cada nova requisição ao próximo da lista (round-robin)."
1738
+ ],
1739
+ "scene": {
1740
+ "entities": {
1741
+ "lb": "lb",
1742
+ "a": "a",
1743
+ "b": "b",
1744
+ "c": "c"
1745
+ },
1746
+ "links": {
1747
+ "la": "la",
1748
+ "lb2": "lb2",
1749
+ "lc": "lc"
1750
+ },
1751
+ "flows": [
1752
+ {
1753
+ "route": [
1754
+ [
1755
+ "la",
1756
+ "lb2",
1757
+ "lc"
1758
+ ]
1759
+ ],
1760
+ "rate": 3
1761
+ }
1762
+ ]
1763
+ }
1764
+ },
1765
+ {
1766
+ "short": "Health check",
1767
+ "chapter": "technique",
1768
+ "title": "Quem não responde sai da lista",
1769
+ "body": [
1770
+ "A cada 5 s ele chama <code>GET /health</code> em cada servidor. Duas falhas seguidas tiram o servidor do rodízio; duas respostas boas o trazem de volta."
1771
+ ],
1772
+ "scene": {
1773
+ "entities": {
1774
+ "b": {
1775
+ "dead": 1,
1776
+ "tags": [
1777
+ {
1778
+ "text": "2 falhas",
1779
+ "tone": "red",
1780
+ "icon": "alert"
1781
+ }
1782
+ ]
1783
+ }
1784
+ },
1785
+ "links": {
1786
+ "lb2": {
1787
+ "style": "off"
1788
+ }
1789
+ },
1790
+ "flows": [
1791
+ {
1792
+ "route": [
1793
+ [
1794
+ "la",
1795
+ "lc"
1796
+ ]
1797
+ ],
1798
+ "rate": 3
1799
+ },
1800
+ {
1801
+ "route": [
1802
+ "la"
1803
+ ],
1804
+ "kind": "ping",
1805
+ "rate": 0.5
1806
+ },
1807
+ {
1808
+ "route": [
1809
+ "lc"
1810
+ ],
1811
+ "kind": "ping",
1812
+ "rate": 0.5
1813
+ }
1814
+ ]
1815
+ }
1816
+ }
1817
+ ]
1818
+ },
1819
+ "cache-aside": {
1820
+ "title": "Cache-aside por dentro",
1821
+ "summary": "Acerto, erro e expiração.",
1822
+ "legend": [
1823
+ "req",
1824
+ "res",
1825
+ "write"
1826
+ ],
1827
+ "defs": {
1828
+ "entities": {
1829
+ "app": {
1830
+ "type": "block",
1831
+ "x": -9,
1832
+ "y": 0,
1833
+ "s": 3.4,
1834
+ "h": 40,
1835
+ "color": "blue",
1836
+ "glyph": "server",
1837
+ "label": [
1838
+ "API"
1839
+ ]
1840
+ },
1841
+ "r": {
1842
+ "type": "block",
1843
+ "x": 1,
1844
+ "y": -6,
1845
+ "s": 3.2,
1846
+ "h": 34,
1847
+ "color": "yellow",
1848
+ "glyph": "zap",
1849
+ "label": [
1850
+ "Redis"
1851
+ ]
1852
+ },
1853
+ "db": {
1854
+ "type": "db",
1855
+ "x": 1,
1856
+ "y": 7,
1857
+ "label": "postgres"
1858
+ }
1859
+ },
1860
+ "links": {
1861
+ "ar": {
1862
+ "from": "app",
1863
+ "to": "r",
1864
+ "label": "GET produto:42",
1865
+ "via": "y",
1866
+ "heads": "both"
1867
+ },
1868
+ "ad": {
1869
+ "from": "app",
1870
+ "to": "db",
1871
+ "label": "SELECT",
1872
+ "via": "y",
1873
+ "heads": "both"
1874
+ },
1875
+ "aw": {
1876
+ "from": "app",
1877
+ "to": "r",
1878
+ "label": "SET ttl=60s",
1879
+ "pts": [
1880
+ "app",
1881
+ [
1882
+ "app",
1883
+ "r-3"
1884
+ ],
1885
+ [
1886
+ "r",
1887
+ "r-3"
1888
+ ],
1889
+ "r"
1890
+ ],
1891
+ "style": "dashed"
1892
+ }
1893
+ }
1894
+ },
1895
+ "steps": [
1896
+ {
1897
+ "short": "Acerto",
1898
+ "chapter": "technique",
1899
+ "title": "Acerto: responde do cache",
1900
+ "body": [
1901
+ "A API pergunta ao Redis primeiro. Se a chave existe, responde em ~2 ms sem tocar no banco."
1902
+ ],
1903
+ "scene": {
1904
+ "entities": {
1905
+ "app": "app",
1906
+ "r": "r",
1907
+ "db": "db"
1908
+ },
1909
+ "links": {
1910
+ "ar": "ar"
1911
+ },
1912
+ "flows": [
1913
+ {
1914
+ "route": [
1915
+ "ar"
1916
+ ],
1917
+ "bounce": true,
1918
+ "rate": 2
1919
+ }
1920
+ ]
1921
+ }
1922
+ },
1923
+ {
1924
+ "short": "Erro",
1925
+ "chapter": "technique",
1926
+ "title": "Erro: lê do banco e guarda",
1927
+ "body": [
1928
+ "Se a chave não existe, lê do postgres e grava no Redis com TTL de 60 s. O próximo pedido do mesmo produto acerta."
1929
+ ],
1930
+ "scene": {
1931
+ "links": {
1932
+ "ad": "ad",
1933
+ "aw": "aw"
1934
+ },
1935
+ "flows": [
1936
+ {
1937
+ "route": [
1938
+ "ad"
1939
+ ],
1940
+ "bounce": true,
1941
+ "rate": 0.8
1942
+ },
1943
+ {
1944
+ "route": [
1945
+ "aw"
1946
+ ],
1947
+ "kind": "write",
1948
+ "rate": 0.8
1949
+ }
1950
+ ]
1951
+ },
1952
+ "callout": {
1953
+ "kind": "warn",
1954
+ "text": "Ao atualizar o produto, apague a chave — senão o cache serve o preço antigo até o TTL vencer."
1955
+ }
1956
+ }
1957
+ ]
1958
+ },
1959
+ "webhook-rapido": {
1960
+ "title": "O webhook por dentro",
1961
+ "summary": "Validar, enfileirar e responder — em ~40 ms.",
1962
+ "legend": [
1963
+ "msg",
1964
+ "res"
1965
+ ],
1966
+ "defs": {
1967
+ "entities": {
1968
+ "in": {
1969
+ "type": "block",
1970
+ "x": -10,
1971
+ "y": 0,
1972
+ "s": 3.4,
1973
+ "h": 34,
1974
+ "color": "blue",
1975
+ "glyph": "shield-check",
1976
+ "label": [
1977
+ "assinatura"
1978
+ ]
1979
+ },
1980
+ "q": {
1981
+ "type": "block",
1982
+ "x": 0,
1983
+ "y": 0,
1984
+ "s": 3.4,
1985
+ "h": 34,
1986
+ "color": "yellow",
1987
+ "glyph": "inbox",
1988
+ "label": [
1989
+ "enfileira"
1990
+ ]
1991
+ },
1992
+ "ok": {
1993
+ "type": "block",
1994
+ "x": 10,
1995
+ "y": 0,
1996
+ "s": 3.4,
1997
+ "h": 34,
1998
+ "color": "green",
1999
+ "glyph": "check",
2000
+ "label": [
2001
+ "200 OK"
2002
+ ]
2003
+ }
2004
+ },
2005
+ "links": {
2006
+ "iq": {
2007
+ "from": "in",
2008
+ "to": "q",
2009
+ "label": "válida"
2010
+ },
2011
+ "qo": {
2012
+ "from": "q",
2013
+ "to": "ok",
2014
+ "label": "~40 ms"
2015
+ }
2016
+ }
2017
+ },
2018
+ "steps": [
2019
+ {
2020
+ "short": "Valida",
2021
+ "chapter": "technique",
2022
+ "title": "Confere quem chamou",
2023
+ "body": [
2024
+ "O handler confere a assinatura HMAC do provedor. Sem assinatura válida, <code>401</code> e nada mais acontece."
2025
+ ],
2026
+ "scene": {
2027
+ "entities": {
2028
+ "in": "in",
2029
+ "q": "q"
2030
+ },
2031
+ "links": {
2032
+ "iq": "iq"
2033
+ },
2034
+ "flows": [
2035
+ {
2036
+ "route": [
2037
+ "iq"
2038
+ ],
2039
+ "kind": "msg",
2040
+ "rate": 1.2
2041
+ }
2042
+ ]
2043
+ }
2044
+ },
2045
+ {
2046
+ "short": "Responde",
2047
+ "chapter": "technique",
2048
+ "title": "Enfileira e responde",
2049
+ "body": [
2050
+ "Grava a mensagem na fila e devolve <code>200</code> imediatamente. Todo o resto acontece fora da requisição."
2051
+ ],
2052
+ "scene": {
2053
+ "entities": {
2054
+ "ok": "ok"
2055
+ },
2056
+ "links": {
2057
+ "qo": "qo"
2058
+ },
2059
+ "flows": [
2060
+ {
2061
+ "route": [
2062
+ "iq",
2063
+ "qo"
2064
+ ],
2065
+ "kind": "msg",
2066
+ "rate": 1.2
2067
+ }
2068
+ ]
2069
+ }
2070
+ }
2071
+ ]
2072
+ }
2073
+ }
2074
+ }