Skip to main content

Use UUIDv7 in DynamoDB to save money and improve performance

· 3 min read
Javid
Creator & Software Engineer

UUIDv7 time-ordered IDs diagram

Introduction

Don't forget to use UUIDv7. As of May 2024, it's standardized under RFC9562. It's time to start using it in your database designs.

UUIDv7 brings time-ordering to your IDs, which introduces time-ordering to your IDs, offering significant advantages and cost savings, particularly for DynamoDB users.

Why UUIDv7?

UUIDv7 is a time-ordered UUID that combines the best of both worlds. The uniqueness of UUIDs and the time-ordering capabilities of timestamps. Unlike traditional UUIDs (v4), which are completely random, UUIDv7 embeds a timestamp in its first 48 bits, making it sortable by creation time.

Example

For example, you have a data model about a company and its employees.

PK: Organization#1
SK: Employee#<uuidv7>

Now you can:

  • Sort in ascending or descending order
  • Filter data within specific time ranges
  • Query employees after or before certain dates

Conclusion

UUIDv7 is a game-changer for DynamoDB users. By leveraging its time-ordering capabilities, you can design more efficient access patterns and save costs by reducing the need for additional GSIs.

Frequently Asked Questions

How does UUIDv7 differ from UUIDv4?

UUIDv7 embeds a 48-bit timestamp in the first part of the UUID, making it sortable by creation time. UUIDv4 is completely random and provides no ordering. UUIDv7 maintains the same uniqueness guarantees while adding time-ordering benefits for database performance.

What's the performance impact of using UUIDv7 in DynamoDB?

UUIDv7 improves DynamoDB performance by enabling more efficient range queries and reducing the need for additional GSIs. The time-ordering allows for natural pagination and date-based filtering using sort key conditions, which are more cost-effective than filter expressions.

Are there any JavaScript libraries for generating UUIDv7?

Yes, libraries like uuid and uuidv7 support UUIDv7 generation.

Next steps

Model data like a pro and save on DynamoDB costs:

👉 Get the Scale to Zero AWS Kit for production-ready DynamoDB tables, helper functions, and serverless best-practices.