%3CLINGO-SUB%20id%3D%22lingo-sub-1369218%22%20slang%3D%22en-US%22%3EReaching%20Azure%20SQL%20DB%20connection%20limits%20in%20many%20ways%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1369218%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20supporting%20Azure%20SQL%20DB%20Connectivity%20case%20we%20sometimes%20receive%20errors%20message%20saying%20that%20was%20an%20Azure%20SQL%20DB%20issue%2C%20but%20sometimes%20this%20is%20just%20an%20application%20problem.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20as%20Database%20Administrators%20do%20not%20need%20to%20be%20WebApp%20experts%20but%20we%20need%20to%20have%20some%20tools%20to%20check%20if%20issue%20is%20on%20Azure%20SQL%20DB%20or%20on%20the%20client%20side%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI've%20created%20an%20%3CSTRONG%3EAzure%20Function%3C%2FSTRONG%3E%20sample%20code%20for%20testing%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FFonsecaSergio%2FAzureFunctionAppTestConnectivity%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FFonsecaSergio%2FAzureFunctionAppTestConnectivity%3C%2FA%3E%26nbsp%3B(%3CSTRONG%3EFunction_ConnectSQL_PoolLimit%3C%2FSTRONG%3E)%20and%20in%20this%20test%20I%20will%20just%20open%20multiple%20connection%20in%20a%20while%20loop.%20This%20troubleshooting%20steps%20should%20be%20similar%20also%20to%20a%20WebApp.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3Ewhile%20(Aux%2B%2B%20%26lt%3B%20LoopLimit)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%20%20%20%20%20log.LogInformation(String.Format(%22Loop%20%7B0%7D%22%2C%20Aux))%3B%3CBR%20%2F%3E%20%20%20%20%20SqlConnection%20sqlConnection2%20%3D%20new%20SqlConnection(ConnectionString)%3B%3CBR%20%2F%3E%20%20%20%20%20sqlConnection2.Open()%3B%3CBR%20%2F%3E%7D%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20lets%20start%20breaking%20the%20limits%20in%20the%20scenarios%20below%3A%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E1%20-%20Application%20connection%20pool%20setting%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E2%20-%20Pre-login%20handshake%20error%20%2F%26nbsp%3BWebApp%20high%20CPU%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E3%20-%20Azure%20SQL%20DB%20connection%20limit%3C%2FP%3E%0A%3CP%20class%3D%22lia-indent-padding-left-30px%22%3E4%20-%20SNAT%20Port%26nbsp%3BExhaustion%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1353396876%22%20id%3D%22toc-hId--1350803140%22%3E%3CFONT%20size%3D%225%22%3E1%20-%20Application%20connection%20pool%20setting%3C%2FFONT%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20this%20test%20I'll%20limit%20the%20application%20pool%20limit%20to%2010%20connections%20using%20connection%20string%20parameter%20%22%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3EMax%20Pool%20Size%3D10%3C%2FSTRONG%3E%3C%2FFONT%3E%22%3C%2FP%3E%0A%3CPRE%3EServer%3Dtcp%3ASERVERNAME.database.windows.net%2C1433%3BInitial%20Catalog%3Dsandbox%3B%3CBR%20%2F%3EPersist%20Security%20Info%3DFalse%3BUser%20ID%3DXXXXXXXXX%3BPassword%3DXXXXXXXXX%3B%3CBR%20%2F%3EEncrypt%3DTrue%3BTrustServerCertificate%3DFalse%3BConnection%20Timeout%3D30%3B%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3EMax%20Pool%20Size%3D10%3B%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FPRE%3E%0A%3CP%3EAnd%20I%20get%20error%20like%20below%3C%2FP%3E%0A%3CPRE%3ETimeout%20expired.%20The%20timeout%20period%20elapsed%20prior%20to%20obtaining%20a%20connection%20from%20the%20pool.%20%3CBR%20%2F%3EThis%20may%20have%20occurred%20because%20%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3Eall%20pooled%20connections%20were%20in%20use%20and%20max%20pool%20size%20was%20reached%3C%2FSTRONG%3E%3C%2FFONT%3E.%3C%2FPRE%3E%0A%3CP%3EAs%20the%20message%20says%20its%20a%20connection%20pool%20setting%2C%20not%20an%20Azure%20SQL%20DB%20limit%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId--662835402%22%20id%3D%22toc-hId--660241666%22%3ESolution%3C%2FH3%3E%0A%3CP%3EIn%20this%20scenario%20you%20can%20just%20config%20your%20application%20to%20be%20able%20to%20open%20more%20connections%2C%20or%20avoid%20opening%20too%20many%20connections%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--673338506%22%20id%3D%22toc-hId--670744770%22%3E%3CFONT%20size%3D%225%22%3E2%20-%20Pre-login%20handshake%20error%20%2F%26nbsp%3B%3C%2FFONT%3E%3CFONT%20size%3D%225%22%3EWebApp%20high%20CPU%3C%2FFONT%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20this%20test%20used%20a%20%3CSTRONG%3ESTANDARD%20Tier%20(S1)%3C%2FSTRONG%3E%26nbsp%3Bfor%20the%20App%20Service%20Plan.%20Also%20sending%20parameter%20to%20Azure%20Function%20(HighCPU%20%3D%201)%20that%20will%20do%20some%20math%20just%20to%20increase%20CPU%20usage.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnd%20we%20may%20get%20an%20error%20like%20sample%20below%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22log-content%20info-content%22%3E2020-05-08T21%3A49%3A23.749%20%5BInformation%5D%20Executing%20'Function_ConnectSQL_PoolLimit'%20(Reason%3D'This%20function%20was%20programmatically%20called%20via%20the%20host%20APIs.'%2C%20Id%3D2fcc3b09-e3e4-4125-a615-282be3d76f7d)%3CBR%20%2F%3E...%3CBR%20%2F%3E2020-05-08T21%3A49%3A34.527%20%5BInformation%5D%20Loop%201078%3CBR%20%2F%3E2020-05-08T21%3A49%3A34.528%20%5BInformation%5D%20Loop%201079%3CBR%20%2F%3E2020-05-08T21%3A49%3A34.528%20%5BInformation%5D%20Loop%201080%3CBR%20%2F%3E2020-05-08T21%3A49%3A34.528%20%5BInformation%5D%20Loop%201081%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%3ELog%20output%20threshold%20exceeded.%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%3E2020-05-08T21%3A49%3A41.141%20%5BError%5D%202020-05-08%2009%3A49%3A24%20CONNECTION%20ERROR%20-%20%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%20color%3D%22%23FF0000%22%3E%3CSTRONG%3EConnection%20Timeout%20Expired.%20The%20timeout%20period%20elapsed%20while%20attempting%20to%20consume%20the%20pre-login%20handshake%20acknowledgement.%3C%2FSTRONG%3E%20%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%3EThis%20could%20be%20because%20the%20pre-login%20handshake%20failed%20or%20the%20server%20was%20unable%20to%20respond%20back%20in%20time.%20%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%3EThe%20duration%20spent%20while%20attempting%20to%20connect%20to%20this%20server%20was%20-%20%5BPre-Login%5D%20%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3Einitialization%3D5895%3C%2FSTRONG%3E%3C%2FFONT%3E%3B%20handshake%3D29%3B%3C%2FFONT%3E%3CBR%20%2F%3E%3CFONT%20size%3D%222%22%3E2020-05-08T21%3A49%3A41.249%20%5BInformation%5D%20Executed%20'Function_ConnectSQL_PoolLimit'%20(Succeeded%2C%20Id%3D2fcc3b09-e3e4-4125-a615-282be3d76f7d)%3C%2FFONT%3E%3C%2FPRE%3E%0A%3CP%3E%3CLI-WRAPPER%3E%3C%2FLI-WRAPPER%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20error%20may%20have%20many%20reasons%2C%20but%20the%20most%20common%20scenario%20is%20the%3CSTRONG%3E%20WebApp%20have%20with%20CPU%20so%20HIGH%20that%20it%20does%20not%20have%20CPU%20power%20even%20to%20process%20this%20connection%20fast%20enough%3C%2FSTRONG%3E.%20If%20your%20client%20cannot%20complete%20a%20connection%20in%20%3CSTRONG%3EMax%205%20sec%3C%2FSTRONG%3E%20it%20may%20fail%20with%20the%20error%20below%2C%20because%20this%20is%20how%20the%20Azure%20SQL%20Gateway%20was%20designed%20and%20we%20can%20check%20the%20time%20that%20took%20based%20on%20the%20message%20%22%3CFONT%20size%3D%222%22%3E%5BPre-Login%5D%20%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3Einitialization%3D5895%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FFONT%3E%22%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20further%20investigation%26nbsp%3Byou%20can%20go%20to%20your%20Azure%20Function%26nbsp%3B%3CSTRONG%3EApplication%20Service%20Plan%20%26gt%3B%20Metrics%3C%2FSTRONG%3E%20and%20look%20for%20%3CSTRONG%3ECPU%20Percentage%20(MAX)%3C%2FSTRONG%3E%20and%20check%20for%20long%20CPU%20peaks%20that%20match%20your%20errors%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-08%2022_54_36-Clipboard.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F190280i9601EEFB181B7022%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-05-08%2022_54_36-Clipboard.png%22%20alt%3D%222020-05-08%2022_54_36-Clipboard.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EIn%20this%20case%20is%20just%20a%20test%20scenario%20with%20just%20one%20request%20making%20all%20the%20pressure%2C%20on%20production%20workloads%20you%20may%20find%20high%20cpu%20for%20longer%20periods%20of%20workloads%20in%20a%20production%20environment.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20also%20capture%20a%20%3CSTRONG%3Enetwork%20trace%3C%2FSTRONG%3E%20from%20the%20WebApp%20you%20can%20see%20how%20long%20the%20connection%20time%20took%3C%2FP%3E%0A%3CP%3EJust%20go%20to%20%3CSTRONG%3EDiagnose%20and%20solve%20problems%20%26gt%3B%20Collect%20Network%20trace%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-12%2017_36_50-Window.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F191023iA4444B4C3979D8E4%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-05-12%2017_36_50-Window.png%22%20alt%3D%222020-05-12%2017_36_50-Window.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EChange%20the%20period%20of%20time%20the%20collection%20will%20run%20and%20repro%20the%20issue%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-12%2017_37_43-Window.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F191022i1B56E174FD645DC9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-05-12%2017_37_43-Window.png%22%20alt%3D%222020-05-12%2017_37_43-Window.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EAfter%20that%20you%20can%20just%20download%20it%20from%20Kudu%20Console%20as%20explained%20in%20the%20window%20above%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-13%2018_02_43-Window.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F191409i477B3D7792D1E069%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-05-13%2018_02_43-Window.png%22%20alt%3D%222020-05-13%2018_02_43-Window.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThen%20you%20can%20open%20it%20on%20Netmon%20or%20Wireshark%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20issue%20could%20have%20been%20caused%20my%20some%20other%20reasons%20like%20network%20and%26nbsp%3BIf%20we%20look%20on%20just%20this%20trace%20it%20looks%20like%20it%20that%20it%20just%20took%20a%20long%20time%20for%20reply%2C%20and%20if%20you%20are%20unsure%2C%20check%20for%20CPU%20as%20mentioned%20above%2C%20or%20open%20a%20support%20request%20for%20further%20investigation.%20But%20in%20this%20trace%20we%20can%20see%20that%20the%20initial%20connection%20took%20more%20than%205%20sec%2C%20and%20that's%20why%20it%20was%20disconnected.%3C%2FP%3E%0A%3CP%3EFor%20this%20second%20test%20the%20error%20message%20was%20%22%3CFONT%20size%3D%222%22%20color%3D%22%23FF0000%22%3E%3CSTRONG%3E%5BPre-Login%5D%20initialization%3D5555%3B%20handshake%3D14%3B%3C%2FSTRONG%3E%3C%2FFONT%3E%22%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-12%2017_56_57-Window.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F191024i8882418799AD948C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-05-12%2017_56_57-Window.png%22%20alt%3D%222020-05-12%2017_56_57-Window.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-17222968%22%20id%3D%22toc-hId-19816704%22%3ESolution%3C%2FH3%3E%0A%3CP%3EIn%20this%20scenario%20a%20quick%20mitigation%20is%20just%20scale%20up%20the%20Azure%20Function%20to%20higher%20tier%20to%20have%20more%20CPU.%20And%20you%20may%20also%20investigate%20why%20the%20CPU%20got%20so%20high%20like%20this%20huge%20loop%20I%20created.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-6719864%22%20id%3D%22toc-hId-9313600%22%3E3%20-%20Azure%20SQL%20DB%20connection%20limit%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20application%20%2F%20CPU%20limit%20is%20not%20a%20problem%2C%20lets%20change%20parameter%20to%20try%20to%20open%20now%205000%20connections.%26nbsp%3B%22%3CFONT%20color%3D%22%230000FF%22%3E%3CSTRONG%3EMax%20Pool%20Size%3D5000%3C%2FSTRONG%3E%3C%2FFONT%3E%22%20and%20I%20may%20get%20error%20like%20below%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EResource%20ID%20%3A%202.%20%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3EThe%20session%20limit%20for%20the%20database%20is%20300%20and%20has%20been%20reached.%3C%2FSTRONG%3E%20%3C%2FFONT%3E%3CBR%20%2F%3ESee%20'http%3A%2F%2Fgo.microsoft.com%2Ffwlink%2F%3FLinkId%3D267637'%20for%20assistance.%3CSPAN%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CP%3EIn%20this%20scenario%20I'm%20using%20a%20%22%3CSTRONG%3EBasic%3C%2FSTRONG%3E%22%20SQL%20DB%20database%20that%20have%20limit%20of%20%3CSTRONG%3Emax%20300%20sessions%3C%2FSTRONG%3E%20because%20of%20number%20of%20cores%20available%20that%20can%20handle%20a%20specific%20number%20of%20connections%20as%20documented%20at%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-dtu-resource-limits-single-databases%23basic-service-tier%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CFONT%20size%3D%222%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fsql-database%2Fsql-database-dtu-resource-limits-single-databases%23basic-service-tier%3C%2FFONT%3E%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%20class%3D%22table%22%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%3E%3CFONT%20size%3D%222%22%3E%3CSTRONG%3ECompute%20size%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FTH%3E%0A%3CTH%3E%3CFONT%20size%3D%222%22%3E%3CSTRONG%3EBasic%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E%3CFONT%20size%3D%222%22%3EMax%20DTUs%3C%2FFONT%3E%3C%2FTD%3E%0A%3CTD%3E%3CFONT%20size%3D%222%22%3E5%3C%2FFONT%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CFONT%20size%3D%222%22%3EIncluded%20storage%20(GB)%3C%2FFONT%3E%3C%2FTD%3E%0A%3CTD%3E%3CFONT%20size%3D%222%22%3E2%3C%2FFONT%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CFONT%20size%3D%222%22%3EMax%20storage%20choices%20(GB)%3C%2FFONT%3E%3C%2FTD%3E%0A%3CTD%3E%3CFONT%20size%3D%222%22%3E2%3C%2FFONT%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CFONT%20size%3D%222%22%3EMax%20in-memory%20OLTP%20storage%20(GB)%3C%2FFONT%3E%3C%2FTD%3E%0A%3CTD%3E%3CFONT%20size%3D%222%22%3EN%2FA%3C%2FFONT%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CFONT%20size%3D%222%22%3EMax%20concurrent%20workers%20(requests)%3C%2FFONT%3E%3C%2FTD%3E%0A%3CTD%3E%3CFONT%20size%3D%222%22%3E30%3C%2FFONT%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E%3CSTRONG%3E%3CFONT%20size%3D%222%22%20color%3D%22%230000FF%22%3EMax%20concurrent%20sessions%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3CTD%3E%3CSTRONG%3E%3CFONT%20size%3D%222%22%20color%3D%22%230000FF%22%3E300%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-697281338%22%20id%3D%22toc-hId-699875074%22%3ESolution%3C%2FH3%3E%0A%3CP%3EIn%20this%20scenario%20or%20you%20need%20to%20control%20the%20number%20of%20connections%20in%20the%20application%20side%2C%20or%20if%20you%20really%20need%20more%20concurrent%20session%20you%20need%20to%20scale%20up%20to%20higher%20tier.%20As%20a%20sample%20if%20you%20use%20%3CSTRONG%3ES9%20it%20can%20handle%2030.000%20sessions.%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-686778234%22%20id%3D%22toc-hId-689371970%22%3E%3CSTRONG%3E4%20-%20SNAT%20Port%26nbsp%3B%3C%2FSTRONG%3E%3CSTRONG%3EExhaustion%3C%2FSTRONG%3E%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20that%20you%20application%20have%20enough%20CPU%20and%20Database%20Tier%20can%20handle%20more%20concurrent%20session%20we%20may%20hit%20on%20another%20limit%3A%20The%20%3CSTRONG%3Enumber%20of%20TCP%20Ports%20open%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CLI-WRAPPER%3E%3C%2FLI-WRAPPER%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E2020-05-08T10%3A21%3A59.754%20%5BInformation%5D%20Executing%20'Function_ConnectSQL_PoolLimit'%20(Reason%3D'This%20function%20was%20programmatically%20called%20via%20the%20host%20APIs.'%2C%20Id%3D4f62abab-6369-42fc-bbd3-a818e5a54b57)%20%3CBR%20%2F%3E...%0A2020-05-08T10%3A22%3A05.342%20%5BInformation%5D%20Loop%201656%3CBR%20%2F%3E2020-05-08T10%3A22%3A05.365%20%5BInformation%5D%20Loop%201657%3CBR%20%2F%3E2020-05-08T10%3A22%3A05.392%20%5BInformation%5D%20Loop%201658%3CBR%20%2F%3E2020-05-08T10%3A22%3A05.417%20%5BInformation%5D%20Loop%201659%3CBR%20%2F%3E2020-05-08T10%3A22%3A26.486%20%5BError%5D%202020-05-08%2010%3A21%3A59%20CONNECTION%20ERROR%20-%20%3CBR%20%2F%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3EA%20network-related%20or%20instance-specific%20error%20occurred%20while%20establishing%20a%20connection%20to%20SQL%20Server.%3C%2FSTRONG%3E%20%3C%2FFONT%3E%3CBR%20%2F%3E%3CSTRONG%3E%3CFONT%20color%3D%22%23FF0000%22%3EThe%20server%20was%20not%20found%20or%20was%20not%20accessible.%3C%2FFONT%3E%3C%2FSTRONG%3E%20Verify%20that%20the%20instance%20name%20is%20correct%20and%20that%20SQL%20Server%20is%20configured%20to%20allow%20remote%20connections.%20%3CBR%20%2F%3E(provider%3A%20TCP%20Provider%2C%20error%3A%200%20-%20A%20connection%20attempt%20failed%20because%20the%20connected%20party%20did%20not%20properly%20respond%20after%20a%20period%20of%20time%2C%20or%20established%20connection%20failed%20because%20connected%20host%20has%20failed%20to%20respond.)%3CBR%20%2F%3E2020-05-08T10%3A22%3A26.512%20%5BInformation%5D%20Executed%20'Function_ConnectSQL_PoolLimit'%20(Succeeded%2C%20Id%3D4f62abab-6369-42fc-bbd3-a818e5a54b57)%0A%3C%2FPRE%3E%0A%3CP%3E%3CLI-WRAPPER%3E%3C%2FLI-WRAPPER%3E%3C%2FP%3E%0A%3CDIV%20class%3D%22log-content%20info-content%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CLI-WRAPPER%3E%3C%2FLI-WRAPPER%3E%3C%2FP%3E%0A%3CP%3EThe%20message%20in%20this%20case%20is%20not%20clear.%26nbsp%3BIt%20looks%20like%20a%20%3CSTRONG%3Enetwork%20issue%3C%2FSTRONG%3E%20or%20an%26nbsp%3B%3CSTRONG%3EAzure%20SQL%20DB%20issue%3C%2FSTRONG%3E%20as%20the%20message%20is%20%22%3CSTRONG%3Eserver%20was%20not%20found%3C%2FSTRONG%3E%22%2C%20but%20lets%20look%20deeper.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20we%20look%20on%20Azure%20SQL%20Resource%20Health%20we%20can%20see%20no%20issues%20on%208th%20May%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-13%2013_59_12-Window.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F191317i6B28537ECE9FB447%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-05-13%2013_59_12-Window.png%22%20alt%3D%222020-05-13%2013_59_12-Window.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFrom%20the%20Azure%20Function%20we%20can%20check%20how%20many%20connection%20were%20opened%20using%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EFunction%20App%20%26gt%3B%20Diagnose%20and%20Solve%20Problems%20%26gt%3B%26nbsp%3B%3CSTRONG%3ETCP%20Connections%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-13%2015_10_01-Window.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F191345i2D1DA5AD1A919AF7%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%222020-05-13%2015_10_01-Window.png%22%20alt%3D%222020-05-13%2015_10_01-Window.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20the%20image%20below%20we%20can%20see%20the%20%3CSTRONG%3Enumber%20of%20outbound%20connections%3C%2FSTRONG%3E%20at%20some%20point%20and%20we%20can%20all%20see%20what%20endpoint%20was%20reached%20in%20this%20case%20%3CSTRONG%3E104.40.155.247%3A11054%3C%2FSTRONG%3E.%20Azure%20SQL%20uses%20port%201433%20for%20the%20gateway%20and%20ports%2011000-11999%20for%20the%20SQL%20that%20hosts%20your%20database.%20More%20info%20at%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-support-blog%2Fazure-sql-db-connectivity-troubleshooting%2Fba-p%2F1158677%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Ehttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-support-blog%2Fazure-sql-db-connectivity-troubleshooting%2Fba-p%2F1158677%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-13%2015_03_17-Window.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F191339i330F59FA06977CDC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-05-13%2015_03_17-Window.png%22%20alt%3D%222020-05-13%2015_03_17-Window.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20how%20to%20be%20sure%20that%20you%20hit%20the%26nbsp%3B%3CSTRONG%3ESNAT%20Port%20Exhaustion%3C%2FSTRONG%3E%26nbsp%3Bproblem%20in%20this%20scenario%2C%20follow%20the%20procedure%20below%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EGo%20to%20Azure%20Function%20%26gt%3B%26nbsp%3B%3CSTRONG%3EDiagnose%20and%20solve%20problems%20%26gt%3B%26nbsp%3BLook%20for%20SNAT%20Port%20Exhaustion.%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23FF0000%22%3E%3CSTRONG%3E*WAIT%20SOME%20MINUTES%20BEFORE%20CHEKING%20IT.%20On%20My%20tests%20the%20data%20didn%C2%B4t%20load%20right%20after%20the%20issue%20happened%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-08%2011_08_53-Clipboard.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F190026iF0F9BC83F8EE1327%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-05-08%2011_08_53-Clipboard.png%22%20alt%3D%222020-05-08%2011_08_53-Clipboard.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ELook%20for%20SNAT%20Port%20Exhaustion%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-08%2011_09_33-Clipboard.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F190027iD7A9466EF7660465%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%222020-05-08%2011_09_33-Clipboard.png%22%20alt%3D%222020-05-08%2011_09_33-Clipboard.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E*NOTE%3A%20this%20will%20%3CSTRONG%3Enot%20be%20available%20for%20Free%20App%20Service%20Plan%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-08%2011_10_03-FunctionAppTestConnection%20-%20Microsoft%20Azure.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F190028i6322A94EB29DA68A%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%222020-05-08%2011_10_03-FunctionAppTestConnection%20-%20Microsoft%20Azure.png%22%20alt%3D%222020-05-08%2011_10_03-FunctionAppTestConnection%20-%20Microsoft%20Azure.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20can%20see%20in%20the%20report%20that%20%3CSTRONG%3Esome%20ports%20failed%20to%20open%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%222020-05-08%2012_04-SNAT.png%22%20style%3D%22width%3A%20487px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F190036iDBB1B0FDDF6367EF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%222020-05-08%2012_04-SNAT.png%22%20alt%3D%222020-05-08%2012_04-SNAT.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1377339708%22%20id%3D%22toc-hId-1379933444%22%3ESolution%3C%2FH3%3E%0A%3CDIV%3EThe%20best%20practice%20is%20to%20%3CSTRONG%3Ekeep%20it%20below%20128%20SNAT%20port%20each%20instance%3C%2FSTRONG%3E.%20Here%20is%20how%20it%20is%20calculated%20%3CA%20tabindex%3D%22-1%22%20title%3D%22https%3A%2F%2F4lowtherabbit.github.io%2Fblogs%2F2019%2F10%2Fsnat%2F%22%20href%3D%22https%3A%2F%2F4lowtherabbit.github.io%2Fblogs%2F2019%2F10%2FSNAT%2F%22%20target%3D%22_blank%22%20rel%3D%22noreferrer%20noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2F4lowtherabbit.github.io%2Fblogs%2F2019%2F10%2FSNAT%2F%3C%2FA%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3ESpecific%20about%20Azure%20Function%20it%20may%20be%20able%20to%20auto%20scale%20the%20number%20of%20instances%20as%20documented%20at%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-functions%2Ffunctions-scale%23runtime-scaling%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-functions%2Ffunctions-scale%23runtime-scaling%3C%2FA%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3EBut%20in%20this%20scenario%20you%20can%20quickly%20mitigate%20scaling%20up%20your%20App%20Service%20plan%20and%20you%20need%20to%20understand%20why%20so%20many%20connections%20are%20open%20at%20same%20time.%20Use%20connection%20pooling%20to%20reuse%20connections%20and%20follow%20some%20.NET%20best%20practices%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECheck%20more%20information%20below%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2F4lowtherabbit.github.io%2Fblogs%2F2019%2F10%2FSNAT%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2F4lowtherabbit.github.io%2Fblogs%2F2019%2F10%2FSNAT%2F%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fload-balancer%2Fload-balancer-outbound-connections%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fload-balancer%2Fload-balancer-outbound-connections%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fazure.github.io%2FAppService%2F2018%2F03%2F01%2FDeep-Dive-into-TCP-Connections-in-App-Service-Diagnostics.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fazure.github.io%2FAppService%2F2018%2F03%2F01%2FDeep-Dive-into-TCP-Connections-in-App-Service-Diagnostics.html%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-functions%2Fmanage-connections%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-functions%2Fmanage-connections%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1369218%22%20slang%3D%22en-US%22%3E%3CP%3EWhen%20supporting%20Azure%20SQL%20DB%20Connectivity%20case%20we%20sometimes%20receive%20errors%20message%20saying%20that%20was%20an%20Azure%20SQL%20DB%20issue%2C%20but%20sometimes%20this%20is%20just%20an%20application%20problem%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1369218%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzureSQLDB%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Econnectivity%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

