<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Leonce Medewanou</title>
    <description>The latest articles on DEV Community by Leonce Medewanou (@leonce_medewanou_0775d42c).</description>
    <link>https://dev.to/leonce_medewanou_0775d42c</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3916113%2F784ebe3f-e438-439d-9a92-61585be49a18.jpg</url>
      <title>DEV Community: Leonce Medewanou</title>
      <link>https://dev.to/leonce_medewanou_0775d42c</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/leonce_medewanou_0775d42c"/>
    <language>en</language>
    <item>
      <title>I redirected laravel/nightwatch to my own Postgres and hit 13,400 payloads/s on a single instance</title>
      <dc:creator>Leonce Medewanou</dc:creator>
      <pubDate>Wed, 06 May 2026 13:48:05 +0000</pubDate>
      <link>https://dev.to/leonce_medewanou_0775d42c/i-redirected-laravelnightwatch-to-my-own-postgres-and-hit-13400-payloadss-on-a-single-instance-43ch</link>
      <guid>https://dev.to/leonce_medewanou_0775d42c/i-redirected-laravelnightwatch-to-my-own-postgres-and-hit-13400-payloadss-on-a-single-instance-43ch</guid>
      <description>&lt;p&gt;If you run a Laravel app on a hosted observability platform like Nightwatch, you've probably sampled your telemetry down to keep the bill manageable. I wanted to keep all of it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;laravel/nightwatch&lt;/code&gt; is Laravel's official observability SDK and the instrumentation itself is genuinely good. It's the hosted side that bothered me. Ingestion is usage-priced, throughput is bounded by what you're willing to pay for, and your telemetry lives in someone else's warehouse. Plenty of teams are happy with that trade.&lt;/p&gt;

&lt;p&gt;Others aren't: high-traffic apps that don't want to sample, regulated stacks where stack traces can't leave the perimeter, smaller teams whose Postgres already has the headroom to absorb the writes. They want the same SDK pointed somewhere else.&lt;/p&gt;

&lt;p&gt;So I wrote an agent that intercepts Nightwatch's ingest binding and redirects payloads to a local TCP socket, then drains them into a Postgres database I provision. On a single instance it sustains around &lt;strong&gt;13,400 payloads/s&lt;/strong&gt;. That's enough headroom for an app doing 2,000-5,000 req/s without sampling.&lt;/p&gt;

&lt;h2&gt;
  
  
  The architecture
&lt;/h2&gt;

&lt;p&gt;Three layers, each chosen to solve a specific bottleneck.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;laravel/nightwatch
    │
   TCP
    │
    ▼
ReactPHP listener
    │
    ▼
SQLite WAL buffer
    │
    ▼
Postgres (COPY protocol)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ingest path and the drain path are decoupled. Ingest must never block on Postgres. Drain must never lose data if Postgres goes away.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layer 1: non-blocking ingest with ReactPHP
&lt;/h2&gt;

&lt;p&gt;The TCP listener is a &lt;code&gt;ReactPHP\Socket\TcpServer&lt;/code&gt; running on a single event loop. One process, accepting payloads from many concurrent connections and pushing them into the buffer. PHP-FPM workers don't enter the picture. Nightwatch's ingest binding is hijacked at request shutdown to write to the local TCP socket instead of phoning home to Laravel Cloud.&lt;/p&gt;

&lt;p&gt;The wire protocol is deliberately minimal: &lt;code&gt;[length]:[version]:[tokenHash]:[payload]&lt;/code&gt;, with gzip detected by magic byte (&lt;code&gt;0x1f 0x8b&lt;/code&gt;) and the &lt;code&gt;xxh128&lt;/code&gt; token hash truncated to 7 chars. The reason it stays that minimal is that the agent never re-encodes the payload. Nightwatch sends JSON, the buffer stores it as-is, and the drain worker is the first process that parses it, only because it needs to route fields to the right columns. Skipping a &lt;code&gt;json_decode&lt;/code&gt;/&lt;code&gt;json_encode&lt;/code&gt; round-trip on the hot path was worth roughly 30-50µs per payload in profiling, which is a meaningful chunk of the per-payload budget at this rate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layer 2: SQLite WAL as the buffer
&lt;/h2&gt;

&lt;p&gt;Why SQLite for a buffer? Because it's the only embedded database that gives you crash-safe writes at the speed of a memory-mapped file, with zero ops overhead.&lt;/p&gt;

&lt;p&gt;The pragma sequence matters and broke me once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;busy_timeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;journal_mode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;WAL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;synchronous&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;NORMAL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;cache_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;64000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;-- ~64 MB&lt;/span&gt;
&lt;span class="n"&gt;PRAGMA&lt;/span&gt; &lt;span class="n"&gt;mmap_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;268435456&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;-- 256 MB&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;busy_timeout&lt;/code&gt; has to be set &lt;strong&gt;before&lt;/strong&gt; &lt;code&gt;journal_mode = WAL&lt;/code&gt;. If you do it the other way, the first concurrent write under load races and one of the writers gets &lt;code&gt;SQLITE_BUSY&lt;/code&gt; immediately instead of waiting. I lost an afternoon to this.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;synchronous = NORMAL&lt;/code&gt; on the buffer is fine because Postgres is the durable store. The buffer just needs to survive a process crash, not a kernel panic.&lt;/p&gt;

