Data Flows and Database Management
1. Database Architecture
1.1 Database System
Emby uses SQLite as the main database, with optional support for SQL Server in enterprise environments. The database is stored in the application's data directory and contains all metadata, user settings, authentication data, and usage statistics.
1.2 Main Database Structure
Emby creates and manages multiple database files:
library.db - Main database containing:
- Tables for users and authentication
- Media metadata (movies, series, music)
- Playback progress data
- User settings and preferences
activity.db - Stores activity data and logs
channels.db - Data about channels and plugins
2. Data Flow in the Application
2.1 Authentication and Authorization Process
sql
CREATE TABLE Users (
Id INTEGER PRIMARY KEY,
Name TEXT NOT NULL,
Password TEXT,
Salt TEXT,
IsActive BOOLEAN,
IsAdministrator BOOLEAN
);sql
CREATE TABLE AccessTokens (
Id TEXT PRIMARY KEY,
UserId INTEGER,
DeviceId TEXT,
DateCreated DATETIME,
DateLastActivity DATETIME,
AppName TEXT
);Authentication flow:
- Client sends login request with credentials
- Server validates in database using password hash
- On success, generates an access token and saves it in AccessTokens
- Token is returned to client for future requests
2.2 Media Library Management
sql
CREATE TABLE TypedBaseItems (
Id INTEGER PRIMARY KEY,
Type TEXT NOT NULL,
Path TEXT,
Name TEXT,
MetadataLanguage TEXT,
MetadataCountryCode TEXT,
ProviderIds TEXT,
PremiereDate DATETIME,
EndDate DATETIME
);sql
CREATE TABLE MediaItems (
Id INTEGER PRIMARY KEY,
Type TEXT,
MediaType TEXT,
Container TEXT,
TotalBitrate INTEGER,
Path TEXT,
Size INTEGER
);sql
CREATE TABLE UserData (
Id INTEGER PRIMARY KEY,
UserId INTEGER,
ItemId INTEGER,
PlayCount INTEGER,
IsFavorite BOOLEAN,
PlaybackPositionTicks INTEGER,
LastPlayedDate DATETIME
);Library scanning flow:
- Emby monitors configured directories for changes
- When detecting new files, extracts metadata using plugins
- Saves metadata in TypedBaseItems and MediaItems tables
- Updates search index
2.3 Playback Progress and User Data
sql
CREATE TABLE PlaybackStatus (
Id INTEGER PRIMARY KEY,
UserId INTEGER,
ItemId INTEGER,
PositionTicks INTEGER,
AudioStreamIndex INTEGER,
SubtitleStreamIndex INTEGER,
VolumeLevel INTEGER,
IsPaused BOOLEAN,
PlaybackStartTime DATETIME,
LastPlaybackUpdate DATETIME
);Playback tracking flow:
- Client reports progress to server at regular intervals
- Server updates PlaybackStatus and UserData
- Data is used for synchronization between devices
- Upon reconnection, client retrieves last saved position
2.4 Plugin and Extensions System
sql
CREATE TABLE Plugins (
Id INTEGER PRIMARY KEY,
Name TEXT NOT NULL,
Version TEXT,
AssemblyPath TEXT,
Configuration TEXT,
Status INTEGER
);CREATE TABLE PluginConfigurations ( PluginId INTEGER, Key TEXT, Value TEXT );
3. File and Directory Structure
bash
programdata
├── cache
│ ├── images
│ ├── metadata
│ ├── transcode
│ └── certificates
│
├── config
│ ├── system.xml
│ ├── networking.xml
│ ├── users.db
│ ├── authentication.db
│ └── policies.json
│
├── data
│ ├── library.db
│ ├── library.db-shm
│ ├── library.db-wal
│ ├── sessiondata
│ ├── stats
│ └── scheduledtasks
│
├── logs
│ ├── embyserver.txt
│ ├── transcoding-*.txt
│ └── ffmpeg-*.log
│
├── metadata
│ ├── Movies
│ ├── TV Shows
│ ├── Music
│ └── People
│
├── notifications
│ ├── smtp.json
│ └── push.json
│
├── plugins
│ ├── <plugin_name>.dll
│ ├── <plugin_folder>
│ └── configurations.json
│
├── root
│ ├── default.xml
│ └── images
│
└── updates
├── server
└── plugins4. Useful Commands for Administration
4.1 Database Backup
sql
cp /var/lib/emby/data/library.db /backup/emby/library_backup_$(date +%Y%m%d).db4.2 Database Repair
sql
sqlite3 /var/lib/emby/data/library.db "VACUUM;"4.3 Integrity Check
sql
sqlite3 /var/lib/emby/data/library.db "PRAGMA integrity_check;"4.4 User Password Reset
sql
sqlite3 /var/lib/emby/data/library.db "UPDATE Users SET Password=NULL, Salt=NULL WHERE Name='username';"4.5 Cache Cleanup
sql
rm -rf /var/lib/emby/data/cache/*4.6 Database Statistics Check
sql
sqlite3 /var/lib/emby/data/library.db ".tables"
sqlite3 /var/lib/emby/data/library.db "SELECT name FROM sqlite_master WHERE type='table';"4.7 Active Connections Monitoring
sql
lsof | grep emby | grep library.db4.8 Database Optimization
sql
sqlite3 /var/lib/emby/data/library.db "ANALYZE;"
sqlite3 /var/lib/emby/data/library.db "REINDEX;"5. Data Flow at Content Loading
- Media file is detected in monitored directory
- Emby extracts metadata using configured providers
- Poster/backdrop images are generated and stored in cache
- Information is indexed for fast search
- Notifications are sent to clients for UI update
6. Synchronization Between Devices
- All user data is centralized on server
- Clients use REST API to access data
- WebSockets for real-time notifications
- Local cache on client for performance
- Conflict resolution based on timestamps