Session concurrency and locking

I've been maintaining an application for the last 6 years. During those 6 years, we noticed what we came to call "the bad problem".

This application stores its sessions in a postgres database(so that we can load balance the web servers, and not use sticky sessions). During the busy season, we would notice db backends building up. Eventually, they could build up to the point where memory on the db server was exhausted, and new connections would be rejected. This was bad. For the longest time we couldn't find the source of our problem. We theorized it had to do with session locking, and we spent a lot of time building tools to track down how it happened.

short summary of tools created to debug:
startProfile('smarty','display');endProfile('smarty','display');
tracksession.php(a tool to show all the requests by a certain sessionId from the profile log)
set statement_timeout

Short summary of problem:
When a browser loses its internet connection unexpectedly, Apache can't tell that the connection is dead, so waits as long as its Timeout directive for a packet to be received from the client. This means that in PHP, an echo() call can hang as long as 120 seconds by default on newer apache installations. Any time your application will send data to the client, you should/must call session_write_close to avoid locking the session for the length of Apache's Timeout directive.

Long description of tools for tracking the problem:
startProfile('smarty','display');endProfile('smarty','display');
startProfile and endProfile are functions for logging performance of requests. By using the define('logprofile', 'print'); in your etc file, you can see what the measurements are, which is useful in development. define('logprofile', '/home/apps/log/profile'); will log the performance to a file(actually two files in the directory, profile.log and longest.log). Zoop already log's the total request time, and the time taken in sql queries, as well as the number of sql queries and the longest sql query. By adding startProfile('myapp', 'myfunc'); at the beginning of a function and endProfile('myapp','myfunc'); at the end, you can add profiling of your own functions. The first param, 'myapp' is the grouping used. All the calls to startProfile('myapp') will be grouped together and reported as a total time in the log. It will also log which 'myfunc' was the longest. A line from the profile.log can look like this:

"01/26/09 02:49:59.0779", "@ROOT/keepOpen/get", "1", "0.018956899642944", "0.0052809715270996", "query_497d24d71c653", "/home/apps/code/myapp/index.php @ line 51", "0.0052809715270996", "session", "1", "0.0051000118255615", "0.0051000118255615", "session_connect", "/home/apps/code/myapp/index.php @ line 51", "https://example.com/myapp/index.php/keepOpen", "3c928e7093a0da3ef8e91409a05fdf61"

First is the time the request started in UTC. Then the name of each zone in the request followed by the page in the request, then the method(get or post). zone_default is known as @ROOT. then it displays the number of sql queries, the total time of the request, the time spent in seql queries, a code to look up the longest query in longest.log, the location the longest query was called from, and the length of the longest query. Then it begins to list the other groupings you have specified. In this case the request called startProfile('session', 'session_connect');endProfile('session','session_connect'); and that was all. The first item after "session" is the number of calls in the "session" group, the total time, the longest call, the name of the longest call, the location of the longest call, then the url of the request and the session_id.

My favorite tool we created is tracksession.php. It uses the profile.log to see exactly what requests were made during a session from start to finish, and then we can diagnose any performance problems the user may see. It greps the profile.log file for a string that you give it, returning the rows that match. It's quite simple, but very helpful. It can be used to either track a session, or simply to see all the requests that are similar, or were made in the same minute.

Timeout values were masking our problem in a couple ways. PHP has "max_execution_time" that is set by default to 30 seconds in the php.ini. Obviously our requests were taking much longer than that. From a note at http://us2.php.net/set-time_limit

Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running.

So our database calls were not being terminated after 30 seconds, but waiting indefinitely for the lock. And the original request that hung was not being terminated after 30 seconds, but waiting for apache to timeout the connection. Using sql_query("SET statement_timeout 1000");, we are able to control at least the script waiting for the lock. It will now wait for a second at a time. If you then allow the query to be retried 10 times, you can give a script 10 seconds to get the lock, then trigger_error and terminate the script, minimizing the buildup of db processes.

I'll be committing some of these tools to zoop. I hope you enjoy them!

Session and Concurrency

