Sub topic 6
Consistency: what you can rely on after a write
This chapter assumes you can put, get, list and delete objects with the AWS CLI or boto3, as in the earlier chapters on keys and tooling. You also need to know that S3 is a distributed service that stores each object in multiple Availability Zones. The chapter explains which guarantees S3 gives after a write, which operations the guarantees do not cover, and how to design around the gaps.
What "consistency" means here
A distributed store keeps copies of your data on many machines. Consistency describes what a reader sees after a write has been acknowledged. There are two models:
- Strong consistency: once a write succeeds, every later read sees it.
- Eventual consistency: after a write, readers may see old data for a while. Given enough time, all readers converge on the new data.
Before December 2020, S3 was eventually consistent for overwrites, deletes and listings. Code often needed retries, or helper systems such as a separate metadata index, to work around it. Today S3 gives strong read-after-write consistency for object operations in all Regions. You pay nothing extra, and performance and availability are unchanged.
What S3 guarantees for objects
After a successful response, these operations are immediately visible to every subsequent request:
| Operation | What you can rely on |
|---|---|
| `PutObject` of a new key | A following `GetObject`, `HeadObject` or list sees the object. |
| `PutObject` over an existing key | A following read returns the new content, never the old content. |
| `DeleteObject` | A following `GetObject` returns `404`, and a list no longer includes the key. |
| `ListObjectsV2` | The listing reflects all writes and deletes acknowledged before the call. |
| Metadata and tag changes | `HeadObject` and tag reads reflect the update. |
Two further properties matter:
- Atomic per object. A read returns either the complete old object or the complete new object. It never returns a mix of the two or a partial object. This also holds while a write is in flight, so a reader never sees a half-written object.
- Consistent listings. A list is no longer a stale approximation of the bucket. If your code uploads a file and then lists the prefix, the file appears.
Seeing it in code
This script overwrites a key, reads it back, deletes it and then lists the prefix. Because of strong consistency, no sleep or retry loop is needed. Use the bucket name from earlier chapters.
import boto3
s3 = boto3.client("s3")
bucket = "my-example-bucket-123456789012"
key = "config/settings.json"
s3.put_object(Bucket=bucket, Key=key, Body=b'{"version": 1}')
s3.put_object(Bucket=bucket, Key=key, Body=b'{"version": 2}')
body = s3.get_object(Bucket=bucket, Key=key)["Body"].read()
print(body) # b'{"version": 2}'
s3.delete_object(Bucket=bucket, Key=key)
listing = s3.list_objects_v2(Bucket=bucket, Prefix="config/")
keys = [obj["Key"] for obj in listing.get("Contents", [])]
print(key in keys) # FalseDelete retries are safe to repeat. DeleteObject on a missing key returns success, so a retry after a timeout does not fail.
What strong consistency does not give you
The guarantee covers a single object operation, as seen by readers who ask S3 directly. These situations still need your attention.
Concurrent writers to one key
If two clients write the same key at nearly the same time, both writes succeed and the one with the later timestamp wins. S3 has no object locking in the sense of a mutex. Neither writer is told that the other existed. The result is a classic lost update:
- Client A reads
state.json. - Client B reads
state.json. - Both modify their copy and write it back.
- One set of changes is silently lost.
No multi-object transactions
Each object write is atomic, but a group of writes is not. If you upload data.parquet and then manifest.json, a reader can see the new data file with the old manifest. Likewise, aws s3 sync or a batch upload exposes objects one at a time. Common remedies:
- Write the manifest last, and have readers treat the manifest as the signal that a batch is complete.
- Write each batch under a new, unique prefix, such as
exports/2026-11-26T1900/, and publish the prefix name through a single small object that you overwrite.
Bucket-level configuration
The guarantee applies to objects. Bucket configuration calls follow a different model and can take time to propagate. Examples are bucket policies, versioning state, lifecycle rules, and bucket creation or deletion. In practice:
- A policy change may not apply to every request instantly. Do not assume a new
Denyblocks traffic the moment the call returns. - AWS advises waiting a short while after enabling versioning on an existing bucket before you write objects to it.
- A deleted bucket name can remain visible in bucket listings briefly, and may not be reusable at once.
Scripts that create a bucket, configure it and write to it in one run should check the configuration or retry on transient errors. A bucket creation script from an infrastructure tool usually handles this for you.
Replication and notifications
These features work after the write and are not part of it:
- Replication is asynchronous. A write in the source bucket is acknowledged before it reaches the destination, so a read from the destination Region can return older data or a
404. Replication has its own timing behavior, covered in a later chapter. - Event notifications are delivered at least once and are not guaranteed to arrive in order. A consumer can see the same event twice, or see an event for an older write after one for a newer write.
Caches and intermediaries
S3 is consistent, but anything you put in front of it may not be. A CDN such as CloudFront, a browser cache, an HTTP proxy or an application cache can serve a stale copy until it expires. If readers must see updates quickly, use a new key for each version (such as app.4f2a1c.js) or invalidate the cache.
Coordinating writers with conditional writes
S3 supports conditional requests on PutObject. They let you build compare-and-set behavior on top of strongly consistent storage:
If-None-Match: *writes only if the key does not exist. It works as a simple "create once" lock.If-Match: <ETag>writes only if the object's current ETag matches the value you read. It protects against lost updates.
If the condition fails, S3 returns 412 PreconditionFailed. Concurrent conditional writes to the same key can also produce 409 ConditionalRequestConflict, and you should retry those. The IfNoneMatch and IfMatch parameters require a recent boto3 release, so update the SDK if they are rejected.
import boto3
from botocore.exceptions import ClientError
s3 = boto3.client("s3")
bucket = "my-example-bucket-123456789012"
try:
s3.put_object(
Bucket=bucket,
Key="locks/nightly-job",
Body=b"owner=worker-a",
IfNoneMatch="*",
)
print("lock acquired")
except ClientError as err:
code = err.response["Error"]["Code"]
if code in ("PreconditionFailed", "ConditionalRequestConflict"):
print("another worker holds the lock")
else:
raiseA lock object has no expiry of its own. Add a lifecycle rule or store a timestamp, and decide how a crashed worker's lock is cleared.
For read-modify-write cycles, use HeadObject or GetObject to read the ETag, change the data, then call put_object with IfMatch=<that ETag>. If you get PreconditionFailed, reread and try again.
Quick decision guide
| Situation | What to do |
|---|---|
| Upload, then immediately read or list | Nothing special. The result is consistent. |
| Several files must appear together | Write a manifest last, or publish a new prefix pointer. |
| Two clients may update the same object | Use `If-Match` with the ETag, or give each writer its own key. |
| Reading from a replica bucket | Expect lag, and design readers to tolerate a missing or older object. |
| Reacting to events | Make the handler idempotent and tolerate duplicates and reordering. |
| Serving through a CDN or cache | Use versioned key names or invalidate. |
| Script changes bucket configuration, then relies on it | Verify the setting, or retry for a short period. |
Exercise
A worker uploads results/part-1.csv and results/part-2.csv, then lists the results/ prefix and starts processing. A second team's job reads results/ while the upload is still running. Does strong consistency make the second job safe? What would you change?
Show answer
No. Strong consistency guarantees that each acknowledged object appears in listings and reads immediately, but there is no atomic multi-object write. The second job can list part-1.csv while part-2.csv is still uploading and process an incomplete set. Change the design so completion is signaled by a single object. For example, upload the parts first, then write results/_SUCCESS or a manifest that names every part, and have readers process data only after that object exists. Alternatively, write each batch under a unique prefix and publish the prefix name in one small object.
Exercise
Two instances of a service each read state.json, change it and write it back. Sometimes one instance's change disappears. Why does this happen, and how can you fix it using S3 features?
Show answer
S3 has no object locking, so when two writes to the same key overlap, both succeed and the later timestamp wins, silently discarding the other change. Fix it with conditional writes: read the object and keep its ETag, then call PutObject with IfMatch set to that ETag. If another instance wrote first, S3 returns 412 PreconditionFailed, and the instance rereads the object, reapplies its change and retries. Versioning alone would keep the lost data recoverable but would not prevent the conflict.
Exercise
Which of these can still return stale data right after a successful write: a GetObject in the same bucket, a GetObject from a Cross-Region Replication destination, or a GetObject through a CDN with a one-hour cache time? Explain each.
Show answer
The same-bucket GetObject is strongly consistent and returns the new data. The replication destination can return stale data or a 404, because replication is asynchronous and the source write is acknowledged before the copy arrives. The CDN can return the old copy until its cache expires or you invalidate it, since the cache sits outside S3's consistency guarantee.