简体   繁体   中英

Best way to store a timestamp requested by a lot of users at the same time

Let's say i have a timestamp (like : 1505148597) representing the last message posted in a tchat.

Then i have about 10000 users requesting /lastTimestamp.php every second. This page returns the timestamp so users know if they have to fetch new messages.

Should I :

a) store the timestamp in database and make a sql request everytime a user calls /lastMessageTimestamp.php to return it

b) store the timestamp in a file on the server and open it everytime a user calls /lastMessageTimestamp.php to return it

c) store it in memory and access it somehow ?

What is the most optimized way to do it ? My biggest concern is performance and server load.

Note : i'm not looking for another way to do a tchat, this is the way i want to do it, i just want to know what the best solution is to make it (the example is very simplified), it's not even for a tchat. In fact i'm using Server Sent Events, so basically the 10k users are opening a stream with the server, and server is doing a

while(1) { 
   //check the timestamp with one of the solutions
   sleep(1);
}

You should put a caching layer in front to handle the load. I would suggest Redis: https://redis.io/

This is the best example use of a memory store => store once retrieve many times. You always store the last timestamp (once).

Either: apcu apc_store or

memcached Memcached::set

Memcached is a little more complex to set up, but can help in very high volume scenarios

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM