Search code examples
microservices

How to keep HTTP API in sync across different services


For example, I have Service A and Service B where B initiates HTTP requests to A.

How to ensure that the endpoint provided by service A is the same one used by service B.

An counterexample would be service A updates endpoint params, but service B does not catch it.

A remedy would be, both service A and service B import some HTTP endpoint definition from a package, which serves as the single source of truth.

I feel this part of ensuring consistency is best effort based. But I want to know if there is some practical techniques towards that purpose.


Solution

  • A shared lib/package to hold the contract as you suggest is not a guarantee as each service actually needs to import/use the latest version of this external lib/package once it has changed...

    There are a few things you can do:

    1. Contract based testing - on your CI/CD pipeline you can have service B call service A (on an integration environment) to verify that the contract still holds (connected to mocks on both sides would be easier to isolate contract test vs full integration test).
    2. A good convention for updating an API would be to keep as much backwards compatibility as possible.
    3. In case you can't be backwards compatible you can use API versioning.