Feb 22

Scaling Hasura with GraphQL edge caching for RMRK’s NFT marketplaces

If all you imagine you can do with a NFT is buy, sell or trade, think again. RMRK, an advanced NFT protocol, opens up a whole new world: interactivity. RMRK NFTs can be nested and composed, they can equip and unequip other NFTs, and can even evolve and breed!

For instance, if you own a plot of land in the metaverse, you could purchase a billboard and then transfer ownership of the billboard to your plot of land, thereby nesting one NFT within another. (Why might you do this? Because a brand might want to purchase space on your billboard.)

After launching in August 2020, RMRK released these next generation NFTs on the Kusama blockchain in September 2021. One exciting thing about these NFTs is that they pave the way for community governance.

Going back to the billboard idea, the owner can decide to distribute tokens for the billboard to the community -- with shared ownership comes shared decision making. Once brands come knocking at the door, hoping to display their ads, the community decides which brand to go with. This would never happen in the "real world" -- neighborhoods don't own their surrounding billboards. But in the metaverse, how we make decisions and who has the power to do so is built by entirely different rules. That's the power of decentralization.

Building an off-chain indexer with Hasura

RMRK built their MVP entirely on the blockchain, but that meant users of their NFT marketplaces Singular and Kanaria were querying the chain on every page load. While the MVP was certainly viable, clients were waiting from 30 seconds to a couple of minutes for those requests to complete as the blockchain grew to 300 MB (and some users added 45,000 🔥 emojis to evolve a single NFT). It was as if the pain of dial-up internet had time traveled to 2021.

With no Marty McFly around to help, RMRK built their own indexer to store the latest state of the chain in a more efficient data structure to avoid recomputing it for every user. They chose Hasura as their main datastore, allowing clients to access the data via Hasura's GraphQL API.

As they say, necessity is the mother of invention, and RMRK's indexer is no exception. It not only solved the performance problem of having to download all the history at once, but also gives RMRK an opportunity to offer a better UX on their marketplaces with pagination, advanced filtering, and searching.

At first, after building their indexer, their GraphQL response times stayed below one second. Problem solved, right?

Well, not so fast.

Suddenly, RMRK started growing as more and more people found out about the project. It grew from nothing to 230k NFTs, 40k NFT holders and 200k monthly visitors to its marketplaces in less than three months and became a victim of its own success; their database was constantly overwhelmed by the heavy user demand and growth. This led to slow response times and some major traffic spikes even caused downtime --- a nightmare! On top of that, regional IP restrictions also cropped up; users in Russia and Asia weren't able to access their servers in Amsterdam.

RMRK needed a solution that would help them scale and maintain high performance no matter how much their user base grows and no matter where they reside.

Enter GraphCDN.

Scaling Hasura with GraphQL edge caching

As Ilia, co-founder and lead developer at RMRK explained:

"We got a lot of traffic, so our database had some 100% CPU spikes, which caused downtime. Naturally, we explored edge caching because that's the only way you can solve these growing pains properly and get the performance we need as well."

After putting GraphCDN in front of their Hasura GraphQL API, RMRK’s origin immediately had to handle 40% less traffic, specifically removing the need to repeatedly serve some of their most expensive queries, and their p99 response time dropped from over 1s for uncached responses to ~50ms for cached responses around the globe. They didn’t experience any more downtime and, on top of that, their users across Russia and Asia were able to access their marketplaces again!

With GraphCDN resolving RMRK’s scaling bottlenecks, the RMRK team is now able to re-focus on deepening its core functionality. One of RMRK’s plans is to decentralize more of their infrastructure over time, because like all blockchain aficionados, they want to support decentralization.

Stay tuned.