SpacetimeDB and BitCraft
Game Update<br>SpacetimeDB and BitCraft<br>August 28, 2023
SpacetimeDB and BitCraft<br>This is going to be a relatively short blog post! I just wanted to talk to the BitCraft community a little bit about SpacetimeDB. In this post, I’m going to explain what SpacetimeDB is, what problems it solves for BitCraft, what it means for BitCraft development, and how it should allow us to do super interesting things with player mods and game extensions in the future.<br>What is SpacetimeDB?<br>SpacetimeDB is the server-side game engine that BitCraft is built on. You can think of SpacetimeDB as both a game server and database combined into one.<br>The normal architecture for MMORPG server-side backends is to have client applications (the player’s machine) connect to game servers (big computers running in a data center) which run the logic of the game simulation and control how and what data gets stored in the database (other big computers running the same data center). The data stored on the game servers is wiped when the servers restart (i.e. the RAM of the MMORPG) and the database stores all persistent data (i.e. hard drive of the MMORPG).<br>SpacetimeDB turns this architecture inside out. Rather than game servers sitting in between the clients and the database, in SpacetimeDB you upload your game’s logic into the database itself and have clients connect directly to the database.<br>Okay, so what is the point of doing this?<br>Well, SpacetimeDB is a “serverless” platform. This may seem like a silly word to use since it runs on servers, but it’s actually an important point. What “serverless” means is that the logic of the game does not reference or even know about the fact that it is running on a physical server machine.<br>In a sense, this is the same thing that your PC operating system does. It makes it so that programs that run on your PC don’t have to know about the specific hard drives or RAM modules or network cards that exist on your computer, and thereby dramatically simplifies the task of writing PC programs.<br>In that context you could think of SpacetimeDB as being a distributed cloud operating system. It takes the many machines, devices, and systems of a cloud datacenter and makes them look like a single giant logical computer.<br>The point being always to simplify the job of the developer. Because your game’s logic is running inside the database itself, the database can act like an operating system. Rather than having to wrangle a vast array of game servers and databases, you instead just need to upload your program to SpacetimeDB and that’s it.<br>How does it help BitCraft?<br>That’s all great, but how does this help us develop our game, BitCraft?<br>SpacetimeDB solves seven MMORPG game development challenges, each of which could otherwise cripple a game development team of our size.<br>Persistence<br>One of the things that makes MMORPGs so difficult to build is that so much of the data of the game world is persistent. Typically persisting data is a complicated and finicky process involving connecting to an external database like Postgres or MySQL and sending data back and forth from the game servers. What makes it so complicated is that the game programmer always needs to be synchronizing data stored in the game server with data that is stored in the database. Serializing that data back and forth with the database is not only awful for performance, but it’s an enormous source of bugs, since it’s not clear what data needs to be saved and when. If the programmer is not incredibly careful about using database transactions then you can easily have game breaking bugs like gold dupes or other nasty data corruption or data loss issues.<br>Additionally, BitCraft has waaaaaaaaay more persistence than your average MMORPG. Because the whole world is editable we need to persist everything from the player inventories to the positions of buildings and resources to the height of the terrain itself.<br>In SpacetimeDB all data is persisted by default.[1] That means BitCraft developers don’t even have to think about this problem at all. Gone.<br>Permissions<br>MMORPGs are essentially complicated 3D rendered social networks, and therefore have all of the complicated permissions issues that arise when implementing a social network. Facebook/Meta for example has spent an enormous amount of engineering resources to make it easy for their developers to specify the rules for what users are allowed to do to manipulate the social graph.<br>We take inspiration from the engineering done on both social networks and blockchains where permissions are also critical to the safe functioning of the system. In SpacetimeDB, for any function that is accessible to clients you can assert that the caller of the function has permission to do the thing that they’re trying to do, or otherwise fail the transaction and rollback any changes that were made.<br>This paradigm means that complicated access control lists and server side logic become a simple assert at the start of your function....