# PostgreSQL + JSON -- the best of both worlds

- Speaker: [Hadar Davidovitch](/wiki/speakers/hadar-davidovitch.md)
- Event: [Reversim Summit 2014](/wiki/years/2014.md)
- Video: https://www.youtube.com/watch?v=WUhM13j2aCk
- Transcript: [/transcripts/2014/14-WUhM13j2aCk.md](/transcripts/2014/14-WUhM13j2aCk.md)
- Language: Hebrew · Type: talk · Level: intermediate
- Topics: [Databases](/wiki/topics/databases.md), [Architecture](/wiki/topics/architecture.md), [Performance](/wiki/topics/performance.md), [JavaScript](/wiki/topics/javascript.md)

> Written by an LLM from machine transcripts. It can contain errors. Check the transcript before you quote it.

## Summary

The speaker shares a team's journey of optimizing an application management layer that suffered from performance issues caused by deep object trees and heavy ORM usage. After evaluating NoSQL alternatives like document stores, the team chose to stick with PostgreSQL and leverage its native JSON support for handling document-like data while maintaining strong transactional consistency. By carefully defining document boundaries and implementing a lazy migration strategy, they achieved significant performance improvements without rewriting their entire technology stack. The talk emphasizes that architectural decisions should be driven by specific problem definitions rather than adopting trends.

## Key points

- [5:26](https://www.youtube.com/watch?v=WUhM13j2aCk&t=326s) The team faced scalability and performance challenges when handling unexpectedly large application object trees and heavy concurrent user requests.
- [6:34](https://www.youtube.com/watch?v=WUhM13j2aCk&t=394s) Heavy reliance on an ORM for complex object graphs led to inefficient lazy loading, N+1 queries, and excessive memory usage.
- [11:01](https://www.youtube.com/watch?v=WUhM13j2aCk&t=661s) The team explored NoSQL databases, analyzing CAP theorem trade-offs and determining that consistency was more important for this use case than high availability or partition tolerance.
- [17:50](https://www.youtube.com/watch?v=WUhM13j2aCk&t=1070s) They evaluated document stores and recognized the value of treating hierarchical object trees as single documents while managing references and denormalization.
- [21:05](https://www.youtube.com/watch?v=WUhM13j2aCk&t=1265s) The final decision was to use PostgreSQL with its native JSON data type, retaining a single data source for both relational data and documents.
- [31:32](https://www.youtube.com/watch?v=WUhM13j2aCk&t=1892s) A lazy migration strategy was implemented, serializing old data into JSON upon read and cleaning up legacy tables asynchronously to avoid downtime.
- [37:59](https://www.youtube.com/watch?v=WUhM13j2aCk&t=2279s) Practical implementation required careful validation of document boundaries, handling inheritance in serialization, and properly configuring ORM equals, hashCode, and deep copy methods.

## Takeaways

- Choose data storage technologies based on specific problem requirements rather than following general trends.
- Native JSON support in relational databases can bridge the gap between relational integrity and document-oriented flexibility.
- Plan data migrations carefully using lazy loading patterns to ensure zero downtime for active users.

## Questions this talk answers

- When should a development team choose PostgreSQL with JSON over a dedicated NoSQL document store?
- How can you migrate a legacy relational schema to a document-based structure without causing system downtime?
- What are the common pitfalls when mapping complex object hierarchies into JSON columns using an ORM?

## Technologies

PostgreSQL, Hibernate, Spring, Tomcat, Cassandra, Hazelcast, Jackson

## Tags

architecture, database performance, json, migration, nosql, orm, postgresql