SQL Server allows multiple clients to use the same database concurrently. As such, it needs to control concurrent access to shared data, to ensure data integrity - when multiple clients update the same data, or clients attempt to read data that is in the process of being changed by another client. SQL Server provides two modes of concurrency control: pessimistic concurrency and optimistic concurrency. When pessimistic concurrency control is being used, SQL Server controls concurrent access by using locks. Locks can be either shared or exclusive. Exclusive lock grants the user exclusive access to the data - no other user can access the data as long as the lock is held. Shared locks are used when some data is being read - multiple users can read from data locked with a shared lock, but not acquire an exclusive lock. The latter would have to wait for all shared locks to be released. Locks can be applied on different levels of granularity - on entire tables, pages, or even on a per-row basis on tables. For indexes, it can either be on the entire index or on index leaves. The level of granularity to be used is defined on a per-database basis by the database administrator. While a fine grained locking system allows more users to use the table or index simultaneously, it requires more resources. So it does not automatically turn into higher performing solution. SQL Server also includes two more lightweight mutual exclusion solutions - latches and spinlocks - which are less robust than locks but are less resource intensive. SQL Server uses them for DMVs and other resources that are usually not busy. SQL Server also monitors all worker threads that acquire locks to ensure that they do not end up in deadlocks - in case they do, SQL Server takes remedial measures, which in many cases is to kill one of the threads entangled in a deadlock and rollback the transaction it started.[19] To implement locking, SQL Server contains the Lock Manager. The Lock Manager maintains an in-memory table that manages the database objects and locks, if any, on them along with other metadata about the lock. Access to any shared object is mediated by the lock manager, which either grants access to the resource or blocks it.

SQL Server also provides the optimistic concurrency control mechanism, which is similar to the multiversion concurrency control used in other databases. The mechanism allows a new version of a row to be created whenever the row is updated, as opposed to overwriting the row, i.e., a row is additionally identified by the ID of the transaction that created the version of the row. Both the old as well as the new versions of the row are stored and maintained, though the old versions are moved out of the database into a system database identified as Tempdb. When a row is in the process of being updated, any other requests are not blocked (unlike locking) but are executed on the older version of the row. If the other request is an update statement, it will result in two different versions of the rows - both of them will be stored by the database, identified by their respective transaction IDs.

Matt John 15 Oct 2009

Load Balancing With Proxies

I am curious with the presence of MYSQL proxy and Apache or Squid proxy does the application still need to know anything about the load balancing which you had written into the application? I know that MYSQL proxy did not exist until recently and you had written the software starting 6 years ago, but taking that into account would you still need to load balance within the application and not use the servers?

jmorant@cloud9l... 16 Oct 2009

Apache reverse proxy load balancing

A late reply :). With the right configuration, the application doesn't need to know anything about the load balancing. With "ProxyPreserveHost On", zoop will correctly build SCRIPT_URL, VIRTUAL_URL and BASE_HREF. I don't know much about Mysql Proxy, we use Postgresql, and currently just have the one server in use by the application.

john 08 Jul 2010

hi

the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. heroes of newerth

strobin 10 Feb 2011

I really like your website

I really like your website because you gave me about info and resource I've never seen that before. I' l stay tuned on your web site
cheap auto insurance quotes wood pellet maker
wood pellet maker

smallyfish 04 Apr 2011

thanks

A good informative post that you have shared and appreciate your work for sharing the information.Masterboard

hushcat 15 Apr 2011

Die Privathaftpflicht

Die Privathaftpflicht abschliessen.
Das Termingeld vergleichen.

kimmole 25 May 2011

Wonderful post! This is very

Wonderful post! This is very useful to many readers like me. Being a student, I am requiring myself to read articles more often and your writing just caught my interest. Thank you so much!prestiti personali

monkey 365 27 May 2011

Sequencing was created

Sequencing was created because we had a document creation process that required a user to follow a certain order of pages within one or more zones. There were multiple places that needed to lead to this process.
professional web design company

daniel naveen 17 Jun 2011

Interesting post and thanks