When supporting Azure SQL DB Connectivity case we sometimes receive errors message saying that was an Azure SQL DB issue, but sometimes this is just an application problem.

 

We as Database Administrators do not need to be WebApp experts but we need to have some tools to check if issue is on Azure SQL DB or on the client side

 

I've created an Azure Function sample code for testing https://github.com/FonsecaSergio/AzureFunctionAppTestConnectivity (Function_ConnectSQL_PoolLimit) and in this test I will just open multiple connection in a while loop. This troubleshooting steps should be similar also to a WebApp.

 

while (Aux++ < LoopLimit)
{
log.LogInformation(String.Format("Loop {0}", Aux));
SqlConnection sqlConnection2 = new SqlConnection(ConnectionString);
sqlConnection2.Open();
}

 

So lets start breaking the limits in the scenarios below:

1 - Application connection pool setting

2 - Pre-login handshake error / WebApp high CPU

3 - Azure SQL DB connection limit

4 - SNAT Port Exhaustion

 

 

1 - Application connection pool setting

 

For this test I'll limit the application pool limit to 10 connections using connection string parameter "Max Pool Size=10"

Server=tcp:SERVERNAME.database.windows.net,1433;Initial Catalog=sandbox;
Persist Security Info=False;User ID=XXXXXXXXX;Password=XXXXXXXXX;
Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Max Pool Size=10;

