Aggregate Event Stream ถูกเข้าถึงและอ่านได้จากหลาย thread พร้อม ๆ กัน ซึ่งเปิดโอกาสให้เกิด concurrency conflict ขึ้นได้จริง และถ้าปล่อยไว้โดยไม่จัดการ ก็อาจส่งผลให้ Aggregate เกิด state ที่ไม่ถูกต้องขึ้นมาเป็นจำนวน เท่าใดก็ได้ ลองพิจารณาสถานการณ์ที่ thread สองตัวพยายามแก้ไข Event Stream ในเวลาเดียวกัน ดังที่แสดงใน Figure A.10
Figure A.10. สอง thread แข่งกันแก้ไข instance เดียวกันของ Aggregate ที่ออกแบบด้วย A+ES
วิธีแก้สถานการณ์นี้ที่ง่ายที่สุดคือการใช้ EventStoreConcurrencyException ในขั้นตอนที่ 4 โดยปล่อยให้ exception นี้ถูกส่งต่อขึ้นไปเรื่อย ๆ จนถึง client ปลายทาง:
public class EventStoreConcurrencyException : Exception
{
public List<IEvent> StoreEvents { get; set; }
public long StoreVersion { get; set; }
}
เมื่อ exception นี้ถูกจับได้ที่ client ปลายทาง ผู้ใช้ก็คงจะถูกแนะนำให้ลองทำ operation นั้นซ้ำด้วยตนเอง
แต่ก่อนจะเลือกใช้แนวทางแรกนั้น คุณคงเห็นด้วยว่าการมีแนวทางการ retry ที่เป็นมาตรฐานน่าจะดีที่สุด ดังนั้นเมื่อ Event Store ของเราทำการ throw EventStoreConcurrencyException เราก็จะพยายามกู้สถานการณ์กลับมาได้ทันที:
void Update(CustomerId id, Action<Customer> execute)
{
while(true)
{
EventStream eventStream = _eventStore.LoadEventStream(c.Id);
var customer = new Customer(eventStream.Events);
try
{
execute(customer);
_eventStore.AppendToStream(
c.Id, eventStream.Version, customer.Changes);
return;
}
catch (EventStoreConcurrencyException)
{
// fall through and retry, with optional brief delay
}
}
}
ในกรณีที่เกิด concurrency conflict ขึ้น เราจะเพิ่มขั้นตอนพิเศษเหล่านี้เพื่อเอาชนะปัญหา:
1. Thread 2 จับ exception ได้และปล่อยให้มันหลุดผ่านไป (fall through) โดยการควบคุมจะย้อนกลับไปที่จุดเริ่มต้น ของ while loop จากนั้น Events 1–5 จะถูกโหลดเข้าไปใน Customer instance ตัวใหม่
2. Thread 2 รัน delegate ซ้ำบน Customer ที่โหลดมาใหม่ ซึ่งตอนนี้จะสร้าง Events 6–7 และ Events เหล่านี้จะถูก append ต่อจาก Event 5 ได้สำเร็จ
ถ้าการต้องรัน behavior ของ Aggregate ซ้ำมีค่าใช้จ่ายสูงเกินไป หรือไม่สามารถทำได้ด้วยเหตุผลบางประการ (เช่น ต้องใช้ค่าใช้จ่ายสูงในการ integration กับระบบของ third-party เพื่อสั่งซื้อสินค้าหรือเรียกเก็บเงินบัตรเครดิต) เราก็อาจต้องเลือกใช้กลยุทธ์แบบอื่น
ดังที่แสดงใน Figure A.11 กลยุทธ์หนึ่งที่ว่านี้ก็คือ Event conflict resolution ซึ่งถูกนำมาใช้เพื่อลดจำนวน concurrency exception ที่เกิดขึ้นจริง ต่อไปนี้คือวิธีที่การใช้ conflict resolution แบบง่าย ๆ จะทำงานได้:
Figure A.11. การใช้ Event conflict resolution กับ Event Stream ของ Aggregate
void UpdateWithSimpleConflictResolution(
CustomerId id, Action<Customer> execute)
{
while (true)
{
EventStream eventStream = _eventStore.LoadEventStream(id);
Customer customer = new Customer(eventStream.Events);
execute(customer);
try
{
_eventStore.AppendToStream(
id, eventStream.Version, customer.Changes);
return;
}
catch (EventStoreConcurrencyException ex)
{
foreach (var failedEvent in customer.Changes)
{
foreach (var succeededEvent in ex.ActualEvents)
{
if (ConflictsWith(failedEvent, succeededEvent))
{
var msg = string.Format("Conflict between {0} and {1}",
failedEvent, succeededEvent);
throw new RealConcurrencyException(msg, ex);
}
}
}
// there are no conflicts and we can append
_eventStore.AppendToStream(
id, ex.ActualVersion, customer.Changes);
}
}
}
ในกรณีนี้ method ตรวจจับความขัดแย้ง ConflictsWith() ถูกใช้เพื่อเปรียบเทียบ Aggregate Events แต่ละตัวว่าขัดแย้งกับ Events ที่ถูก append เข้าไปใน Event Store พร้อมกันหรือไม่ (ตามที่รายงานมาใน exception)
method การแก้ไขความขัดแย้งนี้มักถูกนิยามขึ้นเป็นราย ๆ ไปตาม Aggregate Root แต่ละตัว ขึ้นอยู่กับ behavior แบบเฉพาะที่มันรองรับ อย่างไรก็ตาม ยังมี ConflictsWith() implementation ที่ใช้ได้กับ Aggregate ส่วนใหญ่:
bool ConflictsWith(IEvent event1, IEvent event2)
{
return event1.GetType() == event2.GetType();
}
การแก้ไขความขัดแย้งในกรณีส่วนใหญ่นี้ยึดตามกฎง่าย ๆ ข้อหนึ่ง: Events ที่เป็น type เดียวกันจะขัดแย้งกันเสมอ แต่ Events ที่เป็นคนละ type กันจะไม่ขัดแย้งกัน