Overcoming database scaling issues with Fauna’s serverless offering
Tags: fauna, database, serverless
Published: 2022-07-07 18:43:05

With the amount of data generated every second, scaling high-volume applications is hard. Scaling databases is harder, even if you’re hosting your database in the cloud. You need to choose the right database for your application, but what if you need data modeled in several different ways? Spinning up MongoDB as your document database, MySQL as your relational database, and a few additional databases might be an option, but that could prevent you from scaling your application.

One solution is [Fauna](https://docs.fauna.com/fauna/current/learn/introduction/what_is_fauna), a [serverless document-relational database](https://fauna.com/serverless) that gives you the freedom of a document-oriented database along with the safety of using a relational database. Although many databases now come with a serverless offering, Fauna is the only one designed and built as a serverless database to enable schema flexibility, easy integration, and reliable scaling.

This article will take you through some of the prominent features of Fauna and demonstrate why it’s a better option than MongoDB or CockroachDB.

## Why you should choose Fauna

Fauna stands out compared to competitors like MongoDB, CockroachDB, and DynamoDB. Because it’s natively serverless, it scales quickly and efficiently. It’s one of the few databases that solves the problem of globally distributed ACID transactions, which is an essential feature that neither MongoDB nor CockroachDB offers.

The following is a closer look at what Fauna offers and why it’s superior to other databases.

### It’s fully serverless

There are a lot of databases that come with a serverless offering, such as [MongoDB Atlas](https://www.mongodb.com/cloud/atlas/serverless). However, MongoDB Atlas has essentially been repurposed to work as a serverless database and wasn’t designed for that. Meanwhile, Fauna is [only serverless](https://fauna.com/client-serverless#why-fauna). It’s designed to help you concentrate on making your product better rather than on managing database infrastructure just to keep your application running.

Because it’s serverless, you don’t have to take care of any routine database operations (DBOps) tasks that require a lot of time, such as sharding, cluster management, replication, and version upgrades. You also don’t have to manage any connection pools, because it works on a serverless invocation model. Additionally, it doesn’t have a cold start problem.

All of this makes Fauna much more developer-friendly than any other multipurpose database.

### It scales automagically

MongoDB was built to scale horizontally at the expense of ACID transactions, meaning that instead of immediate consistency, you get eventual consistency. This won’t work for many use cases. Moreover, sharding in MongoDB requires maintenance and downtime, and you must set a custom shard key per collection. As for resiliency, MongoDB can be vulnerable to failures caused by network partitions or cluster node outages.

Fauna, however, was built to be a globally distributed, multi-data-center, active-active, serverless database from the get-go. It succeeds with high scalability because its Calvin-based architecture allows for both [horizontal and vertical scaling](https://codingcat.dev/podcast/2-5-scaling-transactional-data-globally-with-fauna) and ensures that every node in a Fauna cluster performs the same roles (unlike in many other distributed databases) as a [query coordinator, data replica, and log replica](https://fauna-assets.s3.amazonaws.com/public/FaunaDB-Technical-Whitepaper.pdf). The beauty of this architecture is that the provisioning and management of cluster nodes happen entirely behind the scenes without bothering you.

### It offers global low latency

Database calls are costly. To improve database performance and enhance the user experience by lowering the response latency, you need to reduce the number of calls to your database as much as possible. Fauna does precisely that by [batching the transactions](http://cs.yale.edu/homes/thomson/publications/calvin-sigmod12.pdf) and applying them across geographical regions, so that you can go through and update multiple collections in one query.

Fauna is still dependent on external network latency. The low latency [lays the foundation](https://fauna.com/blog/real-world-database-latency) for enabling strong or immediate consistency. Many use cases require strong consistency to work, such as high-frequency trading, banking, or high-volume booking systems. If you attempt to use MongoDB or CockroachDB for these use cases, you might not be able to scale them efficiently.

### It provides distributed ACID transactions

Guaranteeing ACID transactions in a globally distributed database was a complex problem to solve, but Yale University’s [Calvin](http://cs.yale.edu/homes/thomson/publications/calvin-sigmod12.pdf) paper changed everything. [FaunaDB implemented a system](https://fauna.com/blog/serializability-vs-strict-serializability-the-dirty-secret-of-database-isolation-levels) based on Calvin in which, according to Fauna, “Every read issued by an application is guaranteed to reflect the writes of transactions that have been completed before when the read was issued.”

Databases such as CockroachDB don’t offer a high level of serializability. They have traditionally needed Network Time Protocol (NTP) clocks to be synchronized across nodes to ensure that the serializability isn’t violated because of time drift.

In MongoDB, dirty reads and lost transactions are plausible if and when the database fails. Because MongoDB doesn’t have read-side coordination for transactions spanning multiple shards, consistency can be violated because the [default read consistency level](https://fauna.com/blog/comparison-of-transaction-models-in-document-databases) allows that to happen.

Only Fauna has implemented a Calvin-style protocol that guarantees the highest level of transaction serializability without using a clock and without increasing latency. Fauna uses strict serializability to enable consistency across all nodes in all geographical regions and prevent stale reads. It achieves this by maintaining a log position that helps guarantee a monotonically increasing number of global transactions. To enable transactional order monotonicity, each of Fauna’s data centers uses a synchronization schema for sharing the log position with all query coordinators. Fauna also [shares systems information publicly](https://status.fauna.com/).

### It provides security natively

Fauna has a host of database security features that you usually have to build separately for other databases. In addition to standard fine-grained [attribute-based access control](https://docs.fauna.com/fauna/current/security/abac), Fauna offers OAuth 2.0 integration out of the box. You can use an identity provider to authenticate your users to Fauna.

Fauna’s key-based API is great at authenticating connections with particular permissions down to the level of database objects. Even after authentication, the queries use further token-based authentication on a case-by-case basis. You’d probably need to have a token lease manager like [HashiCorp Vault](https://www.hashicorp.com/products/vault) to do this for another database. This feature isn’t available in either MongoDB or CockroachDB.

Having said that, the security implementation of both CockroachDB and MongoDB is quite advanced, as both databases provide basic authentication, encryption, and authorization—but not all features are available in all the plans. Encryption at rest in CockroachDB, for instance, is only available in the Enterprise version, while MongoDB doesn’t have that restriction. Fauna, on the other hand, provides all of these features in all its plans.

### It enables more powerful queries

MongoDB and CockroachDB both have minimal support for custom or user-defined functions. Fauna uses an API-first approach to reading and writing data, meaning you can query the data using several interfaces (such as GraphQL) by exposing its API. Fauna comes with its own query language, FQL, which is an [expression-oriented query language](https://docs.fauna.com/fauna/current/api/fql/). You can also write user-defined functions (UDFs). In contrast, CockroachDB doesn’t support GraphQL by default, but you can find ways to work around that. MongoDB provides support for GraphQL via the Atlas App Services API.

In FQL, everything returns values. This enables you to use complex control structures and conditionals to process and compute your data in the shape and form you want. You can also use a mix of these interfaces to develop your applications. If you’re going to query documents, relational data, graph data, or a combination of all of those, you should be able to do it pretty easily with Fauna.

FQL also allows you to query different types of data. For instance, you can use the [temporality](https://docs.fauna.com/fauna/current/learn/understanding/temporality) feature of Fauna to issue point-in-time (PIT) queries. This is especially useful when dealing with time series data or [streaming data](https://docs.fauna.com/fauna/current/learn/understanding/streaming), which is a real possibility with Fauna. While MongoDB [natively supports streaming data](https://www.mongodb.com/docs/manual/changeStreams/), CockroachDB has [limited support for streaming](https://www.cockroachlabs.com/blog/from-batch-to-streaming-data-real-time-monitoring-with-snowflake-looker-and-cockroachdb/), which you can capitalize on by building on top.

## Conclusion

There are multiple reasons why Fauna is a better choice for building highly scalable and blazing fast applications. Its Calvin-based architecture has enabled Fauna to be one of a kind, providing globally distributed transactions with the highest level of ACID compliance possible.

With its support for GraphQL and FQL, Fauna also proves to be highly developer-friendly. It gives you the option to use document-oriented database storage and retrieval while providing a relational-database-like experience.

Fauna offers fully managed provisioning, maintenance, scaling, sharding, replication, and correctness. Using Fauna, all you have to do is focus on creating great applications.

To see how Fauna can help you, [request a demo](https://go.fauna.com/contact-us) or [sign up](https://dashboard.fauna.com/accounts/register) and start using Fauna for free.

*Kovid Rathee is a data and infrastructure engineer working as a senior consultant at Servian in Melbourne, Australia.*