{"id":628,"date":"2013-06-03T01:31:20","date_gmt":"2013-06-02T23:31:20","guid":{"rendered":"http:\/\/codingexplained.com\/?p=628"},"modified":"2017-06-11T22:06:08","modified_gmt":"2017-06-11T20:06:08","slug":"solving-concurrent-request-blocking-in-php","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php","title":{"rendered":"Solving Concurrent Request Blocking in PHP"},"content":{"rendered":"<p>If you have tried to make several concurrent AJAX requests (e.g. with jQuery) to a PHP script, then you may have experienced that the requests were not really handled concurrently. Instead, the requests were completed sequentially and thus blocking each other. The HTTP connection limit in the browser is probably not the cause, as most browsers have a limit of <a href=\"http:\/\/www.browserscope.org\/?category=network\" title=\"Browser connection limits\" rel=\"nofollow\" target=\"_blank\">at least six concurrent HTTP connections<\/a>.<\/p>\n<p>The more likely explanation is that your script is either writing or reading session data. By default, session data in PHP is stored in files on the file system. Normally writes to the file occur once the script terminates, and to prevent concurrent writes, an exclusive lock is obtained on the session file. This means that at any given time, only one script (i.e. process or thread) may operate on the session data. As a result, other concurrent requests are simply waiting for the lock to be released such that the execution can proceed &#8211; one at a time. This causes the execution to be sequential rather than parallel as expected.<\/p>\n<p>Luckily, PHP provides the <span class=\"code\"><a href=\"http:\/\/php.net\/manual\/en\/function.session-write-close.php\" target=\"_blank\" rel=\"nofollow\" title=\"Function documentation\">session_write_close<\/a><\/span> that can be used to resolve this. What this function does is to write the session data to the file and release the lock, which then lets other requests access it. Therefore, as soon as all the necessary data has been saved to the session, this function should be invoked. At that point, the request will stop blocking other concurrent requests for the same script. It will still be possible to read session data after the function call, but no writing can occur.<\/p>\n<p>The code below is an example of a scenario where the <span class=\"code\">session_write_close<\/span> function can be used to increase the throughput of the script.<\/p>\n<pre><code class=\"php\">session_start();\r\n\r\n$_SESSION['some'] = 'value';\r\n$i = 0;\r\n\r\nwhile ($i < 10) {\r\n\tsleep(1);\r\n\t$i++;\r\n}<\/code><\/pre>\n<p>While the above example is purely theoretical, imagine that five concurrent AJAX requests are made to the script. Each execution lasts for roughly ten seconds. This means that each request will block the subsequent request with approximately ten seconds. This only happens because session data is accessed, and thus a lock is obtained on the session file. As you can imagine, this blocking is not necessary because the session data is only changed <em>before<\/em> the time consuming part of the script. As such, one can close the session immediately after changing the data to save time, because then the request will no longer block other requests. This approach is reflected in the code snippet below.<\/p>\n<pre><code class=\"php\">session_start();\r\n\r\n$_SESSION['some'] = 'value';\r\nsession_write_close(); \/\/ From here on out, concurrent requests are no longer blocked\r\n$i = 0;\r\n\r\nwhile ($i < 10) {\r\n\tsleep(1);\r\n\t$i++;\r\n}<\/code><\/pre>\n<p>You will still be able to read session data after closing the session, so you only have to be cautious about when you are <em>changing<\/em> session data. After introducing this simple function call, you should notice that your requests are now executed in parallel and are no longer blocking each other (after the function call). This can potentially improve the throughput of your application greatly, depending on the use case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Making multiple concurrent AJAX requests to a PHP script, for example, may have left you wondering why the requests are not executed in parallel as expected. Rather, the requests are blocking each other. Chances are that this is caused by using sessions within the PHP script.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[6],"tags":[69,68,40,64],"series":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Solving Concurrent Request Blocking in PHP<\/title>\n<meta name=\"description\" content=\"Are your concurrent PHP requests blocking each other? This is usually caused by using sessions. This article explains how to make them execute in parallel.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Solving Concurrent Request Blocking in PHP\" \/>\n<meta property=\"og:description\" content=\"Are your concurrent PHP requests blocking each other? This is usually caused by using sessions. This article explains how to make them execute in parallel.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php\" \/>\n<meta property=\"og:site_name\" content=\"Coding Explained\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:published_time\" content=\"2013-06-02T23:31:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T20:06:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"444\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bo Andersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:site\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bo Andersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php\",\"url\":\"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php\",\"name\":\"Solving Concurrent Request Blocking in PHP\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2013-06-02T23:31:20+00:00\",\"dateModified\":\"2017-06-11T20:06:08+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Are your concurrent PHP requests blocking each other? This is usually caused by using sessions. This article explains how to make them execute in parallel.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Solving Concurrent Request Blocking in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codingexplained.com\/#website\",\"url\":\"https:\/\/codingexplained.com\/\",\"name\":\"Coding Explained\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codingexplained.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\",\"name\":\"Bo Andersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"caption\":\"Bo Andersen\"},\"description\":\"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!\",\"sameAs\":[\"https:\/\/codingexplained.com\",\"https:\/\/www.facebook.com\/codingexplained\",\"https:\/\/www.linkedin.com\/in\/ba0708\",\"https:\/\/twitter.com\/codingexplained\",\"https:\/\/www.youtube.com\/c\/codingexplained\"],\"url\":\"https:\/\/codingexplained.com\/author\/andy\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Solving Concurrent Request Blocking in PHP","description":"Are your concurrent PHP requests blocking each other? This is usually caused by using sessions. This article explains how to make them execute in parallel.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php","og_locale":"en_US","og_type":"article","og_title":"Solving Concurrent Request Blocking in PHP","og_description":"Are your concurrent PHP requests blocking each other? This is usually caused by using sessions. This article explains how to make them execute in parallel.","og_url":"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2013-06-02T23:31:20+00:00","article_modified_time":"2017-06-11T20:06:08+00:00","og_image":[{"width":1200,"height":444,"url":"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png","type":"image\/png"}],"author":"Bo Andersen","twitter_card":"summary_large_image","twitter_creator":"@codingexplained","twitter_site":"@codingexplained","twitter_misc":{"Written by":"Bo Andersen","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php","url":"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php","name":"Solving Concurrent Request Blocking in PHP","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2013-06-02T23:31:20+00:00","dateModified":"2017-06-11T20:06:08+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Are your concurrent PHP requests blocking each other? This is usually caused by using sessions. This article explains how to make them execute in parallel.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/php\/solving-concurrent-request-blocking-in-php#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Solving Concurrent Request Blocking in PHP"}]},{"@type":"WebSite","@id":"https:\/\/codingexplained.com\/#website","url":"https:\/\/codingexplained.com\/","name":"Coding Explained","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codingexplained.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d","name":"Bo Andersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","caption":"Bo Andersen"},"description":"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!","sameAs":["https:\/\/codingexplained.com","https:\/\/www.facebook.com\/codingexplained","https:\/\/www.linkedin.com\/in\/ba0708","https:\/\/twitter.com\/codingexplained","https:\/\/www.youtube.com\/c\/codingexplained"],"url":"https:\/\/codingexplained.com\/author\/andy"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":false,"jetpack_shortlink":"https:\/\/wp.me\/p3mJkW-a8","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/628"}],"collection":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":12,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":3028,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/628\/revisions\/3028"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=628"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}