Overview
Lichess is built as a highly scalable, fully asynchronous chess server that handles millions of concurrent users. The architecture separates concerns across multiple layers and services, optimized for real-time gameplay and low latency.
System Components
Core Application (lila)
The main Lichess server, known as lila (li[chess in sca]la), is built with:Scala 3
Modern, type-safe functional programming language providing excellent concurrency support
Play Framework 2.8
Web application framework handling HTTP requests, routing, and MVC structure
Akka Streams
Reactive streams for handling asynchronous, non-blocking data flows
MacWire
Compile-time dependency injection for managing component wiring
lila.app.Lila:main, which bootstraps the Play server and initializes all modules.
Module Architecture
Lichess is organized into 83 modular components in the/modules directory, each with specific responsibilities:
Level 1 - Foundation Modules
Level 1 - Foundation Modules
Core modules providing base functionality:
core- Basic types and utilitiescoreI18n- Internationalization core
Level 2 - UI & Data Structures
Level 2 - UI & Data Structures
ui- User interface components and templatescommon- Shared utilities used across modulestree- Chess game tree representation
Level 3 - Infrastructure
Level 3 - Infrastructure
db- Database access layer for MongoDBroom- Real-time room/channel managementsearch- Elasticsearch integration for game search
Level 4 - Core Services
Level 4 - Core Services
memo- Caching layer using Caffeine/Scaffeinerating- ELO rating calculations
Level 5 - Domain Logic
Level 5 - Domain Logic
game- Game state and logicuser- User accounts and profilespuzzle- Tactical puzzlesstudy- Shared analysis boardsanalyse- Computer analysis integrationgathering- Base for tournaments and events
Level 6+ - Feature Modules
Level 6+ - Feature Modules
Higher-level features like:
tournament,swiss,simul- Competition formatsrelay- Broadcast of live gameschallenge- Challenge systemchat,msg- Communicationsecurity,mod- Moderation and safety- And many more…
Module dependencies are carefully structured to prevent circular dependencies. The
build.sbt file explicitly defines the dependency hierarchy.Frontend Architecture
Client-Side Stack
TypeScript
Type-safe JavaScript for all client code
Snabbdom
Lightweight virtual DOM library for efficient UI updates
Sass
CSS preprocessing with variables and mixins
esbuild
Ultra-fast JavaScript bundler
UI Package Structure
The frontend is organized as a pnpm monorepo workspace in/ui:
package.json- Dependencies and build configurationsrc/- TypeScript source files- Custom
buildproperty defining bundle/sync/hash rules
Build System
The customui/build script:
- Bundles TypeScript modules with esbuild
- Generates content-hashed filenames for cache busting
- Creates manifest files listing asset URLs
- Syncs npm dependencies to
/public/npm - Watches for changes in development mode
/public/compiled with 8-character content hashes in filenames.
Data Layer
MongoDB
Primary database storing:- 4.7+ billion games in compressed format
- User profiles and preferences
- Tournament and simul data
- Studies and analysis annotations
- Forum posts and messages
Lichess uses ReactiveMongo 1.1.0-RC20, a reactive, non-blocking MongoDB driver for Scala. Database queries are fully asynchronous using Scala Futures.
Redis
In-memory data store for:- WebSocket coordination between lila and lila-ws
- Real-time pub/sub messaging
- Session management
- Rate limiting
- Distributed locks
Elasticsearch
Powers the game search engine, indexing:- Game metadata and results
- Player information
- Opening variations
- Advanced query filters
Real-Time Communication
WebSocket Server (lila-ws)
A separate server handles all WebSocket connections:- Written in Scala
- Optimized for connection handling
- Communicates with main lila server via Redis pub/sub
- Handles move broadcasts, chat, and live updates
Communication Flow
- Client connects to lila-ws via WebSocket
- lila-ws subscribes to Redis channels
- lila publishes events to Redis
- lila-ws broadcasts to connected clients
Chess Engine Integration
Stockfish
The world’s strongest open-source chess engine:- Used for computer analysis
- Evaluates positions and suggests best moves
- Provides multi-PV (principal variation) analysis
Fishnet
Distributed computing network for analysis:- Community members donate CPU time
- Client software available at lichess-org/fishnet
- Analyzes games asynchronously
- Results stored in
evalCachemodule
Chess Logic
scalachess
Pure Scala chess library (separate repository):- Implements all chess rules and variants
- Move validation and generation
- Position representation
- PGN parsing and export
- Rating calculations
project/Dependencies.scala:
Request Handling
HTTP Flow
Asynchronous Execution
All I/O operations are non-blocking:- Database queries return
Future[Result] - WebSocket messages use Akka Streams
- HTTP calls use Play WS client
- Rate limiting uses Redis Lua scripts
Lichess uses
scala.concurrent.ExecutionContextOpportunistic as the default executor for better performance.Caching Strategy
Multi-layer caching for optimal performance:CDN
Cloudflare caches static assets globally with 1-year TTL
Application Cache
Caffeine/Scaffeine in-memory caches for frequently accessed data
Redis
Distributed cache shared across application instances
site.FE782A9C.js) ensure cache invalidation when files change.
Monitoring & Observability
Kamon
Integrated for metrics and monitoring:- System metrics (CPU, memory, GC)
- Application metrics (request rates, latencies)
- Custom business metrics
- Exports to InfluxDB and Prometheus
Security Features
Security Module
Handles:- Authentication and authorization
- Password hashing with bcrypt
- OAuth token management
- Rate limiting per IP/user
- Proxy/VPN detection using MaxMind and IP2Proxy
- User-agent parsing for bot detection
Play Configuration
Security settings inconf/base.conf:
- Session cookie configuration
- CSRF protection
- HTTP header limits
- Request size limits
Deployment Architecture
Development Setup
- Single machine running all components
- MongoDB and Redis on localhost
- lila on port 9663
- lila-ws on port 9664
Production Setup
- Multiple application servers
- Separate database clusters
- CDN for static assets
- Load balancers
- Monitoring infrastructure
Production Diagram
See the architecture diagram for production topology as of July 2022
Key Dependencies
Backend Libraries
Backend Libraries
- Akka 2.6.21 - Actor system and streams
- ReactiveMongo 1.1.0-RC20 - MongoDB driver
- Lettuce 7.5.0 - Redis client
- Caffeine 3.2.3 - High-performance caching
- Play JSON 3.0.6 - JSON serialization
- Kamon 2.8.1 - Metrics and monitoring
Frontend Libraries
Frontend Libraries
- @lichess-org/chessground ^10.0.2 - Chessboard UI
- @lichess-org/pgn-viewer ^2.5.9 - PGN viewer
- chessops ^0.15 - Chess operations
- snabbdom 3.5.1 - Virtual DOM
Linting & Formatting
Linting & Formatting
- oxlint ^1.50.0 - Fast TypeScript linter
- oxfmt ^0.35.0 - Code formatter
- stylelint ^17.3.0 - CSS/Sass linter
- scalafmt - Scala code formatting
Configuration Management
Configuration uses HOCON (Human-Optimized Config Object Notation):base.conf- Shared defaultsversion- Version informationapplication.conf- Local overrides
Browser Support
Lichess supports modern browsers:| Browser | Version | Support Level |
|---|---|---|
| Firefox | 115+ | Full (recommended) |
| Chrome/Chromium | 112+ | Full |
| Edge | 109+ | Full |
| Safari | 13.1+ | Reasonable |
| Opera | 91+ | Reasonable |
Performance Characteristics
Lichess architecture is optimized for:- High concurrency - Millions of concurrent connections
- Low latency - Real-time move transmission under 100ms
- Horizontal scalability - Stateless application servers
- Efficient resource usage - Asynchronous I/O reduces thread overhead
- Fast page loads - Content-hashed assets with aggressive caching
The fully asynchronous design using Scala Futures and Akka Streams allows handling massive traffic with relatively modest hardware.
Next Steps
Module Deep Dive
Explore individual modules in detail
API Reference
Learn about the HTTP API
Contributing
Start contributing to Lichess
lila-ws Repository
WebSocket server source code

