@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,390 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: caching-strategies-complete
|
|
3
|
+
title: 缓存策略完整指南
|
|
4
|
+
domain: backend
|
|
5
|
+
category: 01-standards
|
|
6
|
+
difficulty: intermediate
|
|
7
|
+
tags: [backend, caching, complete, http, redis, strategies, 应用层缓存, 概述]
|
|
8
|
+
quality_score: 70
|
|
9
|
+
last_updated: 2026-06-15
|
|
10
|
+
---
|
|
11
|
+
# 缓存策略完整指南
|
|
12
|
+
|
|
13
|
+
## 概述
|
|
14
|
+
|
|
15
|
+
缓存是提升系统性能和降低数据库负载的核心手段。正确的缓存策略可以将响应时间从秒级降至毫秒级,但错误的缓存策略会导致数据不一致、缓存雪崩等严重问题。本指南覆盖 Redis、Memcached、CDN、HTTP 缓存和应用缓存的完整策略。
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## 缓存层次架构
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
客户端 -> CDN -> 反向代理缓存 -> 应用层缓存 -> 分布式缓存 -> 数据库
|
|
23
|
+
L1 L2 L3 L4 L5 L6
|
|
24
|
+
|
|
25
|
+
L1: 浏览器缓存 (HTTP Cache-Control)
|
|
26
|
+
L2: CDN 边缘缓存 (CloudFlare/CloudFront)
|
|
27
|
+
L3: Nginx/Varnish 反向代理缓存
|
|
28
|
+
L4: 进程内缓存 (LRU/本地 Map)
|
|
29
|
+
L5: Redis/Memcached 分布式缓存
|
|
30
|
+
L6: 数据库查询缓存
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Redis 缓存
|
|
36
|
+
|
|
37
|
+
### 基本操作
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
import redis
|
|
41
|
+
import json
|
|
42
|
+
from datetime import timedelta
|
|
43
|
+
|
|
44
|
+
r = redis.Redis(host="localhost", port=6379, decode_responses=True)
|
|
45
|
+
|
|
46
|
+
# 缓存读取模式 (Cache-Aside)
|
|
47
|
+
def get_user(user_id: str) -> dict | None:
|
|
48
|
+
# 1. 先查缓存
|
|
49
|
+
cached = r.get(f"user:{user_id}")
|
|
50
|
+
if cached:
|
|
51
|
+
return json.loads(cached)
|
|
52
|
+
|
|
53
|
+
# 2. 缓存未命中,查数据库
|
|
54
|
+
user = db.query("SELECT * FROM users WHERE id = %s", user_id)
|
|
55
|
+
if user is None:
|
|
56
|
+
# 缓存空值,防止缓存穿透
|
|
57
|
+
r.set(f"user:{user_id}", json.dumps(None), ex=60)
|
|
58
|
+
return None
|
|
59
|
+
|
|
60
|
+
# 3. 写入缓存
|
|
61
|
+
r.set(f"user:{user_id}", json.dumps(user), ex=3600)
|
|
62
|
+
return user
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 缓存更新模式
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
# 模式1: Cache-Aside (旁路缓存) - 最常用
|
|
69
|
+
def update_user(user_id: str, data: dict):
|
|
70
|
+
db.execute("UPDATE users SET ... WHERE id = %s", user_id)
|
|
71
|
+
r.delete(f"user:{user_id}") # 删除缓存,下次读取时重建
|
|
72
|
+
|
|
73
|
+
# 模式2: Write-Through (直写缓存)
|
|
74
|
+
def update_user_write_through(user_id: str, data: dict):
|
|
75
|
+
db.execute("UPDATE users SET ... WHERE id = %s", user_id)
|
|
76
|
+
user = db.query("SELECT * FROM users WHERE id = %s", user_id)
|
|
77
|
+
r.set(f"user:{user_id}", json.dumps(user), ex=3600) # 同步更新缓存
|
|
78
|
+
|
|
79
|
+
# 模式3: Write-Behind (异步写回)
|
|
80
|
+
def update_user_write_behind(user_id: str, data: dict):
|
|
81
|
+
r.set(f"user:{user_id}", json.dumps(data), ex=3600)
|
|
82
|
+
queue.send("user_update", {"user_id": user_id, "data": data})
|
|
83
|
+
# 异步消费者批量写入数据库
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### 缓存穿透防护
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
import hashlib
|
|
90
|
+
|
|
91
|
+
# 布隆过滤器防穿透
|
|
92
|
+
from pybloom_live import BloomFilter
|
|
93
|
+
|
|
94
|
+
user_bloom = BloomFilter(capacity=1000000, error_rate=0.01)
|
|
95
|
+
|
|
96
|
+
# 初始化时加载所有 ID
|
|
97
|
+
for user_id in db.query("SELECT id FROM users"):
|
|
98
|
+
user_bloom.add(user_id)
|
|
99
|
+
|
|
100
|
+
def get_user_safe(user_id: str) -> dict | None:
|
|
101
|
+
# 布隆过滤器快速判断
|
|
102
|
+
if user_id not in user_bloom:
|
|
103
|
+
return None # 确定不存在
|
|
104
|
+
|
|
105
|
+
cached = r.get(f"user:{user_id}")
|
|
106
|
+
if cached == "null": # 缓存空值
|
|
107
|
+
return None
|
|
108
|
+
if cached:
|
|
109
|
+
return json.loads(cached)
|
|
110
|
+
|
|
111
|
+
user = db.query("SELECT * FROM users WHERE id = %s", user_id)
|
|
112
|
+
if user is None:
|
|
113
|
+
r.set(f"user:{user_id}", "null", ex=60) # 缓存空值 60s
|
|
114
|
+
return None
|
|
115
|
+
|
|
116
|
+
r.set(f"user:{user_id}", json.dumps(user), ex=3600)
|
|
117
|
+
return user
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 缓存雪崩防护
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
import random
|
|
124
|
+
|
|
125
|
+
def set_with_jitter(key: str, value: str, base_ttl: int):
|
|
126
|
+
"""添加随机抖动,避免大量 key 同时过期"""
|
|
127
|
+
jitter = random.randint(0, base_ttl // 10)
|
|
128
|
+
r.set(key, value, ex=base_ttl + jitter)
|
|
129
|
+
|
|
130
|
+
# 互斥锁防止缓存击穿
|
|
131
|
+
def get_hot_data(key: str) -> dict:
|
|
132
|
+
cached = r.get(key)
|
|
133
|
+
if cached:
|
|
134
|
+
return json.loads(cached)
|
|
135
|
+
|
|
136
|
+
lock_key = f"lock:{key}"
|
|
137
|
+
if r.set(lock_key, "1", nx=True, ex=10): # 获取锁
|
|
138
|
+
try:
|
|
139
|
+
data = db.query_heavy_data(key)
|
|
140
|
+
r.set(key, json.dumps(data), ex=3600)
|
|
141
|
+
return data
|
|
142
|
+
finally:
|
|
143
|
+
r.delete(lock_key)
|
|
144
|
+
else:
|
|
145
|
+
# 未获取锁,等待后重试
|
|
146
|
+
time.sleep(0.1)
|
|
147
|
+
return get_hot_data(key)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Redis 数据结构应用
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
# 排行榜 (Sorted Set)
|
|
154
|
+
r.zadd("leaderboard", {"user:1": 100, "user:2": 85, "user:3": 92})
|
|
155
|
+
top_10 = r.zrevrange("leaderboard", 0, 9, withscores=True)
|
|
156
|
+
|
|
157
|
+
# 计数器 (String + INCR)
|
|
158
|
+
r.incr("page_views:homepage")
|
|
159
|
+
r.incrby("api_calls:today", 1)
|
|
160
|
+
|
|
161
|
+
# 分布式锁
|
|
162
|
+
def acquire_lock(resource: str, ttl: int = 10) -> str | None:
|
|
163
|
+
token = str(uuid.uuid4())
|
|
164
|
+
if r.set(f"lock:{resource}", token, nx=True, ex=ttl):
|
|
165
|
+
return token
|
|
166
|
+
return None
|
|
167
|
+
|
|
168
|
+
def release_lock(resource: str, token: str):
|
|
169
|
+
script = """
|
|
170
|
+
if redis.call("get", KEYS[1]) == ARGV[1] then
|
|
171
|
+
return redis.call("del", KEYS[1])
|
|
172
|
+
end
|
|
173
|
+
return 0
|
|
174
|
+
"""
|
|
175
|
+
r.eval(script, 1, f"lock:{resource}", token)
|
|
176
|
+
|
|
177
|
+
# 会话存储 (Hash)
|
|
178
|
+
r.hset("session:abc123", mapping={
|
|
179
|
+
"user_id": "123",
|
|
180
|
+
"role": "admin",
|
|
181
|
+
"login_at": str(int(time.time())),
|
|
182
|
+
})
|
|
183
|
+
r.expire("session:abc123", 3600)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
---
|
|
187
|
+
|
|
188
|
+
## HTTP 缓存
|
|
189
|
+
|
|
190
|
+
### Cache-Control 策略
|
|
191
|
+
|
|
192
|
+
```nginx
|
|
193
|
+
# 不可变资源(带 hash 的静态文件)
|
|
194
|
+
location /assets/ {
|
|
195
|
+
add_header Cache-Control "public, max-age=31536000, immutable";
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
# HTML 入口文件(总是验证)
|
|
199
|
+
location / {
|
|
200
|
+
add_header Cache-Control "no-cache";
|
|
201
|
+
etag on;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
# API 响应(私有短期缓存)
|
|
205
|
+
location /api/ {
|
|
206
|
+
add_header Cache-Control "private, max-age=0, must-revalidate";
|
|
207
|
+
add_header Vary "Authorization, Accept-Encoding";
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
# 图片(公共中期缓存)
|
|
211
|
+
location /images/ {
|
|
212
|
+
add_header Cache-Control "public, max-age=86400, stale-while-revalidate=3600";
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### ETag 与条件请求
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
# FastAPI ETag 实现
|
|
220
|
+
from fastapi import Request, Response
|
|
221
|
+
import hashlib
|
|
222
|
+
|
|
223
|
+
@app.get("/api/products/{product_id}")
|
|
224
|
+
async def get_product(product_id: str, request: Request, response: Response):
|
|
225
|
+
product = await db.get_product(product_id)
|
|
226
|
+
etag = hashlib.md5(json.dumps(product).encode()).hexdigest()
|
|
227
|
+
|
|
228
|
+
if request.headers.get("if-none-match") == etag:
|
|
229
|
+
return Response(status_code=304)
|
|
230
|
+
|
|
231
|
+
response.headers["ETag"] = etag
|
|
232
|
+
response.headers["Cache-Control"] = "private, max-age=60"
|
|
233
|
+
return product
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## CDN 缓存
|
|
239
|
+
|
|
240
|
+
### CDN 配置策略
|
|
241
|
+
|
|
242
|
+
```yaml
|
|
243
|
+
# CloudFlare Page Rules 示例
|
|
244
|
+
rules:
|
|
245
|
+
- match: "*.example.com/assets/*"
|
|
246
|
+
cache_level: cache_everything
|
|
247
|
+
edge_cache_ttl: 2592000 # 30 天
|
|
248
|
+
|
|
249
|
+
- match: "*.example.com/api/*"
|
|
250
|
+
cache_level: bypass # API 不缓存
|
|
251
|
+
|
|
252
|
+
- match: "*.example.com/"
|
|
253
|
+
cache_level: cache_everything
|
|
254
|
+
edge_cache_ttl: 300 # 5 分钟
|
|
255
|
+
browser_cache_ttl: 0
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### CDN 缓存失效
|
|
259
|
+
|
|
260
|
+
```python
|
|
261
|
+
import requests
|
|
262
|
+
|
|
263
|
+
def purge_cdn_cache(urls: list[str]):
|
|
264
|
+
"""CloudFlare 缓存清除"""
|
|
265
|
+
requests.post(
|
|
266
|
+
f"https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/purge_cache",
|
|
267
|
+
headers={"Authorization": f"Bearer {CF_TOKEN}"},
|
|
268
|
+
json={"files": urls},
|
|
269
|
+
)
|
|
270
|
+
|
|
271
|
+
# 部署后自动清除
|
|
272
|
+
def post_deploy():
|
|
273
|
+
purge_cdn_cache([
|
|
274
|
+
"https://example.com/",
|
|
275
|
+
"https://example.com/manifest.json",
|
|
276
|
+
])
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
---
|
|
280
|
+
|
|
281
|
+
## 应用层缓存
|
|
282
|
+
|
|
283
|
+
### Python LRU 缓存
|
|
284
|
+
|
|
285
|
+
```python
|
|
286
|
+
from functools import lru_cache
|
|
287
|
+
from cachetools import TTLCache
|
|
288
|
+
|
|
289
|
+
# 简单 LRU
|
|
290
|
+
@lru_cache(maxsize=1000)
|
|
291
|
+
def get_config(key: str) -> str:
|
|
292
|
+
return db.query_config(key)
|
|
293
|
+
|
|
294
|
+
# TTL 缓存
|
|
295
|
+
config_cache = TTLCache(maxsize=500, ttl=300)
|
|
296
|
+
|
|
297
|
+
def get_setting(key: str) -> str:
|
|
298
|
+
if key in config_cache:
|
|
299
|
+
return config_cache[key]
|
|
300
|
+
value = db.query_setting(key)
|
|
301
|
+
config_cache[key] = value
|
|
302
|
+
return value
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### Node.js 本地缓存
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
import NodeCache from "node-cache";
|
|
309
|
+
|
|
310
|
+
const localCache = new NodeCache({
|
|
311
|
+
stdTTL: 300, // 默认 5 分钟
|
|
312
|
+
checkperiod: 60, // 每分钟清理过期
|
|
313
|
+
maxKeys: 10000,
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
async function getProduct(id: string): Promise<Product> {
|
|
317
|
+
const cached = localCache.get<Product>(`product:${id}`);
|
|
318
|
+
if (cached) return cached;
|
|
319
|
+
|
|
320
|
+
const product = await db.product.findUnique({ where: { id } });
|
|
321
|
+
if (product) {
|
|
322
|
+
localCache.set(`product:${id}`, product, 600);
|
|
323
|
+
}
|
|
324
|
+
return product;
|
|
325
|
+
}
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
---
|
|
329
|
+
|
|
330
|
+
## 缓存失效策略
|
|
331
|
+
|
|
332
|
+
| 策略 | 说明 | 适用场景 |
|
|
333
|
+
|------|------|----------|
|
|
334
|
+
| TTL 过期 | 设置固定过期时间 | 通用场景 |
|
|
335
|
+
| 主动删除 | 数据变更时删除缓存 | 一致性要求高 |
|
|
336
|
+
| 事件驱动失效 | 监听变更事件清除 | 微服务架构 |
|
|
337
|
+
| 版本号方案 | Key 包含版本号 | 批量失效 |
|
|
338
|
+
| LRU 淘汰 | 空间满时淘汰最久未用 | 内存受限 |
|
|
339
|
+
|
|
340
|
+
```python
|
|
341
|
+
# 事件驱动缓存失效
|
|
342
|
+
async def on_user_updated(event: UserUpdatedEvent):
|
|
343
|
+
# 清除相关缓存
|
|
344
|
+
r.delete(f"user:{event.user_id}")
|
|
345
|
+
r.delete(f"user_profile:{event.user_id}")
|
|
346
|
+
# 清除列表缓存(版本号方案)
|
|
347
|
+
r.incr("users_list_version")
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
---
|
|
351
|
+
|
|
352
|
+
## 监控指标
|
|
353
|
+
|
|
354
|
+
| 指标 | 说明 | 目标 |
|
|
355
|
+
|------|------|------|
|
|
356
|
+
| Hit Rate | 缓存命中率 | > 90% |
|
|
357
|
+
| Miss Rate | 缓存未命中率 | < 10% |
|
|
358
|
+
| Eviction Rate | 淘汰率 | 低且稳定 |
|
|
359
|
+
| Memory Usage | 内存使用量 | < 80% maxmemory |
|
|
360
|
+
| Latency P99 | 缓存访问延迟 | < 5ms |
|
|
361
|
+
|
|
362
|
+
---
|
|
363
|
+
|
|
364
|
+
## 常见反模式
|
|
365
|
+
|
|
366
|
+
| 反模式 | 问题 | 正确做法 |
|
|
367
|
+
|--------|------|----------|
|
|
368
|
+
| 缓存所有数据 | 内存浪费 | 只缓存热点和高频数据 |
|
|
369
|
+
| TTL 统一设置 | 缓存雪崩 | 添加随机抖动 |
|
|
370
|
+
| 不缓存空值 | 缓存穿透 | 空值短 TTL 缓存 |
|
|
371
|
+
| 先更新缓存再更新 DB | 数据不一致 | 先更新 DB,再删除缓存 |
|
|
372
|
+
| 缓存 Key 无前缀 | 命名冲突 | 统一前缀规范 |
|
|
373
|
+
| 不设 maxmemory | OOM | 配置 maxmemory + 淘汰策略 |
|
|
374
|
+
|
|
375
|
+
---
|
|
376
|
+
|
|
377
|
+
## Agent Checklist
|
|
378
|
+
|
|
379
|
+
- [ ] 明确缓存分层策略(客户端/CDN/反向代理/应用/分布式)
|
|
380
|
+
- [ ] 使用 Cache-Aside 模式(先查缓存,未命中查 DB,回填缓存)
|
|
381
|
+
- [ ] 数据更新时先更新数据库,再删除缓存
|
|
382
|
+
- [ ] 缓存 TTL 添加随机抖动防止雪崩
|
|
383
|
+
- [ ] 缓存空值防止穿透(短 TTL)
|
|
384
|
+
- [ ] 热点 Key 使用互斥锁防止击穿
|
|
385
|
+
- [ ] Redis maxmemory 和淘汰策略已配置
|
|
386
|
+
- [ ] 缓存 Key 命名规范(前缀:实体:ID)
|
|
387
|
+
- [ ] 静态资源带 hash,Cache-Control: immutable
|
|
388
|
+
- [ ] HTML 使用 no-cache + ETag
|
|
389
|
+
- [ ] 接入缓存命中率监控,目标 > 90%
|
|
390
|
+
- [ ] 部署时自动清除 CDN 缓存
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: config-and-observability
|
|
3
|
+
title: 配置管理与可观测性规范(商业级必读)
|
|
4
|
+
domain: backend
|
|
5
|
+
category: 01-standards
|
|
6
|
+
difficulty: intermediate
|
|
7
|
+
tags: [配置, config, 12-factor, 环境变量, 密钥, 日志, logging, 指标, metrics, 追踪, tracing, 健康检查, 优雅停机, 商业级]
|
|
8
|
+
quality_score: 95
|
|
9
|
+
last_updated: 2026-06-19
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# 配置管理与可观测性规范(商业级必读)
|
|
13
|
+
|
|
14
|
+
> 商业系统要**可配置、可观测、可运维**。配置外置(12-Factor)、结构化日志、指标、追踪、健康检查、优雅停机——线上出问题时能快速定位,是商业级与玩具的分水岭。
|
|
15
|
+
|
|
16
|
+
## 1. 配置管理(12-Factor)
|
|
17
|
+
|
|
18
|
+
- **配置与代码分离**:所有环境差异(DB 地址、密钥、开关、外部 URL)从**环境变量**读,绝不硬编码、绝不写死在代码里按环境 if。
|
|
19
|
+
- 启动时**校验必需配置**,缺失立即失败并给清晰错误(fail-fast),不要运行到一半才崩。
|
|
20
|
+
- 提供 `.env.example` 列出所有变量(占位值);`.env` 入 gitignore。
|
|
21
|
+
- 密钥走密钥管理(Vault/KMS/云 secret),不进仓库、不进镜像、不进日志。
|
|
22
|
+
- 配置有合理默认 + 类型校验(如端口是数字);分环境(dev/staging/prod)但同一份代码。
|
|
23
|
+
|
|
24
|
+
## 2. 结构化日志
|
|
25
|
+
|
|
26
|
+
- 日志输出**结构化 JSON**(而非纯字符串拼接),便于检索聚合。
|
|
27
|
+
- 每条日志带:时间、级别、**request_id/trace_id**、服务名、关键上下文(userId、orderId)。
|
|
28
|
+
- 级别用对:DEBUG(排查) / INFO(关键业务事件) / WARN(可恢复异常) / ERROR(需关注)。生产默认 INFO。
|
|
29
|
+
- **绝不打印**密码、token、密钥、完整 PII(脱敏)。
|
|
30
|
+
- 日志写 stdout/stderr,由平台收集(12-Factor),不自己管理日志文件轮转。
|
|
31
|
+
- 请求贯穿同一 request_id,从入口到各层到响应可串联。
|
|
32
|
+
|
|
33
|
+
## 3. 指标(Metrics)
|
|
34
|
+
|
|
35
|
+
- 暴露关键指标(Prometheus 风格):
|
|
36
|
+
- **RED**(面向请求的服务):Rate(请求量)、Errors(错误率)、Duration(延迟分布 p50/p95/p99)。
|
|
37
|
+
- **USE**(面向资源):Utilization、Saturation、Errors。
|
|
38
|
+
- 业务指标:下单数、支付成功率、注册转化等。
|
|
39
|
+
- 延迟看分位数(p95/p99),不要只看平均。
|
|
40
|
+
|
|
41
|
+
## 4. 分布式追踪(Tracing)
|
|
42
|
+
|
|
43
|
+
- 跨服务调用传递 trace context(OpenTelemetry / W3C traceparent),串联一次请求经过的所有服务/DB/外部调用。
|
|
44
|
+
- 关键 span 标注耗时与状态,定位慢在哪一环。
|
|
45
|
+
|
|
46
|
+
## 5. 健康检查与优雅停机
|
|
47
|
+
|
|
48
|
+
- 提供 **liveness**(进程活着)与 **readiness**(依赖就绪、可接流量)端点,供 k8s/LB 探测。
|
|
49
|
+
- **优雅停机**:收到 SIGTERM 后停止接新请求、处理完在途请求、关闭连接池/释放资源再退出,避免请求被硬切。
|
|
50
|
+
- 启动顺序:先连依赖(DB/缓存)就绪再标 ready。
|
|
51
|
+
|
|
52
|
+
## 6. 错误与告警
|
|
53
|
+
|
|
54
|
+
- 未捕获异常集中处理 + 上报(Sentry 类);带 request_id 可回溯。
|
|
55
|
+
- 对关键指标设告警阈值(错误率、p99 延迟、队列积压、支付失败率)。
|
|
56
|
+
- 错误响应对客户端模糊(500 不泄露内部),日志记完整上下文。
|
|
57
|
+
|
|
58
|
+
## 7. 反模式(出现即不合格)
|
|
59
|
+
|
|
60
|
+
- 配置硬编码、按环境 if、密钥进代码/镜像。
|
|
61
|
+
- 缺失配置运行到一半才崩(没 fail-fast)。
|
|
62
|
+
- 纯字符串日志、无 request_id、无法串联一次请求。
|
|
63
|
+
- 日志打印密钥/PII。
|
|
64
|
+
- 只有平均延迟、没有 p95/p99;没有错误率/业务指标。
|
|
65
|
+
- 没有健康检查;停机硬切导致在途请求失败。
|
|
66
|
+
|
|
67
|
+
## 8. 最低交付 checklist
|
|
68
|
+
|
|
69
|
+
- [ ] 配置全从 env 读、启动 fail-fast 校验、提供 .env.example、密钥走密钥管理不入仓库。
|
|
70
|
+
- [ ] 结构化 JSON 日志,带 request_id/trace_id 与上下文,脱敏,输出 stdout。
|
|
71
|
+
- [ ] 暴露 RED/USE + 关键业务指标,延迟看 p95/p99。
|
|
72
|
+
- [ ] 跨服务传递 trace context(OpenTelemetry)。
|
|
73
|
+
- [ ] liveness/readiness 端点 + 收到 SIGTERM 优雅停机。
|
|
74
|
+
- [ ] 未捕获异常上报告警;关键指标设阈值告警。
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
**参考**:12-Factor App、OpenTelemetry、Google SRE(RED/USE、SLI/SLO)、Prometheus。
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: data-modeling-and-persistence
|
|
3
|
+
title: 数据建模与持久化规范(商业级后端必读)
|
|
4
|
+
domain: backend
|
|
5
|
+
category: 01-standards
|
|
6
|
+
difficulty: intermediate
|
|
7
|
+
tags: [数据建模, 数据库, 持久化, schema, 迁移, migration, 索引, index, 事务, transaction, n+1, 软删除, 并发, 商业级]
|
|
8
|
+
quality_score: 95
|
|
9
|
+
last_updated: 2026-06-19
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# 数据建模与持久化规范(商业级后端必读)
|
|
13
|
+
|
|
14
|
+
> 框架/数据库无关的持久化硬性规范。数据层一旦设计错,后期改动代价极高。建表、迁移、索引、事务、并发都要按本标准来。
|
|
15
|
+
|
|
16
|
+
## 1. Schema 设计
|
|
17
|
+
|
|
18
|
+
- 每张表有**主键**:用 UUID(分布式友好、不暴露规模)或自增 BigInt(单库简单);全项目统一一种风格。
|
|
19
|
+
- 必备审计列:`created_at`、`updated_at`(带时区,UTC 存储);需要追溯的加 `created_by`/`updated_by`。
|
|
20
|
+
- 字段类型选对:金额用 `DECIMAL`/整数最小单位(**绝不用 float/double 存钱**);时间用 `timestamptz`;枚举用受约束的字符串或原生 enum,不要用魔法数字。
|
|
21
|
+
- **非空约束 + 默认值**显式声明;该唯一的加唯一约束(如 email),别只靠应用层查重(有并发漏洞)。
|
|
22
|
+
- 外键 + 级联策略明确(`ON DELETE RESTRICT/CASCADE/SET NULL`);关系正确(1:1 / 1:N / N:M 用中间表)。
|
|
23
|
+
- 命名一致:表用复数 snake_case(`order_items`),列 snake_case,外键 `<entity>_id`。
|
|
24
|
+
|
|
25
|
+
## 2. 范式与反范式
|
|
26
|
+
|
|
27
|
+
- 默认第三范式(消除冗余、保证一致性)。
|
|
28
|
+
- 仅在有明确读性能证据时,针对性反范式(冗余字段/物化视图),并写清同步策略,别一上来就反范式。
|
|
29
|
+
- JSON 列适合稀疏/可变结构(设置、元数据),但**不要**把核心可查询业务字段塞进 JSON(无法建索引、难约束)。
|
|
30
|
+
|
|
31
|
+
## 3. 迁移(Migration)—— 数据库变更的唯一通道
|
|
32
|
+
|
|
33
|
+
- 所有 schema 变更走**版本化迁移文件**(Flyway/Liquibase/Prisma/Alembic/TypeORM migrations),纳入版本控制,**禁止手改生产库**。
|
|
34
|
+
- 迁移要**可前滚**,关键变更准备回滚方案;一个迁移聚焦一件事。
|
|
35
|
+
- **向后兼容的扩展式变更**(expand/contract):加列→双写→回填→切读→删旧列,分多步上线,避免停机和锁表。
|
|
36
|
+
- 大表加索引/改列用在线 DDL(`CREATE INDEX CONCURRENTLY` 等),避免长事务锁表。
|
|
37
|
+
- 回填大数据分批,别一条 SQL 锁全表。
|
|
38
|
+
|
|
39
|
+
## 4. 索引
|
|
40
|
+
|
|
41
|
+
- 给**高频查询条件、外键、排序字段、唯一约束**建索引。
|
|
42
|
+
- 复合索引遵循最左前缀;覆盖索引减少回表。
|
|
43
|
+
- 不要无脑全建——索引拖慢写入、占空间;按真实查询建。
|
|
44
|
+
- 定期看慢查询日志 + `EXPLAIN`,针对性优化,而非凭感觉。
|
|
45
|
+
|
|
46
|
+
## 5. 事务与一致性
|
|
47
|
+
|
|
48
|
+
- 事务边界在**服务层**(一个用例一个事务),不在 repository、不在 controller。
|
|
49
|
+
- 事务尽量短:把外部调用(发邮件、调第三方)移出事务,失败用补偿/重试,别让外部 IO 拖长锁。
|
|
50
|
+
- 并发写用乐观锁(`version`/`updated_at` 比对,冲突 409)或必要时悲观锁(`SELECT ... FOR UPDATE`)。
|
|
51
|
+
- 跨服务一致性用 Saga/事件 + 幂等消费,而非分布式 2PC。
|
|
52
|
+
- 钱/库存等关键不变量在 DB 层兜底(约束、唯一索引、`CHECK`),不只靠应用逻辑。
|
|
53
|
+
|
|
54
|
+
## 6. 查询性能:消灭 N+1
|
|
55
|
+
|
|
56
|
+
- **N+1 是后端头号性能杀手**:循环里逐条查关联 = N+1 次查询。
|
|
57
|
+
- 解决:预加载(eager load / `JOIN` / `include` / `selectinload`)、批量查(`WHERE id IN (...)`)、DataLoader 批处理。
|
|
58
|
+
- 列表接口务必检查是否产生 N+1;ORM 默认懒加载要警惕。
|
|
59
|
+
- 只查需要的列,别 `SELECT *` 拉大对象。
|
|
60
|
+
|
|
61
|
+
## 7. 软删除与数据生命周期
|
|
62
|
+
|
|
63
|
+
- 需要追溯/合规的数据用**软删除**(`deleted_at` 置时间),查询默认过滤;硬删除仅用于真正一次性数据。
|
|
64
|
+
- 唯一约束要考虑软删除(如 email 唯一 + 未删除)。
|
|
65
|
+
- 明确数据保留策略与归档;个人数据遵守合规(可删除/可导出)。
|
|
66
|
+
|
|
67
|
+
## 8. 仓储层(Repository)边界
|
|
68
|
+
|
|
69
|
+
- Repository 只做持久化:领域对象 ↔ 持久化模型互转、封装查询,**不含业务规则**、**不 commit 事务**。
|
|
70
|
+
- 领域层不感知 ORM;查询构造、SQL、ORM API 都关在 repository 内。
|
|
71
|
+
- 复杂查询给清晰命名的方法(`findOpenOrdersByUser`),别让上层拼条件。
|
|
72
|
+
|
|
73
|
+
## 9. 反模式(出现即不合格)
|
|
74
|
+
|
|
75
|
+
- 手改生产库、不走迁移;迁移不可回滚、一次改一大堆。
|
|
76
|
+
- 用 float 存钱;时间不带时区。
|
|
77
|
+
- 唯一性只靠应用层查重(并发下重复)。
|
|
78
|
+
- 列表接口 N+1;`SELECT *`;无索引的高频查询。
|
|
79
|
+
- 事务写在 controller/repository;事务里调外部服务拖长锁。
|
|
80
|
+
- 核心业务字段塞 JSON 列无法查询/约束。
|
|
81
|
+
- 一把梭硬删除导致无法追溯/合规。
|
|
82
|
+
|
|
83
|
+
## 10. 最低交付 checklist
|
|
84
|
+
|
|
85
|
+
- [ ] 主键 + created_at/updated_at(UTC) + 非空/默认/唯一约束 + 外键级联明确。
|
|
86
|
+
- [ ] 金额非 float、时间带时区、枚举受约束。
|
|
87
|
+
- [ ] 所有 schema 变更走版本化迁移、可回滚、大表在线 DDL/分批回填。
|
|
88
|
+
- [ ] 高频查询/外键/排序有索引;列表无 N+1(预加载/批量)。
|
|
89
|
+
- [ ] 事务边界在服务层、短事务、关键写幂等+乐观锁、DB 层兜底不变量。
|
|
90
|
+
- [ ] 需追溯数据软删除;唯一约束考虑软删除;合规可删可导出。
|
|
91
|
+
- [ ] Repository 只做持久化、不含业务、不 commit。
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
**参考**:数据库范式、Expand/Contract 迁移模式、Use The Index Luke、Saga 模式、12-Factor(后端服务无状态、配置外置)。
|