And I get error like below

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. 
This may have occurred because all pooled connections were in use and max pool size was reached.

As the message says its a connection pool setting, not an Azure SQL DB limit

 

Solution

In this scenario you can just config your application to be able to open more connections, or avoid opening too many connections

 

2 - Pre-login handshake error / WebApp high CPU

 

For this test used a STANDARD Tier (S1) for the App Service Plan. Also sending parameter to Azure Function (HighCPU = 1) that will do some math just to increase CPU usage.

 

And we may get an error like sample below

 

2020-05-08T21:49:23.749 [Information] Executing 'Function_ConnectSQL_PoolLimit' (Reason='This function was programmatically called via the host APIs.', Id=2fcc3b09-e3e4-4125-a615-282be3d76f7d)
...
2020-05-08T21:49:34.527 [Information] Loop 1078
2020-05-08T21:49:34.528 [Information] Loop 1079
2020-05-08T21:49:34.528 [Information] Loop 1080
2020-05-08T21:49:34.528 [Information] Loop 1081
Log output threshold exceeded.
2020-05-08T21:49:41.141 [Error] 2020-05-08 09:49:24 CONNECTION ERROR -
Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement.
This could be because the pre-login handshake failed or the server was unable to respond back in time.
The duration spent while attempting to connect to this server was - [Pre-Login] initialization=5895; handshake=29;
2020-05-08T21:49:41.249 [Information] Executed 'Function_ConnectSQL_PoolLimit' (Succeeded, Id=2fcc3b09-e3e4-4125-a615-282be3d76f7d)

 

