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-devGetting started
The smallest useful thing you can do with it, and what each part means.
table Monster {
id:int;
name:string;
hp:int;
}
root_type Monster;#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();Advanced usage
Where the library earns its place over a simpler alternative.
auto monster = GetMonster(buf);
std::cout << monster->name()->str() << " has HP: " << monster->hp() << std::endl;auto weapons = builder.CreateVectorOfStrings({"sword", "axe", "bow"});flatc --json --monster.fbs monster.jsonOlder schemas can still read newer data as long as fields are added with defaults.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.
