What is gRPC?

gRPC is a high-performance RPC framework that uses Protocol Buffers

gRPC is a modern, open-source, high-performance Remote Procedure Call (RPC) framework that can run in any environment. It uses Protocol Buffers (protobuf) as the interface definition language and supports multiple programming languages. Fern is compatible with gRPC services and can generate SDKs and documentation from your .proto files.

Need help getting started with gRPC and Fern? Get live support here

Below is an example of a gRPC service definition:

user_service.proto
1syntax = "proto3";
2
3package userservice.v1;
4
5// User service for managing user accounts
6service UserService {
7 // Create a new user account
8 rpc CreateUser(CreateUserRequest) returns (User);
9
10 // Get user by ID
11 rpc GetUser(GetUserRequest) returns (User);
12
13 // List users with pagination
14 rpc ListUsers(ListUsersRequest) returns (ListUsersResponse);
15
16 // Update user information
17 rpc UpdateUser(UpdateUserRequest) returns (User);
18
19 // Delete a user account
20 rpc DeleteUser(DeleteUserRequest) returns (google.protobuf.Empty);
21
22 // Stream user events (server streaming)
23 rpc StreamUserEvents(StreamUserEventsRequest) returns (stream UserEvent);
24
25 // Upload user data (client streaming)
26 rpc UploadUserData(stream UploadUserDataRequest) returns (UploadUserDataResponse);
27
28 // Real-time chat (bidirectional streaming)
29 rpc Chat(stream ChatMessage) returns (stream ChatMessage);
30}
31
32// Messages for user operations
33message User {
34 string id = 1;
35 string email = 2;
36 string name = 3;
37 int32 age = 4;
38 google.protobuf.Timestamp created_at = 5;
39 UserStatus status = 6;
40}
41
42message CreateUserRequest {
43 string email = 1;
44 string name = 2;
45 int32 age = 3;
46}
47
48message GetUserRequest {
49 string id = 1;
50}
51
52message ListUsersRequest {
53 int32 page_size = 1;
54 string page_token = 2;
55 string filter = 3;
56}
57
58message ListUsersResponse {
59 repeated User users = 1;
60 string next_page_token = 2;
61 int32 total_count = 3;
62}
63
64message UpdateUserRequest {
65 string id = 1;
66 User user = 2;
67 google.protobuf.FieldMask update_mask = 3;
68}
69
70message DeleteUserRequest {
71 string id = 1;
72}
73
74message StreamUserEventsRequest {
75 string user_id = 1;
76 repeated UserEventType event_types = 2;
77}
78
79message UserEvent {
80 string id = 1;
81 string user_id = 2;
82 UserEventType type = 3;
83 google.protobuf.Timestamp timestamp = 4;
84 google.protobuf.Any data = 5;
85}
86
87message UploadUserDataRequest {
88 oneof data {
89 UserDataChunk chunk = 1;
90 UserDataMetadata metadata = 2;
91 }
92}
93
94message UserDataChunk {
95 bytes data = 1;
96 int64 offset = 2;
97}
98
99message UserDataMetadata {
100 string filename = 1;
101 int64 total_size = 2;
102 string content_type = 3;
103}
104
105message UploadUserDataResponse {
106 string file_id = 1;
107 int64 bytes_uploaded = 2;
108}
109
110message ChatMessage {
111 string id = 1;
112 string user_id = 2;
113 string room_id = 3;
114 string content = 4;
115 google.protobuf.Timestamp timestamp = 5;
116 ChatMessageType type = 6;
117}
118
119// Enums
120enum UserStatus {
121 USER_STATUS_UNSPECIFIED = 0;
122 USER_STATUS_ACTIVE = 1;
123 USER_STATUS_INACTIVE = 2;
124 USER_STATUS_SUSPENDED = 3;
125}
126
127enum UserEventType {
128 USER_EVENT_TYPE_UNSPECIFIED = 0;
129 USER_EVENT_TYPE_CREATED = 1;
130 USER_EVENT_TYPE_UPDATED = 2;
131 USER_EVENT_TYPE_DELETED = 3;
132 USER_EVENT_TYPE_LOGIN = 4;
133 USER_EVENT_TYPE_LOGOUT = 5;
134}
135
136enum ChatMessageType {
137 CHAT_MESSAGE_TYPE_UNSPECIFIED = 0;
138 CHAT_MESSAGE_TYPE_TEXT = 1;
139 CHAT_MESSAGE_TYPE_IMAGE = 2;
140 CHAT_MESSAGE_TYPE_FILE = 3;
141 CHAT_MESSAGE_TYPE_SYSTEM = 4;
142}

gRPC Features

gRPC offers several key features that make it ideal for modern applications:

High Performance

  • Binary protocol with Protocol Buffers
  • HTTP/2 transport for multiplexing and flow control
  • Efficient serialization and deserialization

Multiple Communication Patterns

  • Unary RPCs: Simple request/response
  • Server Streaming: Server sends stream of responses
  • Client Streaming: Client sends stream of requests
  • Bidirectional Streaming: Both sides send streams

Cross-Platform Support

  • Native support for 10+ programming languages
  • Generated client libraries and server stubs
  • Consistent API across all platforms

Setup your fern folder

Start by initializing your fern folder with a gRPC service

1fern init --proto ./path/to/service.proto

This will initialize a directory like the following

fern/
├─ fern.config.json
├─ generators.yml
└─ proto/
├─ user_service.proto
└─ common.proto

gRPC vs REST

FeaturegRPCREST
ProtocolHTTP/2HTTP/1.1
Data FormatProtocol BuffersJSON
PerformanceHighModerate
StreamingNative supportLimited
Browser SupportLimitedFull
SchemaStrongly typedFlexible
Code GenerationBuilt-inThird-party tools

Common Use Cases

gRPC is particularly well-suited for:

  • Microservices Communication: High-performance inter-service communication
  • Real-time Applications: Streaming for live updates and real-time features
  • Mobile Applications: Efficient mobile-to-server communication
  • IoT Systems: Lightweight communication for resource-constrained devices
  • APIs with Strong Contracts: Type-safe communication with schema evolution