Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
WordPressJune 24, 20263 min read

Protocol Buffers in WordPress: High-Performance REST API Serialization

Protocol Buffers for WordPress REST API data exchange significantly reduce payload sizes. Learn how to implement binary serialization for high-performance apps.

WordPressREST APIProtocol BuffersPerformancePHPSerializationCMS

Last month, I was debugging a headless WordPress build where the REST API was consistently choking on massive JSON payloads. We were hitting memory limits in the PHP-FPM worker processes, and the front-end client was spending nearly 300ms just parsing the response string. We needed a way to shrink the data footprint, so I decided to bypass JSON entirely and implement Protocol Buffers for our core data endpoints.

Why Move Beyond JSON?

In a standard WordPress REST API setup, JSON is the default. It's human-readable, sure, but it’s also verbose. Every field name is repeated in every object of an array, and numbers are stored as strings. When you're pushing thousands of rows of custom post data, the overhead is massive.

I’ve previously discussed WordPress REST API Performance: Brotli Compression for Headless SaaS, which is a great first step, but compression only masks the inefficiency of the underlying text format. By switching to binary serialization, you eliminate field name repetition and leverage efficient varint encoding. It’s similar to how we manage high-performance microservices, as seen in Laravel Protocol Buffers Serialization for High-Performance Architectures.

Implementing Protocol Buffers

To get started, you need the google/protobuf PHP library. Install it via Composer in your plugin directory:

Bash
composer require google/protobuf

Once installed, you define your data structure in a .proto file. This acts as your source of truth for both the server and the client.

PROTOBUF
syntax = "proto3";

message Product {
    int32 id = 1;
    string title = 2;
    float price = 3;
}

After defining your schema, you’ll generate the PHP classes using the protoc compiler. These classes give you the methods to serialize and deserialize your data.

Bridging the WordPress REST API

The main challenge is that the WordPress REST API assumes a JSON Content-Type. You’ll need to hook into the rest_pre_dispatch filter to intercept the request and return your binary data.

PHP
add_filter('rest_pre_dispatch', function($result, $server, $request) {
    if ($request->get_header('Accept') !== 'application/x-protobuf') {
        return $result;
    }

    #6A9955">// Fetch your data
    $data = get_products_data();
    
    #6A9955">// Serialize to binary
    $product = new Product();
    $product->setId($data['id']);
    $product->setTitle($data['title']);
    
    #6A9955">// Set headers and return binary string
    header('Content-Type: application/x-protobuf');
    return $product->serializeToString();
}, 10, 3);

Challenges with Binary Serialization

Don't expect this to be a silver bullet for every endpoint. We first tried converting every REST API route to Protobuf, and it was a mistake. Debugging became a nightmare because we couldn't just curl the endpoint and read the response.

Stick to high-throughput endpoints where payload size is the actual bottleneck. Also, remember that you’ll need to handle REST API Field Selection: Solving Data Over-fetching and Dependency Graphs if your binary schemas become too complex. If you don't keep your Protobuf definitions synchronized between your WordPress backend and your front-end client, you’ll end up with silent data corruption.

Performance Gains

In our testing, we saw a reduction in payload size by about 60% for a collection of 500 product objects. More importantly, the PHP CPU usage dropped because the serialization process is significantly faster than json_encode() on large arrays.

If you're still seeing slow responses after moving to binary, check your database layer. I often use WordPress performance: Database-level request coalescing for REST API to ensure the data I'm serializing isn't coming from an N+1 query storm.

FAQ

Does this break WordPress native features like the Block Editor? No. The Block Editor expects JSON. Only use Protobuf for custom, high-performance API routes consumed by your own headless applications.

Can I use this with standard HTTP clients? Yes, but the client must be able to decode the binary format. You'll need to generate the corresponding Protobuf classes for your front-end framework (e.g., in JavaScript or TypeScript).

Is it worth the extra build step? If you have a high-scale application, absolutely. If you're building a standard brochure site, stick to JSON and caching.

I’m still experimenting with how to version these binary schemas as the plugin evolves. Right now, we’re using a simple schema registry, but it feels a bit fragile. If you find a better pattern for managing schema migrations in a WordPress environment, I'd love to hear how you're handling it.

Back to Blog

Similar Posts

WordPressJune 24, 20264 min read

WordPress REST API Middleware: Proxy Pattern Response Transformation

WordPress REST API middleware using the proxy pattern allows for clean, transparent response transformation. Learn to architect scalable headless endpoints.

Read more
WordPressJune 24, 20264 min read

WordPress Performance: Prefetching Middleware for Headless REST API

WordPress performance hinges on reducing cold-start latency. Learn to implement request prefetching middleware to hydrate REST API responses for headless apps.

Read more
WordPressJune 24, 20264 min read

WordPress Performance: How to Implement Request Hedging for REST APIs

WordPress performance hinges on managing P99 latency. Learn how to implement request hedging to fire redundant REST API requests and keep your site fast.

Read more