Data integration and cost optimization achieved with MongoDB Atlas Triggers

This is Kobayashi (@berlinbytes) from the Service Reliability Group (SRG) of the Media Division.
#SRGThe Service Reliability Group primarily provides comprehensive support for the infrastructure surrounding our media services, focusing on improving existing services, launching new ones, and contributing to open-source software (OSS).
This article explains the mechanism and examples of MongoDB Atlas Triggers, and introduces how to improve development efficiency and reduce costs through serverless data synchronization and operational automation.

About MongoDB Atlas


MongoDB Atlas is a fully managed cloud database service (DBaaS) available on major cloud providers such as AWS, Google Cloud, and Azure. It's incredibly convenient because it allows you to build a scalable MongoDB environment in just a few clicks while minimizing infrastructure management.
MongoDB Atlas includes a feature called "Triggers" that streamlines application development and operation. By utilizing Triggers, you can achieve serverless real-time processing triggered by database changes, as well as periodic maintenance tasks.
From here, I'd like to share the results of my research into how Triggers are being used, based on official examples and other sources.

Overview and Mechanism of MongoDB Atlas Triggers


Triggers are a feature that automatically executes predefined server-side logic (Atlas Functions) when a specific event occurs. This allows you to implement event-driven processing and scheduled tasks without having to provision or manage infrastructure.
There are two main types of triggers:

1. Database Triggers

This is a trigger that fires when changes are detected to a MongoDB collection (document insertion, update, deletion, or replacement). Technically, it is based on MongoDB's "Change Streams" feature, which captures change events in real time by monitoring the database's operation log (Oplog). This allows you to build applications that respond instantly to user actions and changes in device state.

2. Scheduled Triggers

This is a trigger that executes a function periodically based on a specified schedule. The execution frequency can be defined using "CRON expressions," which are widely used in Unix-like systems. It is suitable for automating tasks that occur over time, such as nightly batch processing, periodic report generation, data cleanup, or updating tokens for external APIs.

How to use the two triggers


These two triggers can be used interchangeably depending on the objective you want to achieve.
  • Situations where Database Triggers are suitable Use this when you want to react to "immediate changes" caused by users or IoT devices. For example, notifying the ordering system the moment inventory decreases, sending a welcome email upon user registration, or sending a push notification when a chat message is posted. Choose this option when you need to react immediately to changes.
  • Patterns where Scheduled Triggers are suitable: They are suitable for operational tasks that need to be performed at a fixed time, and for cost management. For example, cases such as reducing database specifications outside of business hours to cut costs, aggregating data every morning to update dashboards, or periodically deleting old log data. You would likely choose this option for any time-based processing.

Real-time integration via Database Triggers


Here, we will introduce examples of system integration triggered by data changes and real-time processing, categorized by pattern.

Instant integration with external systems and event-driven systems

Based on data changes in MongoDB Atlas, we invoke external FaaS (Function as a Service) such as Azure Functions and AWS Lambda to synchronize data with other systems.

IoT and real-time control

Database Triggers also appear to be useful for managing the state of physical devices.

Improving the user experience (notifications and synchronization)

It also contributes to improving the user experience in mobile apps and other applications.

Automating operations and reducing costs with Scheduled Triggers


Next, we will introduce examples of how Scheduled Triggers, which are executed at regular intervals, are being used to improve operational efficiency and reduce costs.

Cost optimization (automatic scaling/stopping)

In cloud database cost management, adjusting resources during periods of non-use is highly effective.
  • Atlas Cluster Automation As an open-source tool released by members of MongoDB, a mechanism for simultaneously stopping (Pause) or resuming (Resume) tagged clusters has been made public. By automatically stopping clusters that do not need to be running at night or on weekends, such as development or testing environments, you can prevent unnecessary charges.

Regular maintenance and aggregation

Regular tasks necessary for stable system operation and data utilization are also automated.

summary


MongoDB Atlas Triggers is a tool that can simplify application architecture and promote operational automation.
Database Triggers enable real-time interaction and notifications triggered by data changes, allowing you to build event-driven applications. Scheduled Triggers allow for cost reduction through automated resource adjustments and serverless implementation of regular data maintenance.
By utilizing these features effectively, we can not only reduce development time but also enjoy business benefits such as optimized infrastructure costs and improved user experience. At our company, we are already benefiting from cost optimization through Scheduled Triggers across multiple projects.
If you haven't already taken advantage of this feature, why not start by trying out automatic shutdown settings for your development environment or implementing a simple notification function?
 
SRG is looking for new team members.
If you are interested, please contact us here.