Database Integration (MySQL & MongoDB)

Chapter 10
Polyglot Persistence and Database Selection

Diploma in Full Stack Programming
Database Integration (MySQL & MongoDB)   •   Diploma

Polyglot Persistence and Database Selection

Introduction

Modern applications rarely have identical data-management needs throughout their entire architecture. A payment system may require strict transactional guarantees, an online catalogue may benefit from flexible product attributes, and an analytics service may process large volumes of event data. Treating every problem as if it required the same database can create unnecessary complexity, excessive cost, or unacceptable operational risk.

Polyglot persistence is the deliberate use of more than one data-storage technology within an application or organization. In this chapter, the principal technologies are MySQL, a relational database management system, and MongoDB, a document-oriented database. The objective is not to declare one universally superior. Rather, it is to select the simplest technology—or combination of technologies—that satisfies consistency, scalability, query, transaction, cost, security, and maintainability requirements.

A sound decision begins with business and workload requirements. Database selection is an architectural decision, not merely a programming preference.

Learning Objectives

By the end of this chapter, you should be able to:

  • explain relational, document, and polyglot persistence models;
  • compare MySQL and MongoDB in terms of data structure, consistency, transactions, scalability, querying, cost, and maintainability;
  • distinguish data ownership from data duplication and synchronization;
  • select one database or a combination of databases using explicit criteria;
  • design a practical MySQL–MongoDB integration without unnecessary architectural complexity;
  • identify operational, governance, security, and ethical risks; and
  • justify a database decision in a professional design document.

1. Core Concepts

1.1 The relational model

The relational model represents data as relations, commonly implemented as tables. A row represents an occurrence of an entity, while a column represents an attribute. Relationships between tables are expressed using primary keys and foreign keys.

For example, an order system might contain customers, orders, and order_items. An order item refers to an order through order_id and to a product through product_id. This structure reduces uncontrolled duplication and supports precise integrity rules.

MySQL provides SQL, a declarative language for querying and manipulating relational data. Its transactional storage engines, especially InnoDB, support atomicity, consistency, isolation, and durability (ACID). Constraints such as PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, and CHECK help prevent invalid states.

Relational databases are particularly effective when:

  • entities have stable and well-understood relationships;
  • correctness depends on multiple records changing together;
  • reporting requires joins, grouping, and aggregation;
  • data must obey strong integrity constraints; or
  • the organization already has mature SQL skills and operational practices.

1.2 The document model

MongoDB stores records as BSON documents, a binary representation similar to JSON. A document can contain nested objects and arrays. Related information can therefore be embedded rather than represented in separate tables.

A product document, for example, may contain a common set of fields and a category-specific attributes object. This is useful when product types vary considerably or when an aggregate is normally read as a single unit.

MongoDB collections do not require every document to have the same shape. Nevertheless, flexibility is not the same as the absence of design. Applications should define expected fields, validation rules, indexes, naming conventions, and versioning policies. MongoDB supports schema validation, but teams must consciously implement and maintain it.

MongoDB is particularly effective when:

  • data is naturally represented as aggregates or nested documents;
  • application access patterns are known and primarily document-oriented;
  • fields evolve frequently;
  • high write throughput or horizontal distribution is important; or
  • joining many normalized tables would impose unnecessary complexity.

1.3 Aggregate boundaries

An aggregate is a group of data treated as a unit by the application. In a document database, an aggregate often maps naturally to one document. For example, a shopping cart and its line items may be read and updated together. Embedding these line items can reduce round trips.

Embedding is not always appropriate. If a child collection grows without a practical bound, is independently queried, or is shared by many parents, referencing may be safer. A document that grows continuously can create large reads, update contention, and storage inefficiency.

The central design question is not “Should this be normalized or denormalized?” It is “What are the ownership, lifecycle, size, and access-pattern characteristics of this data?”

2. Comparing MySQL and MongoDB

