Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

A team stores user profiles in MongoDB, but new features need reporting with joins across profiles, orders and payments in PostgreSQL. What would you recommend?

I would not join MongoDB and PostgreSQL data in application code for reports. Instead, I would first define which store is the source of truth for each entity, then stream changes from both stores into an analytics store or data warehouse where reports can join freely. If the profile data has become relational anyway, moving profiles into PostgreSQL, keeping flexible parts in JSONB, may be simpler in the long run, but that choice depends on weighing the migration cost against the ongoing cost of running and syncing two stores.

Why this question is asked

Polyglot persistence, using different databases for different services, works well until a feature needs data from several of them at once. Reporting is where this usually happens first: product wants revenue per customer segment, and the segment lives in MongoDB profiles while revenue lives in PostgreSQL. Interviewers ask this scenario to see whether we can avoid the tempting shortcut and reason about the options with their costs. In this answer, we will see why application-level joins fail for reports, set ownership per entity, design a reporting pipeline, consider moving profiles to PostgreSQL, and decide by comparing costs.

1Why Joining Across Stores in Application Code Fails for Reports

The shortcut is a report endpoint that loads profiles from MongoDB, loads orders and payments from PostgreSQL, and joins them in Java with maps. It works for the first demo with 1,000 users. A real report touches every profile and millions of orders, so the service must pull all of it over the network into memory, which takes minutes, uses gigabytes of heap and puts reporting load on two production databases that serve customers.

Correctness suffers too. The two reads happen at different moments with no shared snapshot, so an order placed between them may be counted without its profile, and every new report means writing and maintaining more join code, with filtering, grouping and paging re-implemented by hand. We would be building a slow, inconsistent query engine.

Java
// the tempting shortcut
Map<String, Profile> profiles = profileRepo.findAll().stream() // 2.4 million documents
.collect(toMap(Profile::getUserId, p -> p)); // ~3 GB of heap
List<Order> orders = orderRepo.findByCreatedAtAfter(monthStart); // 6 million rows
// then grouping and summing by profile.segment in Java ... minutes, and production databases under load

2Define the Source of Truth per Entity

Before moving data anywhere, we decide which store owns each entity, meaning which one is authoritative and where changes are made. Profiles are owned by the profile service in MongoDB; orders and payments by their services in PostgreSQL. Every other copy, in a warehouse, a cache or a search index, is derived from the owner and never edited directly.

This sounds obvious, but without it teams start writing profile fields from the order service "just for reporting", and soon two stores disagree about a user's segment with no rule for which is right. Writing the ownership down settles that before it starts.

3Stream Changes Into an Analytics Store

The robust answer for reporting is to copy data from both stores into a place built for analytical queries, such as a data warehouse or a separate analytics database, and to run reports there. Changes flow continuously through change data capture: Debezium can read PostgreSQL's write-ahead log and MongoDB's change streams and publish each change as an event, typically to Kafka, from where a loader writes it into the warehouse. Reports then join profiles, orders and payments with ordinary SQL, on a copy that is a few minutes behind production and puts no load on it.

Step throughReporting architecture
  1. Reports and dashboardsplain SQL joins across all entities
  2. Analytics store or warehouseprofiles, orders, payments in one place
  3. Loaderapplies change events, idempotent by key
  4. Kafka topicsprofile, order and payment changes
  5. Change data captureMongoDB change streams, PostgreSQL WAL
  6. Operational storesMongoDB (profiles), PostgreSQL (orders, payments)

6 layers, from the top down. Step through them one at a time.

Operational stores serve customers; the warehouse serves reports, minutes behind.

4Should Profiles Move to PostgreSQL Instead?

The reporting need may also reveal something about the profiles themselves. If new features keep needing profiles together with orders, if profile documents have become uniform, and if the team adds things like unique emails that MongoDB enforces only through indexes and code, then the document model may no longer fit. Moving profiles to PostgreSQL, with stable fields as columns and the genuinely varying parts, such as preferences, in a jsonb column, would give operational queries joins and transactions with orders directly.

