mongov2.png

Self-Host Appsmith Using Mongodb Atlas As an External Database

Goal

MongoDB stands as a stalwart companion to Appsmith's versatile architecture. MongoDB's flexible and scalable NoSQL database solution seamlessly integrates with Appsmith, serving as the backbone for data storage. MongoDB is particularly noteworthy because it powers all the Appsmith applications, regardless of the deployment scenario—be it self-hosted or on the cloud, with the Community, Enterprise, or Business edition.

This article delves into MongoDB's crucial role in the Appsmith ecosystem, emphasizing its reliability and performance across all instances. At the end of this article, you will have a self-hosted instance of Appsmith using a decoupled and external MongoDB Database running on MongoDB Atlas, making the Appsmith container stateless. 

Prerequisites

  • A self-hosted Appsmith instance. See the installation guides for installing Appsmith.

  • Ensure your MongoDB user account has readWrite and clusterMonitor roles assigned.

Overview

Appsmith is a turnkey solution for building internal apps in the cloud. It’s a complete workbench for creating graphical interfaces and tools that connect to your existing data sources. Appsmith is deployed as a single Docker container with a single volume for storing persistent data and also houses all five processes in one image via Supervisord. We call this the “modular monolithic” architecture.

Appsmith runs an embedded instance of MongoDB for data storage inside the docket container, but you can also decouple it to external instances and run Appsmith in a serverless/stateless architecture. Using external instances makes the Appsmith container stateless, enabling better performance and scalability. 

MongoDB is the engine that powers the dynamic and responsive nature of Appsmith applications. Its schema-less, document-oriented approach aligns with Appsmith's strategy for flexibility and facilitates a seamless integration process for developers, enabling them to focus on crafting powerful graphical interfaces without the burden of rigid data structures.

As we embark on the journey of self-hosting Appsmith using MongoDB Atlas as a database, we recognize MongoDB's pivotal role in ensuring the system's efficiency, scalability, and reliability.  If you want to learn in more depth about Appsmith's Decoupled/Serverless Architecture, we recommend you read this article that explains it. Let's take a look at how to make this happen.

  1. Configure your MongoDB Atlas Cluster

    Go to https://cloud.mongodb.com/ and create an account or login to your existing account, then create a new Cluster or go to the Cluster you want to use to run Appsmith. Then select a Database Deployment or create a new Database Deployment, and you will see something like this: 

    MONGODB

    Now that you have selected your Database Deployment, go to its Collections to create a new Collection for Appsmith by clicking "Browse Collections" 

    mongodb appsmith cluster

    Now create a new Collection by giving the name you want. In this case, I will call mine appsmithlocal. With the Database Collection made, we now need to configure the access to our Cluster.  Go to the Network Access menu on the left navigation and click the "Add IP Address" button to allow access.

    network access mongodb

    Now, click on the "Allow Access from Anywhere" button to add 0.0.0.0/0 as the allowlist IP, then confirm to save. 

    MONGODB

    IMPORTANT: We do NOT recommend allowing connections from anywhere, it is a bad security practice, and you should only allow the IP address of your Appsmith instance. You can read this article to learn how to Setup a Secure Static Outbound IP Address

    Now go back to the Database Deployment page, and then click the "Connect" button to get the connection string, the structure usually is something like: mongodb+srv://username:password@host/databasename?w=majority&retrywrites=true&authsource=admin&minpoolsize=0

    For example, in my case, the connection string is: mongodb+srv://kevin:********@cluster0.ej7ikqx.mongodb.net/appsmithlocal?w=majority&retrywrites=true&authsource=admin&minpoolsize=0 so this is what you have to copy and keep handy for the following step. 

  2. Configure your Appsmith Instance

    On Appsmith, you can add your external MongoDB connection to Atlas by using one of two ways: using the UI in the Admin Settings or using Environment Variables at your container level.

    Admin settings

    You can use Admin Settings to set up an external MongoDB. The MongoDB URI property is present under Admin Settings > Advanced Settings. Add your MongoDB URI to the setting and click the SAVE & RESTART button. The server restart establishes a connection with the external MongoDB.

    mongodbappsmith

    If you set values using environment variables for your instance, those values take precedence over those specified in the Admin Settings UI.

    Environment variable

    To connect to your external MongoDB Atlas instance, you must update the environment variable APPSMITH_MONGODB_URI. For example:

    APPSMITH_MONGODB_URI=mongodb+srv://kevin:*******@cluster0.ej7ikqx.mongodb.net/appsmithlocal?w=majority&retrywrites=true&authsource=admin&minpoolsize=0
    

    then restart the Appsmith container by using the below command:

    docker-compose restart appsmith
    

Conclusion