247 views
Can you describe how you designed and implemented REST and gRPC APIs for internal and external platform services?
Asked at
AI answer
When designing REST and gRPC APIs for internal and external platform services, I based my approach on separation of responsibilities and requirements for performance and ease of integration.
For REST API:
- Used clear resource-oriented design with understandable URLs and HTTP methods (GET, POST, PUT, DELETE).
- Applied JSON standards for data exchange, ensuring validation and error handling with understandable status codes.
- For external services, added authentication (e.g., OAuth2 or JWT) and rate limiting.
For gRPC API:
- Defined services and methods in protobuf with clear message types.
- Used gRPC for high-performance communication between internal services, where speed and binary protocol are important.
- Implemented streaming where large data transfer or persistent connection is needed.
Example of a simple gRPC service in Go:
syntax = "proto3";
service UserService {
rpc GetUser (UserRequest) returns (UserResponse);
}
message UserRequest {
string user_id = 1;
}
message UserResponse {
string name = 1;
string email = 2;
}
In Go, I implement a server with a GetUser handler that receives user_id and returns user data.
This approach allows using REST for external clients with a convenient and understandable interface, and gRPC for internal services with high performance and strict typing.
Also asked at МТС Cloud
- Tell me about indexes in PostgreSQL: why are they needed, what types are there?
- How did you prioritize and select tasks to perform at your previous job?
- What exactly did you do in the company?
- What metrics, logs, tracing, and alerts were used in the project?
- Question about a slow service For example, when creating an order. There is a request to the service and a response, and between these two actions, we log the products ordered (for example, to count the popularity of products). The analytics service periodically works slowly or times out, and we fail to respond, losing orders. What to do to stop losing orders and money accordingly? Come up with a way to parallelize the order and analytics.