Tenant Sharing
How do you share data between tenants?
While Nile provides a native approach to isolating data between tenants, it also supports sharing data across tenants using shared tables.
For example
- A corporate travel booking site like TripActions (https://www.navan.com/) or Concur helps company employees book flights and manage their trips. Every company that uses the product must see a list of available flights. The list of flights can be stored in a shared table accessible to all tenants in any location.
- A CRM product like Salesforce or Hubspot lists companies by categories that Sales reps can choose from for outreach. The list of companies and their categories is a global list that can be made available to all customers using shared tables.
- Your application is an Infrastructure SaaS product (e.g., Snowflake or Databricks) and wants to provide a list of cloud providers and regions for users to select where they want to deploy their infrastructure. The data about all the cloud providers and the available regions can be in a shared table, and all the tenants can access it.
Creating and inserting data into shared tables
To create a shared table, you must create a standard table without a tenant_id column. Nile ensures that the data in the shared table is available across all tenants.
A table with a list of flights for a corporate travel booking site. The list of flights can be shared across tenants.
Inserting into shared table works as normal with SQL (TBD JS)
Most use cases for shared tables will involve loading data from some external source instead of manual inserts into the table. For example, to populate the flights table, you will typically load the data from an airline data company (eg. Sabre) and also keep it upto date by subscribing to real time changes.
Querying data from shared tables
Since shared tables are just standard Postgres tables, all types of queries work on Shared tables.
Selecting the flights that start from SFO
id | name | from_location | to_location | departure_time | arrival_time |
---|---|---|---|---|---|
643 | United | SFO | NY | 2023-12-22 19:10:25 | 2016-12-23 05:10:25 |
You can also query by joining multiple shared tables. We can join the “flights” table with “airports” table to get more detailed information about the destination from where the flights are leaving.
id | name | from_location | address |
---|---|---|---|
643 | United | SFO | San Francisco, CA 94128 |
Joining data between tenant aware and shared tables
Tenant aware tables can be joined with shared tables to provide relevant information for a specific tenant. For example, let us consider a bookings table that has all the flights booked by employees of a specific company(or tenant) with id 7543a610-f068-45fa-8ba0-7bc884bd29c2. Each booking will refer to the flights id in the ‘flights’ table. It would be trivial to show detailed flight information along with every booking by joining the two tables.
booking_id | employee_id | name | from_destination | to_destination | departure_time | arrival_time | total_price |
---|---|---|---|---|---|---|---|
1345 | 2 | United | SFO | NY | 2023-12-22 19:10:25 | 2016-12-23 05:10:25 | 200.00 |
2456 | 3 | Indian Airways | Mumbai | Delhi | 2024-11-23 19:10:25 | 2024-11-23 01:10:25 | 150.00 |
3240 | 4 | Indian Airways | Mumbai | Delhi | 2024-11-23 19:10:25 | 2024-11-23 01:10:25 | 250.00 |
Foreign keys for shared tables
A shared table can have a foreign key reference to another shared table. In the travel example, the flights table can have a reference to the airport id. So, the ‘flights’ table can be modified to have an airport id column that is a foreign reference to the id column in the ‘airports’ table
Transactions with shared tables
Finally, it is possible to do writes to multiple shared tables in the same transaction. A good use case for this is to ensure multiple shared tables are in a consistent state when they are loaded with data. While the example above showed that the flights and airport tables were populated separately, another alternative is to ensure that for each airport that is inserted in the ‘airports’ table, all the flights are updated in the ‘flights’ table. This is specifically useful when data is bootstrapped from some external source.
Was this page helpful?