Skip to content

Using $sequence to track document order in Appwrite_

Learn how to use Appwrite's $sequence attribute to track document order in your database.

6 min read

Some systems need to reflect the order in which actions happen. A ticketing system, for example, should assign "Ticket #41" before "Ticket #42". But in a user interface, it often makes sense to display the latest tickets first, so "Ticket #42" may appear above #41.

Relying on timestamps to get this right is often not enough. Two documents can be created almost simultaneously, and the sort order might vary. What is needed is a consistent, backend-assigned number that increases with each insert and cannot be modified or skipped.

Appwrite's new $sequence attribute provides exactly this. Every time a document is added to a collection, the system assigns it a unique, auto-incrementing integer. This value reflects the insert history of the collection and can be used for sorting, display, filtering, and pagination.

In this tutorial, we will build a simple web-based support ticket tracker using plain HTML and JavaScript. Each submitted ticket will be stored in Appwrite with a title and description, and each will receive a $sequence number automatically. We will use that number to display and order the tickets.

What you have built

You now have a working support ticket tracker that looks like this:

Support tracker demo
Support tracker demo

  • Each submitted ticket is stored as a document in Appwrite
  • Every document receives a $sequence number, guaranteed to be unique and increasing
  • The interface displays each ticket using that number
  • The ticket list is reliably sorted by creation order

There was no need to write any logic to generate numbers, track counters, or manage collisions. Appwrite's $sequence attribute handled the sequence internally, which was great for our use case.

Conclusion

This tutorial introduced the auto-incrementing $sequence attribute and demonstrated how to use it in a small, working app. It offers a solution to a common need: preserving the order of inserts in a predictable, integer-based way.

Because the value is assigned by your database, $sequence remains stable even as your application grows. You can filter by it, paginate through it, or display it directly in user interfaces. And since it is read-only and immutable, we avoid the risk of errors that often come with custom counters or timestamp sorting.

This pattern can extend far beyond tickets. You can use $sequence for invoices, order numbers, log entries, approval requests, or anything where sequence matters.

Here's a link to the GitHub repository for this tutorial: Simple Support Tracker.

Looking forward to seeing what you build with this!

Resources

Read next

Ready to build?_