PDOException: Packets out of order. Expected 0 received 1. Packet size=23

PDOException: Packets out of order. Expected 0 received 1. Packet size=23

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding the "Packets Out of Order" PDO Exception in High-Load Queues

The error message PDOException: Packets out of order. Expected 0 received 1 is a specific indicator that your application is struggling to maintain synchronization with the database protocol during high-intensity operations. When you encounter this on a robust Digital Ocean Droplet—especially one configured for heavy memory usage like the 256GB, 32vCPU instance described here—it rarely points to a lack of RAM or CPU power. Instead, it signals a fundamental mismatch between how your PHP process is sending data and how the MariaDB server expects to receive it during concurrent write spikes.

This issue often manifests in Laravel projects utilizing Horizon for job queue management. The scenario typically involves a "fire-and-forget" architecture where an initial controller dispatches a job, which then triggers a cascade of events and listeners. If your system broadcasts 30 events or handles 1,136 workers across multiple supervisors, the sheer velocity of write operations can overwhelm the TCP buffer management on the database side.

Many developers instinctively attempt to resolve this by manipulating pdo_mysql settings. A common suggestion is setting PDO::ATTR_EMULATE_PREPARES to true. While this flag controls how prepared statements are handled locally versus on the server, it does not influence packet ordering or buffering logic. Enabling it can inadvertently force integers to be cast as strings, introducing new type-handling bugs without solving the core synchronization error. Similarly, switching DB_HOST from localhost to 127.0.0.1 changes the connection protocol from a Unix socket to TCP/IP. While this is sometimes necessary for cross-container communication or specific driver requirements, it will not fix packet ordering issues if the underlying buffer sizes are misconfigured.

Another frequent adjustment involves modifying /etc/mysql/mariadb.conf.d/50-server.cnf. Increasing max_allowed_packet to 2048M and boosting key_buffer_size or query_cache_size might seem like a logical step for a memory-rich server. However, these settings govern the maximum size of a single packet or the amount of memory reserved for caching, not the flow control mechanism that detects out-of-order packets. In fact, aggressively increasing these values on a production system without adjusting related timeout settings can sometimes exacerbate memory pressure or lead to other resource contention issues.

The root cause often lies in the net_buffer_length and net_read_timeout variables within MariaDB. When a client sends a large batch of queries rapidly—such as a Laravel worker processing thousands of jobs—the server may receive packets that arrive before the previous handshake is fully acknowledged, leading to the "Expected 0 received 1" state.

To address this effectively without disabling prepared statements or breaking type safety:

  1. Audit net_buffer_length: Check your MariaDB configuration. If this value is too small relative to the batch size of your job queue, packets will fragment and arrive out of order. Increasing this buffer can help absorb bursts of traffic.
  2. Review Connection Pooling: Ensure that your application isn't opening and closing connections excessively fast. Reusing connections allows the TCP stack to maintain proper state for packet ordering.
  3. Monitor max_connections: Even with 1,136 workers, ensure that the actual number of active database connections does not hit the limit defined in max_connections. If the connection pool is saturated, new packets may be dropped or reordered as the server attempts to manage limited file descriptors.

If you are using Laravel Horizon, verify that your supervisor count aligns with the actual load. Running 6 supervisors for a massive queue can sometimes create contention if the job distribution isn't perfectly balanced, causing specific workers to spike in write velocity while others sit idle. Adjusting the worker count or ensuring Redis is handling the job distribution efficiently can smooth out these peaks.

Finally, always check mysql_error.log and /var/log/mariadb/error.log immediately after a crash. The absence of an error log entry suggests the issue might be occurring at the network protocol level rather than a SQL syntax or constraint violation. In cases where the database simply goes "away" without logging a failure, the TCP connection may have been reset due to buffer overflows on the server side. Tuning net_read_timeout and ensuring your PHP default_socket_timeout is sufficiently high can prevent premature disconnections during heavy write cycles.

Our network