Don't Make S3 and DynamoDB Two Sources of Truth
This is an English translation of the Japanese original, made with an LLM (Claude Opus 5.5). If anything is unclear, the Japanese version is authoritative. Read the Japanese original
For the Japanese original, the author planned the article and checked its content and references, and an LLM (OpenAI Codex) was used to organize the structure and write the text. The author is responsible for the content.
Suppose you want a small prototype that analyzes a video stream.
It processes the video coming from a camera and lets you check the results later. The analyzed video is saved as short files. At first, just putting the generated videos in Amazon S3 seems good enough. You can find the files by opening the S3 console, and play them by downloading the ones you need.
Once the prototype works, you want to make it a bit easier to use.
- See on a calendar which days have video
- Play the video from a given time continuously
- Overlay the sensor data and analysis results for the same time on the video
- Know how much is stored per device
At this point, a pile of files in S3 makes the screens hard to build. Rather than listing every S3 object each time, it seems easier to search from screens and APIs if you register each video's start time, length, device and storage location in Amazon DynamoDB.
The video itself goes to S3 and the search information to DynamoDB. So far it seems natural. But if you start building without deciding which of the two is the correct data, handling failures in saving and deleting suddenly becomes hard.
You cannot write to S3 and DynamoDB at the same time
In this prototype, suppose a short video file is saved like this:
- Save the video file to S3
- Save the video's start time, length and S3 key to DynamoDB
If both succeed, there is no problem. But the write to DynamoDB can fail after the save to S3 has succeeded. Then the video exists but does not show up in search results.
Reversing the order does not help. If you register in DynamoDB first and then the save to S3 fails, the video appears in search results, but there is nothing to play.
DynamoDB has transactions that update several items together. But you cannot include saving a file to S3 in such a transaction. It is not a mechanism that treats S3 and DynamoDB as one operation in which both succeed or both fail.
AWS documentation on DynamoDB transactions: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/transactions.html
For saving alone, retrying the failed side might seem enough. But once you think about deletion too, things gradually get complicated.
Deletion makes it even harder
When deleting a video, you also need to remove both the S3 file and the DynamoDB search information.
If you delete from DynamoDB first and the S3 deletion fails, a video that cannot be found remains. If you delete from S3 first and the DynamoDB deletion fails, a video that no longer exists remains in search results.
Handling this strictly makes you want states like these:
- Deletion accepted
- Deleting from S3
- Updating DynamoDB
- Partially failed
- Waiting for retry
- Deletion completed
On top of that, you might need to record for a while that a video was deleted, so that a resend from the device does not bring it back. If you delete many videos, you also need checkpoints that show how far processing has gone.
Of course, such a workflow can be built properly. But you only wanted to save and search videos in a small prototype, and the system for keeping two storages in the same state keeps growing.
At this point, it is worth asking whether you need to treat both S3 and DynamoDB as the source of truth at all.
Make what cannot be regenerated the source of truth
If the video itself is lost, you cannot play it even if its start time and file name remain in DynamoDB. On the other hand, if the video is in S3 and its S3 key tells you the device and the recording time, the search information can be rebuilt.
In that case, it works well to split the roles like this:
- S3: the source of truth that holds the fact that a video exists
- DynamoDB: a read model that makes searching from screens and APIs easy
A read model is data converted from the source of truth into a form that is easy to read for a particular use. For example:
- A calendar that shows which days have video
- Time ranges that can be played continuously
- The latest data per device
- Totals of stored items and storage size
These are needed to make the display fast, but they are not the video itself. Make sure they can be regenerated by scanning S3 if they break or disappear.
Use the S3 key as a data structure
To be able to regenerate, the minimum information must be known just by looking at the S3 object.
For example, put information like this in the S3 key or the object metadata:
- The version of the data format
- The identifier of the device that produced the data
- The UTC time when recording started
- The actual recording length
- A stable ID that identifies the same data
If the file name contains only the current time and a random number, you cannot tell what a file represents even after listing it. With an S3 key whose meaning cannot be understood without reading another database, you can no longer remove that database from being a source of truth.
It is also important that the same input always produces the same S3 key. Then a retried save does not multiply into a separate video, and if the same thing already exists, it can be treated as the same result.
S3 also supports conditional writes that do not overwrite existing objects. https://docs.aws.amazon.com/AmazonS3/latest/userguide/conditional-writes.html
Reflect changes quickly with events, and verify later against the source of truth
After a video is saved to S3, you can trigger processing with S3 Event Notifications or Amazon EventBridge and update the DynamoDB read model.
With this approach, the video saving process only needs to confirm that the save to S3 succeeded. Even if the DynamoDB update fails temporarily, the video save is not treated as failed.
However, the events must not become a new source of truth. S3 event notifications are designed to be delivered at least once, so the same event may arrive more than once. They are also not guaranteed to arrive in the order they occurred.
AWS documentation on S3 Event Notifications: https://docs.aws.amazon.com/AmazonS3/latest/userguide/EventNotifications.html
So the event processing must give the same result no matter how many times the same notification is processed. Other options include checking the current S3 object before updating, or accepting only the newer event for the same key.
In addition, periodically check the S3 listing or inventory so that differences from the read model can be repaired.
The division of work is: use events to reflect changes quickly, and make the correct state rebuildable from S3.
Screens also accept some delay
The read model may be updated a little later than S3. So the screens should not assume that the read model is always complete.
For example, the calendar's "there is data on this day" is a hint for choosing a date. When the actual videos are fetched after you click it, the result is decided from the videos that currently exist in S3.
Conversely, a video may be deleted after its retention period ends, after it was shown on the calendar. When returning search results, you do not need to guarantee that the video will still exist in the future. If it does not exist at fetch time, read the latest state again.
Another approach is to check S3 directly only for recently added data, and use the read model for past periods that are settled. Instead of searching everything in S3 each time, change how you read depending on the update frequency and the accuracy you need.
S3 provides strong consistency for PUT and DELETE and for subsequent GET and LIST. This makes it a convenient place to check the current objects. https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html#ConsistencyModel
This does not make DynamoDB unnecessary
The point here is not that you should avoid DynamoDB.
DynamoDB is convenient for searching by date and device, calendar display, checking processing status and so on. For information that cannot be regenerated from the videos in S3, such as descriptions entered by users or approval status, DynamoDB is the source of truth for that information.
What matters is deciding the source of truth for each kind of data.
- Whether a video exists: S3
- Calendars and totals that can be computed from the videos: a regenerable read model
- Names and decisions entered by users: the database where that information is stored
This does not mean a system may have only one source of truth. It means not creating two sources of truth for the same fact.
Write down the failure combinations first
When you need to combine S3 and a database, it is a good idea to write down the order of saving and deleting before you start implementing.
- What happens if only the first write succeeds?
- Does retrying create duplicates?
- If deletion stops halfway, from which side do you resume?
- If you lose all the search data, can it be rebuilt?
- Where is the correct state finally decided?
If answering these questions requires many processing states and compensating actions, you may be treating two storages as double sources of truth.
Saving large data to S3 and search information to DynamoDB is a common setup. That is exactly why deciding first "which one can rebuild the other", rather than "how to write to both", can make saving, deleting and failure recovery much simpler.