TASK
Implementation
Compare gRPC and REST across multiple dimensions. Build both a REST-style and gRPC-style endpoint for the same service and measure the differences.
Implement comparison handlers:
Request: {"type": "rest_call", "msg_id": 1, "method": "GET", "path": "/api/users/1"}
Response: {"type": "rest_call_ok", "in_reply_to": 1, "status": 200, "body": {"id": 1, "name": "Alice"}, "content_type": "application/json", "size_bytes": 28}
Request: {"type": "grpc_call", "msg_id": 2, "service": "Users", "method": "GetUser", "request": {"id": 1}}
Response: {"type": "grpc_call_ok", "in_reply_to": 2, "status": "OK", "response": {"id": 1, "name": "Alice"}, "size_bytes": 9}
Request: {"type": "compare_protocols", "msg_id": 3, "num_calls": 100}
Response: {"type": "compare_protocols_ok", "in_reply_to": 3, "comparison": {
"rest": {"avg_size_bytes": 28, "avg_latency_us": 500, "browser_support": true, "code_gen": false},
"grpc": {"avg_size_bytes": 9, "avg_latency_us": 120, "browser_support": false, "code_gen": true},
"size_reduction_pct": 67.9,
"latency_reduction_pct": 76.0
}}Sample Test Cases
REST call returns JSONTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"rest_call","msg_id":2,"method":"GET","path":"/api/users/1"}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
gRPC call returns protobuf-sized responseTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1"]}}
{"src":"c1","dest":"n1","body":{"type":"grpc_call","msg_id":2,"service":"Users","method":"GetUser","request":{"id":1}}}
Expected Output
{"src": "n1", "dest": "c0", "body": {"type": "init_ok", "in_reply_to": 1, "msg_id": 0}}
Hints
Hint 1▾
gRPC uses HTTP/2 (multiplexing, header compression) while REST typically uses HTTP/1.1
Hint 2▾
Protobuf encoding is 3-10x smaller than JSON for the same data
Hint 3▾
gRPC has built-in code generation; REST requires manual client libraries
Hint 4▾
REST is more browser-friendly; gRPC requires gRPC-Web for browser clients
Hint 5▾
Compare on: serialization size, latency, tooling, debugging ease
OVERVIEW
Theoretical Hub
Concept overview coming soon
Key Concepts
RESTgRPCHTTP/2JSON vs protobufdeveloper experience
main.py
python
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env python3
import sys
import json
def main():
# Your implementation here
for line in sys.stdin:
msg = json.loads(line)
print(json.dumps(msg), flush=True)
if __name__ == "__main__":
main()