Interesting post and thanks for sharing. Some things in here I have not thought about before.
pacquiao vs marquez tickets with Fifa World Cup

zarahfulton 13 Jul 2011

Wieso die Prepaid

Wieso die Prepaid Kreditkarte so beliebt ist.
Eine Kreditkarte testen.

Wieso der Rechtsschutz für viele so wichtig ist.
Einen Unterhalts Rechtsschutz testen.

kammone 25 Sep 2011

I really loved reading your

I really loved reading your blog. It was very well authored and easy to understand. Unlike additional blogs I have read which are really not good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he enjoyed it as well!

admissions essay

devil 02 Jan 2012

looks good but if you're

looks good but if you're installing in separate instances save yourself the effort and use / for the context root... there's no reason to need a URL suffix when you're running separate versions as separate instances.relatiegeschenken

leonardod 09 Jan 2012

should have oneself footing

should have oneself footing and social conscience. Happen frequently in such hype later, traditional media should be judged somewhat to this phenomenon and evaluate, make the public and the height that relevant orgnaization stands in Computerhulp

leonardod 15 Jan 2012

Nice

Nice comment,I would also say here that I love your website,is very interesting.
Dora cooking

ghose 21 Jan 2012

There are plenty of uses for

There are plenty of uses for vehicle tracking devices. The main one is the fight against theft. If a thief steals a car that has some sort of Vehicle Tracking Devices in it, the chances of recovery are greatly increased. Some tracking devices, you can even turn off the engine, so the car stops.

JulianAnd 15 Oct 2011

AMAZING THINGS

I would appreciate you that you pick up an important topic to write a thoroughly informative post on. I hope that you never stop and keep posting such valuable content on this site. I am truly thrilled and now going to ask my Custom Essays writing service provider to write me an essay on this topic.
Write My Essay For Me|| Buy an Essay

johnsonedwardma... 01 Nov 2011

A session is a correlation

A session is a correlation of all messages sent between two endpoints. Instancing refers to controlling the lifetime of user-defined service objects and their related InstanceContext objects. Thanks for sharing the informative post.
Regards,
Animal Sex DVDs

cameron7 30 Oct 2011

I am also interested in this

I am also interested in this topic. I have spent a lot of time on searching this kind of topic.Candle I am lucky at last to find your article. It is very Seattle Furniture informative. Thanks a lot. Hope you can post more articles.

vijaysbhagat 20 Oct 2011

I just would like to give a

I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.personal statement writers

chinkiseo20 27 Nov 2011

I’m not that much of a

I’m not that much of a internet reader to be honest but your blogs really nice, keep it up! I'll go ahead and bookmark your website to come back later. All the best
cell phone spy software

Lucas Campbell 07 Jun 2011

Eine Handyflatrate

Eine Handyflatrate vergleichen.

kmaisso 17 Jun 2011

You made some respectable

You made some respectable points there. I seemed on the web for the problem and found most individuals will go together with along with your website.prestiti on line

monkey 365 18 Jun 2011

ersoxbow

ideas don’t always come easy. All of us need a little kick start from time to time, to get our minds ticking. To get excited. The short black of inspiration. kitchen gifts

ersoxbow 15 Dec 2011

It was amazing. Your thought

It was amazing. Your thought process is wonderful. The way you tell about things is awesome. i always wait for your posts. Great post! Finally i have all the necessery info about it! Thank you so much! Really its remarkable.
mediahuset nova as

Nancy 26 Aug 2011

Die

kammone 25 Sep 2011

I am also interested in this

I am also interested in this topic. I have spent a lot of time on searching this kind InfiniBand of topic. I am lucky at last to find your article. It is very Seattle Furniture informative. Thanks a lot. Hope you can post more articles.

vijaysbhagat 28 Oct 2011

The routes has its exit

The routes has its exit numbers dominate over the other and can sometimes result in having two exits of the same number albeit far from each other along the same highway.Thanks for sharing the informative post. Regards. customremodelersbathrooms-mn.com

samson77 08 Nov 2011

Austin Home Builder

