다음을 통해 공유


동시성 모델(Windows Server AppFabric 캐싱)

Windows Server AppFabric 아키텍처를 사용하면 캐시 클라이언트에 적절한 네트워크 액세스와 구성 설정이 있는 경우 해당 클라이언트가 캐시된 데이터에 공개적으로 액세스할 수 있습니다. 이로 인해 보안과 동시성에 모두 문제가 제기됩니다.

보안 위험을 줄이려면 모든 캐시 클라이언트, 캐시 서버 및 주 데이터 원본 서버가 동일한 도메인의 구성원이고 방화벽 경계 내에 배포되어야 합니다. 또한 캐시 클라이언트에서 응용 프로그램 구성 파일을 보호해야 합니다.

응용 프로그램의 동시성 문제 처리에 도움이 되도록 AppFabric은 낙관적 및 비관적 동시성 모델을 지원합니다. 이러한 모델에 맞출 수 있는 메서드에 대한 자세한 내용은 동시성 메서드(Windows Server AppFabric 캐싱)를 참조하십시오.

낙관적 동시성 모델

낙관적 동시성 모델에서 캐시된 개체에 대한 업데이트는 잠기지 않습니다. 대신에 캐시 클라이언트는 캐시에서 개체를 가져올 때 해당 개체의 현재 버전도 가져와서 저장합니다. 업데이트가 필요한 경우 캐시 클라이언트는 개체의 새 값을 저장된 버전 개체와 함께 보냅니다. 전송된 버전이 캐시에 있는 개체의 현재 버전과 일치하는 경우에만 개체가 업데이트됩니다. 개체에 대한 모든 업데이트는 해당 버전 번호를 변경하므로 다른 사용자의 변경 내용을 덮어쓰지 않습니다.

이 항목의 예제에서는 낙관적 동시성이 데이터 일관성을 유지하는 방법에 대해 설명합니다.

예제

이 예제에서는 2개의 개별 응용 프로그램 서버의 2개 캐시 클라이언트(cacheClientAcacheClientB)가 RadioInventory라는 동일한 캐시된 개체를 업데이트하려고 합니다.

시간 0: 2개 클라이언트가 모두 동일한 개체 검색

시간 0(T0)에서 2개 캐시 클라이언트가 모두 DataCacheItem 클래스를 인스턴스화하여 업데이트하려는 캐시된 개체를 버전 및 태그 정보 등의 캐시된 해당 개체에 연결된 추가 정보와 함께 캡처합니다. 이 내용은 다음 다이어그램과 코드 예제에 설명되어 있습니다.

T0의 "속도" 동시성 모델

'cacheClientA pulls the FM radio inventory from cache
Dim clientACacheFactory As DataCacheFactory = New DataCacheFactory()
Dim cacheClientA As DataCache = _
        clientACacheFactory.GetCache("catalog")
Dim radioInventoryA As DataCacheItem = _
        cacheClientA.GetCacheItem("RadioInventory", "electronics")

'cacheClientB pulls the same FM radio inventory from cache
Dim clientBCacheFactory As DataCacheFactory = New DataCacheFactory()
Dim cacheClientB As DataCache = _
       clientBCacheFactory.GetCache("catalog")
Dim radioInventoryB As DataCacheItem = _
        cacheClientA.GetCacheItem("RadioInventory", "electronics")
//cacheClientA pulls the FM radio inventory from cache
DataCacheFactory clientACacheFactory = new DataCacheFactory();
DataCache cacheClientA = clientACacheFactory.GetCache("catalog");
DataCacheItem radioInventoryA = 
    cacheClientA.GetCacheItem("RadioInventory","electronics");

//cacheClientB pulls the same FM radio inventory from cache
DataCacheFactory clientBCacheFactory = new DataCacheFactory();
DataCache cacheClientB = clientBCacheFactory.GetCache("catalog");
DataCacheItem radioInventoryB= 
    cacheClientA.GetCacheItem("RadioInventory", "electronics");

참고

이 예제에서는 GetCacheItem 메서드를 통해 버전 정보를 가져와서 DataCacheItem 개체를 검색하지만 Get 메서드를 사용하여 검색된 캐시 항목과 연결된 DataCacheItemVersion 개체를 검색할 수도 있습니다.