This error may have many reasons, but the most common scenario is the WebApp have with CPU so HIGH that it does not have CPU power even to process this connection fast enough. If your client cannot complete a connection in Max 5 sec it may fail with the error below, because this is how the Azure SQL Gateway was designed and we can check the time that took based on the message "[Pre-Login] initialization=5895"

 

For further investigation you can go to your Azure Function Application Service Plan > Metrics and look for CPU Percentage (MAX) and check for long CPU peaks that match your errors

2020-05-08 22_54_36-Clipboard.png

In this case is just a test scenario with just one request making all the pressure, on production workloads you may find high cpu for longer periods of workloads in a production environment.

 

You can also capture a network trace from the WebApp you can see how long the connection time took

Just go to Diagnose and solve problems > Collect Network trace

2020-05-12 17_36_50-Window.png

 

Change the period of time the collection will run and repro the issue

2020-05-12 17_37_43-Window.png

After that you can just download it from Kudu Console as explained in the window above

2020-05-13 18_02_43-Window.png

Then you can open it on Netmon or Wireshark

 

This issue could have been caused my some other reasons like network and If we look on just this trace it looks like it that it just took a long time for reply, and if you are unsure, check for CPU as mentioned above, or open a support request for further investigation. But in this trace we can see that the initial connection took more than 5 sec, and that's why it was disconnected.

