- Introduction
- Project setup
- Data Models
- Getting Tenant's Database from the request
- Creating new Tenants
- Conclusion
For quick revision on multi-tenant architecture, refer to my previous blog post on Multi-Tenant Architecture basics and Multi-Tenant Architecture - Planning and Design. In this blog post, I will be discussing the implementation of multi-tenant architecture in a real-world scenarios. well, almost.
We are going to use golang for our backend service. The backend will be a monolithic REST API service that will serve the frontend with the data in the form of JSON. We do no render any HTML in the backend. The frontend will be a separate service that will consume the backend API.
The backend will have a similar structure:
models
- Contains the database models. (we are using gorm for database operations)modules
- Contains the modules for the application. Each module will have its own controller, model, and routes.public
- Contains the public files like images, icons, etc.utils
- Contains the utility functions like database connection, authentication, etc.permissions
are handled using the casbin library.- REST APIs implemented with the GoFiber framework.
For the full implementation, please look into the Awesome Backend repository. For the sake of simplicity, we are focusing on the multi-tenant part of the application.
Get Origin
header from the request
Maintain a map of database connections
Here we maintain a CreatedAt
field also, just to refresh connections in some predefined time interval. This is to ensure that if someone manually stops a certain client, the database connections also gets refreshed. This CONNECTION_REFRESH_INTERVAL
depends on the type of app built and the severity of actions performed on the app.
At the time of picking the database connection, we can also check if it is past CONNECTION_REFRESH_INTERVAL
time, if then, create a new connection, (and add to the tenant connection cache)
So, in a nutshell, the database connection function works like this
This GetDbFromRequestOrigin
function can be directly used to get the tenant db connection. Now you can do any CRUD operations with this database connection.
Now, creating new Tenants is not an easy process. In the demo application, I have keep this in manual mode. But you can try one of these methods.
A sepatate application: You can checkout Awesome Frontend admin app, where I set up creating a new tenant from a separate app. The only users there are the tenant owners.
A separate view: In the same application, users can create their own tenants, but it would be sligtly tricky in terms of how are you segregating requests coming from a different tenant origin to create a new tenant. Other than this, it will be pretty straightforeward
Some kind of Email Invites etc..
This is a very basic implementation of multi-tenant architecture. There are many things that can be improved, like handling custom domains, primary and secondary domains, etc. But this should give you a good starting point to implement multi-tenant architecture in your application.
For those looking for a more complete implementation with code, Please checkout the repositories Awesome Frontend and Awesome Backend for a complete code example.