API Architecture and Integration Design · API Styles and Contracts

gRPC and Event-Driven APIs

Typed RPC for internal services and asynchronous event contracts.

gRPC uses Protocol Buffers over HTTP/2 for efficient, strongly typed service-to-service calls with streaming and generated clients; browsers need gRPC-Web or a gateway. Event-driven APIs publish facts ("order.paid") to a broker; consumers react independently. Document async contracts with AsyncAPI and manage schemas (Avro/Protobuf/JSON Schema) with compatibility rules in a schema registry.

// orders.proto
syntax = "proto3";
package orders.v1;

service OrderService {
  rpc GetOrder(GetOrderRequest) returns (Order);
  rpc WatchOrder(GetOrderRequest) returns (stream OrderEvent);   // server streaming
}

message GetOrderRequest { string id = 1; }
message Order {
  string id = 1;
  string status = 2;
  int64 total_minor = 3;
  string currency = 4;
  // reserved 5; never reuse field numbers of removed fields
}

Common pitfalls

  • Reusing or renumbering protobuf fields (breaks compatibility).
  • Exposing gRPC directly to browsers without a gateway.
  • Events that leak internal database structure.

Key points

  • gRPC for typed internal RPC
  • Events for decoupled reactions
  • Schema evolution rules + registry

Further reading: gRPC introduction

Practice questions

How do you evolve a protobuf message safely?

Answer

Add new fields with new numbers, never change types or reuse numbers of removed fields (mark them reserved), and keep old fields readable until all clients upgrade.

When are events preferable to synchronous API calls?

Answer

When multiple consumers react independently, the producer shouldn't depend on their availability, and eventual consistency is acceptable.

Open in the app to mark lessons complete, take notes and track your progress.