The move itself follows the usual pattern: create the table, backfill from MongoDB, keep both in sync through change events during the transition, switch reads after comparing results, then switch writes and retire the collection. Notice that this solves operational joins, but large reports still belong in an analytics store, because running heavy reporting queries on the production PostgreSQL that serves customers has its own cost.

SQL
CREATE TABLE user_profile (
user_id text PRIMARY KEY,
email text UNIQUE NOT NULL,
segment text NOT NULL,
created_at timestamptz NOT NULL,
preferences jsonb NOT NULL DEFAULT '{}' -- the part that still varies
);

5Weighing Migration Cost Against Ongoing Complexity

The decision is a comparison of costs over time. Keeping MongoDB plus a pipeline costs running two operational databases, the CDC and warehouse infrastructure, and the mental overhead of two models, but no migration. Moving profiles costs a migration project, perhaps several weeks for a team including the dual-running period and changes to every consumer of the profile API, but afterwards one fewer database to run and native joins for features.

I would estimate both in rough engineer-weeks per year and look at the trend. If profile and order features are increasingly intertwined, the ongoing cost grows and the migration pays back. If profiles are mostly read on their own and only reports need the combination, the analytics pipeline alone is enough, and it is needed for serious reporting either way. A common outcome is to build the pipeline now, because it serves reporting under both options, and decide on the profile migration separately.

Options compared
OptionUpfront costOngoing costSolves
Join in application codelowhigh: slow, inconsistent, grows per reportnothing well
CDC into analytics storemediumpipeline to operatereporting
Move profiles to PostgreSQLhigh: migrationlower: one store feweroperational joins
Pipeline now, migrate later if neededmedium, then highdecided by trendboth, staged

6Follow-up questions

Can MongoDB's $lookup or a federated query tool do the join instead?

$lookup only joins collections inside MongoDB. Federated query engines can query both stores in one SQL statement, which is useful for exploration, but they still push the load onto production databases and are usually slower than a warehouse for regular reports.

How fresh does the reporting data need to be?

Most business reports are fine with minutes or even an hour of delay. If a report truly needs live numbers, it is often an operational feature in disguise and should be served by the owning service with a purpose-built read model.

Who owns the pipeline?

Usually a data or platform team owns the infrastructure, while each service team owns the shape of the change events it publishes, treated like an API contract so the warehouse does not break when a service changes its internal schema.

7Conclusion

In this answer, we saw that joining MongoDB and PostgreSQL data in application code for reports is slow, inconsistent and loads production databases. We defined a source of truth for each entity, streamed changes from both stores through change data capture into an analytics store where reports join freely, and considered moving profiles to PostgreSQL with jsonb for their flexible parts when the document model no longer fits. The choice between them comes from weighing a one-time migration cost against the ongoing cost of running two stores.

What to say in the interviewAbout 90 seconds aloud

Open with

My first point is what not to do: I would not join across the two stores in application code for reports. A real report would pull millions of profiles and orders into memory, put reporting load on two production databases, and read them at different moments, so the numbers can be inconsistent, and every new report means more hand-written join code.

Then, if they want more

Before anything else I would write down the source of truth per entity: profiles belong to the profile service in MongoDB, orders and payments to their services in PostgreSQL, and every other copy is derived.

Then for reporting, I would stream changes from both stores, using change data capture like Debezium on MongoDB change streams and the Postgres WAL, through Kafka into an analytics store or warehouse, and run the reports there with normal SQL joins, a few minutes behind production and without touching it.

Separately I would ask whether profiles still fit the document model. If they have become uniform and features keep needing them together with orders, moving them to PostgreSQL, with stable fields as columns and preferences in jsonb, gives us joins and constraints directly.

That is a real migration with backfill and dual running, so I would weigh it against the ongoing cost of two stores. Often the answer is to build the pipeline now, since reporting needs it either way, and decide the profile move based on the trend.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Data reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A team stores user profiles in MongoDB, but new features need reporting with joins...": https://scrivio-interview-library.netlify.app/databases/spring-data-nosql/65a55696c6/