Docs / Outputs / Async writes

Async writes

Send the entries through Messenger and write them on a worker, not during the request.

Enable the message bus

The message_bus option sends the entries through a Messenger bus. It needs symfony/messenger:

terminal
$ composer require symfony/messenger
config/packages/symfonycorp_audit_log.yaml
symfonycorp_audit_log:
    message_bus: ~      # uses messenger.default_bus
    # message_bus: { service: messenger.bus.audit }

Route the entries

Route AuditEntry to an async transport to write the entries on a worker. Without the routing, the bus writes them at once:

config/packages/messenger.yaml
framework:
    messenger:
        transports:
            async: '%env(MESSENGER_TRANSPORT_DSN)%'
        routing:
            'SymfonyCorp\AuditLog\Entry\AuditEntry': async

Run a worker:

terminal
$ bin/console messenger:consume async

Failed messages

Configure a failure_transport. Without it, Messenger drops an entry after the last retry.

Personal data - the transport holds the full entries, with their personal data. Protect it as you protect the audit storage. See Personal data.