Mobile app version of vmapp.org
Login or Join
Gail5422790

: Can AJAX in a CMS slow down your server I am currently developing some plugins for WordPress, and I was wondering which route to take. Let's take an example, you want to display the last

@Gail5422790

Posted in: #Ajax #Optimization #WebDevelopment

I am currently developing some plugins for WordPress, and I was wondering which route to take. Let's take an example, you want to display the last 3 tweets on your page.

Option 1

You do things the normal way inside WordPress. Someone enters the website, while generating the page, you fetch the tweets in php via the twitter api, and just display them where you want.

Now the small problem with this is, that you have to wait for the response from twitter. This takes a few ms. NO real problem, but this is question is just out of curiosity.

Option 2

Here you don't do anything in WordPress on the initial load, but you do have the API inside. Now you just generate the page, and as soon as the page is done on the client side, you do a small AJAX call back to the server via a WordPress plugin, to fetch your latest tweets. Also called asynchronously.

Now the problem with this IMO is that you have much more stress on your server. For starters you have two HTTP requests instead of one. Secondly the WordPress core has to load two times instead of one.

Other options

Now I know there are a lot of other options: 1) Getting the tweets directly via javascript, no stress on the server at all. 2) Cache the tweets so they are fetched from the DB instead of using the API every time. 3) Getting the tweets from an ajax call that is not a WordPress plugin. 4) Many more.

My Question

Now my question is if you only compare 1 and 2, which would be a better choice.

NOTE: I am only interested in comparing 1 and 2. No other options. The plugin I am creating has nothing to do with tweets, it's just an example to illustrate the problem.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Gail5422790

2 Comments

Sorted by latest first Latest Oldest Best

 

@Pope3001725

since you asked just out of curiosity, I wanted to comment on something .
you wrote that loading will take "few MS." well - this is not ALWAYS true.
Depending on your target users - sometimes it can take up to 3 min !!!
For example, I am right now in CHINA , which , as you know, BLOCKS twitter, facebook, youtube etc. the result : your page will have to wait the TIMEOVER - which sometimes is up to 3 min. - before loading . (and not many users will wait that long - I often bailout)
In other countries which I have visited, the "twitterFever" takes up to 20,30sec. load time... So, if your audience is ONLY US, you can discard it . if not - go for the cache .

10% popularity Vote Up Vote Down


 

@Yeniel560

I would certainly recommend caching the tweets in your database or a plain text file. As soon as you start getting a lot of traffic you will be creating many hits to the Twitter API and going over their limit. And since you're not tweeting every 30 seconds day and night (I hope!) you could just end up fetching the same thing over and over.

In terms of updating the tweet cache, you could check on each page load whether it's more than X minutes since the tweets were last cached and if so update the cache. A few ms added to page load once every 30 minutes or hour isn't a problem.

In my experience, the perfect solution is to create a separate script that does the fetch & cache, and run this on a cron job by loading the URL. However you would need a dedicated server for this (or your own machine left on permanently).

EDIT: to answer your comment, if you really can only choose between options 1 and 2, then personally I would go for option 2 so that user load times are shorter. One extra HTTP request isn't a huge deal (especially when you already have several others for scripts, images, etc). But why choose between those 2 when there are better options?

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme