Buffering in Redis
The redis store replaces the insert during the request with one XADD to a Redis stream. A scheduled command moves
the buffered views into the views table, a thousand rows per insert statement.
flowchart LR
record["record()"] -->|"XADD"| stream[("Redis stream")]
stream -->|"batches"| flusher["views:flush"]
flusher -->|"one insert per batch"| table[("views table")]
table --> reads["count(), countByInterval(), scopes"]
Setting it up takes three steps:
-
Run Redis 7 or newer, or any release of Valkey, with the
phpredisextension or Predis 3.3+ (composer require predis/predis). Valkey speaks the Redis protocol, so it needs nothing beyond a connection indatabase.redisthat points at it. -
Switch the store:
'recording' => ['store' => ['driver' => 'redis','redis' => ['connection' => null, // a connection from database.redis, null is the default one],],], -
Schedule the flush:
Schedule::command('views:flush')->everyMinute()->withoutOverlapping();
views:flush --batch=500 sets the batch size, and Recording\Jobs\FlushBufferedViewsJob does the same from a job.
Running several flushers at once is safe.
What to know:
- Counts lag until the next flush, because they read the
viewstable. - A view may land twice if a flusher crashes between inserting a batch and acknowledging it.
ViewRecordedmeans the stream accepted the view, not that the row exists yet.- Leave
recording.queue.enabledoff, it gains nothing on top of the buffer. - Treat Redis like a queue. The stream grows until the flusher runs, so monitor
views:flush. Usemaxmemory-policynoevictionor avolatile-*policy so Redis never evicts the stream, and enable persistence if a restart must not lose up to a minute of views.