I want to track user's last activity. I'm using Vue, Inertia, and Laravel.
I think having a session table or adding a last_activity column in users table is fine. Then update that every request.
Wouldn't it be expensive performance-wise, since it creates transaction to the database every single request? What about recording that data to JS sessionStorage instead?
Writing to the DB on every single request will definitely crush your database once you start seeing real traffic.
JS sessionStorage won't work if you actually need the server to know their status (for example, to show an "online" badge to other users or for backend analytics), since it only lives in the client's browser.
Writing to the DB on every single request will definitely crush your database once you start seeing real traffic.
That's a massive hyperbole, unless your definiton of "real traffic" is thousands of requests per second. And then you're hitting other bottle necks first.
If Laravel accesses the DB during each request, e.g. by pulling a user model, the additional cost of updating a single timestamp is minimal. Most of the overhead comes from establishing a database connection.
I just want the time when the user was last active. The sessions table provided by laravel could suffice it. Would it break if I remove the cols other than the fk and last_activity?