Hi community, I’m a backend engineer and only recently started working with MongoDB, perhaps my question has already been asked.
In our team we benchmarked three different approaches to serializing/deserializing Java objects with the official Java driver - and I was, to say the least, surprised by the results.
- PojoCodecProvider (Generated Class Model via reflection).
Uses ClassModel and reflection (e.g. Field.setAccessible, Constructor.newInstance) to build codecs at runtime.
Slowest approach under heavy load due to reflection overhead.
- Manual Document mapping.
You read and write fields directly with doc.getString(“…”), writer.writeString(“…”, value), etc.
Fastest in benchmarks, but sacrifices type safety, boilerplate, and automatic nested/collection handling.
The shortcomings of these two solutions forced us to develop our own -
3) Build-time JavaPoet-generated Codec classes .
At compile time we reflect over POJO classes, generate Codec implementations with direct BsonWriter/BsonReader calls, and register them in a custom CodecRegistry.
No runtime reflection, yet retains type safety, nested structures, @BsonProperty support, and order-independent decoding.
Performance approaches manual mapping while keeping a clean domain model.
So, we had to implemented the this generator in our company because the official driver doesn’t provide build-time codec generation out of the box.
Questions:
-
Why hasn’t automatic build-time Codec generation been included in the official Java driver?
-
Are there any plans to introduce built-in, build-time Codec generation in the official Java driver?
-
What approaches do you use in your projects or frameworks (Spring Data MongoDB, Micronaut, Quarkus, etc.) to improve serialization performance, and how has that affected your throughput or latency?