0
0
Microservicessystem_design~25 mins

Contract testing (Pact) in Microservices - System Design Exercise

Choose your learning style9 modes available
Design: Contract Testing System using Pact
Design the contract testing system architecture using Pact for microservices communication validation. Out of scope: detailed implementation of microservices themselves, network infrastructure.
Functional Requirements
FR1: Ensure that microservices can communicate correctly by verifying API contracts.
FR2: Allow consumer services to define expectations for provider services.
FR3: Enable provider services to verify they meet consumer expectations before deployment.
FR4: Support automated contract verification in CI/CD pipelines.
FR5: Provide clear reports on contract test results for developers.
FR6: Handle multiple versions of contracts for backward compatibility.
Non-Functional Requirements
NFR1: Support up to 100 microservices communicating asynchronously and synchronously.
NFR2: Contract verification latency should be under 1 minute in CI/CD pipelines.
NFR3: System availability target of 99.9% for contract repository and verification services.
NFR4: Contracts must be stored securely and versioned.
NFR5: Testing should not block production deployments unless contract violations occur.
Think Before You Design
Questions to Ask
❓ Question 1
❓ Question 2
❓ Question 3
❓ Question 4
❓ Question 5
❓ Question 6
Key Components
Contract Repository (storage and versioning)
Consumer Test Suite (generates contracts)
Provider Verification Service
CI/CD Pipeline Integration
Notification and Reporting System
API Gateway or Service Mesh (optional for runtime contract enforcement)
Design Patterns
Consumer-Driven Contract Testing
Publish-Subscribe for contract updates
Versioning and Compatibility Management
Continuous Integration and Continuous Deployment
Fail-fast deployment on contract violation
Reference Architecture
  +-------------------+       +---------------------+       +---------------------+
  |                   |       |                     |       |                     |
  | Consumer Service 1|       | Consumer Service N  |       | Provider Service(s) |
  |  (Generates Pact) |       |  (Generates Pact)   |       |  (Verifies Pact)    |
  +---------+---------+       +----------+----------+       +----------+----------+
            |                            |                             |
            |                            |                             |
            |                            |                             |
            |                            |                             |
            v                            v                             v
  +----------------------------------------------------------------------------------+
  |                          Contract Repository (Pact Broker)                      |
  |  - Stores contracts securely and versioned                                     |
  |  - Allows providers to fetch contracts for verification                         |
  +----------------------------------------------------------------------------------+
                                         |
                                         |
                                         v
                          +-------------------------------+
                          | CI/CD Pipeline Integration     |
                          | - Runs consumer tests to publish|
                          |   contracts                     |
                          | - Runs provider verification    |
                          |   against contracts             |
                          +-------------------------------+
                                         |
                                         v
                          +-------------------------------+
                          | Notification & Reporting System |
                          | - Alerts on contract violations |
                          | - Provides test reports          |
                          +-------------------------------+
Components
Consumer Service
Any microservice framework
Generates Pact contracts describing expected interactions with provider services.
Provider Service
Any microservice framework
Verifies that the provider meets the contract expectations defined by consumers.
Contract Repository (Pact Broker)
Pact Broker (open-source or managed)
Stores, versions, and shares contracts between consumers and providers.
CI/CD Pipeline
Jenkins, GitHub Actions, GitLab CI, or similar
Automates running consumer tests to publish contracts and provider verification tests.
Notification & Reporting System
Email, Slack, or monitoring tools integration
Sends alerts and reports on contract test results to developers and teams.
Request Flow
1. 1. Consumer service runs tests that generate Pact contracts describing expected API interactions.
2. 2. Generated contracts are published to the Contract Repository (Pact Broker) with versioning metadata.
3. 3. Provider service fetches the latest relevant contracts from the Contract Repository.
4. 4. Provider runs verification tests against its API to ensure it meets consumer expectations.
5. 5. Verification results are sent back to the CI/CD pipeline.
6. 6. If verification passes, deployment proceeds; if it fails, alerts are sent via Notification System.
7. 7. Developers review reports and fix contract violations before redeploying.
Database Schema
Entities: - Contract: id, consumer_name, provider_name, version, content (JSON), created_at - VerificationResult: id, contract_id, provider_version, status (pass/fail), run_at, logs - Service: id, name, type (consumer/provider), created_at Relationships: - Contract links one Consumer Service to one Provider Service - VerificationResult links to one Contract and one Provider Service version
Scaling Discussion
Bottlenecks
Contract Repository performance under many concurrent reads/writes.
CI/CD pipeline delays due to large number of contract verifications.
Managing contract version explosion with many microservices and versions.
Notification overload from frequent contract failures.
Ensuring contract verification does not block critical deployments.
Solutions
Use scalable storage backend for Pact Broker with caching and CDN for contract retrieval.
Parallelize contract verification jobs in CI/CD pipelines and use incremental verification.
Implement contract version pruning and compatibility rules to reduce versions tested.
Aggregate notifications and use thresholds to reduce alert noise.
Set deployment policies to allow non-blocking warnings for minor contract changes and block only critical failures.
Interview Tips
Time: Spend 10 minutes understanding requirements and clarifying scope, 20 minutes designing architecture and data flow, 10 minutes discussing scaling and trade-offs, 5 minutes summarizing.
Explain consumer-driven contract testing concept clearly.
Describe how Pact Broker manages contract sharing and versioning.
Show understanding of CI/CD integration for automated verification.
Discuss handling backward compatibility and version management.
Highlight importance of fast feedback and developer notifications.
Address scaling challenges realistically with practical solutions.