Overview
Bench is a command-line utility that orchestrates multiple components to create a complete Frappe application environment. Understanding its architecture helps you troubleshoot issues, extend functionality, and optimize your development workflow.Bench Directory Structure
A typical bench instance has the following directory layout:Key Directories
apps/
apps/
Contains the source code for all installed applications. Each app is a separate directory containing:
- Python modules
- Frontend assets (JS, CSS)
- Configuration files
- Hooks definitions
frappe app is always required as it provides the core framework.sites/
sites/
Stores site-specific data and configuration:
- Site directories: Each site has its own directory with:
site_config.json- Site-specific configurationprivate/- Private files and backupspublic/- Public files accessible via weblocks/- Site-level locks
- apps.txt: List of apps installed across all sites
- common_site_config.json: Configuration shared across all sites
config/
config/
Houses generated configuration files:
- Redis configuration files for cache, queue, and socketio
- Process manager configuration (Supervisor or systemd)
- PID files for running processes
env/
env/
Python virtual environment containing:
- Python interpreter
- Installed Python packages
- Application dependencies
Core Components
Bench integrates several components to create a complete application stack:1. Python Virtual Environment
- Purpose: Isolates Python dependencies
- Location:
env/directory - Default Python: Python 3.10+
- Package Manager: pip (or uv for faster installs)
2. Frappe Framework
- Purpose: Core framework providing ORM, API, UI components
- Location:
apps/frappe/ - Role: Required base for all Frappe applications
- Features:
- Database abstraction layer
- REST API framework
- Background job processing
- User authentication and permissions
3. Database Layer
MariaDB/MySQL:- Stores application data
- Default configuration: localhost:3306
- Configurable via
common_site_config.json:
4. Redis
Bench uses multiple Redis instances for different purposes:| Instance | Default Port | Purpose |
|---|---|---|
| redis_cache | 13000 | Caching layer for application data |
| redis_queue | 11000 | Background job queue management |
| redis_socketio | 13000 | Real-time websocket connections |
common_site_config.json:
5. Node.js & Frontend Build
- Purpose: Frontend asset compilation and development
- Tools:
- Node.js runtime
- npm/yarn for package management
- Webpack/Rollup for bundling
- Output: Compiled assets in
sites/assets/
6. Process Management
Development (via Procfile):- Multiple gunicorn web workers
- Background workers for job processing
- Scheduler for periodic tasks
- Redis instances
Command Routing and Execution
How Bench Commands Work
CLI Entry Point
The
bench command is registered via Python setuptools and points to bench.cli:cliLocation: bench/cli.py:65Command Discovery
Bench uses Click’s
MultiCommandGroup to discover commands from:- Built-in commands in
bench/commands/ - Frappe framework commands
- Custom app commands
bench/commands/__init__.py:1Command Categories
Bench organizes commands into logical groups: Initialization & Setup (bench/commands/make.py:5):
init- Initialize a new benchnew-app- Create new applicationget-app- Clone and install app
bench/commands/update.py:9):
update- Update bench and appsswitch-to-branch- Switch branches
bench/commands/config.py:8):
config- Manage bench configurationset-*commands for various settings
bench/commands/setup.py:13):
setup production- Production environmentsetup nginx- NGINX configurationsetup supervisor- Supervisor configuration
bench/commands/utils.py:8):
start- Start development serverrestart- Restart processesbackup-all-sites- Backup operations
Data Flow
Request Lifecycle
- HTTP Request arrives at NGINX (production) or Werkzeug (development)
- Web Server forwards request to Frappe application
- Frappe processes request:
- Checks Redis cache
- Queries MariaDB if needed
- Enqueues background jobs if needed
- Response is returned to client
- Background Workers process queued jobs asynchronously
Configuration Hierarchy
Bench uses a layered configuration approach:Development vs Production
Development Mode
- Process Manager: Honcho/Procfile
- Web Server: Werkzeug (Flask development server)
- Port: 8000 (default)
- Auto-reload: Enabled
- Debug Mode: Enabled
- Started via:
bench start
Production Mode
- Process Manager: Supervisor or systemd
- Web Server: Gunicorn + NGINX
- Port: 80/443 (standard HTTP/HTTPS)
- Auto-reload: Disabled
- Debug Mode: Disabled
- Setup via:
bench setup production
Key Python Modules
bench.bench.Bench
Main Bench class representing a bench instanceLocation:
bench/bench.py:61bench.app.App
Represents a Frappe applicationHandles app installation, updates, and dependencies
bench.utils.system
System-level utilitiesInitialization, process management, system setup
bench.config.*
Configuration generatorsNGINX, Supervisor, systemd, Redis configs