Search code examples
scalacassandranosqlscala-catszio

Scala, ZIO - retry if data is not inserted into cassandra database


I have a simple method which add data into Cassandra database:

 def addData(data: SomeData): Task[Either[Exception, Unit]]

I use it to add same data to two tables at the same type:

for {
 persistOneTable <- repo.addData(data).mapError(SomeError(_))
 persistSecondTable <- repo2.addData(data).mapError(SomeError(_))
} yield ()

My question is, how should I change this code to retry inserting to table if it failed at first time? I do not want to have inconsistent data in tables, so if it failed after insert to first table I want to retry it and then insert to second table and also retry there until it is added to have exactly same data in both tables


Solution

  • You can call retry on effects and handover a retry policy.

    For example addData(data).retry(policy = Schedule.recurs(5)).
    You can combine schedulers with the && (and) or || (or) operators.

    There are also specialised retry methods on effects like retryN(n: Int).
    Maybe one of them fits your needs.