DEV Community

Mamontil
Mamontil

Posted on

How I Built a 350,000+ ops/s Cache for PHP on Windows Using Rust and FFI

The Story Behind NitroCache

As a PHP developer working on Windows, I've always struggled with the overhead of Redis and Memcached in local environments. Running Docker or WSL2 just to have a fast key-value store felt like overkill. I wanted something native, lightweight, and incredibly fast.

So, I decided to build NitroCache.

πŸ¦€ Why Rust?

I chose Rust for the core engine because I needed:

  1. Memory Safety: Handling shared memory segments can be dangerous; Rust makes it predictable.
  2. Performance: I wanted to achieve near-zero latency.
  3. FFI Compatibility: Rust makes it easy to export C-compatible functions that PHP can call via the FFI extension.

πŸš€ The Architecture: Bypassing the Network Stack

Standard caching solutions use TCP/IP sockets. Even on localhost, this introduces overhead (handshakes, packet processing).

NitroCache uses Shared Memory (shm).

  • The Rust Server manages a dedicated memory segment and handles TTL/eviction.
  • The PHP Client maps that same memory segment into its own process.

The result? We bypass the network stack entirely. Accessing data takes about 16-20 microseconds.

πŸ“Š Benchmarks (Windows 10, PHP 8.4)

In my local tests with 500,000 keys, I achieved:

  • SET: ~61,500 ops/s
  • GET: ~57,400 ops/s (Note: Peak performance in optimized environments reaches much higher).

πŸ› οΈ How to use it

It's as simple as:

$cache = new NitroCache(maxMemoryMb: 512);
$cache->set('key', 'value', 3600);
echo $cache->get('key');
Enter fullscreen mode Exit fullscreen mode

πŸ’– Open Source & Future

NitroCache is completely open-source (MIT). I'm currently in the alpha stage and looking for feedback on:

  • FFI stability in long-running processes.

  • Memory management edge cases.

  • Linux support (it's next on the roadmap!).

Check out the code here:

https://github.com/mamontil/nitro-cache
Enter fullscreen mode Exit fullscreen mode

I'd love to hear your thoughts! Have you ever used FFI in your PHP projects?

Top comments (0)