For this second test the error message was "[Pre-Login] initialization=5555; handshake=14;"

2020-05-12 17_56_57-Window.png

 

Solution

In this scenario a quick mitigation is just scale up the Azure Function to higher tier to have more CPU. And you may also investigate why the CPU got so high like this huge loop I created.

 

3 - Azure SQL DB connection limit

 

If application / CPU limit is not a problem, lets change parameter to try to open now 5000 connections. "Max Pool Size=5000" and I may get error like below

 

Resource ID : 2. The session limit for the database is 300 and has been reached. 
See 'http://go.microsoft.com/fwlink/?LinkId=267637' for assistance.

In this scenario I'm using a "Basic" SQL DB database that have limit of max 300 sessions because of number of cores available that can handle a specific number of connections as documented at 

https://docs.microsoft.com/en-us/azure/sql-database/sql-database-dtu-resource-limits-single-database...

 

Compute size Basic
Max DTUs 5
Included storage (GB) 2
Max storage choices (GB) 2
Max in-memory OLTP storage (GB) N/A
Max concurrent workers (requests) 30
Max concurrent sessions 300

 

Solution

In this scenario or you need to control the number of connections in the application side, or if you really need more concurrent session you need to scale up to higher tier. As a sample if you use S9 it can handle 30.000 sessions.

 