시간 1: 첫 번째 업데이트 성공

시간 1(T1)에서 cacheClientA는 캐시된 개체 RadioInventory를 새 값으로 업데이트합니다. cacheClientAPut 메서드를 실행하면 RadioInventory 캐시 항목과 연결된 버전이 증분됩니다. 이때 cacheClientB에는 오래된 캐시 항목이 포함되어 있습니다. 이 내용은 다음 다이어그램과 코드 예제에 설명되어 있습니다.

T1의 "속도" 동시성 모델

'at time T1, cacheClientA updates the FM radio inventory
Dim newRadioInventoryA As Integer = 155

cacheClientA.Put("RadioInventory", newRadioInventoryA, _
                 radioInventoryA.Version, "electronics")
//at time T1, cacheClientA updates the FM radio inventory
int newRadioInventoryA = 155;

cacheClientA.Put("RadioInventory", newRadioInventoryA, 
    radioInventoryA.Version,"electronics");

시간 2: 두 번째 업데이트 실패

시간 2(T2)에서 cacheClientB는 이제 오래된 버전 번호를 사용하여 RadioInventory 캐시된 개체를 업데이트하려고 합니다. cacheClientA의 변경 내용을 덮어쓰지 않도록 cacheClientBPut 메서드 호출이 실패합니다. 캐시 클라이언트에서는 ErrorCode 속성이 CacheItemVersionMismatch로 설정된 DataCacheException 개체를 발생시킵니다. 이 내용은 다음 다이어그램과 코드 예제에 설명되어 있습니다.

T2의 "속도" 동시성 모델

'later, at time T2, cacheClientB tries to 
'update the FM radio inventory, throws DataCacheException with
'an error code equal to DataCacheErrorCode.CacheItemVersionMismatch.
Dim newRadioInventoryB As Integer = 130

cacheClientB.Put("RadioInventory", newRadioInventoryB, _
                 radioInventoryB.Version, "electronics")
//later, at time T2, cacheClientB tries to 
//update the FM radio inventory, throws DataCacheException with
//an error code equal to DataCacheErrorCode.CacheItemVersionMismatch.
int newRadioInventoryB = 130;

cacheClientB.Put("RadioInventory", newRadioInventoryB,
    radioInventoryB.Version,"electronics");

비관적 동시성 모델

비관적 동시성 모델에서 클라이언트는 작업을 수행하기 위해 개체를 명시적으로 잠급니다. 잠금을 요청하는 다른 작업은 잠금이 해제될 때까지 거부됩니다(시스템이 요청을 차단하지 않음). 개체가 잠기면 잠금 핸들이 출력 매개 변수로 반환됩니다. 개체를 잠금 해제하려면 잠금 핸들이 필요합니다. 잠긴 개체가 해제되기 전에 클라이언트 응용 프로그램이 종료될 경우 잠금을 해제하기 위해 시간 제한이 제공됩니다. 잠김 개체는 만료되지 않지만 만료 시간이 경과된 경우 잠금 해제된 직후에 만료될 수 있습니다. 비관적 동시성 모델과 함께 사용되는 메서드에 대한 자세한 내용은 동시성 메서드(Windows Server AppFabric 캐싱)를 참조하십시오.

참고

작업을 포괄하는 트랜잭션은 지원되지 않습니다. 캐시를 사용하는 응용 프로그램은 잠금 순서를 결정하고 교착 상태(있는 경우)를 검색합니다.

경고

캐시의 잠긴 개체는 캐시 클라이언트에서 Put 메서드를 통해 바꿀 수 있습니다. 캐시 사용 응용 프로그램은 비관적 동시성 모델을 사용하는 항목에 대해 일관되게 PutAndUnlock을 사용합니다.

참고 항목

개념

동시성 메서드(Windows Server AppFabric 캐싱)
Windows Server AppFabric 캐싱 실제 아키텍처 다이어그램
Windows Server AppFabric 캐싱 논리 아키텍처 다이어그램
캐시 클라이언트 개발(Windows Server AppFabric 캐싱)

  2011-12-05