CriterionMySQLMongoDB
Primary modelTables, rows, relationshipsCollections and BSON documents
SchemaExplicit and centrally enforcedFlexible, with optional validation
RelationshipsForeign keys and joinsEmbedding, references, and application-side composition
TransactionsMature multi-statement transactionsAtomic document operations and multi-document transactions
Scaling emphasisVertical scaling and read replicas; sharding is possibleReplication and sharding are core capabilities
Query languageSQLMQL and aggregation pipeline
Best fitIntegrity-heavy relational workloadsAggregate-oriented, variable or rapidly evolving data
Typical riskRigid schema or expensive joins at very large scaleDuplication, weak discipline, and difficult cross-document consistency

This table describes tendencies, not absolute limitations. MySQL can store JSON and scale horizontally in suitable architectures. MongoDB supports transactions, joins through aggregation, and strong consistency options. A decision must therefore be based on the actual workload rather than product stereotypes.

2.1 Consistency and integrity

Consistency means that committed data satisfies defined rules. In MySQL, constraints and transactions can enforce relationships directly in the database. If an order and its payment record must change together, a transaction can commit both changes or neither.

MongoDB provides atomicity for operations on a single document and supports transactions spanning multiple documents and collections. However, multi-document transactions can introduce coordination overhead. A document model is often strongest when the important invariant is contained within one document.

Consistency also involves read behavior. A replicated system may offer different read preferences and consistency trade-offs. A team should explicitly decide whether a user may temporarily read an earlier version of data, or whether every read must reflect the latest committed write.

2.2 Transactions

A transaction is a sequence of operations treated as one logical unit. The desired ACID properties are:

  • Atomicity: all operations occur or none do;
  • Consistency: defined integrity rules remain satisfied;
  • Isolation: concurrent transactions do not produce unacceptable interference; and
  • Durability: committed data survives an appropriate failure.

MySQL transactions are a natural fit for accounting entries, inventory reservations, and transfers between balances. MongoDB transactions are useful where multiple documents must change together, but frequent cross-document transactions may indicate that the aggregate boundary or database choice needs reconsideration.

A transaction does not automatically make a design correct. The application must still choose suitable isolation, handle deadlocks or transient transaction errors, validate input, and use idempotent retry logic where appropriate.

2.3 Scalability and availability

Scalability is the ability to handle increased workload by adding resources or changing the architecture. Vertical scaling increases the capacity of a server. Horizontal scaling distributes workload across multiple servers.

Read replicas can improve read capacity, but replication lag means that a replica may not immediately contain the newest write. Sharding distributes data across partitions according to a shard key. A poor shard key can create hotspots, uneven distribution, or expensive scatter-gather queries.

MongoDB is often selected when distributing large document collections is a primary requirement. MySQL can also support replication, partitioning, clustering, and sharding patterns, but some horizontal designs require additional architecture or tooling. In both systems, scalability depends on indexes, query shape, data distribution, hardware, and operational competence.

2.4 Query patterns

SQL is highly effective for ad hoc analysis, joins, grouping, window functions, and mature reporting tools. A relational schema can represent many-to-many relationships clearly and query them flexibly.

MongoDB queries are effective when the requested information aligns with document boundaries. Its aggregation pipeline supports filtering, projection, grouping, sorting, and transformation. However, deep or frequent cross-collection composition can reduce the advantages of a document model.

A useful method is to list the most important queries before selecting a database. For each query, record:

  1. the entities or documents accessed;
  2. filtering and sorting fields;
  3. expected result size;
  4. read and write frequency;
  5. freshness requirement;
  6. required indexes; and
  7. acceptable latency.

Database selection based only on data shape, without examining queries, is unreliable.

2.5 Cost

Cost includes more than licensing. Relevant components include infrastructure, managed-service charges, storage, backups, network transfer, support, monitoring, migration, development time, training, and incident response.

An apparently inexpensive database can become costly if it requires difficult synchronization, excessive duplication, or specialized staff. Conversely, using a managed service may increase direct spending while reducing operational risk. The correct comparison is total cost of ownership over the expected period, not only monthly server cost.

2.6 Maintainability

