MongoDB Triggers to AWS EventBridge: Simple Use-Cases

Amazon Web Services, or AWS, offers developers and engineers the most scalable, reliable and extensive cloud-based services and applications in the IaaS (Infrastructure as a Service) industry. Experts at Exobyte rely heavily on AWS to run applications and services along with a document-based, NoSQL, database known as MongoDB.

Event-driven architecture is a paradigm that applications and services create, react and respond to different events. EventBridge is a service provided by AWS that developers can use to react and respond to events from a configured source (meaning an external source that is sending an event to EventBridge). One of those sources is a MongoDB database trigger. We’ll provide a brief overview below as well as a few use cases as to how this particular scenario is useful.

What is a Database Trigger?

A database trigger is an action that occurs in response to a database document being inserted, updated or deleted in a collection. If you’re unfamiliar with traditional relational databases, then you might be wondering what a document or collection is. In a MongoDB context, a document is a row and a collection is a table.

Through MongoDB Atlas, MongoDB’s Database as a Service (DaaS) platform, you can configure database triggers to run a function that you define. Or, you can configure the trigger to send the affected document to EventBridge.

The beauty in database triggers is that they follow the event-driven architecture, in that an action occurs in direct response to data being created, updated, or deleted. Imagine the kind of alternative that writes a service and constantly checks every piece of data in your database for changes. Yikes! This would be wildly inefficient. Fortunately, database triggers take all the leg work out of this process and only require simple configuration changes.

What is EventBridge?

As defined by Amazon

“Amazon EventBridge is a serverless event bus that makes it easy to connect applications together using data from your own applications, integrated Software-as-a-Service (SaaS) applications, and AWS services.”

This means that EventBridge is a service that runs on-demand without the use of a dedicated server and can respond to events coming from several different sources.

EventBridge has three core terms: source, bus and target. The source is where the event originates. The bus is what actually processes the incoming event. Finally, the target is where the processed event should be sent to.

Source: AWS

So how do MongoDB and EventBridge correlate? 

MongoDB would be our event source where a database trigger is capturing the impacted data in our database and sending that as an event to the EventBridge bus. The bus then processes the data and then sends it to the appropriate source (target).

To sum it up, EventBridge creates a bridge (as the name implies) between your MongoDB database and your AWS architecture.

Use Cases

Now that you understand how MongoDB and EventBridge work to create a seamless, event-driven architecture, let’s next discuss how you can use this technology in real-life scenarios. One of the most common scenarios is that a chunk of data is created, updated or deleted in a database, and as a result, additional processing/actions are required.

Use Case #1: Inventory Processing

When a customer places an order, that order gets inserted into an orders collection. To maintain accurate stock levels, you then need to update the inventory levels for that particular product. Your database trigger is configured to watch for insert events on your orders collection. In response, EventBridge can send that order to an event target that then processes the order and updates the inventory levels accordingly.

Use Case #2: Email Automation

There are plenty of user actions out there that require some type of email being sent as a result of said action. Any email you might want to send can be inserted into an emails collection with all relevant content information. A database trigger could then be configured on an emails collection. EventBridge would send the email document to a target service that composes an email automatically (per templates you provide) and sends it to the email recipient.

Use Case #3: Data Cleanup

Another common scenario exists when you need data cleanup. In most database designs, all related data might not exist in the same location for easy cleanup. As a result, if you delete data in one location there’s no guarantee that said data will automatically be deleted in all other locations. As a case in point, if a customer sends you a deletion request per their rights under the California’s Consumer Privacy Act, you can utilize a database trigger to make sure that once a customer is deleted, then that their corresponding data is deleted also. 

This data cleanup case use is not just handy for consumer privacy law compliance, but it also saves you server space and lowers your operating costs.

In Conclusion

Using AWS EventBridge to efficiently connect your cloud infrastructure built on AWS to your MongoDB database is a no brainer. EventBridge allows you to seamlessly create an event-driven architecture which is extremely scalable, reliable, and cost-effective.

We hope that you found this quick guide helpful. If you have more questions or need expert assistance, don’t hesitate to reach out to one of our team members at Exobyte today.