public void Consume(Document document, string correlationId) { var threadSleep = 200; while (_lockDb.IsLocked(document.DocumentId, correlationId)) { Thread.Sleep(threadSleep); } while (!_lockDb.Lock(document.DocumentId, correlationId)) { _logger.Log($"CorrelationId: {correlationId} - Unable to acquire lock for document {document.DocumentId}, sleeping for {threadSleep} MS"); Thread.Sleep(threadSleep); } var(documentExists, existingDocument) = _documentDb.GetDocumentIfExists(document.DocumentId, correlationId); if (documentExists) { existingDocument.Count++; _documentDb.UpdateDocument(existingDocument, correlationId); } else { _documentDb.InsertDocument(document, correlationId); } _lockDb.Unlock(document.DocumentId, correlationId); }