1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508---
title: Protocol Buffers vs FDL
sidebar_position: 6
id: proto_vs_fdl
license: |
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
---
This document compares Google's Protocol Buffers (protobuf) with Fory Definition Language (FDL), helping you understand when to use each and how to migrate between them.
## Overview
| Aspect | Protocol Buffers | FDL |
| ---------------------- | --------------------------------- | ----------------------------------- |
| **Primary Purpose** | RPC and message interchange | Cross-language object serialization |
| **Design Philosophy** | Schema evolution, backward compat | Performance, native integration |
| **Reference Tracking** | Not supported | First-class support (`ref`) |
| **Generated Code** | Custom message types | Native language constructs |
| **Serialization** | Tag-length-value encoding | Fory binary protocol |
| **Performance** | Good | Excellent (up to 170x faster) |
## Syntax Comparison
### Package Declaration
**Protocol Buffers:**
```protobuf
syntax = "proto3";
package example.models;
option java_package = "com.example.models";
option go_package = "example.com/models";
```
**FDL:**
```proto
package example.models;
```
FDL uses a single package declaration that maps to all languages automatically.
### Enum Definition
**Protocol Buffers:**
```protobuf
enum Status {
STATUS_UNSPECIFIED = 0;
STATUS_PENDING = 1;
STATUS_ACTIVE = 2;
STATUS_COMPLETED = 3;
}
```
**FDL:**
```proto
enum Status [id=100] {
PENDING = 0;
ACTIVE = 1;
COMPLETED = 2;
}
```
Key differences:
- FDL supports optional type IDs (`[id=100]`) for efficient serialization
- Protobuf requires `_UNSPECIFIED = 0` by convention; FDL uses explicit values
- FDL enum values don't require prefixes
### Message Definition
**Protocol Buffers:**
```protobuf
message User {
string id = 1;
string name = 2;
optional string email = 3;
int32 age = 4;
repeated string tags = 5;
map<string, string> metadata = 6;
}
```
**FDL:**
```proto
message User [id=101] {
string id = 1;
string name = 2;
optional string email = 3;
int32 age = 4;
repeated string tags = 5;
map<string, string> metadata = 6;
}
```
Syntax is nearly identical, but FDL adds:
- Type IDs (`[id=101]`) for cross-language registration
- `ref` modifier for reference tracking
### Nested Types
**Protocol Buffers:**
```protobuf
message Order {
message Item {
string product_id = 1;
int32 quantity = 2;
}
repeated Item items = 1;
}
```
**FDL:**
```proto
message OrderItem [id=200] {
string product_id = 1;
int32 quantity = 2;
}
message Order [id=201] {
repeated OrderItem items = 1;
}
```
FDL supports nested types, but generators may flatten them for languages where nested types are not idiomatic.
### Imports
**Protocol Buffers:**
```protobuf
import "other.proto";
import "google/protobuf/timestamp.proto";
```
**FDL:**
FDL currently requires all types in a single file or uses forward references within the same file.
## Feature Comparison
### Reference Tracking
FDL's killer feature is first-class reference tracking:
**FDL:**
```proto
message TreeNode [id=300] {
string value = 1;
ref TreeNode parent = 2;
repeated ref TreeNode children = 3; // Element refs
ref repeated TreeNode path = 4; // Collection ref
}
message Graph [id=301] {
repeated ref Node nodes = 1; // Shared references preserved (elements)
}
```
**Protocol Buffers:**
Protobuf cannot represent circular or shared references. You must use workarounds:
```protobuf
// Workaround: Use IDs instead of references
message TreeNode {
string id = 1;
string value = 2;
string parent_id = 3; // Manual ID reference
repeated string child_ids = 4;
}
```
### Type System
| Type | Protocol Buffers | FDL |
| ---------- | ------------------------------------------------------------------------------------------------------ | --------------------------------- |
| Boolean | `bool` | `bool` |
| Integers | `int32`, `int64`, `sint32`, `sint64`, `uint32`, `uint64`, `fixed32`, `fixed64`, `sfixed32`, `sfixed64` | `int8`, `int16`, `int32`, `int64` |
| Floats | `float`, `double` | `float32`, `float64` |
| String | `string` | `string` |
| Binary | `bytes` | `bytes` |
| Timestamp | `google.protobuf.Timestamp` | `timestamp` |
| Date | Not built-in | `date` |
| Duration | `google.protobuf.Duration` | Not built-in |
| List | `repeated T` | `repeated T` |
| Map | `map<K, V>` | `map<K, V>` |
| Nullable | `optional T` (proto3) | `optional T` |
| Oneof | `oneof` | Not supported |
| Any | `google.protobuf.Any` | Not supported |
| Extensions | `extend` | Not supported |
### Wire Format
**Protocol Buffers:**
- Tag-length-value encoding
- Variable-length integers (varints)
- Field numbers encoded in wire format
- Unknown fields preserved
**FDL/Fory:**
- Optimized binary format
- Schema-aware encoding
- Type IDs for fast lookup
- Reference tracking support
- Zero-copy deserialization where possible
### Generated Code Style
**Protocol Buffers** generates custom types with builders and accessors:
```java
// Protobuf generated Java
User user = User.newBuilder()
.setId("u123")
.setName("Alice")
.setAge(30)
.build();
```
**FDL** generates native POJOs:
```java
// FDL generated Java
User user = new User();
user.setId("u123");
user.setName("Alice");
user.setAge(30);
```
### Comparison Table
| Feature | Protocol Buffers | FDL |
| -------------------------- | ----------------- | --------- |
| Schema evolution | Excellent | Good |
| Backward compatibility | Excellent | Good |
| Reference tracking | No | Yes |
| Circular references | No | Yes |
| Native code generation | No (custom types) | Yes |
| Unknown field preservation | Yes | No |
| Schema-less mode | No | Yes\* |
| RPC integration (gRPC) | Yes | No |
| Zero-copy deserialization | Limited | Yes |
| Human-readable format | JSON, TextFormat | No |
| Performance | Good | Excellent |
\*Fory supports schema-less serialization without FDL
## When to Use Each
### Use Protocol Buffers When:
1. **Building gRPC services**: Protobuf is the native format for gRPC
2. **Maximum backward compatibility**: Protobuf's unknown field handling is robust
3. **Schema evolution is critical**: Adding/removing fields across versions
4. **You need oneof/Any types**: Complex polymorphism requirements
5. **Human-readable debugging**: TextFormat and JSON transcoding available
6. **Ecosystem integration**: Wide tooling support (linting, documentation)
### Use FDL/Fory When:
1. **Performance is critical**: Up to 170x faster than protobuf
2. **Cross-language object graphs**: Serialize Java objects, deserialize in Python
3. **Circular/shared references**: Object graphs with cycles
4. **Native code preferred**: Standard POJOs, dataclasses, structs
5. **Memory efficiency**: Zero-copy deserialization
6. **Existing object models**: Minimal changes to existing code
## Performance Comparison
Benchmarks show Fory significantly outperforms Protocol Buffers:
| Benchmark | Protocol Buffers | Fory | Improvement |
| ------------------------- | ---------------- | -------- | ----------- |
| Serialization (simple) | 1x | 10-20x | 10-20x |
| Deserialization (simple) | 1x | 10-20x | 10-20x |
| Serialization (complex) | 1x | 50-100x | 50-100x |
| Deserialization (complex) | 1x | 50-100x | 50-100x |
| Memory allocation | 1x | 0.1-0.5x | 2-10x less |
_Benchmarks vary based on data structure and language. See [Fory benchmarks](../benchmarks/) for details._
## Migration Guide
### From Protocol Buffers to FDL
#### Step 1: Convert Syntax
**Before (proto):**
```protobuf
syntax = "proto3";
package myapp;
message Person {
string name = 1;
int32 age = 2;
repeated string emails = 3;
Address address = 4;
}
message Address {
string street = 1;
string city = 2;
}
```
**After (FDL):**
```proto
package myapp;
message Address [id=100] {
string street = 1;
string city = 2;
}
message Person [id=101] {
string name = 1;
int32 age = 2;
repeated string emails = 3;
Address address = 4;
}
```
#### Step 2: Handle Special Cases
**oneof fields:**
```protobuf
// Proto
message Result {
oneof result {
Success success = 1;
Error error = 2;
}
}
```
```proto
// FDL - Use separate optional fields
message Result [id=102] {
optional Success success = 1;
optional Error error = 2;
}
// Or model as sealed class hierarchy in generated code
```
**Well-known types:**
```protobuf
// Proto
import "google/protobuf/timestamp.proto";
message Event {
google.protobuf.Timestamp created_at = 1;
}
```
```proto
// FDL
message Event [id=103] {
timestamp created_at = 1;
}
```
#### Step 3: Add Type IDs
Assign unique type IDs for cross-language compatibility:
```proto
// Reserve ranges for different domains
// 100-199: Common types
// 200-299: User domain
// 300-399: Order domain
message Address [id=100] { ... }
message Person [id=200] { ... }
message Order [id=300] { ... }
```
#### Step 4: Update Build Configuration
**Before (Maven with protobuf):**
```xml
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<!-- ... -->
</plugin>
```
**After (Maven with FDL):**
```xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-fory-types</id>
<phase>generate-sources</phase>
<goals><goal>exec</goal></goals>
<configuration>
<executable>fory</executable>
<arguments>
<argument>compile</argument>
<argument>${project.basedir}/src/main/fdl/schema.fdl</argument>
<argument>--lang</argument>
<argument>java</argument>
<argument>--output</argument>
<argument>${project.build.directory}/generated-sources/fdl</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
```
#### Step 5: Update Application Code
**Before (Protobuf Java):**
```java
// Protobuf style
Person.Builder builder = Person.newBuilder();
builder.setName("Alice");
builder.setAge(30);
Person person = builder.build();
byte[] data = person.toByteArray();
Person restored = Person.parseFrom(data);
```
**After (Fory Java):**
```java
// Fory style
Person person = new Person();
person.setName("Alice");
person.setAge(30);
Fory fory = Fory.builder().withLanguage(Language.XLANG).build();
MyappForyRegistration.register(fory);
byte[] data = fory.serialize(person);
Person restored = (Person) fory.deserialize(data);
```
### Coexistence Strategy
For gradual migration, you can run both systems in parallel:
```java
// Dual serialization during migration
public byte[] serialize(Object obj, Format format) {
if (format == Format.PROTOBUF) {
return ((MessageLite) obj).toByteArray();
} else {
return fory.serialize(obj);
}
}
// Convert between formats
public ForyPerson fromProto(ProtoPerson proto) {
ForyPerson person = new ForyPerson();
person.setName(proto.getName());
person.setAge(proto.getAge());
return person;
}
```
## Summary
| Aspect | Choose Protocol Buffers | Choose FDL/Fory |
| ---------------- | ----------------------- | ---------------------- |
| Use case | RPC, API contracts | Object serialization |
| Performance | Acceptable | Critical |
| References | Not needed | Circular/shared needed |
| Code style | Builder pattern OK | Native POJOs preferred |
| Schema evolution | Complex requirements | Simpler requirements |
| Ecosystem | Need gRPC, tooling | Need raw performance |
Both tools excel in their domains. Protocol Buffers shines for RPC and API contracts with strong schema evolution guarantees. FDL/Fory excels at high-performance object serialization with native language integration and reference tracking support.