Maintainability includes understandability, testability, ease of change, observability, and the availability of skilled personnel. A single database is generally easier to operate than two databases. Polyglot persistence should therefore be introduced only when the benefits are significant and measurable.

A system using both MySQL and MongoDB needs clear ownership rules, migration procedures, backup policies, security controls, monitoring, and failure-handling strategies. Without these, flexibility becomes architectural debt.

3. Polyglot Persistence Patterns

3.1 Database-per-bounded-context

A bounded context is a domain area with its own terminology, rules, and data ownership. For example, payments may be owned by a relational service, while a product-content service may own flexible catalogue documents. Each service manages its own database and exposes an API or event stream rather than permitting arbitrary cross-database writes.

This pattern reduces coupling. It also requires explicit integration contracts and careful handling of eventual consistency.

3.2 System of record and read model

One database may be the authoritative system of record, while another stores a read-optimized projection. For example, MySQL may record orders and payments, while MongoDB stores a customer-facing order-history document.

The projection can be updated through application events, a message broker, or a change-data-capture process. If the projection is temporarily stale, the user interface must communicate or tolerate that condition. The projection should be rebuildable from authoritative data.

3.3 Shared identifiers and references

When records in different databases refer to one another, use stable identifiers such as UUIDs or carefully managed numeric identifiers. A reference across databases is not a foreign key. The application or integration process must detect missing, duplicated, or stale references.

Do not distribute a single business transaction across databases casually. Distributed transactions are complex. Prefer local transactions plus reliable events, idempotent consumers, reconciliation, and compensating actions when business requirements permit.

3.4 Duplication and denormalization

Duplication may improve read performance and reduce joins, but it creates synchronization obligations. Every duplicated field needs an owner, an update mechanism, a freshness expectation, and a repair strategy.

A practical rule is: duplicate data only when the performance or availability benefit is understood, measured, and worth the consistency cost.

4. A Database Selection Framework

A professional selection process can follow these stages.

Stage 1: Define requirements

Identify business invariants, regulatory duties, peak workload, data volume, latency objectives, availability objectives, retention periods, and recovery requirements. Separate mandatory requirements from preferences.

Stage 2: Classify data

For each data set, document:

  • structure and variability;
  • ownership and lifecycle;
  • relationships;
  • approximate size and growth;
  • read and write patterns;
  • transaction boundary;
  • sensitivity and retention; and
  • reporting and integration needs.

Stage 3: Identify the dominant workload

A workload dominated by integrity-heavy multi-record transactions generally favours MySQL. A workload dominated by aggregate reads, variable documents, and distributed ingestion may favour MongoDB. Mixed workloads may justify a hybrid design, but only when each database has a distinct responsibility.

Stage 4: Test representative operations

Create a proof of concept using realistic data volume, indexes, concurrency, failure conditions, and query patterns. Measure latency distributions rather than only averages. Averages can conceal unacceptable tail latency.

Stage 5: Evaluate operational capability

Ask whether the team can monitor, secure, back up, restore, upgrade, and troubleshoot each technology. Evaluate managed services, support agreements, staff skills, and incident procedures.

Stage 6: Choose the simplest satisfactory architecture

A decision matrix can make assumptions visible. Assign each criterion a weight and rate each candidate. A weighted score can be represented as:

Sj = ∑(wi rij)

where Sj is the score for candidate j, wi is the weight of criterion i, and rij is the rating of candidate j on that criterion. Weights should be non-negative and normally sum to 1; ratings must use a documented scale. The score supports discussion but does not replace professional judgment.

CriterionWeightMySQL ratingMongoDB rating
Cross-record integrity0.2553
Flexible document structure0.1535
Complex reporting0.2053
Horizontal distribution0.1535
Team experience0.1554
Operational simplicity0.1054

For illustration, the MySQL score is 0.25×5 + 0.15×3 + 0.20×5 + 0.15×3 + 0.15×5 + 0.10×5 = 4.30. The MongoDB score is 0.25×3 + 0.15×5 + 0.20×3 + 0.15×5 + 0.15×4 + 0.10×4 = 3.80. These values are not universal facts; they depend on the stated weights and ratings.

