Cache Hierarchy

From Server rental store
Jump to navigation Jump to search
  1. Cache Hierarchy

This article details the cache hierarchy used within the MediaWiki infrastructure, explaining how different levels of caching contribute to performance and scalability. Understanding these layers is crucial for system administrators and developers optimizing MediaWiki installations.

Introduction

A cache hierarchy is a system of multiple levels of caches, each with varying speed, size, and cost. The goal is to provide fast access to frequently used data while minimizing the overall cost. In the context of MediaWiki, this means reducing database load and improving page delivery times. We utilize several layers, ranging from browser caching to dedicated server-side caches. Properly configuring and monitoring these caches is vital for a responsive wiki.

Browser Caching

The first line of defense against slow page loads is the user's web browser. Browsers cache static assets like CSS stylesheets, JavaScript files, and images. This dramatically reduces the number of requests made to the server for subsequent visits.

Configuration is typically handled via HTTP headers sent by the web server (e.g., Apache, Nginx). These headers specify how long the browser should cache the content. Longer cache durations are suitable for infrequently changed assets, while shorter durations are necessary for dynamic content.

Header Description
`Cache-Control` Specifies caching directives (e.g., `max-age`, `public`, `private`, `no-cache`).
`Expires` Specifies a date/time after which the response is considered stale.
`ETag` A unique identifier for a specific version of a resource, allowing browsers to check if their cached copy is still valid.
`Last-Modified` The date and time the resource was last modified.

Object Caching (Memcached/Redis)

MediaWiki relies heavily on object caching to store the results of expensive operations, such as database queries and parser output. We currently use both Memcached and Redis for this purpose. Redis is increasingly preferred for its richer data structures and performance characteristics.

  • Memcached: A distributed memory object caching system. It is simple to set up and provides a fast key-value store.
  • Redis: An in-memory data structure store, used as a database, cache and message broker. Offers more advanced features than Memcached.

The `LocalCache` class in MediaWiki handles interaction with these caches. When a function or method is called that can benefit from caching, the `LocalCache` checks if the result is already stored. If so, it returns the cached result directly, avoiding the need to re-execute the expensive operation.

Cache Type Technology Typical Data Stored Performance Characteristics
Object Cache Memcached / Redis Parser cache, API results, database query results, user preferences Very fast read/write access; reduces database load significantly.

Database Query Caching

The database itself also utilizes caching mechanisms. MySQL (our primary database) employs a query cache, which stores the results of SELECT queries. If an identical query is executed again, the database can return the result directly from the cache, avoiding a full disk read.

However, the MySQL query cache has limitations:

  • It's invalidated whenever the underlying tables are modified.
  • It only caches the result of SELECT queries.
  • It can introduce contention under high concurrency.

Therefore, we primarily rely on the object cache (Memcached/Redis) for more robust and scalable caching of query results. MariaDB offers alternative caching mechanisms which are also being evaluated.

Opcode Caching (PHP)

The PHP interpreter compiles PHP code into bytecode, which is then executed. Opcode caching stores this bytecode in memory, eliminating the need to recompile the code for each request. This significantly improves PHP performance.

We utilize OPcache for opcode caching. OPcache is built into PHP and provides a shared memory cache for compiled PHP scripts.

Component Technology Function Configuration
Opcode Cache OPcache Caches compiled PHP bytecode. Configured in `php.ini` (e.g., `opcache.enable=1`, `opcache.memory_consumption=128`)


Transformer Caching

MediaWiki uses transformers to modify content before it is sent to the user. Examples include compressing output (gzip), generating mobile-friendly versions, and adding accessibility features. The results of these transformations can be cached to avoid re-transforming the same content repeatedly. This is typically handled by the web server or a dedicated caching proxy like Varnish.

Monitoring and Tuning

Regular monitoring of cache hit rates is essential. Low hit rates indicate that the cache is not effectively serving its purpose, and adjustments to cache size or configuration may be needed. Tools like Prometheus and Grafana are used to visualize cache performance metrics. Proper tuning involves balancing cache size with available memory, and choosing appropriate cache expiration times. See the Performance Tuning article for more details.

Related Pages


Intel-Based Server Configurations

Configuration Specifications Benchmark
Core i7-6700K/7700 Server 64 GB DDR4, NVMe SSD 2 x 512 GB CPU Benchmark: 8046
Core i7-8700 Server 64 GB DDR4, NVMe SSD 2x1 TB CPU Benchmark: 13124
Core i9-9900K Server 128 GB DDR4, NVMe SSD 2 x 1 TB CPU Benchmark: 49969
Core i9-13900 Server (64GB) 64 GB RAM, 2x2 TB NVMe SSD
Core i9-13900 Server (128GB) 128 GB RAM, 2x2 TB NVMe SSD
Core i5-13500 Server (64GB) 64 GB RAM, 2x500 GB NVMe SSD
Core i5-13500 Server (128GB) 128 GB RAM, 2x500 GB NVMe SSD
Core i5-13500 Workstation 64 GB DDR5 RAM, 2 NVMe SSD, NVIDIA RTX 4000

AMD-Based Server Configurations

Configuration Specifications Benchmark
Ryzen 5 3600 Server 64 GB RAM, 2x480 GB NVMe CPU Benchmark: 17849
Ryzen 7 7700 Server 64 GB DDR5 RAM, 2x1 TB NVMe CPU Benchmark: 35224
Ryzen 9 5950X Server 128 GB RAM, 2x4 TB NVMe CPU Benchmark: 46045
Ryzen 9 7950X Server 128 GB DDR5 ECC, 2x2 TB NVMe CPU Benchmark: 63561
EPYC 7502P Server (128GB/1TB) 128 GB RAM, 1 TB NVMe CPU Benchmark: 48021
EPYC 7502P Server (128GB/2TB) 128 GB RAM, 2 TB NVMe CPU Benchmark: 48021
EPYC 7502P Server (128GB/4TB) 128 GB RAM, 2x2 TB NVMe CPU Benchmark: 48021
EPYC 7502P Server (256GB/1TB) 256 GB RAM, 1 TB NVMe CPU Benchmark: 48021
EPYC 7502P Server (256GB/4TB) 256 GB RAM, 2x2 TB NVMe CPU Benchmark: 48021
EPYC 9454P Server 256 GB RAM, 2x2 TB NVMe

Order Your Dedicated Server

Configure and order your ideal server configuration

Need Assistance?

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