Buffering activities | laravel-activitylog | Spatie

 SPATIE

  Laravel Activity Log
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-activitylog](https://spatie.be/docs/laravel-activitylog/v5)  Advanced-usage  Buffering activities

 Version   v5   v4   v3   v2   v1

 Other versions for crawler [v5](https://spatie.be/docs/laravel-activitylog/v5) [v4](https://spatie.be/docs/laravel-activitylog/v4) [v3](https://spatie.be/docs/laravel-activitylog/v3) [v2](https://spatie.be/docs/laravel-activitylog/v2) [v1](https://spatie.be/docs/laravel-activitylog/v1)

  Buffering activities
- [ Introduction ](https://spatie.be/docs/laravel-activitylog/v5/introduction)
- [ Support us ](https://spatie.be/docs/laravel-activitylog/v5/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-activitylog/v5/requirements)
- [ Installation and Setup ](https://spatie.be/docs/laravel-activitylog/v5/installation-and-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-activitylog/v5/questions-and-issues)
- [ Changelog ](https://spatie.be/docs/laravel-activitylog/v5/changelog)
- [ Upgrading ](https://spatie.be/docs/laravel-activitylog/v5/upgrading)
- [ About us ](https://spatie.be/docs/laravel-activitylog/v5/about-us)

Basic usage
-----------

- [ Logging activity ](https://spatie.be/docs/laravel-activitylog/v5/basic-usage/logging-activity)
- [ Cleaning up the log ](https://spatie.be/docs/laravel-activitylog/v5/basic-usage/cleaning-up-the-log)

Advanced usage
--------------

- [ Logging model events ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/logging-model-events)
- [ Define causer for runtime ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/define-causer-for-runtime)
- [ Using placeholders ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/using-placeholders)
- [ Using multiple logs ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/using-multiple-logs)
- [ Disabling logging ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/disabling-logging)
- [ Customizing actions ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/customizing-actions)
- [ Before logging hook ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/before-logging-hook)
- [ Buffering activities ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/buffering)
- [ Log Options ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/log-options)
- [ Causer Resolver ](https://spatie.be/docs/laravel-activitylog/v5/advanced-usage/causer-resolver)

 Buffering activities
====================

###  On this page

1. [ When to use buffering ](#content-when-to-use-buffering)
2. [ Enabling the buffer ](#content-enabling-the-buffer)
3. [ How it works ](#content-how-it-works)
4. [ Things to be aware of ](#content-things-to-be-aware-of)

By default, each activity is saved to the database immediately with its own `INSERT` query. If your application logs many activities during a single request (for example, when updating multiple models in a loop), this can result in a significant number of queries.

When buffering is enabled, activities are collected in memory during the request and inserted in a single bulk query after the response has been sent to the client.

When to use buffering
-----------------------------------------------------------------------------------------------------------------------

You should only enable buffering if your application logs a high volume of activities per request. For most applications that log just a handful of activities per request, the default behavior is perfectly fine.

Buffering is most useful when:

- You update many models in a single request (e.g., batch operations)
- You have endpoints that trigger a large number of model events
- You want to reduce database load from activity logging

Enabling the buffer
-----------------------------------------------------------------------------------------------------------------

Add this to your `.env` file:

```
ACTIVITYLOG_BUFFER_ENABLED=true
```

Or set it directly in `config/activitylog.php`:

```
'buffer' => [
    'enabled' => true,
],
```

That's it. No other code changes are required. All existing logging code (both automatic model event logging and manual `activity()->log()` calls) will be buffered automatically.

How it works
--------------------------------------------------------------------------------------------

When buffering is enabled:

1. Activities are collected in an in-memory buffer instead of being saved immediately
2. After the response is sent to the client (during the `terminating` phase), all buffered activities are inserted in a single query
3. For queue workers, the buffer is flushed after each job completes

The buffer also registers a shutdown function as a safety net, so activities are flushed even if the application terminates unexpectedly.

Things to be aware of
-----------------------------------------------------------------------------------------------------------------------

### No ID until flush

Buffered activities will not have a database ID until the buffer is flushed. If you need the activity ID immediately after logging, do not enable buffering.

```
$activity = activity()->log('some activity');

// With buffering enabled, $activity->id will be null here.
// The activity will be saved after the response is sent.
```

### Works with Octane

The buffer is registered as a scoped binding, which means it is automatically reset between requests in Laravel Octane. The `terminating` callback fires per request in Octane, so activities are flushed correctly.

### Works with queues

The buffer is automatically flushed after each queued job completes (or fails). Activities logged within a job will be bulk inserted when that job finishes.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
