WordPress REST API Slow Response time
When fetching 26,000 records for a report, expecting the response time to be dictated solely by your application logic is unrealistic if you are calling the standard WordPress REST API directly. The bottleneck you are observing isn't just PHP execution; it is the initialization of the entire WordPress environment. Every request triggers the loading of wp-load.php, which instantiates the bootstrap process: parsing configuration, registering core hooks, loading thousands of plugin files, and initializing theme assets.
This overhead is significant because the REST API endpoint (/wp-json/) does not inherently bypass the application lifecycle. By default, WordPress assumes every request needs to render a full page or load all registered components, resulting in high memory consumption and CPU cycles spent on tasks unrelated to data retrieval. If your goal is fluid performance for large datasets, relying on the default API response structure often leads to timeouts or excessive latency, regardless of how efficient your specific query logic is.
To address this, you must shift the architecture away from a monolithic request-response cycle within the WordPress core loop. The most effective strategy involves decoupling data generation from the presentation layer. Instead of returning 26k records in a single JSON payload via the standard APIâwhich often triggers wp_send_json() to include headers and status codes that slow down serializationâyou should implement a streaming approach or utilize server-side processing queues.
One immediate optimization is to bypass the full bootstrap if possible, but this requires careful handling of database credentials and environment variables outside the standard wp-config.php scope. However, a more robust architectural pattern for large-scale data export is to trigger the data generation process via a REST endpoint that returns an ID or status token, rather than the actual data stream immediately. This allows the backend to process the query asynchronously.
Once the heavy lifting of querying the database and aggregating the 26k records is complete, the system can emit the data in chunks using SSE (Server-Sent Events) or WebSocket connections. This prevents the client from waiting for a massive JSON blob to be constructed in memory, which can cause Out Of Memory errors on both the server and the client.
If you must use synchronous calls, consider implementing caching strategies specifically tailored to report endpoints. If the data is static or changes infrequently, store the serialized result in Redis or Memcached keyed by a hash of the query parameters. This way, subsequent requests hit the cache layer rather than re-initializing WordPress and running the complex aggregation logic again.
Additionally, review your plugin stack. Many plugins hook into wp_loaded or similar events that fire even when no theme assets are needed. For API-only environments, consider deploying a "headless" configuration where you disable unnecessary theme features, disable image processing, and ensure critical plugins do not register scripts or styles on REST requests. You might also need to adjust max_execution_time and memory limits in your server configuration (php.ini) or .htaccess to handle the heavier load of large dataset generation without crashing the worker process.
The fundamental lesson here is that the WordPress REST API is designed for lightweight CRUD operations, not heavy data export tasks. Treating it as a simple HTTP backend for bulk data transfer ignores the cost of the framework itself. By acknowledging the overhead of loading core and plugins, you can architect a solution that separates data processing from web server response times, ensuring the user experience remains fluid even when handling tens of thousands of records.