My mental model for Durable Objects constructor is different than most. As the step 3 in https://dev
My mental model for Durable Objects constructor is different than most.
As the step 3 in https://developers.cloudflare.com/reference-architecture/diagrams/storage/durable-object-control-data-plane-pattern/ describes, I prefer doing the expensive DO initialization in a dedicated
I don't consider the constructor to be the place to do expensive initialization. The DO constructor as of today should be fast, and I only use it to initialize the "in-memory state of the DO". Loading anything from the storage into memory if necessary or just not do anything expensive.
It's different from how most folks think about the DO constructor, but in practice this works much better, especially if your DOs are not receiving enough traffic to stay warm and you have many cold starts, so you need to keep your constructor lightweight.
As the step 3 in https://developers.cloudflare.com/reference-architecture/diagrams/storage/durable-object-control-data-plane-pattern/ describes, I prefer doing the expensive DO initialization in a dedicated
init() call that is part of a resource creation flow. Including any initial SQL table creation and setup. After the init() completes, any other SQL migration is done only when necessary (at the point of access, not the constructor, and it's very fast/noop majority of the time when done properly: https://github.com/lambrospetrou/durable-utils?tab=readme-ov-file#sqlite-schema-migrations ).I don't consider the constructor to be the place to do expensive initialization. The DO constructor as of today should be fast, and I only use it to initialize the "in-memory state of the DO". Loading anything from the storage into memory if necessary or just not do anything expensive.
It's different from how most folks think about the DO constructor, but in practice this works much better, especially if your DOs are not receiving enough traffic to stay warm and you have many cold starts, so you need to keep your constructor lightweight.




