Spend time researching serverless databases, like I did in early 2020, and you'll likely discover Fauna. Fauna is a "global serverless database" with a powerful yet flexible design—it's the modern evolution of the database. If you're a new or solo developer, choosing Fauna gives you benefits you can use to reduce development time and shrink costs, but also simply your technology stack and eliminate the hassle of database upgrade and maintenance.
I'm not a professional developer. I'm a long-serving secondary school Computer Science teacher who enjoys learning new things. Although I have a Computer Science degree, I mainly taught myself to program. Besides, the technologies I studied in school are obsolete now. Like many developers, much of my knowledge comes from playing around trying to build things. On the wall of my classroom, in big black vinyl letters, are the words "Experiment, fail, learn, repeat". That's how I got where I am. So bear in mind as you read, especially if you're an experienced developer, what follows is why I chose Fauna—your opinion might be different.
- Where I started
- Spoilt for choice
- Failed experiments
- Fauna to the rescue
- The benefits of serverless
- Fauna's modern architecture
- The power of FQL
- Some key FQL concepts
- About the author
Researching front end choices, I found the three big players: React, Angular, and Vue. It wasn't difficult to choose one out of the three. However, when it came to the back end, the choices didn't seem so clear. There were "traditional" relational databases and NoSQL databases, but within these two groups there was great diversity.
Then there were all the products that operated as layers over top of existing databases. The flexibility of the cloud had led to a proliferation of database architectures each with different use cases and trade-offs. As a new developer lacking experience, I couldn't make an informed choice. As a result, I'd end up wasting months of development time.
My research suggested GraphQL was the modern way to build an API for my new back end. I started learning GraphQL, writing schemas and trying to implement resolvers. Already burdened with learning Vue.js, I struggled to get very far. To help I turned to Prisma—one of the "layer over top" technologies I had come across.
These architectures appealed to me, coming from a relational database background, because I could keep the PostgreSQL database I knew and gain some modern features. In practise though, the benefits were outweighed by the extra complexity. The Prisma layer needed another server to be set-up, configured, and maintained. It added another point of failure. It was another thing to learn when I had already taken on too much. An experienced developer or team might have more success than I did. Things were starting to get desperate when unexpectedly I came across Fauna.
My journey to Fauna was a long one. It led down several dead ends. I can't remember exactly how I found it—probably while searching for help with GraphQL. Support for GraphQL is a major selling point and it was enough to get me through the door. On a whim, I signed up for a free account.
GraphQL support is one example of the flexibility that Fauna's architecture provides. If you want to use GraphQL you can. I found that once I started understanding Fauna Query Language (FQL) I didn't actually need a GraphQL API. FQL gives you the power to build and manipulate complex data structures so the data you receive is exactly what you need. For example, you can select specific properties from a document or build an object containing properties from related documents.
There was a learning curve with Fauna, but unlike some of the other technologies I had experimented with, I felt like I was actually learning and not just troubleshooting server setups.
For new developers, Fauna's serverless architecture offers many benefits; the most obvious being no server to set up. I used to get some satisfaction from tinkering in the AWS console because it was so easy to start servers. Starting was easy, but ongoing maintenance and updates were a pain and took away from time spent actually developing my application. I can't overstate what a joy it is not to have to worry about servers anymore. With Fauna there's no need to choose a "size" and pay for more than you need or choose a low-end server to save money at the cost of performance. There is no maintenance to do and no updates to install. My front end files are hosted as static files on a content delivery network (CDN). With Fauna as my back end, I don't think about servers anymore. I focus on developing my application.
Besides the benefits of being serverless, Fauna has other advantages. For example, Fauna supports both relational and document-based modelling. This meant my existing relational data could be imported into Fauna and I could work with it in the style I was used to. I discovered I could "de-normalise" some of my data reducing the number of link tables and simplifying queries. With Fauna you don't have to force your data to fit one model.
Being "web-native", Fauna has a web-based dashboard and interactive shell. In the past, I used a third-party application on my computer to interact with my database. It used port 5432, which my work firewall blocked. You can use the dashboard to quickly create, view and edit documents, create indexes, import GraphQL schemas and more. The shell is particularly useful for quickly trying out bits of FQL. I have the shell installed on my computer, but I only ever use the web-based version because it's so convenient.
There are more benefits, like that Fauna is globally distributed, but you don't care—or rather, you don't need to think about it. You get all these benefits simply by using Fauna. It's a huge boon for new and solo developers.
Fauna query language (FQL) is how you interact with Fauna databases. To be successful with Fauna you're going to need to learn a bit of FQL. If you know at least one programming language already, you won't find it too difficult.
Whenever I need to do something new, I check the FQL cheat sheet and there's usually an FQL function that does what I need. Recently, I wanted to add a function to extend a user's trial on my site for another week. I checked the cheat sheet and found the TimeAdd function. The more I learn, the more impressed I am with what you can do in FQL. Instead of writing extra front end code to process data I get exactly what I want using FQL.
Example of FQL, written in the web-based shell, that adds seven days to a given date. The Date function converts the string "2020-11-26" into a Date object and the TimeAdd function adds seven days.
The result showing the extended trial date.
Every document in Fauna has a unique Reference (Ref). You can use a document's Ref in the Get function to fetch a specific document from a collection. It took me an embarrassingly long time to realise that References were collection specific (i.e. the name of the collection forms part of the Ref).
If you don't know the Ref of the document you want, you need to use an Index. Indexes are how you search for documents in collections. As an example, in my database I have a collection called "Course". If I want to fetch the document "262684008356250123" from this collection I can use Get.
Remember, the ID number alone is not enough, you need to use the Collection function too and specify the name of the collection containing the document you want.
The returned document includes its Reference (ref), Timestamp (ts), and of course the data itself.
In many cases, you might not want all the data from a document. In the example above, imagine you only wanted the course name. For that you would use Select.
Example FQL to select one value from a document.
I use Select all the time, but its first parameter took me a while to understand. If you go back and look at the document returned by the Get function, you'll see three top level keys: ref, ts, and data. The array ["data", "name"] says 'look under the "data" key and return the value of "name"'. It's a way to navigate nested objects. The second parameter is the document you're selecting data from.
The result of Select—only the course name is returned.
Alongside Select, it was Let that became an invaluable tool to help me build custom data structures. Perhaps this is where my inexperience shows but Let's description—"the Let function binds one or more variables to a single value or expression"—belies its power. Let is incredibly useful. You can use it to build custom data structures by "binding" an expression to a variable and then using that variable in subsequent expressions.
Following on from the example above, I also have a collection called "Group" that represents the different teaching groups that teachers can create. A group has a course and there's a one-to-many relationship between courses and groups (i.e. a group can only have one course, but many groups might be associated with the same course). Imagine I wanted to fetch a group, but also fetch its course—just the name—at the same time in one query.
Using Let to build a custom data structure with some related data. We bind the group document to the variable "instance" in the first pair of braces. In the second pair, we start by selecting just the group name like we did above. To get the course name we use Select twice—once to select the course Ref, which is stored on the group instance, and again to get just the course name.
The returned object containing just the group name and course name.
It took a while, but I'm glad I found Fauna and decided to give it a try. As a new developer, I've been impressed with it and I think it would suit a wide range of use cases. I can't imagine having to set up and manage my own servers again. It's something I simply don't worry about anymore. I focus on building my application and being as productive as possible. If that appeals to you then you should try Fauna too.
Hello—I'm Owen Hawkridge. I've been a Computer Science and I.T. teacher for over thirteen years. I'm using Fauna to build Examgap, a powerful yet simple system for practising written-answer exam questions. You can reach me via email—email@example.com.