3

Using a JWT authentication, the user doesn't have to log in each time he uses my app.

I would like to save in DB the last time the user used/opened the app.

Technically, I have a set of REST API that could audit each request and save in database the timestamp corresponding to the current user access but ... for each request?!

What is a good practice to store this information without involving database each time the user makes a request?

11
  • Can you explain why you need this? What you are going to use the timestamp for will determine the best way to do this. For a start you probably already are recording each users last access time in your HTTP server logs. Is this enough? Is this to audit user activity later or for some real time analysis? Commented Aug 5, 2015 at 8:16
  • It is a "feature" lf my app: showing the last member connection to others users.
    – Mik378
    Commented Aug 5, 2015 at 8:43
  • In that case I would store the last access time in the database with a threshold of a minute or so. Since you are probably getting the User record on each request you just check the time on the next request. If it is not outside the threshold don't bother updating the database. Fine granularity is not needed in that use case (A user won't care if last request was 1:34 or 1:35). Why make your life hard when the user won't notice. Commented Aug 5, 2015 at 8:52
  • What is the difference with others answers below ?
    – Mik378
    Commented Aug 5, 2015 at 8:55
  • None, I think Ivan is right on the money. Write to the DB and if you are worried about performance issues (not something to worry about at the start) introduce a threshold. That would be the best practice. A bigger issue is retrieving the value for other users. This gets complicated given the network of users (this if Facebooks problem, getting information based on their friend network). But different question :-) Commented Aug 5, 2015 at 9:03

2 Answers 2

3

You can set a threshold.
For instance, you can store that someone visited at 14:00PM, and then audit if the difference between then and the current time is more than 30 minutes, so you have 2 updates per hour at most.

If you have a stateful service, another approach would be to store the time of each request in memory (default session driver, Redis, Memcache, whatever).
That way, you can detect when they leave your app and store their last time just once, at the end.

Update:

Another way would be to serve a request timestamp with your API tokens if you use, say, JWT. If not, you can still send cookies or store them in user's localStorage. Then they will send you their last access time with each request and you can utilize the first suggestion without having to touch the database.

12
  • "and then audit if the difference between then and the current time is more than 30 minutes" => it would involve a database query to read the last saved date to compare to ...
    – Mik378
    Commented Aug 2, 2015 at 0:35
  • Is your api stateless? Commented Aug 2, 2015 at 0:36
  • My api is stateless yes.
    – Mik378
    Commented Aug 2, 2015 at 0:37
  • 1
    They both store data in memory and will give you the fastest access possible. However, Memcache is only a key->value map with 1MB limit per record. Redis does not have these limitations, has multiple data structures that you can store and query and is more versatile and powerful all-round. Commented Aug 2, 2015 at 0:44
  • 1
    You get a timestamp in the request, and send the current time as a new one, which you will get on the next request. No database interaction. When the timestamp you get has more than 30 minutes difference, you insert two visit records, one for the last timestamp and one for the current time (if you need logs). You can't have persistent data without touching any data storage ever. At some point, you have to store it and/or read it. Commented Aug 2, 2015 at 1:23
2

Consider using a queue, where each method access resulting an entry, and use a

  1. timer job
  2. queue length reaching certain threshold

to flush the queue into the database.

2
  • Why a queue? I only need THE LAST timestamp.
    – Mik378
    Commented Aug 2, 2015 at 10:12
  • 2
    But you have many concurrent users. Commented Aug 2, 2015 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.