Starting to understand Multitenancy

The image was taken on a trip of mine to Uruguay — 2018.

If you already know or work with multitenancy, Pay It Forward (read through to understand). If you don’t know, maybe you are already working with it and just don’t know it yet (just like me in the near past).


The multitenancy architecture consists of working with applications that will have multiple users sharing the same database, however, having access only to the data that belongs to them.

It is widely adopted in software as a service (SaaS) applications, as many customers often use it. These same becoming the tenants of this application.
Sometimes some software uses architecture to attack deeper situations, as it was in our case.


We are developing an application where our tenant will work with more than one user to access the platform. However, in the beginning, it was designed for just one login to control it. That is, our tenant would have more residents with him using the same rent.

Then, we decided to stop (we were already starting development on the front end), go back, and reshape our entire database from the beginning.


Before we get there, let’s look at some ways in which we can apply the architecture in various models to what is being used in RBAF Manager.

  • One database for each customer

When accessing the software, it is responsible for directing the application to the cluster responsible for that user’s collections.

  • One collection for each customer

Each user would have a collection of their documents. Something that becomes unworkable for an application that is just starting for several reasons. One key is the cost of maintaining something at this level in the future with a very large user base.

  • One database for all tenants

Preferred by many companies is that it is easy to implement at the beginning of development (it is understood that the model concept is easy), distributing tenants in only one bank becomes the most common path.

And that’s why we decided to work. Our admin user may own other users and these users need to be “tied” to the company they belong to.


It was then decided that our tenant would be called Team since the platform is geared towards sports teams and to control access to the data of this tenant would require that each model of our system had its id.

  • Our basic tenant:
  • The other models followed the pattern below made for `Player`
Player model example

The id of the team is referenced in the model and this becomes its owner.


Using GraphQL we can work with only one endpoint for all requests. This way, when a call is placed on it, we check if the Authorization header is present:

You can function called passing the token created at login.

Passing through it, the token is decoded and we now have access to this user ID.

With the id we consult the User collection and from it, we can access the tenant that he is part of. In this case, the team he belongs to. The team was added at the time of creation, following the same model as the player collection.

UserModel example
getUser example

As soon as the user is returned, it is set in the context of our GraphQL API (line 7).

And just pass it as a parameter in the resolution of a query or mutation, to have access to only that user’s information.

An example is a query , which is used as a form of authentication to find out if there are any users logged in and authorized to access our API data.

Pay It Forward
GraphQL combined with MongoDB is a really powerful stack and starting to use it has allowed us to build some really cool stuff and it’s just the beginning.

With that, the idea of ​​this text is to share a little of what we are building / learning and to receive feedbacks whatever it may be: improvements, corrections, new implementations, etc.

This idea was fueled by the message of the movie: Pay It Forward. In it, Trevor, who is in the seventh grade tries to get a great message across the film about a theory that is based on:

- When you are helped by one person, you must make up for it by helping three other people. Thus creating a Chain of Good.

This is our current, sharing, learning, evolving with the community. Combined with one of our principles, Learning in Public.

Thanks to Sibelius Seraphini who has been helping us in a fantastic way and introduced me to the idea of Pay It Forward applied in IT.

Please, if you want to leave tips, opinions in the comments, call me, we will all learn in public. Follow me on Twitter too.

Fullstack developer at Entria; twitter @daniloab_

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store