News for hackers. Stuff that matters.
Re:Figma Is a File Editor by crabmusket on Thursday July 13, 2023 @12:49AM (#36705122)
> The tricky part is that when multiple users open the same Fig file concurrently, Figma’s infrastructure needs to ensure that they are all connected to the same server. That server can then be the sole authority on the state of that document, and write to it without race conditions.

This is the killer app for CloudFlare's new Durable Objects. They solve the routing and data distribution layer, allowing you to write single-threaded business logic to coordinate changes.

They even have a transactional storage capability which is priced more or less equivalent to DynamoDB (which Figma uses for their write-ahead log).

This pattern also helps scale write transactions without melting your database. http://ithare.com/scaling-stateful-objects/

I try to boost Durable Objects every chance I get, in order to "push the battleship" in the direction of another cloud provider implementing something equivalent.

While this article is written by the plane.dev team, which has an adjacent product, their approach seems more geared towards more demanding backends. Lots of use cases don't need to run e.g. a game or simulation on the backend, they just need a synchronization layer in front of a document store.

---

> They could stuff the whole document into a binary blob or json(b) column next to the metadata.

In my experience doing this in MySQL... do not do this. Once you have a dozens-if-not-hundreds-of-gigabytes large table full of JSON, it becomes a real nuisance. As a halfway measure, I think it would help to have a "blobs only" table separate to the metadata table. But as the OP points out, it is not economical anyway.

Re:Figma Is a File Editor by benatkin on Thursday July 13, 2023 @01:28AM (#36705333)
> While this article is written by the plane.dev team, which has an adjacent product, their approach seems more geared towards more demanding backends. Lots of use cases don't need to run e.g. a game or simulation on the backend, they just need a synchronization layer in front of a document store.

If all you have is a hammer, everything looks like a nail. Vercel and Netlify haven't been known as tools for building good realtime collaborative apps. I suggest that devs who are inspired by this post try having a truly live document.

Here's a list of realtime providers. Impressive? Sure. Easily used for stuff like Figma? Not so much. https://vercel.com/guides/publish-and-subscribe-to-realtime-...

Re:Figma Is a File Editor by calebj0seph on Thursday July 13, 2023 @02:43AM (#36705745)
I'm an engineer on Atlassian's new whiteboard feature for Confluence, which has real-time collaboration like Figma. We've been highly successful using Cloudflare Durable Objects to act as a simple relay for WebSocket messages.

One of the best things is that the Durable Object will automatically run in the closest Cloudflare data center to the first user who connects, which helps keep latency very low.

The alarms feature they added last year has also been great at allowing us to asynchronously snapshot the state of a board certain intervals, rather than us needing to schedule the job from an external system.

Re:Figma Is a File Editor by crabmusket on Thursday July 13, 2023 @04:13AM (#36706305)
Side note: unfortunately for Aussies the nearest DC running durable objects is still in Singapore :(

Source: https://where.durableobjects.live/

Re:Figma Is a File Editor by calebj0seph on Thursday July 13, 2023 @04:59AM (#36706555)
> Currently, durable objects are available in 9.89% of Cloudflare PoPs.

I really hope this becomes higher, the less latency the more compelling Durable Objects become for real-time applications.

Re:Figma Is a File Editor by kentonv on Thursday July 13, 2023 @09:43AM (#36708996)
One of the benefits of our new storage engine[0] is that it'll be much easier for us to host it in any datacenter, rather than just the biggest, best-connected ones. We still have a lot of work to do to make this available to all durable objects and actually start utilizing smaller datacenters this way, but we're working on it.

[0] https://twitter.com/KentonVarda/status/1659551757796515846

(I'm tech lead for Workers and have been focused on this storage engine in particular.)

Re:Figma Is a File Editor by crabmusket on Thursday July 13, 2023 @04:58PM (#36715893)
Great to hear! Your work is really impressve.
Re:Figma Is a File Editor by paulddraper on Thursday July 13, 2023 @03:40PM (#36714901)
Better than California by a long shot
Re:Figma Is a File Editor by 3cats-in-a-coat on Thursday July 13, 2023 @03:17AM (#36705975)
I've been thinking about this. Is this honestly the only solution? When time comes to edit, the artifact needs to be on a single server? Obviously you can redundantly write it to a cluster, but even in a cluster there's a "leader" typically in most architectures.

Any alternatives?

Re:Figma Is a File Editor by crabmusket on Thursday July 13, 2023 @04:13AM (#36706300)
Another solution is to design your data model so that it's fully peer-to-peer , then you don't need a point of centralisation. Except for realtime communication/updates, but you have a lot more leeway.

CRDTs are an obvious way to do this. Or maybe something like CouchDB's document model?

Re:Figma Is a File Editor by mike_hearn on Thursday July 13, 2023 @04:42AM (#36706448)
You can use a scalable database that gives you serializable transactions whilst not requiring you to represent your document in the relational model.

A good example of this architecture would be using FoundationDB [1] with Permazen [2]. In this design there are three layers:

1. A horizontally scaling sorted transactional K/V store. This is provided by FoundationDB. Transactions are automatically ordered within the cluster.

2. A network protocol that can serialize database transactions. Permazen has the ability to do this, you can do things like cache reads, HTTP POST transactions and so on. Stuff you can't easily do with SQL databases.

3. A way to map in-memory objects to/from key/value pairs, with schema migration, indexing and other DB-like features. Permazen also does this.

Permazen can be thought of as an ORM for KV stores. It's a library intended to execute on trusted servers (because the KV store can't do any business logic validation). However, for something like Figma where it's basically trusting the client anyway that doesn't matter. Additionally you can do some tricks with the architecture to support untrusted clients; I've explored these topics with Archie (the Permazen designer) in the past.

The nice thing about this design is that it doesn't require sharding by "file", can scale to large numbers of simultaneous co-authors, and results in a very natural coding model. However, Permazen is a Java library. To use it from a browser would be awkward. That said it has fairly minimal reliance on the JDK. You could probably auto-convert it to Kotlin and then use Kotlin/JS or Kotlin/WASM. But frankly it'd be easier to do that architecture as a real desktop app where you aren't boxed in by the browser's limitations. And of course the design ideas can be implemented in any language, it's just a lot of work.

The writeup mentions a couple of reasons for not using a database:

1. Relational/object mismatch. Permazen+FDB solves this.

2. Cost of a database vs S3. This is mostly an artifact of cloud pricing. Cloud is highly profitable but most of the margin comes from managed databases and other very high level services, not commodity byte storage. Given that FDB is free you could eliminate the cost gap by just running the database yourself, and especially, running it on your own metal.

Because Permazen has a pluggable KV backend and because there are backends that write to files, you can have both worlds - a scalable DB in the cloud and also write to files for individual cases where people don't want to store data on your backend.

https://www.foundationdb.org/ [1]

https://permazen.io/ [2]

Re:Figma Is a File Editor by whizzter on Thursday July 13, 2023 @09:04AM (#36708492)
Well you could do your entire data-model as an CRDT (even with single-server some semblance to an CRDT model can be beneficial but not as needed).

The question then becomes:

- Do you want a complicated data model based on CRDT's that will allow for arbitrary node distribution and failures (That you kinda need to handle anyhow)

- Do you select a "simpler" datamodel where you have single master servers (per-document) but spend a tad more effort on resolving node crashes (and are hopefully infrequent?)

Theoretically CRDT's are prettier, and infra is always scary. But otoh do you want to spend a lot of time on modelling complicated data-models for features that might not even make sense in the long run? (Ie, do you 100% know your product before starting.. or will you need to iterate and update it).

Re:Figma Is a File Editor by kevincox on Thursday July 13, 2023 @12:29PM (#36711879)
I think you want both.

CRDT (or similar solutions like OT) is important if you ever want decent offline support and true real-time collaboration. Even a user with a spotty connection is essentially "occasionally offline". Having a single coordinator "in the Cloud" doesn't really solve the concurrency issues.

However you also want a single coordinator. It will give you the best performance. If you want to have people in a call to be able to move their cursors around and see each other within a second it will be very hard to do with sending everything through a database and polling or similar. But this can fundamentally be seen as an optimization. The client could just push writes and pull new changes to the database every couple of seconds. But this is both less efficient (the client needs to retain more history and do more complex merges) and higher latency.

Re:Figma Is a File Editor by paulgb on Thursday July 13, 2023 @06:36AM (#36707166)
(Author here) you’re absolutely right, Durable Objects are a great product, especially for the “just need a sync layer” use case. The db layer mentioned in the article is built to run on either Durable Objects or (as a regular Linux process) on Plane. https://driftdb.com/

I do see Plane as being relevant to pure sync layers, for cases where you want to run on your own cloud/own metal, or can’t compile your code to run on V8, but it’s good to have options.

Re:Figma Is a File Editor by crabmusket on Thursday July 13, 2023 @07:05AM (#36707399)
Great points, I really hope Plane finds success. I'd forgotten about DriftDB, that's also a very cool spot in the landscape. More diversity and experiments in this space are great.
Re:Figma Is a File Editor by elithrar on Thursday July 13, 2023 @09:04AM (#36708488)
(I lead DO & databases product here at Cloudflare)

Thanks for the kind words! Durable Objects also underpins a tremendous amount of what we build internally as well — it’s fundamentally a very powerful “coordination API”.

FYI: We’re continuing to work on observability & throughput improvements so folks can get more of each DO, on top of the horizontal sharding (“a DO per X”) approach we recommend.

Re:Figma Is a File Editor by amadeuspagel on Thursday July 13, 2023 @09:40AM (#36708945)
I have another use case that DO would be perfect for, and that's sync for offline first apps.

I have two offline first apps, both using PouchDB[1] as client database and CouchDB as server database. I'd love to replace CouchDB with DO.

Maybe you can hire some of the people contributing to PouchDB to build a backend for it using DO?

[1]: https://pouchdb.com

Re:Figma Is a File Editor by crabmusket on Thursday July 13, 2023 @06:14PM (#36716866)
Excited for the observability improvements. You're all doing great work!