Sylius and MongoDB
It’s not just changing connection strings
Slide 2
Slide 2 text
No content
Slide 3
Slide 3 text
MongoDB and Sylius
It’s not a simple story
Slide 4
Slide 4 text
Let’s talk about schemas
Slide 5
Slide 5 text
Relational databases
Some say they are simple
• Normalisation rules help us design “good” schemas
• Have a primary key, add everything that depends on this primary key
• If it’s not directly related to the primary key, extract it
• ???
• Pro
fi
t!
Slide 6
Slide 6 text
MongoDB
Schemas are just as complicated
• MongoDB is not “schemaless”
• It has a schema, but it is
fl
exible
• It avoids some constraints from relational databases
• After all, they’re from the 1970s
Slide 7
Slide 7 text
Don’t think about storing data
Slide 8
Slide 8 text
Think about consuming it
Slide 9
Slide 9 text
Example
Address Book Data
{
"name": "alcaeus",
"email": [
"",
""
],
"phone": [
"",
""
]
}
Time for Queries
Everybody loves queries, right?
SELECT entry.* from entry
JOIN email ON email.entry_id = entry.id
WHERE
email.address = 'something';
Slide 12
Slide 12 text
Effects of Normalisation
Does it help?
• Need to join across tables to
fi
nd our main record
• Only get main data
• Multiple queries required to fetch rest of data
• Alternative: complicated result preparation
• How does this help?
Slide 13
Slide 13 text
Time for Queries #2
I love THOSE queries
{
"email.address": "something"
}
Slide 14
Slide 14 text
Normalisation is a Relic
Slide 15
Slide 15 text
Sylius and MongoDB
Slide 16
Slide 16 text
Sylius and MongoDB
What’s the big problem?
• Sylius uses Doctrine ORM
• Doctrine ORM and Doctrine MongoDB ODM are very similar
• Interfaces are shared, entities can be shared
• How hard can it be?
From Tables to Collections
Making use of advanced schema types
• References point to related data
• Embedded documents include all related data
• Could be used in relational databases (jsonb / complex types)
• Advantages to both
• It’s important to understand when to choose which
Referencing or embedding?
Is there another way?
• Example: linking product variants and their option values
• We don’t own the data, so we should use references
• We don’t want to join, so embedded data would be best
• We can’t reference an embedded document
• Why not do both?
Transactions
Quick facts
• MongoDB is ACID compliant
• You can run multi-document transactions
• No nested transactions
• Good schema design can help you avoid them
• Doctrine MongoDB ODM doesn’t support them
Slide 34
Slide 34 text
Transactions
In a nutshell
• Start transaction
• Do your business logic
• Commit transaction
• (Or abort if you’ve decided otherwise)
• Rollback on error
Slide 35
Slide 35 text
Transactions
In MongoDB
$session = $client->startSession();
$session->startTransaction();
// Complex database logic here
$session->commitTransaction();
Transactions
Retry Failed Transactions
while (1) {
$session->startTransaction();
// Complex database logic here
while (1) {
try {
$session->commitTransaction();
} catch (RuntimeException $e) {
if (
$e->getCode() !== 50 /* MaxTimeMSExpired */ &&
$e->hasErrorLabel('UnknownTransactionCommitResult')
) {
// Retry committing the transaction
continue;
}
if (
$e->hasErrorLabel('TransientTransactionError')
) {
// Restart transaction
continue 2;
}
throw $e;
}
}
}
Slide 38
Slide 38 text
Transactions
Keep It Simple
$doComplexDatabaseLogic = function (Session $session): void {
// Complex database logic here
};
$session = $client->startSession();
\MongoDB\with_transaction(
$session,
$doComplexDatabaseLogic,
);
Slide 39
Slide 39 text
Transactions
With Doctrine MongoDB ODM
$session = $client->startSession();
\MongoDB\with_transaction(
$session,
function (Session $session) use ($documentManager): void {
$documentManager->flush(['session' => $session]);
}
);
Slide 40
Slide 40 text
Transactions
With Doctrine MongoDB ODM
$session = $client->startSession();
Slide 41
Slide 41 text
What’s Next?
Slide 42
Slide 42 text
The ecosystem needs updating
Slide 43
Slide 43 text
Ecosystem Updates
Doctrine MongoDB ODM
• Support transactions in UnitOfWork
• Support new update mechanisms
• Support hybrid references
• Support updating hybrid references
Slide 44
Slide 44 text
Ecosystem Updates
Doctrine Extensions
• Update Translation extension to leverage embedded documents
• Update Tree extension to support nested set in ODM
• Update Sortable extension to support ODM
Slide 45
Slide 45 text
Ecosystem Updates
Sylius
• Create mapping
fi
les for Doctrine MongoDB ODM
• Create repositories and rewrite queries