feat(tenant): add opt-in multi-tenancy with per-tenant key isolation - #2325
Open
ErfanMomeniii wants to merge 2 commits into
Open
feat(tenant): add opt-in multi-tenancy with per-tenant key isolation#2325ErfanMomeniii wants to merge 2 commits into
ErfanMomeniii wants to merge 2 commits into
Conversation
Contributor
Author
|
Hi @matthewmcneely, could you please review? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds optional multi-tenancy to Badger so a single
DBcan serve many tenants (customers, workspaces, environments) while keeping their data isolated. Today the usual workaround is to run a separate Badger instance per tenant, which gets expensive on file descriptors and memory once you have more than a handful. This lets you keep one instance and give each tenant its own isolated keyspace instead.The feature is off by default, so nothing changes for existing users unless they opt in with
WithMultiTenancy(true).Here's the gist of how it's used:
Under the hood each tenant gets a unique 8-byte namespace that Badger puts in front of its keys, so you keep writing plain keys like
user:1and never have to think about the prefix. Two tenants can use the exact same key without colliding.There are two entry points once it's enabled:
db.Tenants()manages the tenants themselves — create, look up by id or name, list, ban and unban, delete, and purge. Tenant records live in the same DB and come back after a restart.Deletejust deregisters the tenant (its data stays around for offline cleanup), whilePurgealso drops all of its keys to free up disk.db.TenantScope(id)is what you use for the tenant's data. Besides the one-shotSet/Get/Deleteshown above, it hasUpdate/Viewwhen you need a real transaction (atomic multi-key writes, read-modify-write), an iterator that only sees that tenant's keys, andNewWriteBatchfor bulk loading.Isolation is enforced on writes: with multi-tenancy on, any write to a namespace that isn't a registered tenant is rejected with
ErrUnknownTenant. Reads are isolated by going throughTenantScope, which confines everything to that tenant's range.A couple of things worth calling out:
NamespaceOffsetto 0, since the scoped iterator relies on the tenant id being a plain key prefix. If you'd already set a non-zero offset explicitly,Openreturns an error rather than silently overriding it.OpenManaged) can use the tenant registry, but notTenantScope— scoped operations assign their own timestamps, which clashes with the user-supplied timestamps managed mode expects. CallingTenantScopethere returnsErrTenantScopeManaged.Backward compatibility was the main constraint. The option defaults to false, the enforcement checks short-circuit when it's off, and legacy plain-key databases stay fully readable after enabling it (only new writes are gated). The existing
BanNamespaceAPI and theSuperFlaground-trip are untouched.There's an end-user guide in
docs/multi-tenancy.mdand a short section in the README.Testing
Added 30 tests in
tenant_test.gocovering the tenant lifecycle, cross-tenant isolation, restart persistence, concurrent creates racing on the same and on distinct names, some key layout edge cases (short/3-char names, empty logical keys), managed-mode behavior, and the backward-compatibility paths. The full package suite passes and the tenant tests are clean under-race.Checklist