How to Supercharge Your Website with PHP 8.2 and Opcache: A Case Study with Nomads.com

How to Supercharge Your Website with PHP 8.2 and Opcache: A Case Study with Nomads.com
Photo by Peter Masełkowski / Unsplash

With today’s web users expecting fast load times and seamless experiences, website performance is no longer a “nice-to-have” — it’s a must. A laggy site can lead to higher bounce rates, reduced conversions, and lost revenue. For a data-heavy website like Nomads.com, where real-time data is crucial for users searching for the best cities to work and travel, performance becomes even more essential.

Recently, Nomads.com upgraded to PHP 8.2 with Opcache enabled, resulting in an impressive reduction in page load times — from 1-2 seconds to an astonishing ~250ms. This transformation was achieved by leveraging Opcache to cache PHP files in memory and innovative data handling techniques.

This guide will walk you through how to utilize PHP 8.2 with Opcache and Just-In-Time (JIT) compilation to achieve similar performance gains for your website.

What is PHP Opcache?

Opcache is a powerful caching mechanism in PHP that stores precompiled PHP code in memory. It eliminates the need for PHP scripts to be recompiled every time they are accessed, leading to faster execution times. By keeping PHP files in-memory, Opcache significantly improves performance, especially for sites with high traffic and large datasets.

Key Benefits of PHP Opcache

  • Reduces Server Load: PHP scripts are cached in-memory, reducing the server's need to recompile them.
  • Improves Page Load Time: Faster load times enhance user experience, engagement, and SEO rankings.
  • Ideal for Large Data Sets: Opcache keeps data quickly accessible, perfect for dynamic, data-driven websites.

PHP 8.2 and Opcache: A Game-Changing Combo

PHP 8.2 brings further enhancements to Opcache, offering more efficient memory management and reducing CPU usage even further, making it ideal for high-demand websites like Nomads.com.

Nomads.com Case Study: A Creative Use of Opcache for Data-Driven Websites

The Nomads.com team recently achieved a 4x to 8x speed improvement in page load times by leveraging PHP 8.2 with Opcache. Here’s a breakdown of how they did it.

Step 1: Caching Data with Opcache

Nomads.com has a database containing millions of data points on cities and countries around the world. This data is essential for users seeking the best cities to live and work in remotely. The team needed a way to keep this data accessible without hitting the database with every page load.

They achieved this by caching the entire data set in RAM using Opcache. By doing so, the site can retrieve the data in less than 1ms, compared to approximately 1 second without caching.

Step 2: Using a PHP Cron Job to Refresh Data

To keep the data fresh, a PHP cron script runs every hour, compressing and updating the database. This script creates a single PHP file containing the final dataset as a PHP array. With Opcache, this file remains in-memory, ensuring the website always has instant access to up-to-date information.

Step 3: Real-Time Filtering with Zero Database Load

Since the data set is in-memory, users can filter cities and countries without constantly hitting the database. This approach reduces server load while allowing for seamless filtering on parameters such as internet speed, air quality, safety scores, and more.

By keeping this frequently updated data in RAM, Nomads.com ensures a lightning-fast user experience even with millions of data points in play.

Check out the full story shared by the Nomads.com team on X (formerly Twitter)

How to Enable Opcache in PHP 8.2

Ready to try Opcache for yourself? Here’s a quick guide to enabling it on your server:

Ensure PHP 8.2+ is Installed: Opcache is bundled with PHP 8+, so upgrading is the first step.

Enable Opcache: Add the following lines to your PHP configuration file (php.ini):

opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.validate_timestamps=1
opcache.revalidate_freq=0

Set up JIT Compiler (Optional but Recommended):
JIT compilation can further speed up your PHP application by converting PHP bytecode into native machine code.

opcache.jit=1205
opcache.jit_buffer_size=100M

Restart the Server: After saving changes to php.ini, restart your server to activate Opcache.

Optimizing Opcache Settings for Maximum Performance

Memory Consumption

For data-heavy websites, increasing opcache.memory_consumption helps store larger files in-memory. The Nomads.com team uses 256MB of Opcache memory, but for smaller projects, 128MB should be sufficient.

Maximum Accelerated Files

Set opcache.max_accelerated_files to the total number of PHP files that need caching. A setting of 10,000 is recommended for sites with a large codebase.

Validate Timestamps

To ensure data freshness, opcache.validate_timestamps should be set to 1. If your site doesn’t frequently change, consider setting it to 0 for an additional performance boost.

The Impact of Opcache on SEO and User Experience

Fast page load times don’t just benefit users — they’re also crucial for SEO. Google prioritizes websites that load quickly, especially on mobile. By reducing load times with PHP 8.2 and Opcache, Nomads.com ensures users have a fast, seamless experience, which in turn contributes to higher SEO rankings and increased traffic.

Conclusion: Unlock Blazing-Fast Speeds with PHP 8.2, Opcache, and JIT

PHP 8.2 with Opcache is a game-changer for websites handling large data sets and high traffic. By caching PHP files and data in memory, it’s possible to achieve near-instant page load times, as Nomads.com’s success demonstrates.

With a few simple configurations, you can boost your site’s performance and user experience, reduce server load, and enhance your SEO. Follow Nomads.com’s lead and leverage PHP 8.2, Opcache, and JIT to supercharge your site today!

Subscribe to codingwithalex

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe