Server rental store

In-Memory Databases

# In-Memory Databases for MediaWiki

This article details the use of in-memory databases (IMDBs) with MediaWiki 1.40, focusing on performance improvements and configuration considerations. Using an IMDB can significantly reduce database latency, leading to faster page loads and a more responsive wiki experience, particularly under high load. However, it's crucial to understand the tradeoffs involved, primarily data persistence.

What are In-Memory Databases?

Traditionally, MediaWiki relies on persistent storage (like MySQL/MariaDB or PostgreSQL) to store all its data. In-memory databases, as the name suggests, store data primarily in RAM. This offers dramatically faster read and write speeds because accessing RAM is orders of magnitude quicker than accessing disk. However, data in RAM is volatile; it's lost when the server restarts or crashes unless explicitly saved to persistent storage.

For MediaWiki, IMDBs are often used as a caching layer *in front of* a persistent database. This means frequently accessed data is served from the IMDB, while less frequent data remains in the persistent storage. This hybrid approach delivers performance benefits without the risk of complete data loss.

Popular IMDB Choices for MediaWiki

Several in-memory database systems are suitable for use with MediaWiki. Here's a comparison of some common options:

Database Description MediaWiki Compatibility Complexity
Redis An open-source, in-memory data structure store, used as a database, cache and message broker. Excellent. Requires the Redis extension for MediaWiki. Medium
Memcached A distributed memory object caching system. Good. Requires the Memcached extension for MediaWiki. Low
SQLite (in-memory mode) A self-contained, serverless, zero-configuration, transactional SQL database engine. Can run entirely in memory. Limited. Not generally recommended for production environments due to scalability concerns. Useful for testing. Low

Configuring MediaWiki with Redis

Redis is generally the preferred choice for an IMDB with MediaWiki due to its versatility and robust feature set. Here's a step-by-step guide to configuring MediaWiki to use Redis:

1. **Install Redis:** Follow the installation instructions for your operating system. See the official Redis documentation for details. 2. **Install the Redis Extension:** Download the Redis extension for MediaWiki and install it according to the instructions on its page. This usually involves copying the extension files to your `extensions/` directory and adding `'Redis'` to your `wfLoadExtensions.php` file. 3. **Configure `LocalSettings.php`:** Add the following lines to your `LocalSettings.php` file, adjusting the host and port as needed:

```php $wgRedis = [ 'host' => '127.0.0.1', 'port' => 6379, 'password' => '', // Optional, if Redis is password protected 'dbindex' => 0, // Optional, select the Redis database ];

$wgCacheDatabaseRedir = true; // Redirect cache lookups to Redis $wgSessionCacheType = 'redis'; // Use Redis for session caching ``` 4. **Configure Caching:** Adjust your `$wgCacheDirectory` setting to a location that Redis can access if you intend to use Redis for file caching as well. Remember to clear your MediaWiki cache after making changes to `LocalSettings.php`. See Help:Cache for more information.

Configuring MediaWiki with Memcached

Memcached offers a simpler caching solution than Redis. Configuration is equally straightforward:

1. **Install Memcached:** Install Memcached on your server. Refer to the Memcached documentation for installation instructions. 2. **Install the Memcached Extension:** Download the Memcached extension for MediaWiki and install it. 3. **Configure `LocalSettings.php`:** Add the following to your `LocalSettings.php`:

```php $wgMemCachedLocalServers = array( '127.0.0.1:11211' // Replace with your Memcached server address and port );

$wgCacheDatabaseRedir = true; // Redirect cache lookups to Memcached ```

Understanding Caching Layers

MediaWiki’s caching system is layered. Here’s a simplified overview:

Cache Layer Data Stored Speed Persistence
Browser Cache Static assets (CSS, JavaScript, images) Fastest Limited (Browser-dependent)
Opcode Cache (e.g., APCu, OPcache) Compiled PHP code Very Fast Session-based
Memcached/Redis Database query results, parsed templates, rendered HTML fragments Fast Volatile (Lost on restart)
Database Cache (MySQL/PostgreSQL Query Cache) Database query results Moderate Limited (Invalidated frequently)
Persistent Database All wiki data Slowest Persistent

It's important to understand where each IMDB fits within this hierarchy. Setting `$wgCacheDatabaseRedir = true` directs MediaWiki to first check the IMDB for cached data *before* querying the persistent database.

Monitoring and Tuning

After implementing an IMDB, it's crucial to monitor its performance. Key metrics include:

⚠️ *Note: All benchmark scores are approximate and may vary based on configuration. Server availability subject to stock.* ⚠️