5. Practical Integration Design

Suppose an application uses MySQL for customers, orders, payments, and stock reservations, while MongoDB stores a flexible product catalogue and search-oriented product views.

The application should define ownership as follows:

  • MySQL owns product identifiers, prices used for checkout, stock, and order records.
  • MongoDB owns descriptive content, category-specific attributes, images, and search metadata.
  • Checkout reads authoritative price and stock from MySQL.
  • Product browsing may read MongoDB, subject to an explicit freshness policy.

A product update can be processed as follows:

  1. validate and commit authoritative changes in MySQL;
  2. publish a durable ProductUpdated event containing the product identifier and version;
  3. consume the event to update the MongoDB projection;
  4. record processing status and retry failures; and
  5. reconcile projections periodically.

Consumers should be idempotent: processing the same event more than once should not create an incorrect result. Include a version or event sequence so that an older event cannot overwrite a newer projection.

A simple application-level pseudocode outline is:

begin MySQL transaction
  validate product identity and business rules
  update authoritative product fields
  write outbox event with product_id and version
commit transaction

outbox worker publishes ProductUpdated
MongoDB consumer applies event if version is newer
consumer records successful processing

The outbox pattern reduces the risk that a database commit succeeds while event publication fails. It does not eliminate operational work: the outbox needs monitoring, retry rules, retention, and cleanup.

6. Illustrative Case Study: Campus Marketplace

Background

This illustrative case concerns a campus marketplace that allows students to list textbooks, electronics, clothing, and services. The application has 80,000 registered users, but demand is highly seasonal. Listings have category-specific fields: a textbook has an ISBN and edition, while clothing has size and colour.

Problem

The first version stored all information in MySQL. As the marketplace grew, the team experienced frequent schema changes, complicated queries for category attributes, and slow browsing during semester openings. However, payments, orders, refunds, and seller balances required strong consistency and auditable transactions.

Analysis

The team identified two different data domains. Financial and contractual records had stable relationships and strict invariants. Listing content had variable structure and was primarily read as an aggregate. Search also required denormalized fields and flexible indexing.

A single MongoDB database was rejected for financial records because the team wanted database-enforced relational constraints, established SQL reporting, and straightforward audit queries. A single MySQL database was rejected for listing content because the document shape changed frequently and catalogue reads were more naturally aggregate-oriented.

Approach and solution

MySQL became the system of record for users, orders, payments, seller balances, and the authoritative transaction price. MongoDB stored listing documents and a browsing projection. Every listing included a stable listing identifier, seller identifier, version, title, category, descriptive attributes, and moderation status.

The team introduced an outbox table in MySQL for business events and a worker that delivered events to a message system. MongoDB consumers were idempotent and used the listing version to reject stale updates. A scheduled reconciliation job compared selected identifiers and versions between systems.

The application never allowed MongoDB browsing data to determine the final amount charged. At checkout, the service re-read price and availability from MySQL and created the order within a MySQL transaction.

Outcome

The catalogue could evolve without frequent relational migrations, while financial operations retained strong integrity. The organization accepted the cost of operating two databases because the domains had different requirements and the ownership boundary was explicit.

Learning points

  1. Polyglot persistence was justified by materially different workloads, not by fashion.
  2. The authoritative source for money and stock was unambiguous.
  3. Read projections were treated as rebuildable, potentially stale data.
  4. Versioning, idempotency, reconciliation, and monitoring were essential.
  5. The architecture would have been excessive for a small application with modest traffic and uniform data.

7. Challenges, Governance, and Responsible Practice

7.1 Failure and recovery

A hybrid system has more failure modes: one database may be available while the other is not; events may be delayed; a consumer may process messages twice; or a projection may become corrupted. Recovery objectives must specify how much data loss is acceptable and how quickly service must be restored.

Backups are not sufficient unless restoration is tested. Teams should document recovery procedures, dependency order, credentials, encryption keys, and reconciliation steps.

7.2 Security and privacy

