Search code examples
mqttmosquitto

AutoClear for Mosquitto Persistence file (mosquitto.db)


In the mosquitto docu it is described that when setting the persistence to true:

the data will be written to disk when mosquitto closes and also at periodic intervals as defined by autosave_interval

Due to the periodic "dump", the mosquitto.db file will increase continuously. How can I automatically clear old data (I don't need a history, just the latest values)


Solution

  • You don't and your premise seems wrong.

    The file should not just continue to grow, when the broker flushes it's in memory database it replaces the one on disc, it doesn't just append to the end.

    If the file is growing then so is your in memory database and you will have problem with that at some point.

    The persistence store holds 3 things.

    1. The current value of any retained messages for a given topic. (assuming messages have a fixed size then this will only grow if the number of retained messages grows)

    2. The persistent session data, e.g. which topics a given client is subscribed to. This will direct reflection of the number of clients you have. Make sure you are setting cleanSession to false if you are using random client ids.

    3. Any unconfirmed messages that have a QOS of greater that 0. These fall into 2 sub groups:

      • Inflight high QOS messages, these should normally be transient held only until it has been fully handled by the broker/subscribers

      • Queued messages for offline clients with persistent sessions and high QOS subscriptions. If you have clients that are currently offline then this will grow dependent on message size/rate until the client comes back online (see point 2 if you don't think you should have clients offline for a long time)

    You can also use the mosquitto option persistent_client_expiration duration to remove persistent sessions after a given length of time if needed.