Docs Menu
Docs Home
/ /
Atlas Architecture Center
/ /
/

Building an Event-Driven Inventory Management System

Unlock real-time analytics, automation, and workforce efficiency in your inventory operations for retail and manufacturing with MongoDB Atlas.

Use cases: Catalog, Application-Driven Analytics

Industries: Retail, Manufacturing

Products: Change Streams, MongoDB Atlas Charts, MongoDB Atlas Search, MongoDB Atlas Triggers

Partners: Next.js

In the competitive business landscape of retail and manufacturing, having the right inventory of goods in the right place at the right time is crucial. Insufficient inventory can lead to operational disruptions and missed opportunities, while excess inventory can increase costs and risks associated with storage. Companies of all sizes struggle with inventory management. Solutions such as a single view of inventory, real-time analytics, and event-driven architectures can help businesses overcome these challenges and elevate their inventory management to the next level. This demo will guide you through the process of building an inventory management system with all the capabilities mentioned above, tailored for diverse industries such as retail and manufacturing.

Effective inventory management is critical to ensure operational success and customer service excellence. With the increasing complexity of supply chains and rising customer expectations, businesses need robust systems to track inventory levels, automate processes, and analyze trends in real time. As industries continue to evolve, the importance of integrated and event-driven inventory solutions becomes more pronounced as they allow businesses to respond swiftly to market demands, minimize costs, and maintain competitiveness.

Businesses must adopt strategies that allow for dynamic adjustments to inventory levels, optimizing stock management, and reducing inefficiencies. Automation and real-time data integration are key components of these strategies. They provide the capabilities needed to enhance operational agility, streamline supply chain processes, and ensure timely delivery of products.

In today’s fast-paced retail landscape, ensuring your inventory is in the right place at the right time is essential. However, the retail industry faces significant challenges in achieving this goal. In 2022, unsold stock in the U.S. surged by a staggering $78 billion, reaching approximately $740 billion — a shocking 12 percent increase.

Retailers today need a unified view of their inventory to meet customer expectations for immediate availability and seamless purchasing experiences. The disparity between online and in-store stock can lead to missed sales opportunities and customer dissatisfaction.

Effective stock management enables retailers to leverage distributed supply chains, moving inventory fluidly between channels to meet demand where it arises. This reduces the risk of dead stock in physical stores and stockouts in online channels, enhancing overall inventory efficiency. Real-time analytics empower retailers to make data-driven decisions, adapting quickly to changing market conditions and consumer behavior. Furthermore, automated processes reduce manual errors and free up staff to focus on value-added activities, such as enhancing service quality. Event-driven architectures facilitate these improvements, allowing for seamless inventory data integration and synchronization across various platforms and devices.

Real-time wind turbine diagnosis

Figure 1. Inventory management overview

MongoDB provides a leading component for modern inventory solutions. We help businesses enhance service quality, workforce efficiency, and optimize stock management by enabling a single view of inventory, event-driven architectures, and real-time analytics. This solution lays the groundwork for advanced scenarios such as integrating IoT and RFID tags, delving into AI/ML forecasting for precise demand prediction, and distributed logistics. The versatility of this solution allows its application not only from warehouse to point of sale but also across the entire supply chain, including manufacturing, transportation, retail, and reverse logistics, making it a valuable asset for diverse business domains.

The inventory management solution leverages a Next.js application seamlessly integrated with MongoDB Atlas, providing a flexible and scalable backend. At the core of this architecture is a MongoDB Atlas Database that houses four key collections: products, transactions, users, and locations. These collections are central to managing inventory, processing transactions, and tracking user and location data.

MongoDB ensures data consistency and integrity through ACID-compliant operations. For instance, when a stock level changes due to a transaction, updates are made to both the products and transactions collections in a way that guarantees reliability and consistency across the system.

Data access between the Next.js application and MongoDB is facilitated by the MongoDB Node.js driver, enabling efficient retrieval and manipulation of information. To enhance the application’s search capabilities, Atlas Search is utilized, offering advanced full-text search features. This allows users to perform complex queries, such as searching by product category or applying filters through facets, improving the overall user experience with quick, intuitive searches.

The system's responsiveness is further enriched by MongoDB Atlas Triggers and Change Streams. Triggers automate backend logic by executing functions in response to database changes. For example, when a product’s stock falls below a certain threshold, a Trigger can automatically reorder inventory. Meanwhile, Change Streams act as real-time listeners, detecting data changes and pushing updates to the application instantly. This ensures that critical alerts, such as low stock notifications, are promptly delivered to inventory managers.

