Protobuf and JSON are two popular methods for structuring data for efficient storage and transfer. Protobuf is more efficient than JSON in terms of byte array size, but it takes longer to convert an object to a byte array and changes to the message structure require code generation. When Protobuf is used to replace JSON at large scale, it can save massive amounts of bandwidth.

Example

Github Repository here

Protobuf file

proto 1

Marshal

Lets start with some go code with the protos already defined and working. Here we have go code that will marshal the object in json and protobuf, then write it to a file and print out the time it took to complete.

start := time.Now()
buf, _ := proto.Marshal(&e)
os.WriteFile("events.protobuf", buf, os.ModePerm)
fmt.Println("marshal events.protobuf", time.Since(start))

start = time.Now()
buf, _ = json.Marshal(&e)
os.WriteFile("events.json", buf, os.ModePerm)
fmt.Println("marshal events.json", time.Since(start))

Results of running the code:

$ go run main.go
marshal events.protobuf     397.726µs
marshal events.json         269.558µs

This indicates it takes slightly longer to marshal data into protobuf, but the file size of events.protobuf is much smaller.

$ ls -al events*
-rwxr-xr-x  1 kylefelter  staff  144 Apr 30 22:35 events.json
-rwxr-xr-x  1 kylefelter  staff   71 Apr 30 22:35 events.protobuf

Unmarshal

Lets try to unmarshal the protobuf and json.

events := publish.EventList{}
buf, _ = os.ReadFile("events.protobuf")
start = time.Now()
proto.Unmarshal(buf, &events)
fmt.Println("unmarshal protobuf", events.String(), time.Since(start))

events = publish.EventList{}
buf, _ = os.ReadFile("events.json")
start = time.Now()
json.Unmarshal(buf, &events)
fmt.Println("unmarshal json", events.String(), time.Since(start))

Running the code you can see tat unmarshalling protobuf takes a bit longer.

$ go run main.go
marshal events.protobuf         397.726µs
marshal events.json             269.558µs
unmarshal protobuf events:{...} 98.805µs
unmarshal json events:{...}     40.391µs

Stay Tuned for my upcomming Golang resource “Mastering Go Application Design” Where I teach you how to create your own protobuf/gRPC service that you can use to build your next project!