4 - SNAT Port Exhaustion

 

Now that you application have enough CPU and Database Tier can handle more concurrent session we may hit on another limit: The number of TCP Ports open

 

2020-05-08T10:21:59.754 [Information] Executing 'Function_ConnectSQL_PoolLimit' (Reason='This function was programmatically called via the host APIs.', Id=4f62abab-6369-42fc-bbd3-a818e5a54b57) 
... 2020-05-08T10:22:05.342 [Information] Loop 1656
2020-05-08T10:22:05.365 [Information] Loop 1657
2020-05-08T10:22:05.392 [Information] Loop 1658
2020-05-08T10:22:05.417 [Information] Loop 1659
2020-05-08T10:22:26.486 [Error] 2020-05-08 10:21:59 CONNECTION ERROR -
A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: TCP Provider, error: 0 - A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)
2020-05-08T10:22:26.512 [Information] Executed 'Function_ConnectSQL_PoolLimit' (Succeeded, Id=4f62abab-6369-42fc-bbd3-a818e5a54b57)

 

The message in this case is not clear. It looks like a network issue or an Azure SQL DB issue as the message is "server was not found", but lets look deeper. 

 

If we look on Azure SQL Resource Health we can see no issues on 8th May

 

2020-05-13 13_59_12-Window.png

 