&lt;p&gt;Rows get a single &lt;code&gt;synced&lt;/code&gt; column with three states: &lt;code&gt;0&lt;/code&gt; (pending), &lt;code&gt;100+workerId&lt;/code&gt; (claimed by drain worker N), &lt;code&gt;1&lt;/code&gt; (drained). Drain workers atomically mark a batch with their own claim value, then SELECT it. The UPDATE is the atomic part; the SELECT just hands the rows to the worker. If a worker dies mid-batch, the parent's &lt;code&gt;SIGCHLD&lt;/code&gt; handler releases its claimed rows back to pending.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layer 3: Postgres COPY for the drain
&lt;/h2&gt;

&lt;p&gt;The drain worker uses &lt;code&gt;pgsqlCopyFromArray()&lt;/code&gt; for the 10 high-volume tables (requests, queries, jobs, logs, cache events, mail, notifications, outgoing requests, scheduled tasks, commands). COPY is roughly 5-10x faster than equivalent multi-row INSERTs at this batch size; the parse-plan overhead per statement disappears, and the wire format is denser.&lt;/p&gt;

&lt;p&gt;INSERT survives for the exception path (which upserts a grouped issue row by fingerprint) and for per-user counters. COPY can't do upserts, so those stay on the slower path. They're also the lowest-volume tables, so it doesn't matter.&lt;/p&gt;

&lt;p&gt;The single biggest single-line change for throughput:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;synchronous_commit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;off&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the 2-5x win. The agent drops &lt;code&gt;synchronous_commit&lt;/code&gt; on the drain connection because durability is already guaranteed upstream by SQLite WAL. Worst case under crash is that the same batch gets COPY'd twice. Acceptable for a monitoring product.&lt;/p&gt;

&lt;p&gt;Batch size is 5,000 rows per COPY call. I tested 1k, 5k, 10k, 50k. Past 5k, Postgres write latency dominates and the buffer fills up faster than the drain can clear it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fork-safety landmine
&lt;/h2&gt;

&lt;p&gt;This took me an entire weekend.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pcntl_fork()&lt;/code&gt; is how the agent spawns N drain workers. Each child needs its own SQLite handle and its own Postgres handle. The naive approach (open both in the parent, fork, and let the children inherit) corrupts the SQLite WAL when the first child exits.&lt;/p&gt;

&lt;p&gt;The fix is unintuitive: &lt;strong&gt;close the parent's SQLite PDO immediately before fork, and recreate it in both the parent and each child after fork.&lt;/strong&gt; PDO sets up file locks and per-connection state that get partially cloned by &lt;code&gt;fork(2)&lt;/code&gt;'s copy-on-write semantics. When the child exits and runs its destructor, it tears down state the parent still thinks it owns.&lt;/p&gt;

&lt;p&gt;There's no clean error message. You just get random &lt;code&gt;SQLITE_CORRUPT&lt;/code&gt; errors hours later with no obvious trigger.&lt;/p&gt;

&lt;p&gt;For Postgres the same rule applies, but the failure mode is more honest: you immediately get "broken pipe" errors because both processes try to read from the same TCP socket.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the bottleneck actually is
&lt;/h2&gt;

&lt;p&gt;After all this, ingest tops out around 13,400 payloads/s on a single instance. That's not the SQLite ceiling (the buffer can absorb much faster than that). It's not Postgres (with 4 drain workers and COPY, it sustains ~22,000 rows/s). It's the &lt;strong&gt;TCP accept loop&lt;/strong&gt; on a single PHP event loop.&lt;/p&gt;

&lt;p&gt;The fix is &lt;code&gt;SO_REUSEPORT&lt;/code&gt; and multiple agent processes listening on the same port. Linux kernel distributes new connections across them. macOS doesn't (it just hands every connection to whichever process accepts first), so this is a Linux-only optimization.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running it alongside Nightwatch
&lt;/h2&gt;

&lt;p&gt;You don't have to rip out the hosted plan to try this. Set &lt;code&gt;NIGHTOWL_PARALLEL_WITH_NIGHTWATCH=true&lt;/code&gt; and the agent's service provider wraps Nightwatch's &lt;code&gt;Core::ingest&lt;/code&gt; binding with a fan-out adapter. Every payload goes to both Laravel Cloud and your local TCP socket, so you can run the two side-by-side and compare what you actually use before committing either way.&lt;/p&gt;

&lt;p&gt;The fan-out runs after Nightwatch has accepted the payload, so it can't break the hosted path you're already paying for.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's open source
&lt;/h2&gt;

&lt;p&gt;The whole thing is MIT, on Packagist as &lt;code&gt;nightowl/agent&lt;/code&gt;, and runs in any Laravel 11 or 12 app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require nightowl/agent
php artisan nightowl:install
php artisan nightowl:agent
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Repo: &lt;a href="https://github.com/lemed99/nightowl-agent" rel="noopener noreferrer"&gt;github.com/lemed99/nightowl-agent&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's a hosted dashboard at &lt;a href="https://usenightowl.com/" rel="noopener noreferrer"&gt;usenightowl.com&lt;/a&gt; if you don't want to build a UI on top of the Postgres tables yourself. The agent runs fine without it.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
