Architecture
Permission Mongo is designed as a high-performance middleware layer between your application and MongoDB, providing REST API, RBAC, versioning, and audit logging.System overview
Core components
HTTP server
Permission Mongo uses fasthttp for high-performance HTTP handling. Key characteristics:- 10x faster than net/http for high-throughput scenarios
- Optimized for 256K concurrent connections
- Zero-allocation routing with atomic operations
pkg/api/server.go
pkg/api/server.go
MongoDB store
The MongoDB store layer (pkg/store/mongo.go) provides:
- Connection pooling (default: 100 connections)
- CRUD operations with RBAC filter injection
- Aggregation pipeline execution
- Index management
cmd/server/main.go:62-77
Redis cache
Redis is used for caching frequently accessed data: Cache keys:pm:policy:{tenant}:compiled- Compiled RBAC policies (TTL: 60s)pm:hier:{tenant}:{user}:subs- User subordinates (TTL: 300s)pm:schema:compiled- Schema definitions (TTL: 60s)
cmd/server/main.go:80-91
Schema validator
Validates documents against schema definitions before write operations. Features:- Type checking (string, number, boolean, date, objectId, array, object)
- Constraints (required, min/max, pattern, enum)
- Nested object and array validation
- Computed field evaluation
pkg/schema/validator.go
Initialization: cmd/server/main.go:94-95
examples/config/schema.yml:18-415.
RBAC engine
The Role-Based Access Control engine enforces permissions at multiple levels: Permission checks:- Collection-level: Can user perform action on collection?
- Document-level: Does document match
whenclause? - Field-level: Which fields can user read/write?
policy.yml
user.id- Current user IDuser.tenant_id- User’s tenantuser.roles- User’s roles arrayuser.$subordinates- All subordinate user IDs (from hierarchy)
pkg/rbac/engine.go
Initialization: cmd/server/main.go:104-105
examples/config/policy.yml:1-178.
Hierarchy resolver
Resolves organizational hierarchy for manager-subordinate relationships. How it works:- Hierarchy defined in schema:
users.manager_idreferencesusers._id - Transitive closure stored in
_pm_hierarchycollection - Cached in Redis for fast lookups
pkg/hierarchy/resolver.go
Initialization: cmd/server/main.go:98-101
Version manager
Tracks document change history in the_pm_versions collection.
Version storage:
GET /{collection}/{id}/versions- List versionsGET /{collection}/{id}/versions/{v}- Get specific versionGET /{collection}/{id}/diff/{v1}/{v2}- Compare versionsPOST /{collection}/{id}/restore/{v}- Restore to version
pkg/version/manager.go, pkg/api/handlers_version.go
Initialization: cmd/server/main.go:108-109
Audit logger
Records all operations for compliance and debugging. Audit log entry:- MongoDB collection with TTL
- Webhook (batch HTTP POST)
- Stdout (for development)
pkg/audit/logger.go
Initialization: cmd/server/main.go:112-113
Hooks manager
Executes pre/post operation hooks for validation, transformation, and notifications. Hook types:pre_create,post_createpre_update,post_updatepre_delete,post_delete
set_field- Set field to value or expressionvalidate- Check condition or failvalidate_ref- Verify reference existshttp- Call external webhook
pkg/hooks/executor.go
Initialization: cmd/server/main.go:116-120
Request flow
Typical request lifecycle for an update operation:pkg/api/handlers_crud.go.
Data storage
MongoDB collections
Redis keys
Project structure
Performance characteristics
Throughput targets
| Operation | Target Latency | Throughput |
|---|---|---|
| GET (cached) | Less than 10ms | 50K+ QPS |
| POST (create) | Less than 25ms | 25K+ QPS |
| PUT (update with versioning) | Less than 30ms | 20K+ QPS |
| Batch (100 docs) | Less than 100ms | 5K+ QPS |
Optimizations
Lock-free router (pkg/api/router.go):
- Atomic operations for route registration
- Zero allocations in hot path
- MongoDB: 100 connections (configurable)
- Redis: 500 connections (configurable)
- Batched inserts to MongoDB
- No latency impact on user requests
- RBAC expressions compiled once and cached
- Reduces CPU usage by 80% vs. parsing on every request
Scalability
Permission Mongo scales to:- 50-100M documents per collection (single instance)
- 500M total documents across all collections
- 1M users in hierarchy
- 25K QPS sustained on standard hardware
Security
Authentication
JWT-based authentication with configurable algorithms:- RS256 (asymmetric, recommended for production)
- HS256 (symmetric, simpler setup)
Authorization
Multi-level RBAC enforcement:- Collection-level - Can user access this collection?
- Action-level - Can user perform this action (create/read/update/delete)?
- Document-level - Does document match
whenclause? - Field-level - Which fields are allowed/denied/masked?
Data protection
Field masking:Next steps
Configuration
Learn how to configure schema, policies, and hooks
API Reference
Complete REST API documentation
RBAC Guide
Master role-based access control
Performance Tuning
Optimize for high throughput