Support » Plugins » Hacks » Handling max_user_connections safely

  • I have a WP plugin that records user activity to the DB.

    I am aware that if the plugin is used on a very heavily trafficked site that max_user_connections errors from an Apache server may still happen regardless of optimizations or how high the limit has been set. What I want to ensure is that the page the user is viewing does not freeze or the browser crash if a max_user_connections error occurs.

    WordPress uses a class called wpdb to interface to the DB. When using $wpdb->insert or $wpdb->update what happens if a max_user_connections error occurs?

    Will these functions return the standard return error of FALSE or will the PHP code “hang” causing a page freeze or browser crash? Does anything special need to be done to prevent any problems?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Before you look into the plugin, does your hardware and bandwidth are able to handle those high traffic. If not too sure, you can do a stress test on the web server if it is own by you otherwise trying to look into those plugins will never resolve the root cause.

    FYI most server will generate error due to overflow of traffic in which the hardware or bandwidth is insufficient to handle it.

    I wrote the plugin myself. Other people will use it so the server hardware and bandwidth will vary depending on what they have.

    That’s why I want to intercept the error so that no matter what the capabilities of the server the plugin will not cause any problems to visitors viewing pages on the site.

    If you’re using $wpdb for your DB queries I wouldn’t be worried about it. WordPress creates one database connection for each page load, so as long as you stick to using the standard $wpdb object you won’t have to worry about it.

    The only time that this would be an issue is if the site is very high volume and the server can’t handle it, but if that’s the case every querey on the page is going to fail, and this would happen when $wpdb is created which will be way before it gets to any of your queries.

    Thanks Michael, at the moment I have the following 2 types of DB operation, that are triggered by a user clicking on a link or scrolling (after the page has loaded):

    $wpdb->insert($table_name, $data);
    $dataId = $wpdb->insert_id;


    $wpdb->update( $table_name, ... );

    Can you see a problem with this?

    There’s no connection problems in what you’re doing – unless you’re using something like AJAX to send a whole bunch of requests back to the server for every page. Like I said before: 1 request = 1 database connection. The number of queries doesn’t factor into the connection count, only the server load and as long as you kpe things smple, it should not be a problem.

    With what you’ve shown there I would not be concerned. The DB queries to generate a single page a whole lot more hard-core then that so your script would be a very small percentage of overall loading stress.

    Ok, thanks that’s very helpful. Sorry one last clarification.

    Does the Ajax call to the PHP script that does the DB operations count as one and the same process? Or does the Ajax call count as one and then the wpdb operation count as another one because of the MySQL operation?

    If they count as 2 processes and max_user_connections is reached after the Ajax call (unlikely I know) would the PHP function simply fail to run or die gracefully because it cannot access/allocate the global wpdb?

    Or have I misunderstood?

    Any page request will count as one request, and will generate one connection, so if your page loads, and then loads two AJAX calls, it will be three connections. This is because they are seperate calls to the server, so each one will require a database connection to be set up.

    If you get to the point that the max connections error happens, it will die (not gracefully) in the same way that you’ve seen it happen now.

    Have you considered if your wordpress site is under DDOS attack? For such a case, i think your plugins will may get crashed due to overloaded connection requests. I bet you shall set some parameter to prevent such a case.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Handling max_user_connections safely’ is closed to new replies.