Data linkage and cost optimization achieved with MongoDB Atlas Triggers

This is Kobayashi (@berlinbytes) from the Service Reliability Group (SRG) of the Media Headquarters.
#SRG(Service Reliability Group) is a group that mainly provides cross-sectional support for the infrastructure of our media services, improving existing services, launching new ones, and contributing to OSS.
This article explains the mechanism and examples of MongoDB Atlas Triggers, and introduces ways to improve development efficiency and reduce costs through serverless data linkage 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 extremely convenient because it allows you to create a scalable MongoDB environment with just a few clicks while minimizing infrastructure management.
MongoDB Atlas has a feature called "Triggers" that streamlines application development and operations. By utilizing Triggers, you can perform real-time processing triggered by database changes, as well as regular maintenance tasks, all in a serverless manner.
From here, I would like to introduce the results of my research into how Triggers is being used, based on official examples.

What are MongoDB Atlas Triggers and how do they work?


Triggers is a feature that automatically executes pre-defined server-side logic (Atlas Functions) when specific events occur. You can implement event-driven processing or periodic tasks without having to provision or manage infrastructure.
There are two main types of Triggers:

1. Database Triggers

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

2. Scheduled Triggers

This trigger executes a function periodically based on a specified schedule. You can define the execution frequency using the "CRON" expression, which is widely used in Unix-like systems. This is suitable for automating tasks that occur over time, such as nightly batch processing, regular report generation, data cleanup, or token renewal for external APIs.

How to use the two triggers


These two triggers can be used depending on the purpose you want to achieve.
  • Database Triggers are a good fit Use Database Triggers when you want to respond to "instant changes" caused by users or IoT devices. For example, notifying an ordering system the moment inventory gets low, sending a welcome email upon user registration, or sending a push notification when a chat message is posted. Choose this option if you need to respond immediately to changes.
  • Scheduled Triggers are suitable for: They're ideal for operational tasks that are performed in bulk at set times, as well as for cost management. For example, you might want to reduce costs by lowering database specifications outside of business hours, aggregate data every morning to update a dashboard, or periodically delete old log data. You'll probably want to choose Scheduled Triggers for any processing that requires a specific time as the starting point.

Real-time integration with Database Triggers


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

Instant integration with external systems and event-driven

When data changes occur on MongoDB Atlas, external FaaS (Function as a Service) services such as Azure Functions and AWS Lambda are invoked to synchronize data with other systems.

IoT and Real-Time Control

Database Triggers also seem to be useful for managing the state of physical devices.
  • Bosch (Smart Building) This technology was used in a prototype for controlling lighting equipment from an AR app. When a document representing the "light status" in MongoDB is updated, a trigger is fired, and the actual light is turned on/off via the API. The database acts as an intermediary between the app and the physical device, synchronizing the state.

Improved user experience (notifications and synchronization)

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

Automate operations and reduce costs with Scheduled Triggers


Next, we will introduce some examples of how Scheduled Triggers, which are executed periodically, have been used to improve operational efficiency and reduce costs.

Cost optimization (auto-scaling/stopping)

Throttling resources during periods of inactivity is a very effective way to manage cloud database costs.
  • Atlas Cluster Automation This official open-source tool from MongoDB provides a mechanism for simultaneously pausing and resuming tagged clusters. By automatically pausing clusters that do not need to be running at night or on holidays, such as in development or testing environments, you can prevent unnecessary charges.

Regular maintenance and counting

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 integration and notifications triggered by data changes, allowing you to build event-driven applications. "Scheduled Triggers" allows you to reduce costs by automatically adjusting resources and perform regular data maintenance without a server.
By using these functions appropriately, it seems possible to not only reduce development time, but also enjoy business benefits such as optimizing infrastructure costs and improving the user experience. Our company has also benefited from cost optimization using Scheduled Triggers in multiple projects.
If you haven't yet taken advantage of it, why not start by setting up an automatic shutdown of your development environment and implementing a simple notification function?
 
SRG is looking for people to work with us.
If you are interested, please contact us here.