Skip to content

FlatBuffers

Serialization & FormatsSerializationC++

What it is

FlatBuffers is a highly efficient cross-platform serialization library developed by Google. It allows direct access to serialized data without parsing/unpacking, making it ideal for games, mobile apps, and high-performance applications.

FlatBuffers uses a schema definition file (`.fbs`) to describe structured data. The `flatc` compiler generates C++ (and other language) code, which can then be used to build and access serialized objects directly from memory.

Installation

sudo apt install flatbuffers-compiler libflatbuffers-dev

Getting started

The smallest useful thing you can do with it, and what each part means.

Defining a schema
table Monster {
  id:int;
  name:string;
  hp:int;
}
root_type Monster;
Defines a simple schema for a `Monster` object in a `.fbs` file.
Serializing data
#include "monster_generated.h"
#include <flatbuffers/flatbuffers.h>

flatbuffers::FlatBufferBuilder builder;
auto name = builder.CreateString("Orc");
MonsterBuilder monster(builder);
monster.add_id(1);
monster.add_name(name);
monster.add_hp(100);
auto orc = monster.Finish();
builder.Finish(orc);

uint8_t* buf = builder.GetBufferPointer();
size_t size = builder.GetSize();
Builds a `Monster` object and serializes it into a FlatBuffer.

Advanced usage

Where the library earns its place over a simpler alternative.

Accessing data without deserialization
auto monster = GetMonster(buf);
std::cout << monster->name()->str() << " has HP: " << monster->hp() << std::endl;
FlatBuffers allows direct access to serialized data without unpacking.
Using vectors
auto weapons = builder.CreateVectorOfStrings({"sword", "axe", "bow"});
FlatBuffers supports strongly typed vectors for lists of data.
JSON interoperability
flatc --json --monster.fbs monster.json
FlatBuffers can serialize/deserialize JSON for debugging or compatibility.
Version tolerance
Older schemas can still read newer data as long as fields are added with defaults.
FlatBuffers supports forward and backward compatibility in schemas.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

Buffer too small / corrupted
Always validate buffers with `Verifier` before accessing data.
Schema mismatch
Ensure `.fbs` schema used at runtime matches the generated headers.
Alignment errors
Ensure buffers are properly aligned when mapped from external sources.

Best practices

  • Use FlatBuffers for performance-critical applications like games and real-time systems.
  • Prefer accessing fields directly from the buffer instead of copying data.
  • Design schemas with optional fields for forward compatibility.
  • Use vectors for large repeated data instead of nested tables.
  • Avoid excessive nesting in schemas, as it can reduce performance.

Background

Why it exists, and what it was reacting to.

FlatBuffers was created at Google by Wouter van Oortmerssen in 2014 to overcome performance limitations of Protocol Buffers in gaming and mobile environments. Unlike Protobuf, FlatBuffers does not require deserialization: data can be directly accessed from the buffer, enabling faster performance with lower memory usage. It has become widely used in game engines, real-time applications, and machine learning frameworks like TensorFlow Lite.