Jump to content

[SOLVED] Which of the 2 will use up less bandwidth?


Wes1890

Recommended Posts

I hope this is the right forum to post this.. im not needing code help, and it is about my website application so this one seems right.. but anyways..

 

 

I have a thing on my site that a use can create (like a personal image), and I currently have it to where every time a user accesses the image (from anywhere, any site), it gets a random row from the database (more than 300 rows in this particular table) and displays it on the image.. and this is EVERY TIME THE IMAGE IS LOADED

 

now, I was wondering if it would be easier on my server, to setup a cron job (every 30-45 seconds) which runs a script that sets a random var for every image that is being accessed...so instead of the image getting info from the DB every time it's loaded, it will only happen every 30-45 seconds..

 

now it seems that using a cron job would be the best way.. but since im not really sure, i decided to ask..

 

i hope im making sense.. im just not wanting to kill my server ;)

 

thanks

Link to comment
Share on other sites

Querying the database for one row per request really isn't going to cause any major overhead under normal circumstances, not usually something you would need worry about unless these images are getting thousands of hits/second. It might actually cause more overhead to generate the file every 30 seconds. Can you be sure these images are being requested more than once every 30 seconds as it stands?

 

Either way, this has nothing at all to do with bandwidth.

Link to comment
Share on other sites

Well, lets say i used one of the images here in my sig.. itd get many views then.. times that by a couple hundred people.. and thats if they only use 1 image, they might use 2 or 3... so yeah, after a little while the script will be ran many many times per day

 

and why wouldnt that involve bandwidth? if it doesnt, then im all confused about bandwidth because alot of people will be accessing my scripts

Link to comment
Share on other sites

Whether you build the image dynamically or just load a static file, the same bandwidth would be used (providing the images are the same size).

 

Anyway, there really is no definative answer to your question. It all depends on your server and your traffic, theres just too many variables. If you can see thatr the images are being requested more than once every 30 seconds as it stands, then yeah, it may be something to think about.

Link to comment
Share on other sites

Sounds like you're more worried about the server load than the bandwidth.

 

One way to go about it is this:

 

* Someone makes a request to your script

* Have it pull the data from the db and save it to a cache file, in the file include a timestamp of when the data was created.

* Next time someone makes a request to the script it will check the variable with the timestamp, if more than x seconds have passed then have it make a request to the db and overwrite the data in the file along with updating the timestamp variable.

* If x seconds haven't passed then the script will read the data from the cache file and use it instead.

This way if there aren't any requests to the script it won't continue to update the cache file, it'd be a waste of processing.

-----

Another way you could do it so the script wouldn't have to wait on the db to return data is:

* As you said have a cronjob but change up the logic a bit.

* cronjob runs a script that will check the timestamp of a file under your web directory, compare the timestamp to the current time and if it is within x amount of seconds then have the cronjob script dump the db data to the cache file.

* When a request is made to your script it will use touch to update the timestamp of the file if the timestamp was > how often you wanted it to update. That way next time your cronjob script runs it will see the updated timestamp and update the data in the cache file.

So each time a script is ran it will only read data from the cache file.

In your cronjob script you can have it so that if it fails to get the results from the db then it won't update the cache file, this way if for some reason or another the db was to die then your script on your site would still function.

 

 

Link to comment
Share on other sites

Another way you could do it so the script wouldn't have to wait on the db to return data is:

* As you said have a cronjob but change up the logic a bit.

* cronjob runs a script that will check the timestamp of a file under your web directory, compare the timestamp to the current time and if it is within x amount of seconds then have the cronjob script dump the db data to the cache file.

* When a request is made to your script it will use touch to update the timestamp of the file if the timestamp was > how often you wanted it to update. That way next time your cronjob script runs it will see the updated timestamp and update the data in the cache file.

So each time a script is ran it will only read data from the cache file.

In your cronjob script you can have it so that if it fails to get the results from the db then it won't update the cache file, this way if for some reason or another the db was to die then your script on your site would still function.

 

that is actually a good idea.. ill set that up real quick and see how it goes.. much appreciated

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.