Sub topic 6
Consistency: what you can rely on after a write
This chapter assumes you can create a bucket, upload and list objects with the AWS CLI or boto3, and that you know what an S3 key is. It explains what S3 guarantees when you read right after you write, and where your own design can still produce surprises.
The guarantee
Amazon S3 provides strong read-after-write consistency for all requests in all Regions. This applies to new objects, overwrites, deletes, and list operations. Once S3 returns a success response to a write, every later read sees that write.
Before December 2020, S3 was eventually consistent for overwrites, deletes and listings. Older blog posts and workarounds, such as adding sleeps or keeping a separate index of keys, are no longer needed.
| Operation | What you see after a successful write |
|---|---|
| PUT of a new key | GET returns the new object immediately |
| PUT over an existing key | GET returns the new content, never the old one |
| DELETE of a key | GET returns 404, and the key is absent from listings |
| PUT or DELETE, then LIST | The listing reflects the change |
Seeing it in code
The following script writes an object, overwrites it, and reads it back with no delay. The read always returns the second version.
import boto3
s3 = boto3.client("s3")
bucket = "my-example-bucket-123456789012"
key = "demo/consistency.txt"
s3.put_object(Bucket=bucket, Key=key, Body=b"version one")
s3.put_object(Bucket=bucket, Key=key, Body=b"version two")
body = s3.get_object(Bucket=bucket, Key=key)["Body"].read()
print(body.decode()) # version two
listing = s3.list_objects_v2(Bucket=bucket, Prefix="demo/")
print([o["Key"] for o in listing.get("Contents", [])]) # includes demo/consistency.txt
s3.delete_object(Bucket=bucket, Key=key)
listing = s3.list_objects_v2(Bucket=bucket, Prefix="demo/")
print(listing.get("KeyCount")) # 0Concurrent writes
Strong consistency does not mean writes are coordinated. If two clients write the same key at nearly the same time, S3 keeps the one with the latest timestamp. S3 does not merge them or tell either client about the other. Reads during the race return one complete object, never a mix of bytes from both.
If your application needs to avoid overwriting someone else's work, use a conditional write. The IfNoneMatch="*" parameter on put_object makes the request fail with a 412 error when the key already exists.
import boto3
from botocore.exceptions import ClientError
s3 = boto3.client("s3")
try:
s3.put_object(
Bucket="my-example-bucket-123456789012",
Key="locks/job-42",
Body=b"owner=worker-1",
IfNoneMatch="*",
)
print("Created, we own the lock")
except ClientError as err:
if err.response["Error"]["Code"] == "PreconditionFailed":
print("Key already exists")
else:
raiseYour boto3 version must be recent enough to support conditional writes. Upgrade it if you get a parameter validation error.
What consistency does not cover
- Other systems. Caches, CDNs and browsers can serve stale copies. A CloudFront distribution may return the old object until its cache expires or you invalidate it.
- Your own retries. A timed-out PUT may have succeeded. Reading the key before retrying tells you the real state.
- Event notifications. These are delivered at least once and can arrive late or out of order. Do not treat their order as the order of writes.
- Replication. Cross-Region replication is asynchronous. The destination bucket can lag behind the source by seconds or longer.
- Bucket configuration changes. Changes to settings such as policies or versioning are eventually consistent. Wait a short time after changing them before depending on them.
Common errors
- Adding
time.sleep()after a PUT. It is unnecessary and slows your code. - Assuming "last writer wins" is safe for shared files. Use conditional writes or versioning.
- Testing replication by reading the destination bucket immediately.
Exercise
A worker uploads results/out.json and then lists the prefix results/ to confirm it. The listing does not show the key. Is eventual consistency the likely cause? What would you check instead?
Show answer
No. S3 listings are strongly consistent, so a successful PUT appears in the next LIST. Check that the PUT really returned success (a timeout or an exception could hide a failure), that the bucket and prefix are exactly the same in both calls, and that you are reading the first page of results, since listobjectsv2 returns at most 1,000 keys per call and you may need pagination.