Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing data into session and storing to database upon "major" action

I know there are hundreds of these questions but what I am asking however is slightly different.

When the user logs in I would like to get all their data from each table in a database and store it in a session variable (obviously not sensative data such as encrypted password/salts etc basically data that would be useless or have no value to a hacker!!), and whilst the user uses the website the relevant data stored in the session will be used as opposed to accessing the database everytime. Moreover when the data is changed or added this will be written or added to the session file, and upon a major action such as "saving" or "loggin out" the new/changed data will be written to the database.

The reason I wish to do this is simply for efficieny, I want my application to not only be fast but less resource consuming. I am no expert on either which may explain why my idea makes no differnece or is more resource intensive.

If there is an alternative to my solution please let me know or if there is something to improve on my solution I will be glad to hear it.

Thank you. My application is using PHP and mysql.

like image 810
Yusaf Khaliq Avatar asked Feb 26 '14 12:02

Yusaf Khaliq


2 Answers

If any of these don't apply to your app, then please ignore. In general, I'm against using sessions as caches (especially if anything in the session is going to be written back to the DB). Here's why.

  • Editing the session requires a request from the user. Editing a php session outside of the request-response cycle is very difficult. So if a user Alice makes a change which affects Bob, you have no way to dirty Bob's cache
  • You can't assume users will log out. They may just leave so you have to deal with saving info if the session times out. Again, this is difficult outside of the request-response cycle and you can't exactly leave session files lying around forever until the user comes back (php will gc them by default)
  • If the user requires authentication, you're storing private information in the session. Some users may not be happy about that. More importantly, a hacker could imploy that private information to conduct a social engineering attack against the end-user.
  • Mallory (a hacker) might not be able to use the information you put in the session, but she can poison it (ie. cache poisoning), thereby causing all sorts of problems when you write your cache to your permanent storage. Sessions are easier to poison then something like redis or memcache.

TL;DR Lots of considerations when using a session cache. My recommendation is redis/memcache.

like image 104
tazer84 Avatar answered Oct 23 '22 01:10

tazer84


You can also go for local-storage in HTML5, check The Guide and THE PAST, PRESENT & FUTURE OF LOCAL STORAGE FOR WEB APPLICATIONS

Local Storage in HTML5 actually uses your browsers sqlite database that works as cookies but it stores data permanently to your browser

  1. unless someone by force remove the data from the browser finding the data files
  2. Or if someone remove/uninstall browser completely,
  3. or if someone uses the application in private/incognito mode of the browser,

What you need to do

  1. Copy the schema for required tables and for required columns and update data at a regular interval
  2. you dont have to worry about user's state, you only have to update the complete data from the localStorage to mysql Server (and from the mysql server to localStorage if required) every time user backs to your application and keep updating the data at regular interval

Now this is turning out to be more of localStorage but I think this is one of the best solution available for me.

like image 22
zzlalani Avatar answered Oct 23 '22 02:10

zzlalani