[Answer]-Should I implement revisioning using database triggers or using django-reversion?

1👍

You need to think about the pattern of db usage in your website.

Which may be unique to you, however most web apps read much more often than they write to the db. In fact it’s fairly common to see optimisations done, to help scaling a web app, which trade off more complicated ‘save’ operations to get faster reads. An example would be denormalisation where some data from related records is copied to the parent record on each save so as to avoid repeatedly doing complicated aggregate/join queries.

This is just an example, but unless you know your specific situation is different I’d say don’t worry about doing a bit of extra work on save.

One caveat would be to consider excluding some models from the revisioning system. For example if you are using Django db-backed sessions, the session records are saved on every request. You’d want to avoid doing unnecessary work there.

As for doing it via triggers vs Django app… I think the main considerations here are not to do with performance:

  • Django app solution is more ‘obvious’ and ‘maintainable’… the app will be in your pip requirements file and Django INSTALLED_APPS, it’s obvious to other developers that it’s there and working and doesn’t need someone to remember to run the custom SQL on the db server when you move to a new server
  • With a db trigger solution you can be certain it will run whenever a record is changed by any means… whereas with Django app, anyone changing records via a psql console will bypass it. Even in the Django ORM, certain bulk operations bypass the model save method/save signals. Sometimes this is desirable however.

Another thing I’d point out is that your production webserver will be multiprocess/multithreaded… so although, yes, a lengthy db write will block the webserver it will only block the current process. Your webserver will have other processes which are able to server other requests concurrently. So it won’t block the whole webserver.

So again, unless you have a pattern of usage where you anticipate a high frequency of concurrent writes to the db, I’d say probably don’t worry about it.

Leave a comment