@umacloud/knowledge 1.0.1
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.
- package/00-governance/governance-capabilities.md +557 -0
- package/00-governance/knowledge-map.md +39 -0
- package/00-governance/maintenance-policy.md +76 -0
- package/00-governance/review-checklist.md +81 -0
- package/README.md +13 -0
- package/ai/01-standards/agent-development-complete.md +691 -0
- package/ai/01-standards/llm-application-complete.md +488 -0
- package/ai/01-standards/mlops-complete.md +798 -0
- package/ai/01-standards/prompt-engineering-complete.md +646 -0
- package/ai/01-standards/rag-architecture-complete.md +649 -0
- package/ai/02-playbooks/llm-evaluation-playbook.md +847 -0
- package/ai/03-checklists/ai-project-checklist.md +215 -0
- package/ai/04-antipatterns/ai-antipatterns.md +661 -0
- package/ai/05-cases/case-rag-production.md +147 -0
- package/ai/06-glossary/ai-glossary.md +162 -0
- package/ai/agent-evaluation-benchmark.md +53 -0
- package/ai/ai-agent-memory-context-management.md +41 -0
- package/ai/ai-cost-capacity-optimization-playbook.md +42 -0
- package/ai/ai-data-security-and-compliance-playbook.md +37 -0
- package/ai/ai-domain-index-and-checklist.md +40 -0
- package/ai/ai-governance-maturity-model.md +50 -0
- package/ai/ai-model-selection-and-routing-strategy.md +47 -0
- package/ai/ai-observability-and-oncall-runbook.md +52 -0
- package/ai/ai-rag-engineering-playbook.md +42 -0
- package/ai/ai-red-team-and-safety-evaluation.md +42 -0
- package/ai/ai-release-readiness-and-rollback-gate.md +42 -0
- package/ai/llm-agent-engineering-deep-dive.md +57 -0
- package/ai/prompt-and-tool-guardrails.md +52 -0
- package/api/01-standards/enterprise-api-standards.md +198 -0
- package/api/01-standards/rest-api-design-guide.md +63 -0
- package/api/02-playbooks/api-pagination-playbook.md +93 -0
- package/api/02-playbooks/graphql-production-playbook.md +176 -0
- package/api/03-checklists/api-review-checklist.md +55 -0
- package/api/04-antipatterns/api-antipatterns.md +112 -0
- package/architecture/01-standards/api-gateway-patterns.md +496 -0
- package/architecture/01-standards/cloud-native-patterns.md +644 -0
- package/architecture/01-standards/distributed-systems-patterns.md +591 -0
- package/architecture/01-standards/event-driven-architecture.md +595 -0
- package/architecture/01-standards/microservices-patterns-complete.md +968 -0
- package/architecture/01-standards/microservices-patterns.md +495 -0
- package/architecture/01-standards/system-design-interview.md +664 -0
- package/architecture/02-playbooks/microservices-patterns-playbook.md +137 -0
- package/architecture/02-playbooks/migration-playbook.md +780 -0
- package/architecture/02-playbooks/system-design-playbook.md +779 -0
- package/architecture/03-checklists/architecture-decision-checklist.md +297 -0
- package/architecture/04-antipatterns/architecture-antipatterns.md +417 -0
- package/architecture/05-cases/case-netflix-microservices.md +413 -0
- package/architecture/06-glossary/architecture-glossary.md +164 -0
- package/architecture/adr-template-and-examples.md +38 -0
- package/architecture/api-gateway-deep-dive.md +1291 -0
- package/architecture/configuration-management.md +1162 -0
- package/architecture/distributed-transactions.md +1220 -0
- package/architecture/microservices-complete.md +735 -0
- package/architecture/resilience-and-disaster-patterns.md +37 -0
- package/architecture/service-governance.md +1198 -0
- package/architecture/system-architecture-deep-dive.md +37 -0
- package/backend/01-standards/analytics-and-growth.md +65 -0
- package/backend/01-standards/api-and-error-conventions.md +120 -0
- package/backend/01-standards/application-layering-and-packaging.md +160 -0
- package/backend/01-standards/auth-implementation.md +104 -0
- package/backend/01-standards/backend-framework-idioms.md +74 -0
- package/backend/01-standards/background-jobs-and-async.md +66 -0
- package/backend/01-standards/caching-strategies-complete.md +390 -0
- package/backend/01-standards/config-and-observability.md +77 -0
- package/backend/01-standards/data-modeling-and-persistence.md +94 -0
- package/backend/01-standards/django-complete.md +1765 -0
- package/backend/01-standards/email-and-notifications.md +64 -0
- package/backend/01-standards/fastapi-complete.md +925 -0
- package/backend/01-standards/file-upload-and-storage.md +66 -0
- package/backend/01-standards/graphql-api-complete.md +416 -0
- package/backend/01-standards/llm-application-standard.md +78 -0
- package/backend/01-standards/message-queue-patterns.md +379 -0
- package/backend/01-standards/microservices-and-distributed.md +78 -0
- package/backend/01-standards/nestjs-complete.md +2167 -0
- package/backend/01-standards/payment-integration.md +80 -0
- package/backend/01-standards/rate-limiting-complete.md +451 -0
- package/backend/01-standards/realtime-and-websocket.md +65 -0
- package/backend/01-standards/search-and-filtering.md +64 -0
- package/backend/01-standards/spring-boot-complete.md +445 -0
- package/backend/02-playbooks/api-design-playbook.md +718 -0
- package/backend/02-playbooks/email-send-playbook.md +130 -0
- package/backend/02-playbooks/file-upload-s3-playbook.md +153 -0
- package/backend/02-playbooks/typescript-enterprise-playbook.md +133 -0
- package/backend/02-playbooks/websocket-realtime-playbook.md +154 -0
- package/backend/03-checklists/api-launch-checklist.md +189 -0
- package/backend/04-antipatterns/backend-antipatterns.md +1051 -0
- package/blockchain/01-standards/blockchain-basics.md +557 -0
- package/blockchain/01-standards/smart-contract-development.md +1315 -0
- package/cicd/01-standards/deployment-and-delivery-standard.md +96 -0
- package/cicd/01-standards/github-actions-complete.md +473 -0
- package/cicd/01-standards/release-and-store-submission.md +75 -0
- package/cicd/02-playbooks/cicd-pipeline-playbook.md +144 -0
- package/cicd/02-playbooks/release-management-playbook.md +605 -0
- package/cicd/03-checklists/pipeline-security-checklist.md +168 -0
- package/cicd/04-antipatterns/cicd-antipatterns.md +589 -0
- package/cicd/05-cases/case-deployment-automation.md +221 -0
- package/cicd/05-cases/case-gitops-transformation.md +212 -0
- package/cicd/06-glossary/cicd-glossary.md +114 -0
- package/cicd/cicd-blueprint-deep-dive.md +38 -0
- package/cicd/release-readiness-gate.md +37 -0
- package/cloud-native/01-standards/container-security.md +741 -0
- package/cloud-native/01-standards/kubernetes-complete.md +812 -0
- package/cloud-native/02-playbooks/api-gateway-playbook.md +155 -0
- package/cloud-native/02-playbooks/gitops-with-argocd.md +760 -0
- package/cloud-native/02-playbooks/k8s-troubleshooting-playbook.md +1942 -0
- package/cloud-native/02-playbooks/message-queue-playbook.md +129 -0
- package/cloud-native/02-playbooks/multicloud-governance.md +726 -0
- package/cloud-native/02-playbooks/serverless-patterns.md +788 -0
- package/cloud-native/02-playbooks/service-mesh-playbook.md +612 -0
- package/cloud-native/02-playbooks/terraform-iac-playbook.md +143 -0
- package/cloud-native/03-checklists/container-security-checklist.md +431 -0
- package/cloud-native/03-checklists/k8s-production-readiness-checklist.md +460 -0
- package/cloud-native/04-antipatterns/container-antipatterns.md +660 -0
- package/cloud-native/04-antipatterns/k8s-antipatterns.md +743 -0
- package/cloud-native/05-cases/case-k8s-migration.md +478 -0
- package/cloud-native/05-cases/case-k8s-scaling.md +642 -0
- package/cloud-native/05-cases/case-k8s-security-incident.md +397 -0
- package/cloud-native/06-glossary/cloud-native-glossary.md +337 -0
- package/cross-platform/01-standards/cross-platform-frameworks.md +83 -0
- package/cross-platform/01-standards/platform-selection-and-architecture.md +77 -0
- package/data/01-standards/elasticsearch-complete.md +2098 -0
- package/data/01-standards/postgresql-complete.md +1613 -0
- package/data/01-standards/redis-complete.md +1527 -0
- package/data/02-playbooks/database-optimization-playbook.md +403 -0
- package/data/02-playbooks/elasticsearch-production-playbook.md +132 -0
- package/data/03-checklists/database-launch-checklist.md +187 -0
- package/data/04-antipatterns/database-antipatterns.md +873 -0
- package/data/05-cases/case-database-migration.md +310 -0
- package/data/06-glossary/database-glossary.md +440 -0
- package/data/data-governance-and-modeling-deep-dive.md +39 -0
- package/data-engineering/01-standards/airflow-complete.md +523 -0
- package/data-engineering/01-standards/kafka-complete.md +1521 -0
- package/data-engineering/02-playbooks/spark-etl-playbook.md +496 -0
- package/data-engineering/03-checklists/pipeline-launch-checklist.md +194 -0
- package/data-engineering/04-antipatterns/data-pipeline-antipatterns.md +684 -0
- package/data-engineering/05-cases/case-real-time-pipeline.md +355 -0
- package/data-engineering/06-glossary/data-engineering-glossary.md +429 -0
- package/database/01-standards/database-schema-standards.md +147 -0
- package/database/02-playbooks/postgresql-optimization-quick.md +52 -0
- package/database/02-playbooks/postgresql-performance-optimization.md +58 -0
- package/database/02-playbooks/postgresql-production-playbook.md +146 -0
- package/database/02-playbooks/redis-caching-playbook.md +117 -0
- package/database/03-checklists/database-review-checklist.md +50 -0
- package/database/04-antipatterns/database-antipatterns.md +112 -0
- package/design/01-standards/ui-design-system-complete.md +423 -0
- package/design/02-playbooks/design-handoff-playbook.md +254 -0
- package/design/02-playbooks/design-review-playbook.md +388 -0
- package/design/03-checklists/design-review-checklist.md +246 -0
- package/design/04-antipatterns/design-antipatterns.md +378 -0
- package/design/05-cases/case-design-system-adoption.md +328 -0
- package/design/06-glossary/design-glossary.md +329 -0
- package/design/ui-full-lifecycle-cross-platform-playbook.md +571 -0
- package/design/ux-system-deep-dive.md +38 -0
- package/design-systems/00-craft-rules.md +71 -0
- package/design-systems/aesthetic-families.md +43 -0
- package/design-systems/anti-ai-slop.md +162 -0
- package/design-systems/bold-geometric.md +120 -0
- package/design-systems/brutalist-bold.md +103 -0
- package/design-systems/editorial-clean.md +109 -0
- package/design-systems/glass-aurora.md +108 -0
- package/design-systems/modern-minimal.md +145 -0
- package/design-systems/premium-luxury.md +106 -0
- package/design-systems/product-type-design-map.md +48 -0
- package/design-systems/soft-warm.md +123 -0
- package/design-systems/tech-utility.md +113 -0
- package/desktop/01-standards/desktop-app-standard.md +72 -0
- package/desktop/01-standards/desktop-design.md +71 -0
- package/development/00-governance/document-template.md +41 -0
- package/development/01-standards/api-versioning-strategies.md +432 -0
- package/development/01-standards/authentication-patterns-complete.md +479 -0
- package/development/01-standards/css-architecture-complete.md +550 -0
- package/development/01-standards/database-migration-strategies.md +484 -0
- package/development/01-standards/elasticsearch-complete.md +347 -0
- package/development/01-standards/git-complete.md +371 -0
- package/development/01-standards/golang-complete.md +1565 -0
- package/development/01-standards/graphql-complete.md +298 -0
- package/development/01-standards/javascript-bundlers-complete.md +469 -0
- package/development/01-standards/javascript-typescript-complete.md +528 -0
- package/development/01-standards/jest-complete.md +275 -0
- package/development/01-standards/linux-complete.md +234 -0
- package/development/01-standards/logging-observability-complete.md +526 -0
- package/development/01-standards/microservices-communication.md +502 -0
- package/development/01-standards/mongodb-complete.md +406 -0
- package/development/01-standards/oauth2-complete.md +285 -0
- package/development/01-standards/performance-optimization-complete.md +289 -0
- package/development/01-standards/playwright-complete.md +247 -0
- package/development/01-standards/postgresql-complete.md +456 -0
- package/development/01-standards/pytest-complete.md +340 -0
- package/development/01-standards/python-async-programming.md +902 -0
- package/development/01-standards/python-complete.md +956 -0
- package/development/01-standards/python-decorators-complete.md +799 -0
- package/development/01-standards/python-design-patterns.md +2854 -0
- package/development/01-standards/python-packaging-distribution.md +420 -0
- package/development/01-standards/python-testing-strategies.md +607 -0
- package/development/01-standards/python-web-frameworks-comparison.md +471 -0
- package/development/01-standards/redis-complete.md +317 -0
- package/development/01-standards/rest-api-complete.md +316 -0
- package/development/01-standards/rust-complete.md +578 -0
- package/development/01-standards/typescript-advanced-types.md +1513 -0
- package/development/01-standards/web-security-complete.md +292 -0
- package/development/02-playbooks/api-design-playbook.md +810 -0
- package/development/02-playbooks/database-migration-playbook.md +580 -0
- package/development/02-playbooks/debugging-playbook.md +692 -0
- package/development/02-playbooks/feature-delivery-playbook.md +430 -0
- package/development/02-playbooks/incident-hotfix-playbook.md +387 -0
- package/development/02-playbooks/performance-optimization-playbook.md +531 -0
- package/development/02-playbooks/performance-tuning-playbook.md +652 -0
- package/development/02-playbooks/refactor-playbook.md +403 -0
- package/development/02-playbooks/release-playbook.md +469 -0
- package/development/03-checklists/architecture-review-checklist.md +168 -0
- package/development/03-checklists/data-migration-checklist.md +157 -0
- package/development/03-checklists/oncall-handover-checklist.md +173 -0
- package/development/03-checklists/pr-checklist.md +158 -0
- package/development/03-checklists/production-readiness-checklist.md +190 -0
- package/development/03-checklists/release-readiness-checklist.md +154 -0
- package/development/03-checklists/security-review-checklist.md +182 -0
- package/development/04-antipatterns/api-antipatterns.md +657 -0
- package/development/04-antipatterns/architecture-antipatterns.md +686 -0
- package/development/04-antipatterns/backend-antipatterns.md +648 -0
- package/development/04-antipatterns/cicd-antipatterns.md +540 -0
- package/development/04-antipatterns/code-smell-antipatterns.md +571 -0
- package/development/04-antipatterns/data-antipatterns.md +658 -0
- package/development/04-antipatterns/database-antipatterns.md +578 -0
- package/development/04-antipatterns/frontend-antipatterns.md +635 -0
- package/development/04-antipatterns/reliability-antipatterns.md +700 -0
- package/development/04-antipatterns/security-antipatterns.md +747 -0
- package/development/05-cases/case-api-version-migration.md +428 -0
- package/development/05-cases/case-authorization-hardening.md +383 -0
- package/development/05-cases/case-bluegreen-rollback.md +466 -0
- package/development/05-cases/case-cache-snowball-protection.md +485 -0
- package/development/05-cases/case-ci-cd-pipeline.md +544 -0
- package/development/05-cases/case-database-scaling.md +500 -0
- package/development/05-cases/case-db-hotspot-optimization.md +487 -0
- package/development/05-cases/case-incident-mttr-reduction.md +563 -0
- package/development/05-cases/case-microservice-migration.md +375 -0
- package/development/05-cases/case-performance-optimization.md +406 -0
- package/development/05-cases/case-security-incident-response.md +345 -0
- package/development/06-glossary/full-stack-glossary.md +166 -0
- package/development/09-maturity/quarterly-audit-template.md +35 -0
- package/development/11-ui-excellence/ui-aesthetic-system.md +41 -0
- package/development/11-ui-excellence/ui-engineering-excellence.md +435 -0
- package/development/12-scenarios/development-scenarios-guide.md +565 -0
- package/development/13-implementation-assets/implementation-toolkit.md +282 -0
- package/development/13-implementation-assets/knowledge-gates-execution.md +43 -0
- package/development/14-full-lifecycle/software-lifecycle-gates.md +511 -0
- package/development/15-lifecycle-templates/project-templates-collection.md +791 -0
- package/development/api-contract-and-versioning-guide.md +36 -0
- package/development/api-governance-complete.md +43 -0
- package/development/backend-engineering-complete.md +43 -0
- package/development/code-review-quality-complete.md +43 -0
- package/development/concurrency-reliability-complete.md +43 -0
- package/development/database-engineering-complete.md +43 -0
- package/development/engineering-effectiveness-complete.md +43 -0
- package/development/engineering-standards-deep-dive.md +38 -0
- package/development/frontend-engineering-complete.md +43 -0
- package/development/performance-capacity-complete.md +43 -0
- package/development/refactor-migration-complete.md +42 -0
- package/development/refactoring-and-techdebt-playbook.md +37 -0
- package/development/security-in-development-complete.md +43 -0
- package/devops/01-standards/cicd-pipeline-complete.md +262 -0
- package/devops/01-standards/docker-complete.md +1490 -0
- package/devops/01-standards/github-actions-complete.md +337 -0
- package/devops/01-standards/kubernetes-complete.md +638 -0
- package/devops/01-standards/terraform-complete.md +2117 -0
- package/devops/02-playbooks/docker-compose-playbook.md +233 -0
- package/devops/02-playbooks/docker-k8s-production-playbook.md +186 -0
- package/devops/02-playbooks/docker-production-playbook.md +952 -0
- package/edge-iot/01-standards/edge-iot-complete.md +473 -0
- package/experts/architect/api-design.md +178 -0
- package/experts/architect/methodology.md +124 -0
- package/experts/architect/security.md +75 -0
- package/experts/backend-lead/methodology.md +216 -0
- package/experts/devops/methodology.md +160 -0
- package/experts/frontend-lead/methodology.md +178 -0
- package/experts/product-manager/industry/ecommerce.md +43 -0
- package/experts/product-manager/industry/saas.md +40 -0
- package/experts/product-manager/methodology.md +97 -0
- package/experts/qa-lead/methodology.md +123 -0
- package/experts/qa-lead/test-strategy.md +128 -0
- package/experts/uiux-designer/methodology.md +125 -0
- package/frontend/01-standards/accessibility-complete.md +532 -0
- package/frontend/01-standards/accessibility-standard.md +74 -0
- package/frontend/01-standards/admin-dashboard-and-crud.md +72 -0
- package/frontend/01-standards/design-tokens-complete.md +444 -0
- package/frontend/01-standards/forms-and-validation.md +77 -0
- package/frontend/01-standards/frontend-architecture-and-layering.md +119 -0
- package/frontend/01-standards/i18n-and-localization.md +65 -0
- package/frontend/01-standards/nextjs-complete.md +451 -0
- package/frontend/01-standards/react-complete.md +713 -0
- package/frontend/01-standards/react-hooks-complete-guide.md +1100 -0
- package/frontend/01-standards/react-hooks-complete.md +1171 -0
- package/frontend/01-standards/seo-and-web-vitals.md +77 -0
- package/frontend/01-standards/state-management-complete.md +444 -0
- package/frontend/01-standards/vue-complete.md +499 -0
- package/frontend/01-standards/vue3-complete.md +2002 -0
- package/frontend/01-standards/web-framework-best-practices.md +64 -0
- package/frontend/01-standards/web-performance-complete.md +495 -0
- package/frontend/02-playbooks/accessibility-a11y-playbook.md +161 -0
- package/frontend/02-playbooks/frontend-performance-playbook.md +707 -0
- package/frontend/02-playbooks/i18n-internationalization-playbook.md +120 -0
- package/frontend/02-playbooks/performance-optimization-playbook.md +163 -0
- package/frontend/02-playbooks/react-nextjs-production-playbook.md +167 -0
- package/frontend/02-playbooks/react-state-management-playbook.md +173 -0
- package/frontend/03-checklists/component-quality-checklist.md +166 -0
- package/frontend/03-checklists/frontend-launch-checklist.md +299 -0
- package/frontend/04-antipatterns/frontend-antipatterns.md +886 -0
- package/frontend/05-cases/case-performance-optimization.md +274 -0
- package/harmony/01-standards/harmonyos-arkts-standard.md +75 -0
- package/harmony/01-standards/harmonyos-design.md +65 -0
- package/high-quality-engineering-playbook.md +54 -0
- package/incident/01-standards/incident-response-complete.md +303 -0
- package/incident/02-playbooks/chaos-engineering-playbook.md +883 -0
- package/incident/02-playbooks/postmortem-playbook.md +398 -0
- package/incident/03-checklists/incident-readiness-checklist.md +181 -0
- package/incident/04-antipatterns/incident-antipatterns.md +490 -0
- package/incident/05-cases/case-cascade-failure.md +176 -0
- package/incident/06-glossary/incident-glossary.md +114 -0
- package/incident/postmortem-and-response-deep-dive.md +39 -0
- package/industries/ecommerce/ecommerce-complete.md +631 -0
- package/industries/education/education-complete.md +555 -0
- package/industries/fintech/fintech-complete.md +501 -0
- package/industries/gaming/gaming-complete.md +587 -0
- package/industries/healthcare/healthcare-complete.md +452 -0
- package/low-code/01-standards/low-code-complete.md +944 -0
- package/miniprogram/01-standards/ai-common-mistakes.md +61 -0
- package/miniprogram/01-standards/miniprogram-custom-navbar-capsule.md +77 -0
- package/miniprogram/01-standards/miniprogram-design.md +61 -0
- package/miniprogram/01-standards/miniprogram-standard.md +81 -0
- package/mobile/01-standards/android-material-design.md +70 -0
- package/mobile/01-standards/flutter-complete.md +384 -0
- package/mobile/01-standards/ios-design-hig.md +78 -0
- package/mobile/01-standards/mobile-app-standard.md +85 -0
- package/mobile/01-standards/react-native-complete.md +352 -0
- package/mobile/02-playbooks/mobile-cross-platform-playbook.md +175 -0
- package/mobile/02-playbooks/mobile-performance.md +473 -0
- package/mobile/03-checklists/mobile-release-checklist.md +234 -0
- package/mobile/04-antipatterns/mobile-antipatterns.md +798 -0
- package/mobile/05-cases/case-app-performance.md +500 -0
- package/mobile/05-cases/case-app-startup-optimization.md +218 -0
- package/mobile/06-glossary/mobile-glossary.md +484 -0
- package/observability/01-standards/observability-standards.md +103 -0
- package/observability/02-playbooks/prometheus-grafana-playbook.md +135 -0
- package/observability/02-playbooks/structured-logging-playbook.md +73 -0
- package/observability/03-checklists/observability-checklist.md +54 -0
- package/observability/04-antipatterns/observability-antipatterns.md +106 -0
- package/operations/01-standards/prometheus-monitoring-complete.md +1578 -0
- package/operations/02-playbooks/capacity-planning-playbook.md +620 -0
- package/operations/03-checklists/production-launch-checklist.md +365 -0
- package/operations/04-antipatterns/operations-antipatterns.md +664 -0
- package/operations/05-cases/case-sre-practices.md +581 -0
- package/operations/06-glossary/operations-glossary.md +120 -0
- package/operations/aiops-anomaly-detection.md +758 -0
- package/operations/capacity-planning.md +1061 -0
- package/operations/chaos-engineering.md +659 -0
- package/operations/incident-command-system.md +38 -0
- package/operations/observability-complete.md +442 -0
- package/operations/slo-sli-playbook.md +517 -0
- package/operations/sre-operations-deep-dive.md +39 -0
- package/package.json +8 -0
- package/performance/01-standards/performance-and-scalability.md +80 -0
- package/performance/01-standards/performance-standards.md +156 -0
- package/performance/02-playbooks/query-optimization-playbook.md +103 -0
- package/performance/03-checklists/performance-checklist.md +56 -0
- package/performance/04-antipatterns/performance-antipatterns.md +146 -0
- package/product/01-standards/product-management-complete.md +285 -0
- package/product/02-playbooks/feature-launch-playbook.md +207 -0
- package/product/02-playbooks/user-research-playbook.md +532 -0
- package/product/03-checklists/feature-launch-checklist.md +275 -0
- package/product/04-antipatterns/product-antipatterns.md +355 -0
- package/product/05-cases/case-mvp-to-scale.md +384 -0
- package/product/06-glossary/product-glossary.md +462 -0
- package/product/feature-prioritization-framework.md +40 -0
- package/product/kpi-and-metric-tree.md +37 -0
- package/product/product-discovery-and-prd-deep-dive.md +41 -0
- package/quantum/01-standards/quantum-complete.md +1186 -0
- package/security/01-standards/api-security-complete.md +511 -0
- package/security/01-standards/container-runtime-security.md +574 -0
- package/security/01-standards/data-protection-gdpr.md +543 -0
- package/security/01-standards/owasp-top10-complete.md +1890 -0
- package/security/01-standards/secure-coding-baseline.md +90 -0
- package/security/01-standards/supply-chain-security.md +441 -0
- package/security/01-standards/web-security-checklist.md +108 -0
- package/security/01-standards/zero-trust-architecture.md +521 -0
- package/security/02-playbooks/auth-sso-playbook.md +166 -0
- package/security/02-playbooks/incident-response-security-playbook.md +588 -0
- package/security/02-playbooks/owasp-api-security-playbook.md +129 -0
- package/security/02-playbooks/payment-integration-playbook.md +119 -0
- package/security/02-playbooks/penetration-testing-playbook.md +517 -0
- package/security/03-checklists/security-audit-checklist.md +356 -0
- package/security/04-antipatterns/security-coding-antipatterns.md +580 -0
- package/security/05-cases/case-log4shell-incident.md +537 -0
- package/security/05-cases/case-major-breaches.md +468 -0
- package/security/06-glossary/security-glossary.md +212 -0
- package/security/compliance-automation.md +993 -0
- package/security/container-security.md +680 -0
- package/security/devsecops-complete.md +426 -0
- package/security/sast-dast-sca.md +775 -0
- package/security/secrets-management.md +594 -0
- package/security/security-architecture-deep-dive.md +37 -0
- package/security/threat-modeling-stride-playbook.md +40 -0
- package/seed-templates/auth-system.md +59 -0
- package/seed-templates/blog-content.md +94 -0
- package/seed-templates/dashboard.md +89 -0
- package/seed-templates/docs-site.md +73 -0
- package/seed-templates/e-commerce.md +50 -0
- package/seed-templates/saas-landing.md +92 -0
- package/seed-templates/settings-page.md +51 -0
- package/testing/01-standards/test-strategy-and-layering.md +83 -0
- package/testing/01-standards/testing-strategy-complete.md +422 -0
- package/testing/01-standards/unit-testing-best-practices.md +118 -0
- package/testing/02-playbooks/e2e-testing-playbook.md +988 -0
- package/testing/02-playbooks/testing-strategy-playbook.md +126 -0
- package/testing/03-checklists/test-strategy-checklist.md +208 -0
- package/testing/04-antipatterns/testing-antipatterns.md +718 -0
- package/testing/05-cases/case-testing-transformation.md +300 -0
- package/testing/06-glossary/testing-glossary.md +110 -0
- package/testing/risk-based-test-matrix.md +36 -0
- package/testing/testing-strategy-deep-dive.md +37 -0
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: distributed-systems-patterns
|
|
3
|
+
title: 分布式系统模式
|
|
4
|
+
domain: architecture
|
|
5
|
+
category: 01-standards
|
|
6
|
+
difficulty: intermediate
|
|
7
|
+
tags: [agent, architecture, checklist, distributed, patterns, systems, 实战代码示例, 常见陷阱]
|
|
8
|
+
quality_score: 70
|
|
9
|
+
last_updated: 2026-06-15
|
|
10
|
+
---
|
|
11
|
+
# 分布式系统模式
|
|
12
|
+
|
|
13
|
+
## 概述
|
|
14
|
+
分布式系统面临网络不可靠、节点故障、时钟偏差等固有挑战。本指南覆盖CAP定理、一致性模型、分区容忍、Leader选举、共识算法等核心模式,帮助团队理解并正确应对分布式系统的复杂性。
|
|
15
|
+
|
|
16
|
+
## 核心概念
|
|
17
|
+
|
|
18
|
+
### 1. CAP定理
|
|
19
|
+
分布式系统最多只能同时满足以下三项中的两项:
|
|
20
|
+
- **Consistency(一致性)**: 所有节点在同一时刻看到相同数据
|
|
21
|
+
- **Availability(可用性)**: 每个请求都能收到响应(不保证最新)
|
|
22
|
+
- **Partition Tolerance(分区容忍)**: 网络分区时系统继续运行
|
|
23
|
+
|
|
24
|
+
实际选择:
|
|
25
|
+
- **CP系统**: 牺牲可用性保一致性 — ZooKeeper/etcd/HBase
|
|
26
|
+
- **AP系统**: 牺牲一致性保可用性 — Cassandra/DynamoDB/CouchDB
|
|
27
|
+
- 网络分区不可避免,所以实际是在C和A之间权衡
|
|
28
|
+
|
|
29
|
+
### 2. 一致性模型
|
|
30
|
+
|
|
31
|
+
| 模型 | 强度 | 描述 | 代表 |
|
|
32
|
+
|------|------|------|------|
|
|
33
|
+
| 强一致性 | 最强 | 读到最新写入 | 单机数据库/Raft |
|
|
34
|
+
| 线性一致性 | 强 | 实时顺序保证 | etcd/ZooKeeper |
|
|
35
|
+
| 顺序一致性 | 中强 | 全序但非实时 | — |
|
|
36
|
+
| 因果一致性 | 中 | 因果关系有序 | MongoDB(多数读写) |
|
|
37
|
+
| 最终一致性 | 弱 | 最终收敛到一致 | DNS/DynamoDB/S3 |
|
|
38
|
+
| 读己之写 | 弱+ | 能读到自己的写入 | 社交Feed |
|
|
39
|
+
|
|
40
|
+
### 3. 分布式系统8大谬误
|
|
41
|
+
1. 网络是可靠的
|
|
42
|
+
2. 延迟为零
|
|
43
|
+
3. 带宽无限
|
|
44
|
+
4. 网络是安全的
|
|
45
|
+
5. 拓扑不变
|
|
46
|
+
6. 只有一个管理员
|
|
47
|
+
7. 传输成本为零
|
|
48
|
+
8. 网络是同构的
|
|
49
|
+
|
|
50
|
+
## 实战代码示例
|
|
51
|
+
|
|
52
|
+
### 分布式锁(Redis)
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
# Redis分布式锁(Redlock简化版)
|
|
56
|
+
import redis.asyncio as redis
|
|
57
|
+
import uuid
|
|
58
|
+
import asyncio
|
|
59
|
+
|
|
60
|
+
class DistributedLock:
|
|
61
|
+
"""Redis分布式锁"""
|
|
62
|
+
|
|
63
|
+
def __init__(self, redis_client: redis.Redis, lock_name: str, ttl: int = 30):
|
|
64
|
+
self.redis = redis_client
|
|
65
|
+
self.lock_name = f"lock:{lock_name}"
|
|
66
|
+
self.ttl = ttl
|
|
67
|
+
self.token = str(uuid.uuid4())
|
|
68
|
+
self._renewal_task = None
|
|
69
|
+
|
|
70
|
+
async def acquire(self, timeout: float = 10.0) -> bool:
|
|
71
|
+
"""获取锁(带超时)"""
|
|
72
|
+
deadline = asyncio.get_event_loop().time() + timeout
|
|
73
|
+
while asyncio.get_event_loop().time() < deadline:
|
|
74
|
+
acquired = await self.redis.set(
|
|
75
|
+
self.lock_name, self.token, nx=True, ex=self.ttl
|
|
76
|
+
)
|
|
77
|
+
if acquired:
|
|
78
|
+
# 启动续期任务
|
|
79
|
+
self._renewal_task = asyncio.create_task(self._auto_renew())
|
|
80
|
+
return True
|
|
81
|
+
await asyncio.sleep(0.1)
|
|
82
|
+
return False
|
|
83
|
+
|
|
84
|
+
async def release(self):
|
|
85
|
+
"""释放锁(原子操作,确保只释放自己的锁)"""
|
|
86
|
+
if self._renewal_task:
|
|
87
|
+
self._renewal_task.cancel()
|
|
88
|
+
lua_script = """
|
|
89
|
+
if redis.call("get", KEYS[1]) == ARGV[1] then
|
|
90
|
+
return redis.call("del", KEYS[1])
|
|
91
|
+
else
|
|
92
|
+
return 0
|
|
93
|
+
end
|
|
94
|
+
"""
|
|
95
|
+
await self.redis.eval(lua_script, 1, self.lock_name, self.token)
|
|
96
|
+
|
|
97
|
+
async def _auto_renew(self):
|
|
98
|
+
"""自动续期(防止长任务锁过期)"""
|
|
99
|
+
try:
|
|
100
|
+
while True:
|
|
101
|
+
await asyncio.sleep(self.ttl // 3)
|
|
102
|
+
lua_script = """
|
|
103
|
+
if redis.call("get", KEYS[1]) == ARGV[1] then
|
|
104
|
+
return redis.call("expire", KEYS[1], ARGV[2])
|
|
105
|
+
else
|
|
106
|
+
return 0
|
|
107
|
+
end
|
|
108
|
+
"""
|
|
109
|
+
result = await self.redis.eval(
|
|
110
|
+
lua_script, 1, self.lock_name, self.token, self.ttl
|
|
111
|
+
)
|
|
112
|
+
if not result:
|
|
113
|
+
break
|
|
114
|
+
except asyncio.CancelledError:
|
|
115
|
+
pass
|
|
116
|
+
|
|
117
|
+
async def __aenter__(self):
|
|
118
|
+
acquired = await self.acquire()
|
|
119
|
+
if not acquired:
|
|
120
|
+
raise TimeoutError(f"Failed to acquire lock: {self.lock_name}")
|
|
121
|
+
return self
|
|
122
|
+
|
|
123
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
124
|
+
await self.release()
|
|
125
|
+
|
|
126
|
+
# 使用
|
|
127
|
+
async def process_order(order_id: str):
|
|
128
|
+
lock = DistributedLock(redis_client, f"order:{order_id}")
|
|
129
|
+
async with lock:
|
|
130
|
+
order = await get_order(order_id)
|
|
131
|
+
await process(order)
|
|
132
|
+
await save_order(order)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### 断路器模式
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
# 断路器(Circuit Breaker)
|
|
139
|
+
from enum import Enum
|
|
140
|
+
from dataclasses import dataclass
|
|
141
|
+
import time
|
|
142
|
+
import asyncio
|
|
143
|
+
|
|
144
|
+
class CircuitState(str, Enum):
|
|
145
|
+
CLOSED = "closed" # 正常:请求通过
|
|
146
|
+
OPEN = "open" # 断开:请求直接失败
|
|
147
|
+
HALF_OPEN = "half_open" # 半开:允许少量探测
|
|
148
|
+
|
|
149
|
+
@dataclass
|
|
150
|
+
class CircuitBreakerConfig:
|
|
151
|
+
failure_threshold: int = 5 # 失败次数阈值
|
|
152
|
+
recovery_timeout: float = 30.0 # 恢复超时(秒)
|
|
153
|
+
half_open_max_calls: int = 3 # 半开状态最大探测数
|
|
154
|
+
success_threshold: int = 2 # 半开状态成功阈值
|
|
155
|
+
|
|
156
|
+
class CircuitBreaker:
|
|
157
|
+
def __init__(self, name: str, config: CircuitBreakerConfig = None):
|
|
158
|
+
self.name = name
|
|
159
|
+
self.config = config or CircuitBreakerConfig()
|
|
160
|
+
self.state = CircuitState.CLOSED
|
|
161
|
+
self.failure_count = 0
|
|
162
|
+
self.success_count = 0
|
|
163
|
+
self.last_failure_time = 0.0
|
|
164
|
+
self.half_open_calls = 0
|
|
165
|
+
|
|
166
|
+
async def call(self, func, *args, **kwargs):
|
|
167
|
+
"""通过断路器执行函数"""
|
|
168
|
+
if self.state == CircuitState.OPEN:
|
|
169
|
+
if time.time() - self.last_failure_time > self.config.recovery_timeout:
|
|
170
|
+
self.state = CircuitState.HALF_OPEN
|
|
171
|
+
self.half_open_calls = 0
|
|
172
|
+
self.success_count = 0
|
|
173
|
+
else:
|
|
174
|
+
raise CircuitOpenError(f"Circuit {self.name} is open")
|
|
175
|
+
|
|
176
|
+
if self.state == CircuitState.HALF_OPEN:
|
|
177
|
+
if self.half_open_calls >= self.config.half_open_max_calls:
|
|
178
|
+
raise CircuitOpenError(f"Circuit {self.name} half-open limit reached")
|
|
179
|
+
self.half_open_calls += 1
|
|
180
|
+
|
|
181
|
+
try:
|
|
182
|
+
result = await func(*args, **kwargs)
|
|
183
|
+
self._on_success()
|
|
184
|
+
return result
|
|
185
|
+
except Exception as e:
|
|
186
|
+
self._on_failure()
|
|
187
|
+
raise
|
|
188
|
+
|
|
189
|
+
def _on_success(self):
|
|
190
|
+
if self.state == CircuitState.HALF_OPEN:
|
|
191
|
+
self.success_count += 1
|
|
192
|
+
if self.success_count >= self.config.success_threshold:
|
|
193
|
+
self.state = CircuitState.CLOSED
|
|
194
|
+
self.failure_count = 0
|
|
195
|
+
else:
|
|
196
|
+
self.failure_count = 0
|
|
197
|
+
|
|
198
|
+
def _on_failure(self):
|
|
199
|
+
self.failure_count += 1
|
|
200
|
+
self.last_failure_time = time.time()
|
|
201
|
+
if self.failure_count >= self.config.failure_threshold:
|
|
202
|
+
self.state = CircuitState.OPEN
|
|
203
|
+
|
|
204
|
+
# 使用
|
|
205
|
+
user_service_breaker = CircuitBreaker("user-service", CircuitBreakerConfig(
|
|
206
|
+
failure_threshold=5,
|
|
207
|
+
recovery_timeout=30,
|
|
208
|
+
))
|
|
209
|
+
|
|
210
|
+
async def get_user_safe(user_id: int):
|
|
211
|
+
try:
|
|
212
|
+
return await user_service_breaker.call(user_client.get_user, user_id)
|
|
213
|
+
except CircuitOpenError:
|
|
214
|
+
# 降级:返回缓存数据
|
|
215
|
+
return await cache.get(f"user:{user_id}")
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### 一致性哈希
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
# 一致性哈希(用于分布式缓存/分片)
|
|
222
|
+
import hashlib
|
|
223
|
+
from bisect import bisect_right
|
|
224
|
+
from typing import TypeVar
|
|
225
|
+
|
|
226
|
+
T = TypeVar("T")
|
|
227
|
+
|
|
228
|
+
class ConsistentHash:
|
|
229
|
+
"""一致性哈希环"""
|
|
230
|
+
|
|
231
|
+
def __init__(self, nodes: list[str] = None, virtual_nodes: int = 150):
|
|
232
|
+
self.virtual_nodes = virtual_nodes
|
|
233
|
+
self.ring: list[int] = []
|
|
234
|
+
self.node_map: dict[int, str] = {}
|
|
235
|
+
|
|
236
|
+
for node in (nodes or []):
|
|
237
|
+
self.add_node(node)
|
|
238
|
+
|
|
239
|
+
def _hash(self, key: str) -> int:
|
|
240
|
+
return int(hashlib.md5(key.encode()).hexdigest(), 16)
|
|
241
|
+
|
|
242
|
+
def add_node(self, node: str):
|
|
243
|
+
"""添加节点(含虚拟节点)"""
|
|
244
|
+
for i in range(self.virtual_nodes):
|
|
245
|
+
virtual_key = f"{node}:vn{i}"
|
|
246
|
+
hash_val = self._hash(virtual_key)
|
|
247
|
+
self.ring.append(hash_val)
|
|
248
|
+
self.node_map[hash_val] = node
|
|
249
|
+
self.ring.sort()
|
|
250
|
+
|
|
251
|
+
def remove_node(self, node: str):
|
|
252
|
+
"""移除节点"""
|
|
253
|
+
for i in range(self.virtual_nodes):
|
|
254
|
+
virtual_key = f"{node}:vn{i}"
|
|
255
|
+
hash_val = self._hash(virtual_key)
|
|
256
|
+
self.ring.remove(hash_val)
|
|
257
|
+
del self.node_map[hash_val]
|
|
258
|
+
|
|
259
|
+
def get_node(self, key: str) -> str:
|
|
260
|
+
"""获取key对应的节点"""
|
|
261
|
+
if not self.ring:
|
|
262
|
+
raise ValueError("No nodes in hash ring")
|
|
263
|
+
hash_val = self._hash(key)
|
|
264
|
+
idx = bisect_right(self.ring, hash_val) % len(self.ring)
|
|
265
|
+
return self.node_map[self.ring[idx]]
|
|
266
|
+
|
|
267
|
+
def get_nodes(self, key: str, count: int = 3) -> list[str]:
|
|
268
|
+
"""获取key对应的多个节点(副本)"""
|
|
269
|
+
if not self.ring:
|
|
270
|
+
return []
|
|
271
|
+
nodes = []
|
|
272
|
+
hash_val = self._hash(key)
|
|
273
|
+
idx = bisect_right(self.ring, hash_val)
|
|
274
|
+
|
|
275
|
+
for i in range(len(self.ring)):
|
|
276
|
+
node = self.node_map[self.ring[(idx + i) % len(self.ring)]]
|
|
277
|
+
if node not in nodes:
|
|
278
|
+
nodes.append(node)
|
|
279
|
+
if len(nodes) >= count:
|
|
280
|
+
break
|
|
281
|
+
return nodes
|
|
282
|
+
|
|
283
|
+
# 使用
|
|
284
|
+
ring = ConsistentHash(["cache-1", "cache-2", "cache-3"])
|
|
285
|
+
target = ring.get_node("user:12345") # -> "cache-2"
|
|
286
|
+
|
|
287
|
+
# 添加节点时只有少量key需要重新分配
|
|
288
|
+
ring.add_node("cache-4")
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### 向量时钟(因果一致性)
|
|
292
|
+
|
|
293
|
+
```python
|
|
294
|
+
# 向量时钟(Vector Clock)— 检测因果关系和冲突
|
|
295
|
+
from copy import deepcopy
|
|
296
|
+
|
|
297
|
+
class VectorClock:
|
|
298
|
+
"""向量时钟"""
|
|
299
|
+
|
|
300
|
+
def __init__(self, node_id: str):
|
|
301
|
+
self.node_id = node_id
|
|
302
|
+
self.clock: dict[str, int] = {}
|
|
303
|
+
|
|
304
|
+
def increment(self):
|
|
305
|
+
"""本地事件,递增自己的计数器"""
|
|
306
|
+
self.clock[self.node_id] = self.clock.get(self.node_id, 0) + 1
|
|
307
|
+
return deepcopy(self.clock)
|
|
308
|
+
|
|
309
|
+
def update(self, other_clock: dict[str, int]):
|
|
310
|
+
"""收到消息,合并时钟"""
|
|
311
|
+
for node, count in other_clock.items():
|
|
312
|
+
self.clock[node] = max(self.clock.get(node, 0), count)
|
|
313
|
+
self.increment()
|
|
314
|
+
|
|
315
|
+
@staticmethod
|
|
316
|
+
def compare(clock_a: dict, clock_b: dict) -> str:
|
|
317
|
+
"""
|
|
318
|
+
比较两个向量时钟:
|
|
319
|
+
- "before": a happened-before b
|
|
320
|
+
- "after": b happened-before a
|
|
321
|
+
- "concurrent": 并发(可能冲突)
|
|
322
|
+
- "equal": 相同
|
|
323
|
+
"""
|
|
324
|
+
all_nodes = set(clock_a.keys()) | set(clock_b.keys())
|
|
325
|
+
a_less = False
|
|
326
|
+
b_less = False
|
|
327
|
+
|
|
328
|
+
for node in all_nodes:
|
|
329
|
+
va = clock_a.get(node, 0)
|
|
330
|
+
vb = clock_b.get(node, 0)
|
|
331
|
+
if va < vb:
|
|
332
|
+
a_less = True
|
|
333
|
+
elif va > vb:
|
|
334
|
+
b_less = True
|
|
335
|
+
|
|
336
|
+
if a_less and not b_less:
|
|
337
|
+
return "before"
|
|
338
|
+
elif b_less and not a_less:
|
|
339
|
+
return "after"
|
|
340
|
+
elif not a_less and not b_less:
|
|
341
|
+
return "equal"
|
|
342
|
+
else:
|
|
343
|
+
return "concurrent" # 冲突!
|
|
344
|
+
|
|
345
|
+
# 使用
|
|
346
|
+
vc1 = VectorClock("node-1")
|
|
347
|
+
vc2 = VectorClock("node-2")
|
|
348
|
+
|
|
349
|
+
vc1.increment() # {node-1: 1}
|
|
350
|
+
vc1.increment() # {node-1: 2}
|
|
351
|
+
|
|
352
|
+
# node-1 发送消息给 node-2
|
|
353
|
+
vc2.update(vc1.clock) # {node-1: 2, node-2: 1}
|
|
354
|
+
|
|
355
|
+
# node-1 和 node-2 各自独立更新
|
|
356
|
+
vc1.increment() # {node-1: 3}
|
|
357
|
+
vc2.increment() # {node-1: 2, node-2: 2}
|
|
358
|
+
|
|
359
|
+
# 检测:这两个更新是并发的
|
|
360
|
+
result = VectorClock.compare(vc1.clock, vc2.clock) # "concurrent"
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### 重试与退避策略
|
|
364
|
+
|
|
365
|
+
```python
|
|
366
|
+
# 完整的重试策略
|
|
367
|
+
import random
|
|
368
|
+
import asyncio
|
|
369
|
+
from functools import wraps
|
|
370
|
+
|
|
371
|
+
class RetryConfig:
|
|
372
|
+
def __init__(
|
|
373
|
+
self,
|
|
374
|
+
max_retries: int = 3,
|
|
375
|
+
base_delay: float = 1.0,
|
|
376
|
+
max_delay: float = 60.0,
|
|
377
|
+
exponential_base: float = 2.0,
|
|
378
|
+
jitter: bool = True,
|
|
379
|
+
retryable_exceptions: tuple = (Exception,),
|
|
380
|
+
):
|
|
381
|
+
self.max_retries = max_retries
|
|
382
|
+
self.base_delay = base_delay
|
|
383
|
+
self.max_delay = max_delay
|
|
384
|
+
self.exponential_base = exponential_base
|
|
385
|
+
self.jitter = jitter
|
|
386
|
+
self.retryable_exceptions = retryable_exceptions
|
|
387
|
+
|
|
388
|
+
def retry(config: RetryConfig = None):
|
|
389
|
+
"""重试装饰器(指数退避+抖动)"""
|
|
390
|
+
if config is None:
|
|
391
|
+
config = RetryConfig()
|
|
392
|
+
|
|
393
|
+
def decorator(func):
|
|
394
|
+
@wraps(func)
|
|
395
|
+
async def wrapper(*args, **kwargs):
|
|
396
|
+
last_exception = None
|
|
397
|
+
for attempt in range(config.max_retries + 1):
|
|
398
|
+
try:
|
|
399
|
+
return await func(*args, **kwargs)
|
|
400
|
+
except config.retryable_exceptions as e:
|
|
401
|
+
last_exception = e
|
|
402
|
+
if attempt == config.max_retries:
|
|
403
|
+
break
|
|
404
|
+
|
|
405
|
+
# 计算延迟
|
|
406
|
+
delay = min(
|
|
407
|
+
config.base_delay * (config.exponential_base ** attempt),
|
|
408
|
+
config.max_delay,
|
|
409
|
+
)
|
|
410
|
+
if config.jitter:
|
|
411
|
+
delay = delay * (0.5 + random.random())
|
|
412
|
+
|
|
413
|
+
logger.warning(
|
|
414
|
+
"Retry attempt",
|
|
415
|
+
attempt=attempt + 1,
|
|
416
|
+
max_retries=config.max_retries,
|
|
417
|
+
delay=delay,
|
|
418
|
+
error=str(e),
|
|
419
|
+
)
|
|
420
|
+
await asyncio.sleep(delay)
|
|
421
|
+
|
|
422
|
+
raise last_exception
|
|
423
|
+
return wrapper
|
|
424
|
+
return decorator
|
|
425
|
+
|
|
426
|
+
# 使用
|
|
427
|
+
@retry(RetryConfig(
|
|
428
|
+
max_retries=3,
|
|
429
|
+
base_delay=1.0,
|
|
430
|
+
retryable_exceptions=(TimeoutError, ConnectionError),
|
|
431
|
+
))
|
|
432
|
+
async def call_external_api(url: str):
|
|
433
|
+
async with httpx.AsyncClient() as client:
|
|
434
|
+
response = await client.get(url, timeout=5.0)
|
|
435
|
+
response.raise_for_status()
|
|
436
|
+
return response.json()
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
### 分布式ID生成(Snowflake)
|
|
440
|
+
|
|
441
|
+
```python
|
|
442
|
+
# Snowflake ID生成器
|
|
443
|
+
import time
|
|
444
|
+
import threading
|
|
445
|
+
|
|
446
|
+
class SnowflakeIDGenerator:
|
|
447
|
+
"""Twitter Snowflake风格的分布式ID生成器
|
|
448
|
+
64位: 1位符号 + 41位时间戳 + 10位机器ID + 12位序列号
|
|
449
|
+
"""
|
|
450
|
+
|
|
451
|
+
EPOCH = 1704067200000 # 2024-01-01 00:00:00 UTC
|
|
452
|
+
|
|
453
|
+
def __init__(self, worker_id: int, datacenter_id: int = 0):
|
|
454
|
+
assert 0 <= worker_id < 32, "worker_id must be 0-31"
|
|
455
|
+
assert 0 <= datacenter_id < 32, "datacenter_id must be 0-31"
|
|
456
|
+
|
|
457
|
+
self.worker_id = worker_id
|
|
458
|
+
self.datacenter_id = datacenter_id
|
|
459
|
+
self.sequence = 0
|
|
460
|
+
self.last_timestamp = -1
|
|
461
|
+
self.lock = threading.Lock()
|
|
462
|
+
|
|
463
|
+
def _current_millis(self) -> int:
|
|
464
|
+
return int(time.time() * 1000)
|
|
465
|
+
|
|
466
|
+
def generate(self) -> int:
|
|
467
|
+
with self.lock:
|
|
468
|
+
timestamp = self._current_millis()
|
|
469
|
+
|
|
470
|
+
if timestamp == self.last_timestamp:
|
|
471
|
+
self.sequence = (self.sequence + 1) & 0xFFF # 4096
|
|
472
|
+
if self.sequence == 0:
|
|
473
|
+
# 等待下一毫秒
|
|
474
|
+
while timestamp <= self.last_timestamp:
|
|
475
|
+
timestamp = self._current_millis()
|
|
476
|
+
else:
|
|
477
|
+
self.sequence = 0
|
|
478
|
+
|
|
479
|
+
self.last_timestamp = timestamp
|
|
480
|
+
|
|
481
|
+
return (
|
|
482
|
+
((timestamp - self.EPOCH) << 22)
|
|
483
|
+
| (self.datacenter_id << 17)
|
|
484
|
+
| (self.worker_id << 12)
|
|
485
|
+
| self.sequence
|
|
486
|
+
)
|
|
487
|
+
|
|
488
|
+
# 使用
|
|
489
|
+
id_gen = SnowflakeIDGenerator(worker_id=1, datacenter_id=0)
|
|
490
|
+
order_id = id_gen.generate() # 18位数字ID,全局唯一,趋势递增
|
|
491
|
+
```
|
|
492
|
+
|
|
493
|
+
## 最佳实践
|
|
494
|
+
|
|
495
|
+
### 1. 网络不可靠假设
|
|
496
|
+
- 所有远程调用都要有超时
|
|
497
|
+
- 实现重试(指数退避+抖动)
|
|
498
|
+
- 断路器防止级联故障
|
|
499
|
+
- 降级策略(缓存/默认值)
|
|
500
|
+
|
|
501
|
+
### 2. 一致性选择
|
|
502
|
+
- 根据业务需求选择一致性级别
|
|
503
|
+
- 金融/支付:强一致性
|
|
504
|
+
- 社交/推荐:最终一致性
|
|
505
|
+
- 库存:因果一致性(避免超卖)
|
|
506
|
+
- 不要过度追求强一致性(代价是可用性)
|
|
507
|
+
|
|
508
|
+
### 3. 幂等性设计
|
|
509
|
+
- 所有写操作必须幂等
|
|
510
|
+
- 使用唯一请求ID去重
|
|
511
|
+
- 数据库使用UPSERT
|
|
512
|
+
- 消息消费使用去重表
|
|
513
|
+
|
|
514
|
+
### 4. 分区策略
|
|
515
|
+
- 选择好的分片键(均匀分布、查询友好)
|
|
516
|
+
- 避免跨分区事务
|
|
517
|
+
- 预留扩容空间(虚拟分片)
|
|
518
|
+
- 监控数据倾斜
|
|
519
|
+
|
|
520
|
+
### 5. 时间处理
|
|
521
|
+
- 不依赖壁钟时间排序(时钟偏差)
|
|
522
|
+
- 使用逻辑时钟(Lamport/向量时钟)
|
|
523
|
+
- NTP同步并监控偏差
|
|
524
|
+
- 使用单调时钟(monotonic clock)测量延迟
|
|
525
|
+
|
|
526
|
+
## 常见陷阱
|
|
527
|
+
|
|
528
|
+
### 陷阱1: 忽略网络分区
|
|
529
|
+
```python
|
|
530
|
+
# 错误: 假设分布式锁永远可靠
|
|
531
|
+
lock = await redis.lock("order:123")
|
|
532
|
+
# 如果Redis分区,锁可能被多个客户端同时持有
|
|
533
|
+
|
|
534
|
+
# 正确: Fencing Token
|
|
535
|
+
# 每次获取锁时附带递增的token
|
|
536
|
+
# 下游操作检查token是否最新
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
### 陷阱2: 同步调用链过长
|
|
540
|
+
```
|
|
541
|
+
# 错误: A→B→C→D→E 同步调用
|
|
542
|
+
# 总延迟 = sum(所有延迟), 可用性 = product(所有可用性)
|
|
543
|
+
# 5个服务各99.9%可用 → 总可用性 = 99.5%
|
|
544
|
+
|
|
545
|
+
# 正确: 异步解耦非关键路径
|
|
546
|
+
# A→B(同步,关键) → Event → C/D/E(异步,非关键)
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
### 陷阱3: 分布式事务
|
|
550
|
+
```python
|
|
551
|
+
# 错误: 尝试跨服务分布式事务(2PC)
|
|
552
|
+
# 性能差、可用性低、难以恢复
|
|
553
|
+
|
|
554
|
+
# 正确: 使用Saga或事件驱动最终一致性
|
|
555
|
+
# 每个服务本地事务 + 补偿操作
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
### 陷阱4: 不考虑数据倾斜
|
|
559
|
+
```python
|
|
560
|
+
# 错误: 用用户ID分片,某些大客户数据集中
|
|
561
|
+
# 导致某个分片压力远大于其他
|
|
562
|
+
|
|
563
|
+
# 正确: 监控分片大小和负载
|
|
564
|
+
# 使用组合分片键或二级分片
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
## Agent Checklist
|
|
568
|
+
|
|
569
|
+
### 基础设计
|
|
570
|
+
- [ ] CAP权衡已评估(CP/AP)
|
|
571
|
+
- [ ] 一致性模型已选择并文档化
|
|
572
|
+
- [ ] 幂等性设计覆盖所有写操作
|
|
573
|
+
- [ ] 分布式ID方案已确定
|
|
574
|
+
|
|
575
|
+
### 韧性设计
|
|
576
|
+
- [ ] 所有远程调用有超时和重试
|
|
577
|
+
- [ ] 断路器已配置
|
|
578
|
+
- [ ] 降级策略已实现
|
|
579
|
+
- [ ] 消息消费幂等
|
|
580
|
+
|
|
581
|
+
### 数据一致性
|
|
582
|
+
- [ ] 分片策略合理(键均匀分布)
|
|
583
|
+
- [ ] 跨服务一致性通过Saga/事件保证
|
|
584
|
+
- [ ] 冲突检测和解决策略已定义
|
|
585
|
+
- [ ] 时钟偏差已考虑
|
|
586
|
+
|
|
587
|
+
### 可观测性
|
|
588
|
+
- [ ] 分布式追踪贯穿全链路
|
|
589
|
+
- [ ] 一致性检查有监控
|
|
590
|
+
- [ ] 分片负载有监控
|
|
591
|
+
- [ ] 网络延迟/分区有告警
|