Read, Modify and Update on AWS S3 atomically
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Amazon S3 (Simple Storage Service) is an object storage service from AWS, which offers industry-leading scalability, data availability, security, and performance. Users often need to read, modify, and update files stored in S3, but it is crucial to understand that S3 does not natively support atomic operations on objects. This means that read, modify, and write operations are not automatically atomic, which can be a critical consideration in application design.
Understanding Atomicity
Atomicity refers to a transaction's ability to be entirely completed or not completed at all, where no intermediate states are visible to other operations. This is crucial in environments where concurrent accesses to data can lead to inconsistencies.
Working with S3: The Challenge of Atomic Operations
S3 operates on a consistency model that guarantees eventual consistency but it's important to highlight that as of December 2020, AWS announced that S3 now supports strong read-after-write consistency automatically for all applications without changes or performance impact. This improvement simplifies the management of data consistency. However, the services still lack native support for atomic read-modify-write sequences, which means developers need to manage concurrency explicitly to maintain data integrity.
Strategies for Handling Atomicity
1. Using Versioning
Enable versioning on your S3 buckets. This automatically keeps multiple versions of an object in the same bucket, which enables you to preserve, retrieve, and restore every version of every object stored in your bucket. Here's how versioning can help achieve atomicity:
- Read the latest version: Always fetch the latest version of the file.
- Modify and update: Upload the modified file as a new version.
- Handle conflicts: If another process has written a new version of the object since it was last read, determine the appropriate conflict resolution strategy (e.g., last write wins, merge changes, etc.).
2. Leveraging AWS Lambda
Use AWS Lambda in conjunction with S3 to perform operations triggered by S3 events. For instance, you can set up a Lambda function to trigger on PUT operations. The Lambda can then handle necessary modifications atomically in a controlled environment.
3. Using S3 Object Lock
For use cases requiring immutable data storage (e.g., regulatory archives), S3 Object Lock can manage data immutability. It prevents an object from being deleted or overwritten for a fixed amount of time or indefinitely.
4. Implementing Conditional Writes
You can use the x-amz-meta headers to implement a form of optimistic locking. Store a version number or timestamp in the metadata. When writing the modified object back, you can include this version number in a Condition header to ensure that the object has not been modified since it was last read.
Technical Example: Read, Modify, and Update Operation
Here's a simplified example to demonstrate a read, modify, and update pattern using Python and boto3, AWS's SDK for Python:
Summary Table
| Feature | Description | Consideration |
| Versioning | Keeps multiple versions of an object. | Resolves conflicts but increases storage. |
| AWS Lambda | Executes code based on S3 triggers. | Adds latency and complexity. |
| S3 Object Lock | Prevents object deletion or modification. | Useful for compliance and archival. |
| Conditional Writes | Writes based only on specific conditions. | Provides a method to handle write conflicts. |
Conclusion
While AWS S3 does not directly support atomic read, modify, and write operations, using the strategies outlined above can help manage data integrity and consistency effectively. Each method has its applications and limitations, and the best choice depends on specific use cases and data management requirements.
Related reading
- Reading a file from a private S3 bucket to a pandas dataframe
- Reading a JSON file from S3 using Python boto3
- reading a packaged file in aws lambda package
- Reading contents of a gzip file from a AWS S3 in Python
- Reading data from bucket in Google ml-engine tensorflow
- Reading data from S3 using Lambda
- Receiving Email is not working in Amazon SES
- Recommended GCE service account authentication inside Docker container?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.