This sounds like something is invalidating your session information. How are your user sessions configured. If you are using something like memcache/memcached then you *must* use a separate instance for sessions as opposed to normal caching.
Thanks
I did some additional research and found out that the following starts appearing in the logs for all the users configured to authenticate via LDAP.
The user with id '0' updated the profile for the user with id 'XYZ'.
The above actually starts appearing in the logs around 830pm Pacific but the users get kicked out by 9pm and they see Error writing to database message on the screen and when they click on more information they get redirected to https://docs.moodle.org/31/en/error/moodle/dmlwriteexception