I have not been able to find such kind of information throughout the search engines and internet. It's been fabelously informative to read your blog and i am going to suggest it to another fellow as well.Austin Home Builder

Lucky127 14 Jan 2012

Thanks for the marvelous

Thanks for the marvelous posting! I certainly enjoyed reading it, you can be a great author.I will make certain to bookmark your blog and definitely will come back down the road. I want to encourage you to ultimately continue your great posts, have a nice day! SEO Chicago | Curt Tallada

CurtTallada 16 Sep 2011

Wonderful post. I am

Wonderful post. I am searching awesome news and idea. What I have found from Austin SEO your site,it is actually highly content. You have spent long time for this post. I do not like to waste time by commenting but how can I stop me reading this post. Thanks!

vijaysbhagat 20 Oct 2011

agwe

Its one of the good platform for awareness of people. Keep sharing such stuff in the future too.fine crusher

xiaobu 07 Dec 2011

Excellent site, keep up the

Excellent site, keep up the good work my colleagues would love this. I read a lot of blogs on a daily basis Blade Server and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks

vijaysbhagat 15 Nov 2011

Great blog, just looking

Great blog, just looking around some blogs, seems a pretty nice platform you are using. I'm currently using Irrigation Systems Wordpress for a few of my sites but looking to change one of them over to a platform similar to yours as a trial run. Anything in particular you would recommend about it?

vijaysbhagat 24 Nov 2011

session

it must be put into our attention all the latest happenings for us not to experience any hassle again.Second Hand Golf Clubs

chellax 16 Jun 2011

Excellent site, keep up the

Excellent site, keep up the good work my colleagues would love this. I read a lot of blogs on a daily basis Alaska Fishing Lodge and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks

vijaysbhagat 15 Nov 2011

Large scale transport of

Large scale transport of wood pellets made by wood pellet mills in chinato general consumers as well as retailers is actually efficient, because the wood pellets occupy a tiny space in high densities.

rayfox 12 Oct 2011

Thanks a lot for sharing

Thanks a lot for sharing this useful and attractive information and I will be waiting for Candles other interesting posts from you in the nearest future.keep it up

vijaysbhagat 28 Oct 2011

Thanks for taking the time

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. Outdoor Lighting LED If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me

vijaysbhagat 15 Nov 2011

Thanks a lot

Thanks a lot, it is really useful to me.
Scot

ScotWetherington 10 Jan 2012

Nice to be visiting your

Nice to be visiting your blog again, it has been months for me.Well this article that i've been Gun Dog Training waited for so long.I need this article to complete my assignment in the college, and it has same topic with your article.Thanks, great sha

vijaysbhagat 24 Nov 2011

learner

I am a learner of php and mysql. All of your comments are helpful for me to go details on these programming language. crazy taxi

jamesalan 20 Dec 2011

Re:

Thanks for the awesome article..keep up the good work.

Hemorrhoids Treatment

vegitaboss 10 Jan 2012

Great point

Great point about the MYSQL proxy. I would like to know as well.

Fletcher
email marketing solutions | email marketing services.

fletcherfinn 22 Feb 2011

The age of a bike is

The age of a bike is inversely proportional to motorcycle insurance rates. Meaning, the younger your bike, the more expensive the motorcycle insurance rate is. About Corllins University AND About Hill University AND Woodfield University

markweee 16 Apr 2011

Please keep posting

This site is very good, it's a pitty you don't actualize it...
peso ideal

amorpeso 05 May 2011

Excellent site, keep up the

Excellent site, keep up the good work my colleagues would love this. I read a lot of blogs on a daily basis Votive Candles and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks

vijaysbhagat 16 Nov 2011

Great article ! I would like

Great article ! I would like to hear from individuals who have used the aTaxidermy Schools I am just beginning to consider do my own website just for personal thrill, but which application do I use to assist in the development ?

vijaysbhagat 24 Nov 2011

printed balloons

So once you’ve made your first coffee, and checked the weather, flicked though your emails, jump onto our site and see if today is your day. printed balloons

lidged 06 Jan 2012

RE

This app working for me.
pc audit software

amanda1 16 Apr 2011