Use least-privilege accounts, encrypted connections, secret rotation, network controls, audit logging, and environment separation. Sensitive personal data should not be copied into a read model without a purpose. Data duplication increases the number of places requiring access control, retention enforcement, deletion, and breach assessment.

Privacy regulations may grant rights to access, correct, or delete personal data. A hybrid architecture must locate all copies and derived views of a person’s data. Retention policies must cover backups and event logs where legally applicable.

7.3 Auditability and accountability

Financial and administrative decisions should be traceable. Store who performed an action, when it occurred, which version was affected, and what business reason applied. Do not rely on mutable display documents as the only audit record.

7.4 AI and contemporary development

AI-assisted coding can generate schemas, queries, migrations, and synchronization code, but generated output must be reviewed. AI tools may reproduce insecure connection handling, omit indexes, mishandle personal data, or assume incorrect transaction semantics. Developers remain responsible for testing concurrency, authorization, failure recovery, and data governance.

Contemporary systems increasingly use event streams, search indexes, analytical warehouses, and managed database services alongside operational databases. Each additional store should have a clearly stated purpose, owner, lifecycle, and recovery plan.

Best Practices

  • Start with business invariants and access patterns, not database popularity.
  • Prefer one database when it satisfies requirements adequately.
  • Use MySQL for strong relational integrity, complex joins, and transaction-heavy records.
  • Use MongoDB for well-defined document aggregates and genuinely variable structures.
  • Keep authoritative ownership explicit.
  • Treat cross-database projections as potentially stale and rebuildable.
  • Version events and make consumers idempotent.
  • Test realistic workload, failure, and recovery scenarios.
  • Monitor replication lag, event age, query latency, errors, storage, and backup success.
  • Document why each database exists and what would justify removing it.
  • Review privacy, deletion, retention, and access controls across every copy.

Chapter Summary

Relational and document databases solve overlapping but distinct classes of problems. MySQL offers tables, constraints, joins, mature transactions, and strong support for integrity-heavy workloads. MongoDB offers document-oriented storage, flexible structures, aggregate alignment, and practical capabilities for distributed and high-volume workloads. Neither technology is universally superior.

Polyglot persistence combines technologies when separate domains have materially different requirements. Successful designs define ownership, avoid casual cross-database transactions, manage duplication deliberately, and use events, versioning, idempotency, reconciliation, and observability. A selection framework should evaluate consistency, transaction boundaries, query patterns, scalability, cost, security, operational capability, and maintainability. The best architecture is the simplest one that meets the real requirements with acceptable risk.

Key Terms

Relational model: A data model based on relations represented as tables with rows and columns.

Document model: A model that stores records as self-contained, potentially nested documents.

Polyglot persistence: The deliberate use of multiple data-storage technologies in one system or organization.

ACID: Atomicity, consistency, isolation, and durability properties of reliable transactions.

Atomicity: The property that a transaction completes entirely or has no effect.

Aggregate: A group of data treated as a unit by application operations.

Embedding: Storing related information inside a document.

Referencing: Connecting records through identifiers rather than nesting their full data.

Schema validation: Rules used to restrict document structure or field values.

Replication: Maintaining copies of data on multiple database servers.

Sharding: Distributing data across partitions or servers according to a partitioning rule.

Read model: A data representation optimized for particular queries or consumers.

System of record: The authoritative source for a particular class of data.

Eventual consistency: A condition in which replicas or projections may temporarily differ but converge after updates are delivered.

Idempotency: The property that repeating an operation produces the same correct result as performing it once.

Outbox pattern: A method that stores an event in the same transaction as a business change before publishing it asynchronously.

Bounded context: A domain boundary with its own terminology, rules, and ownership.

Total cost of ownership: The complete direct and indirect cost of operating a technology over its useful period.

Reconciliation: A process of comparing systems and correcting missing, stale, or inconsistent records.

Review Questions

Short Answer Questions

  1. What is polyglot persistence?
  2. State two advantages of MySQL for transaction-heavy applications.
  3. Why might a MongoDB document be preferable to several normalized tables for a product catalogue?
  4. What is the difference between embedding and referencing?
  5. Why should a read projection be treated as potentially stale?

