Skip to main content

Writers

A writer method gets exclusive, atomic access to state in order to modify it. Any modifications a writer makes to state are persisted after the method returns, but before returning to the caller. If the method fails (or if it is part of a transaction which aborts), its modifications to state are not persisted.

A writer method gets passed a context of type WriterContext. A WriterContext can only be used to make calls to reader methods (that is not a typo, writer's can only call other reader's).

Here's an example of a writer method called Deposit on our Account state that increments the account's current balance by some amount in the request:

from bank.v1.bank_rbt import Account

class AccountServicer(Account.Servicer):

async def Deposit(
self,
context: WriterContext,
request: DepositRequest,
) -> DepositResponse:
self.state.balance += request.amount
return DepositResponse(updated_balance=self.state.balance)

In addition to returning a response and updating state, a writer can also schedule async tasks, which are atomically started (or enqueued) if and only if the writer completes successfully.