SpacetimeDB icon indicating copy to clipboard operation
SpacetimeDB copied to clipboard

Recommended Approach for Instanced Content

Open shelbyd opened this issue 9 months ago • 2 comments

The realtime parts of my game are highly instanced (instances or raid in WoW, matches in Fortnite/DotA/etc). What is the recommended approach to do that with spacetime db?

I see two options, instance IDs or db for each instance.

With instance ids, we'd add a instance_id column to every table that has instances content. We'd have to create indexes and pollute many queries with WHERE instance_id = ?. It might also have difficulty scaling at some point.

With multiple DBs, we'd init a new DB for each instance and shut it down after the match is over. This might have a high cost for startup and shutdown. It also might not easily support replays or other innate features well.

Which would the creators of the system or expert users recommend?

shelbyd avatar Mar 13 '25 01:03 shelbyd

I'm curious about this too. I'm working on app with similar concept (multiple users collaborating in a single room/instance).

Option one seems to be terrible for both security and as you already mentioned scaling and cleanness of code/DB.

Option two would be perfect but does SpacetimeDB even support dynamically created databases in runtime?

Are there any other ways? Or is SpacetimeDB currently not suited for such cases?

RZetko avatar May 02 '25 20:05 RZetko

Sorry to necro an old issue here but we have a feature coming in the future which will essentially allow a module to spawn another module. This would probably be the ideal approach to having "instanced" dungeons or like spawning instanced matches for players to play in and then when the match is done you could just collect some stats and then dispose of the DB instance.

For now what I would probably recommend is rolling your own sort of sidecar + "inter-module communication". For this you would need to setup a client which connects to your DB and subscribes to some private tables (a client can see private tables if you use a token for the identity that owns the database). Then just read from some sort of table called create_matches and then create a new database based on the data in that row. Then you could do a callback back into the module called match_created with the new info (probably the new database identity). Then this should be inserted into some table so that clients can see the new database identifier and then they can connect to that new database. This approach is sub-optimal at the moment but without an official implementation for "forking databases" and "inter-module communication" I believe this is the only way to do this.

If enough people request this example we would be willing to create a new cookbook entry to give an example on how to do this: https://github.com/clockworklabs/spacetimedb-cookbook

I hope this was helpful, let me know if you have more questions 🙂

jdetter avatar Oct 31 '25 15:10 jdetter