Skip to main content

Convex Tutorial: Scaling your app

Convex was designed from the ground up for scale. In the previous section we already talked about how keeping your actions small and most of your logic in queries and mutations are crucial to building fast scalable backends.

Let's talk about a few other ways to keep your app fast and scalable.

Indexed queries

Indexes tell the database to create a lookup structure to make it really fast to filter data. If, in our chat app we wanted to build a way to look up messages from just one user, we'd tell Convex to index the user field in the messages table and write the query with the withIndex syntax.

Learn how to use indexes.

Too many writes on the same document

Let's say you decide to show a counter in your app. You may write a mutation that reads a number field, adds 1, and updates the same field in the database. At some point, this pattern may cause an optimistic concurrency control conflict. That means that the database isn't able to handle updating the document that fast. All databases have trouble with this sort of pattern.

There are a few ways to deal with this, including building something called a sharded counter...

But before you go learn advanced scaling techniques on your own, there is a better way with Convex components.

Scaling best practices with Convex Components

In the case of the counter above, the Convex team has already built a scalable counter Convex component for you to use.

Convex components are installed in your Convex backend as an npm library. They are sandboxed, so they can't read your app's tables or call your app's functions unless explicitly provided.

As you build more complicated features like AI agent workflows, leaderboards, feature flags or rate limiters, you may find that there is already a Convex component that solves this problem.

Components directory

Wrap up

We've covered a lot of ground in this tutorial. We started by building a chat app with queries, mutations and the database that form the fundamental building blocks of the Convex sync engine. We then called an external API from our backend, using the scheduler to coordinate the work. Finally, we learned that Convex components give you scaling best practices in neat packages.

If you are looking for more tips, read our best practices and join the community.

Convex enables you to build your MVP fast and then scale to new heights. Many great products have already done so. You're in good company.