clockworklabs / clockworklabs/SpacetimeDB

Sync Inter-database Communication (Sync IDC)

Open
#5,199 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

feature-request
Dominant language
Rust
Stars
25.2k
Forks
1.1k
Avg merge
2d 7h
Merged PRs (30d)
46

Description

I would like to be able to call a reducer on another database from within my database. I want the reducer to be able to return the result, and I would like it to do so atomically:

Receiver (payments) — a plain reducer that returns a value. ctx.sender is the calling database's identity.

  #[spacetimedb::reducer]
  fn charge(ctx: &ReducerContext, customer: Identity, amount: u64) -> Result<u64, String> {
      let mut acct = ctx.db.accounts().customer().find(customer).ok_or("no account")?;
      if acct.balance < amount { return Err("insufficient funds".into()); }  // aborts the whole tx
      acct.balance -= amount;
      let new_balance = acct.balance;
      ctx.db.accounts().customer().update(acct);
      Ok(new_balance)
  }

Caller (orders) — the call is inline; the local insert and the remote charge commit together or both roll back.

  use payments;

  #[spacetimedb::reducer]
  fn place_order(ctx: &ReducerContext, item_id: u64, price: u64) -> Result<(), String> {
      let payment_db = ctx.db.config().single().payment_db;  // a stored payments::Identity

      // Sync cross-database call (Level 7, 2PC). `?` aborts the whole distributed tx on Err.
      let new_balance = payments::Identity(payment_db).reducers.charge(ctx.sender, price)?;

      ctx.db.orders().insert(Order { id: 0, customer: ctx.sender, item_id, price, balance_after: new_balance });
      Ok(())
  }

Requested by @cloutiertyler via the SpacetimeDB site.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file or test is named. Start by locating the reducer entry point and existing transaction or 2PC handling, then trace how a reducers.charge call could cross database boundaries. Done means the remote reducer returns a value and both databases commit atomically or roll back together.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
databases, distributed-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.