Bridging on-chain and off-chain data in NFTs with Fauna
Tags: fauna, blockchain, database, webdev
Published: 2021-11-29 17:45:07

In [Building scalable dApps with Ethereum, Polygon, and Fauna](https://fauna.com/blog/127/building-scalable-dapps-with-ethereum-polygon-and-fauna?utm_source=DevTo&utm_medium=referral&utm_campaign=DevTo_OffchainOnchain), we explored the challenges in building dApps with completely on-chain architectures. These challenges are rooted in the scaling limitations of Layer-1 blockchains like Ethereum, which are optimized for security and provenance over speed and scalability. Outside the context of scale, since blockchains often operate as de facto public ledgers, hosting private or sensitive data on chain can also be problematic for applications that are straddling decentralized and centralized information systems — think login or bank account information, healthcare data, etc. In a similar vein, with the increased adoption of NFTs, it’s critical for dApps built around NFTs to find an off-chain data storage solution that can store metadata securely and scale efficiently.

Apps built on blockchain technology are subject to what Ethereum co-founder Vitalik Buterin has called the *blockchain trilemma* — an ongoing compromise for developers and participants in the blockchain ecosystem between scalability, security, and decentralization. Because blockchains operate as immutable ledgers, and thus have enormous utility for data that needs to be hosted in a decentralized/immutable manner, this also results in slower transaction times.

With the growth in usage through a use case like NFTs, which are still ultimately tethered to centralized systems for the majority of users interfacing with the NFT ecosystem, they must naturally also have off-chain data elements to ensure privacy. Generally, unstructured data, personal data, and images will reside off chain in a typical NFT dApp due to transaction processing times and costs, privacy concerns, or some combination thereof with purely on-chain architectures.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eok0q6xj5ugeyydk6wr4.png)

## NFT architecture
Storing, accessing, and distributing data in NFT dApps fall on a spectrum between being completely on chain and partly off chain. Because NFTs are minted on a blockchain, NFT marketplaces and creators must make decisions that are inevitably mediated by the blockchain trilemma of scalability, security, and decentralization. As such, NFT marketplaces like OpenSea, Rarible, and NBA TopShot, while all leveraging a [Layer-1](https://fauna.com/blog/127/building-scalable-dapps-with-ethereum-polygon-and-fauna?utm_source=DevTo&utm_medium=referral&utm_campaign=DevTo_OffchainOnchain) platform like Ethereum for consensus and hosting the [smart contract](https://ethereum.org/en/developers/docs/smart-contracts/), must also host other forms of data that aren’t always suitable for a blockchain. NFT marketplaces and creators are exploring a range of solutions to how to best navigate this balance between on-chain and off-chain data, with some combination of metadata and media being stored on-chain or off-chain.

## Fauna’s approach to off-chain NFT data storage
Let’s review a common NFT marketplace use case, with metadata containing user login and image information residing in an off-chain database.

This fictional NFT marketplace called *Faunibles* hosts the smart contract that dictates the terms of the NFT on Ethereum, the metadata in Fauna, and the image/media in [IPFS](https://ipfs.io/) or another distributed file hosting solution. With a combination of edge compute solutions like [CloudFlare](https://www.cloudflare.com/hp) or [Fastly](https://www.fastly.com/) as the compute layer and Fauna as the database, *Faunibles* gets started quickly with no server administration, and benefits from low latency with global distribution for the compute and database layers.

While Fauna isn’t a decentralized database, it has some of the same advantages of blockchain [due to its global distribution and consistency](https://fauna.com/blog/trusting-fauna?utm_source=DevTo&utm_medium=referral&utm_campaign=DevTo_OffchainOnchain). Other key benefits include:
* Temporality
* GraphQL Interface
* ABAC

## Off-chain storage concerns for NFT marketplaces and how Fauna addresses them

### Posterity, long-term permanence and data redundancy
One of the biggest advantages of storing data on chain is the promise of permanence; off-chain data storage methods could lead to NFT owners with a smart contract that has a link to unavailable content. Fauna is a distributed, ACID compliant database — ensuring that any data written to the database is distributed across multiple nodes to guarantee consistency.

### Ease of use
Fauna natively supports [GraphQL](https://docs.fauna.com/fauna/current/tutorials/graphql/quick_start?utm_source=DevTo&utm_medium=referral&utm_campaign=DevTo_OffchainOnchain) and doesn’t require any database administration. Having GraphQL support makes it simpler for developers to use Fauna alongside blockchain tools like The Graph (which also supports GraphQL), enabling dApps to interoperate between off-chain and on-chain data using GraphQL — all while leaving provisioning, sharding, scaling, and other time-sensitive server administration to Fauna.

### Fine-grained data security
NFTs must achieve high security and reliability across their data storage systems and avoid exposing private data in all chains and databases. Attribute-based access control (ABAC) enables dApp teams to have confidence that they can store sensitive user information like wallet addresses in their off-chain databases securely alongside the rest of their application data.

### Advanced indexing
Fauna provides powerful indexing capabilities that improve the performance of searching, sorting, and combining results from multiple tables or collections. This ensures that users of dApps aren’t hit with high latency from their off-chain databases when querying between on-chain and off-chain data. This is particularly important when building latency-sensitive applications like an NFT marketplace.

## How Fauna solves your off-chain database needs and requirements
Fauna provides a seamless, interoperable environment for your NFT off-chain data needs. Fauna is a document-relational database delivered as an API — you don’t have to worry about any infrastructure operations, and it’s automatically globally distributed as your user base grows. It empowers developers with the essential capabilities needed to build reliable and scalable off-chain storage for NFTs and ship apps faster.

## How to get started with Fauna
Fauna is easy to use and quick to get started. [Sign up](https://dashboard.fauna.com/accounts/register?utm_source=DevTo&utm_medium=referral&utm_campaign=DevTo_OffchainOnchain) for our forever free pricing [plan](https://fauna.com/pricing?utm_source=DevTo&utm_medium=referral&utm_campaign=DevTo_OffchainOnchain) to explore all the capabilities and start building! If you want to discuss your NFT use case with us or need more assistance, please [reach out to us](https://www2.fauna.com/contact-us?utm_source=DevTo&utm_medium=referral&utm_campaign=DevTo_OffchainOnchain). We’re excited to see what problems you solve for your customers and partner with you in your journey!

_[Check out this video](https://www.youtube.com/watch?v=YS3FbyPxxO0) to explore how a current Fauna customer [DigitalAX](https://www.digitalax.xyz/homepage) is leveraging Fauna’s native GraphQL capabilities to index data both on and off-chain in building its digital fashion NFT marketplace._