During this session we will cover the best practices for implementing a real-time inventory with MongoDB. This includes properly model quantities and stores to avoid large numbers of documents being indexed, how to efficiently use geo-indexing to find the closest store with a specific item available and how to run aggregation to gather interesting inventory stats. We will also cover operational considerations, like how to make inventory queries and updates from anywhere be low-latency and resilient to network partitions via tag-aware sharding.
3. Inventory – Traditional Architecture
Relational DB
System of Records
Nightly
Batches
Analytics,
Aggregations,
Reports
Caching
Layer
Field Inventory
Internal &
External Apps
Point-in-time
Loads
5. Inventory – Principles
• Single view of the inventory
• Used by most services and channels
• Read-dominated workload
• Local, real-time writes
• Bulk writes for refresh
• Geographically distributed
• Horizontally scalable
6. Requirement Challenge MongoDB
Single view of
inventory
Ensure availability of
inventory information on
all channels and
services
Developer-friendly,
document-oriented
storage
High volume,
low latency reads
Anytime, anywhere
access to inventory
data without
overloading the system
of record
Fast, indexed reads
Local reads
Horizontal scaling
Bulk updates,
intra-day deltas
Provide window-in-time
consistency for highly
available services
Bulk writes
Fast, in-place updates
Horizontal scaling
Rapid application
development cycles
Deliver new services
rapidly to capture new
opportunities
Flexible schema
Rich query language
Agile-friendly iterations
Inventory – Requirements
7. Inventory – Target Architecture
Relational DB
System of Records
Analytics,
Aggregations,
Reports
Field Inventory
Internal &
External Apps
Inventory
Assortments
Shipments
Audits
Products
Stores
Point-in-time
Loads
Nightly
Refresh
Real-time
Updates
11. Stores – Sample Queries
• Get a store by storeId
db.stores.find({ "storeId" : "store0" })
• Get a store by zip code
db.stores.find({ "address.zip" : "12345" })
17. Inventory – Sample Queries
• Get all items in a store
db.inventory.find({ storeId : "store100" })
• Get quantity for an item at a store
db.inventory.find({
"storeId" : "store100",
"productId" : "p200"
})
18. Inventory – Sample Queries
• Get quantity for a sku at a store
db.inventory.find(
{
"storeId" : "store100",
"productId" : "p200",
"vars.sku" : "sku11736"
},
{ "vars.$" : 1 }
)
19. Inventory – Sample Update
• Increment / decrement inventory for an item at a
store
db.inventory.update(
{
"storeId" : "store100",
"productId" : "p200",
"vars.sku" : "sku11736"
},
{ "$inc" : { "vars.$.q" : 20 } }
)