For real-time analytics, Atlas Charts provides a powerful tool to visualize data directly from MongoDB without the need for ETL pipelines. This allows decision-makers to track key metrics, like inventory levels or sales trends, in real time. With workload isolation enabled, analytics queries run on a dedicated node, ensuring that operational performance remains unaffected.

This cohesive architecture not only supports real-time data interactions but also streamlines processes by automating key tasks. It allows the Next.js application to provide a responsive and dynamic user experience, underpinned by MongoDB’s robust data management features. Through this integration, the solution effectively balances performance, automation, and scalability, making it well-suited for modern inventory management challenges.

Inventory management workflow with Triggers and Change Streams.

Figure 2. Event-driven workflow overview: Triggers allow automatic replenishment and Change Streams enable real-time low stock alerts

Inventory management system with MongoDB.

Figure 3. Inventory management system architecture using MongoDB Atlas and Next.js

The primary collections — products, transactions, users and locations — form the foundation of our inventory management system. These collections represent your stock, incoming and outgoing movements that affect stock levels, your app users, and the different locations that the inventory system is serving.

What sets the document data model apart from traditional tabular models is its remarkable flexibility, making it the perfect choice for achieving a single view of inventory. With a tabular approach, achieving a comprehensive overview of your inventory would typically involve complex joins across multiple tables. However, with MongoDB's document model, this complexity is eliminated.

For instance, in the transactions collection we take advantage of the Extended Reference pattern when referencing an item. We not only include the product.id, but also the product.name to be displayed in the UI without the need of a join or an extra query to the products collection.

If we now consider the products collection, we see how it encompasses what would have otherwise demanded over 10 tables in a relational system. For example, the product variants are contained as an embedded array in the same document, and other attributes such as product stock or locations would traditionally require additional tables. This transformational power stems from the document model's inherent flexibility. It not only empowers a more intuitive design but also ensures that related data is stored together, optimizing access.

Equivalent relational model for product.

Figure 4. Product collection equivalent in a tabular setup

Database model with MongoDB.

Figure 5. Take note of how complex objects and arrays are embedded directly within the document

{
"type": "inbound",
"location": {
"origin": {
"type": "warehouse"
},
"destination": {
"type": "factory",
"id": {
"$oid": "65c63cb61526ffd3415fadbd"
},
"name": "Bogatell Factory"
}
},
"placement_timestamp": {
"$date": "2024-04-08T15:13:58.822Z"
},
"items": [
{
"sku": "CT001-PT-MDB0001",
"name": "Programmable Thermostats",
"unit": "pieces",
"delivery_time": {
"amount": 3,
"unit": "seconds"
},
"status": [
{
"name": "placed",
"update_timestamp": {
"$date": "2024-04-08T15:13:58.822Z"
}
},
{
"name": "arrived",
"update_timestamp": {
"$date": "2024-04-08T15:14:03.741Z"
}
}
],
"amount": 15,
"product": {
"id": {
"$oid": "65cce1a4ccdfb7402dbb4db4"
},
"name": "Controls and Thermostats",
"image": {
"url": "/images/products/thermostats.png"
}
}
}
],
"automatic": true,
"transaction_number": 133
}

Building this solution involves five major steps, encompassing MongoDB Atlas setup, your app configuration, and optional personalization steps. In the GitHub repository you will find detailed instructions on how to build this solution.

1

Create a MongoDB Atlas cluster and replicate the collections, views, and indexes from the demo database.

2

Create a database trigger that automatically issues a replenishment order when stock levels fall below a defined threshold. For demo purposes, set up a second trigger to simulate an order’s progress from placement to fulfillment.

3

Configure search indexes for full-text search and filter facets in the products and transactions collections.

4

Enhance your application's visualization and analytics capabilities with Atlas Charts. Unlock real-time analytics and create custom charts based on your preferences.

5

Clone the GitHub repository to your local machine, update the environment variables with your own values, install the dependencies, and run the app locally on http://localhost:3000.

Throughout this solution, we’ve covered multiple MongoDB topics. Here’s a quick recap of the key learnings:

  • Real-time alerts: Implement real-time low-stock alerts in your frontend using Change Streams.

  • Workflow automation: Leverage database triggers to automate stock replenishment workflows.

  • Real-time analytics: Stay on top of your data, analyze trends, and make informed decisions in a timely manner with Atlas Charts.

  • Single view of inventory: Take advantage of the flexibility of the document model to create a comprehensive single view of inventory.

Remember that this is just the beginning. Feel free to explore, tweak, and enhance your inventory management system to fit your own needs.

  • Dr. Humza Akhtar, MongoDB

  • Ramiro Pinto Prieto, MongoDB

  • Tamar Alphaidze, MongoDB

Back

On this page