Long Answer Questions

  1. Compare MySQL and MongoDB in relation to schema, relationships, transactions, scalability, querying, and maintainability.
  2. Explain the ACID properties and discuss why they matter in payment or inventory systems.
  3. Describe a systematic framework for selecting a database for a new application.
  4. Explain how the outbox pattern supports reliable integration between MySQL and MongoDB.
  5. Discuss the operational and governance risks of introducing a second database technology.

Analytical / Application Questions

  1. A booking system must prevent two users from reserving the same seat and must produce financial reports involving customers, bookings, and payments. Would you select MySQL, MongoDB, or both? Justify your answer using transaction and query requirements.
  2. An online catalogue contains thousands of product categories, each with different attributes, and browsing traffic is much higher than editing traffic. Design an appropriate data model and explain where MongoDB and MySQL might be used.
  3. A team proposes copying customer email addresses, balances, and order totals into MongoDB for faster dashboards. Evaluate the proposal, identify governance risks, and recommend ownership, synchronization, and privacy controls.

Multiple Choice Questions with Answers

Question 1

Which characteristic most strongly favours MySQL for a business operation?

A. Highly variable nested attributes with few relationships

B. Strict multi-record integrity and complex joins

C. Unbounded document growth without a schema

D. A requirement to store all data as JSON

Correct Answer: B. Strict multi-record integrity and complex joins

Question 2

What is the main purpose of a system of record?

A. To provide an optional cache with no owner

B. To serve as the authoritative source for a class of data

C. To eliminate the need for backups

D. To guarantee that every read is from a replica

Correct Answer: B. To serve as the authoritative source for a class of data

Question 3

Which MongoDB design choice is usually appropriate when related data is read and updated together as one bounded aggregate?

A. Embedding the related data in one document

B. Creating a separate database for every field

C. Removing all validation rules

D. Using a foreign-key constraint across collections

Correct Answer: A. Embedding the related data in one document

Question 4

Why is idempotency important in an event consumer?

A. It prevents all network failures

B. It allows repeated delivery to produce one correct logical result

C. It forces every event to be processed synchronously

D. It replaces the need for access control

Correct Answer: B. It allows repeated delivery to produce one correct logical result

Question 5

In the weighted scoring formula Sj = ∑(wi rij), what does wi represent?

A. The rating of candidate j

B. The number of database servers

C. The weight assigned to criterion i

D. The storage size of criterion i

Correct Answer: C. The weight assigned to criterion i

Question 6

Which statement about a cross-database reference is correct?

A. It is automatically enforced like a MySQL foreign key

B. It requires application or integration logic to handle missing or stale references

C. It guarantees atomic updates in both databases

D. It removes the need for stable identifiers

Correct Answer: B. It requires application or integration logic to handle missing or stale references

Question 7

What is a principal risk of duplicating data into a read model?

A. The source database becomes relational

B. The duplicate may become stale or inconsistent

C. Queries can no longer be indexed

D. Transactions become impossible in the source system

Correct Answer: B. The duplicate may become stale or inconsistent

Question 8

Which workload is generally a strong candidate for a relational database?

A. A payment transfer requiring several records to change atomically

B. A catalogue with category-specific nested attributes only

C. A document archive with no relationships or reporting

D. An event stream where every record has a different shape

Correct Answer: A. A payment transfer requiring several records to change atomically

Question 9

What does the outbox pattern primarily reduce?

A. The risk that a committed database change has no published corresponding event

B. The need to define business ownership

C. The amount of storage required for backups

D. The number of fields in a document

Correct Answer: A. The risk that a committed database change has no published corresponding event

Question 10

Which principle best reflects responsible polyglot persistence?

A. Use as many database technologies as possible

B. Choose the simplest architecture that satisfies the requirements

C. Put financial authority in the fastest read cache

D. Avoid monitoring because replicas provide availability

Correct Answer: B. Choose the simplest architecture that satisfies the requirements

Database Integration (MySQL & MongoDB)
Chapter 10 of 14