재우니의 블로그

ASP.NET MVC 의 connection timeout , connection lifetime 조사 및 알아보기

 

번역이 깔끔하지 않을 수 있습니다.

그리고 아래 connection lifetime 지정에 대해 서로 다르게 이야기 하는 부분이 있습니다.  ^^;

 

 

Case 1

 

질문..

 

https://stackoverflow.com/q/2748706

 

Connection Timeout and Connection Lifetime

What is the advantage and disadvantage of connection timeout=0? And what is the use of Connection Lifetime=0? e.g (Database=TestDB; port=3306; Uid=usernameID; Pwd=myPassword; Server=192.168...

stackoverflow.com

What is the advantage and disadvantage of connection timeout=0? And what is the use of Connection Lifetime=0? e.g (Database=TestDB; port=3306; Uid=usernameID; Pwd=myPassword; Server=192.168.10.1; Pooling=false; Connection Lifetime=0; Connection Timeout=0) and what is the use of Connection Pooling?

 

connection timeout = 0 일 경우, 장단점은 무엇일까?

그리고 connection lifetime=0 의 사용 용도가 뭔가요?

예를 들어..

(Database=TestDB; port=3306; Uid=usernameID; Pwd=myPassword; Server=192.168.10.1; Pooling=false; Connection Lifetime=0; Connection Timeout=0)

그리고.. connection pooling 의 사용 용도가 뭔가요?

 


 

답변

 

https://stackoverflow.com/a/2748741

 

Connection Timeout and Connection Lifetime

What is the advantage and disadvantage of connection timeout=0? And what is the use of Connection Lifetime=0? e.g (Database=TestDB; port=3306; Uid=usernameID; Pwd=myPassword; Server=192.168...

stackoverflow.com

 

Timeout is how long you wait for a response from a request before you give up. TimeOut=0 means you will keep waiting for the connection to occur forever. Good I guess if you are connecting to a really slow server that it is normal if it takes 12 hours to respond :-). Generally a bad thing. You want to put some kind of reasonable timeout on a request, so that you can realize your target is down and move on with your life.

 

timeout 은 해지하기 전에 요청(request) 에 대한 응답을 얼마나 대기하는지를 의미합니다. timeout=0 의 의미는 발생하는 connection 을  무한의 시간으로 ... 유지시키는 것을 의미합니다. 만약에 느린 서버에 연결한다면 응답하는데 12 시간이 걸리면 정상이라고 생각합니다. ㅎㅎㅎ 일반적으로 이는 좋지 않은 방법입니다. 그래서 여러분은 요청(request) 에 대한 timeout 의 적합한 시간을 할당해 주는것을 원할 겁니다.

 


 

Connection Lifetime
 
= how long a connection lives before it is killed and recreated. A lifetime of 0 means never kill and recreate. Normally not a bad thing, because killing and recreating a connection is slow. Through various bugs your connections may get stuck in an unstable state (like when dealing with weird 3 way transactions).. but 99% of the time it is good to keep connection lifetime as infinite.

 

Connection Lifetime = connection 이 끊기고 재생성되기 전에 연결되는 시간을 의미합니다. timeout 이 0이면 절대 죽이고 다시 만들지 않습니다. 연결을 종료하고 다시 만드는 속도가 느리기 때문에 일반적으로 이는 나쁜 것은 아닙니다. 

단, connection 에 대한 다양한 버그로 불안정한 상태에 빠질 수 있습니다 (예 : 이상한 3 방향 트랜잭션을 처리 할 때). 그러나 99 %의 시간 동안 연결 수명을 무한대로 유지하는 것이 좋습니다.

 


 

Connection pooling
 
is a way to deal with the fact that creating a connection is very slow. So rather than make a new connection for every request, instead have a pool of say, 10, premade connections. When you need one, you borrow one, use it, and return in. You can adjust the size of the pool to change how your app behaves. Bigger pool = more connections = more threads doing stuff at a time, but this could also overwhelm whatever you are doing.

 

Connection pooling  connection 생성하는게 매우 느려서 이를 사용하는데에 있어서 좋은 방법입니다. 따라서 매번 요청(request)에 ​​대해 새로운 connection 을 생성하기보다 이미 생성된 connection pool 을 사용하는게 좋습니다. 이는 필요한 경우 빌려서 사용하고 돌아옵니다. 풀(pool) 크기를 조정하여 앱의 동작(behaves) 방식을 변경할 수 있습니다.

pool 이 클수록 = 더 많은 connection들 = 더 많은 스레드들로 한 번에 작업을 수행하지만,  이로 인해 수행중인 작업이 부담될 수도 있습니다.

 


 

In summary:

ConnectionTimeout=0
 
is bad, make it something reasonable like 30 seconds.

ConnectionLifetime=0
 
is okay

ConnectionPooling=disabled
 
is bad, you will likely want to use it.

 

 

요약을 하자면,

  • ConnectionTimeout=0 은 좋지 않다. 합리적인 초단위(30초)로 명시해 주는게 좋다.
  • ConnectionLifetime=0 은 좋다.
  • ConnectionPooling=disabled 은 좋지 않다. pool 을 사용으로 활성화 해주는게 좋다.

 

 


Case 2

 

 

http://mssql.org/LectureSQL/31_csharp/20_ConnectionPooling2.htm

 

C#

강좌 전 아놀드의 잡담> 이번 시간에는 Connection Pooling 2번째 시간입니다. 첫 번째 시간에 무엇을 다루었는지, 별로 기억이 나진 않지만, 어쨌든 Connection Pool에 대해서 잠시 기억을 더듬어 보면 하나의 Process내에서 제공하는 database 연결에 대한 풀입니다. 여기서 중요한 것은 하나의 Process 내에서 제공한다는 점입니다. COM+ 가 Hosting 되는 서비스를 ASP.NET(IIS) 와 Windows Ser

mssql.org

부분발췌

 

  1. "Server=Arnold;uid=sa;pwd=nold;database=Northwind;"
  2. "Server=Arnold; Integrated Security=true;Database=NorthWind"
  3. "Server=Arnold: Integrated Security=true;Min Pool Size=1;Max Pool Size=100;" --기본설정값
  4. "Server=localhost;uid=sa;pwd=xxx;database=northwind;Connection LifeTime=20" -- lifetime 20초 지정

 

1번처럼 Default로 Connection Pool에 대해서 값을 명시하지 않은 경우 Connection에 대한 오류가 발생하지 않는다면 그대로 사용하셔도 상관없지만, Connection에 대한 Timeout (Connection에 대한 요청이 일정시간을 초과한 경우) 등의 문제가 발생하는 경우, 4번처럼 Connection LifeTime을 적용해 주시면 될 것 같습니다. 

LifeTime을 너무 짧게 주면 기존에 사용하던 Connection을 Close하고 새로운 연결이 pool에서 사용되기 때문에 피해야 하고, Connection Pool Time(4~7 min) 보다는 짧은 시간으로 주셔야 겠네요.

 

 


아래 질문은 pool size 는 최대 500 이며, connection lifetime 의 값을 지정할 경우 server 의 성능에 대한 문의 사항입니다.

 

질문 1

 

web.config에 지정된 연결 문자열에서 Max Pool size = 500 및 Connection Lifetime = 60 초를 설정하면 서버 성능에 미치는 영향을 이해하고 싶었습니다.

연결 최대 풀 크기와 연결 수명 = 60 초를 설정하는 것이 위험합니까? 기본 연결 최대 풀 크기 (예 : 200)에 대해 예외가 발생함에 따라이 구성을 추가 했으므로 Microsoft 포럼뿐만 아니라 다른 포럼에서 설명한대로 연결을 올바르게 닫을지라도 500으로 늘려야합니까?

 

https://stackoverflow.com/questions/46174179/performance-impact-on-sql-server-for-configuring-connection-lifetime-and-max-con

 

Performance Impact on sql server for configuring Connection Lifetime and Max Connection Pool values in connection string

I wanted to understand what will be the impact of performance on server if i set Max Pool size =500 and Connection Lifetime=60 seconds in connection string specified in web.config Is it dangerous ...

stackoverflow.com

답변 중 하나..

 

https://stackoverflow.com/questions/46174179/performance-impact-on-sql-server-for-configuring-connection-lifetime-and-max-con#comment79311472_46174179

 

If the connection lifetime is too long, you will unnecessarily hold physical connections open. If the connection lifetime is too short, you will create more TCP/IP connections than necessary, which slows down your application and can exhaust the ephemeral port range. I would err on the side of caution and not change the default lifetime at all, but if you do change it, adjusting it downwards is more likely to cause harm than upwards. Changing it has no impact on how many connections are available to your application, that's always covered by the pool size.

 

connection lifetime 이 매우 길면 물리적인 connectioin 연결이 불필요하게 유지하게 됩니다. 그렇다고 connection lifetime 이 매우 짧으면,  tcp/ip 의 필요 이상으로 많이 생성하게 되어 구현한 application 이 점점 느려지고 임시 포트 범위가 소진 될 수 있습니다. 이로 인해 개인적으로 매우 신중을 기해야 하므로 lifetime 기본값을 전혀 변경하지 않았습니다. 하지만 이를 변경하고자 할 경우, lifetime 을 짧게 조정하는 것은 크게 조정하는 것보다 문제를 발생할 수 있습니다. 이를 변경해도 응용 프로그램에서 사용 가능한 connection 수에는 영향을 미치지 않으며 항상 풀 Size(크기)로 적용됩니다.

 


 

주관적인 입장

 

 

connection lifetime 을 지정하느냐 아니면 그냥 기본값으로 둬서 mssql 이 알아서 connection 을 해지(close) 하도록 하는게 맞는지에 대해 서핑을 해봤는데, 정확하게 어떻게 해라는 것은 없고, 성능 모니터링 확인이 가능하면 advanced 하게 기본값을 변경하여 값을 조절해도 되고, 이것저것 잘 모르겠고 알아서 해주길 바란다면 기본값을 두는게 좋다네요.

그리고 min pool size 와 max pool size 지정은 직접 기재해 주는게 좋습니다. min pool size 는 기본이 0 이며, max pool size 는 100 입니다. 만약에 100 초과 되면 connection pool 에 대한 오류발생이 됩니다. 따라서 트래픽을 모니터링해서 많은 접속자가 있다면 max pool size 의 100 초과 숫자를 지정해 주시기 바랍니다.