From the Azure Function we can check how many connection were opened using:

  • Function App > Diagnose and Solve Problems > TCP Connections

2020-05-13 15_10_01-Window.png

 

In the image below we can see the number of outbound connections at some point and we can all see what endpoint was reached in this case 104.40.155.247:11054. Azure SQL uses port 1433 for the gateway and ports 11000-11999 for the SQL that hosts your database. More info at https://techcommunity.microsoft.com/t5/azure-database-support-blog/azure-sql-db-connectivity-trouble...

 

2020-05-13 15_03_17-Window.png

 

 

But how to be sure that you hit the SNAT Port Exhaustion problem in this scenario, follow the procedure below

 

Go to Azure Function > Diagnose and solve problems > Look for SNAT Port Exhaustion.

 

*WAIT SOME MINUTES BEFORE CHEKING IT. On My tests the data didn´t load right after the issue happened

 

2020-05-08 11_08_53-Clipboard.png

Look for SNAT Port Exhaustion

2020-05-08 11_09_33-Clipboard.png

*NOTE: this will not be available for Free App Service Plan

2020-05-08 11_10_03-FunctionAppTestConnection - Microsoft Azure.png

 

We can see in the report that some ports failed to open

2020-05-08 12_04-SNAT.png

 

Solution

The best practice is to keep it below 128 SNAT port each instance. Here is how it is calculated https://4lowtherabbit.github.io/blogs/2019/10/SNAT/
 
Specific about Azure Function it may be able to auto scale the number of instances as documented at https://docs.microsoft.com/en-us/azure/azure-functions/functions-scale#runtime-scaling
 

But in this scenario you can quickly mitigate scaling up your App Service plan and you need to understand why so many connections are open at same time. Use connection pooling to reuse connections and follow some .NET best practices

 

Check more information below: