System ArchitectureSolution Architect

Explain the architectural approaches to building APIs for integrating internal and external consumers: when to use REST, gRPC, or GraphQL?

Pass interviews with Hintsage AI assistant

Answer.

The choice of API architectural style depends on scalability, speed, flexibility, compatibility requirements, and the specifics of the API consumers.

  • REST: Suitable for most internal and external integrations due to its simplicity (HTTP, JSON), idempotent operations, caching, and broad tool support.
  • gRPC: Used for high performance and bidirectional streaming, low latency, and strict schema (Protobuf). It is excellent for internal services and microservices, where performance requirements outweigh compatibility.
  • GraphQL: Allows the client to choose the data structure. It is great for external mobile and front-end clients when flexibility and traffic minimization are required.

Code example (gRPC service in Go):

// Protocol description service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } // Implementation func (s *server) SayHello(ctx context.Context, req *pb.HelloRequest) (*pb.HelloReply, error) { return &pb.HelloReply{Message: "Hello " + req.Name}, nil }

Key features:

  • Different architectural styles (REST, gRPC, GraphQL) solve different problems
  • gRPC is better for internal APIs with high speed and typing requirements
  • GraphQL is optimal for complex public APIs with many query variations and a large number of front-end consumers

Trick questions.

Can GraphQL be used to replace all REST or gRPC APIs?

No, GraphQL is not always rational — it complicates caching, logging, makes securing the API difficult, and can be excessive for simple tasks where REST or gRPC would be simpler and more reliable.

Can REST guarantee strict typing?

Only partially. REST is defined by contract consistency (Swagger/OpenAPI), but by nature works with loosely typed formats (JSON/HTTP), whereas gRPC provides strong static typing through Protobuf schemas.

In what cases is gRPC not recommended for public APIs?

gRPC is less compatible with browsers and poorly integrates with HTTP/1.1 infrastructure (e.g., proxies, firewalls). For public web APIs, REST or GraphQL is better.