Skip to content
Français
Account: not signed in, sync available My learning
Select theme
Français

Synchronous, asynchronous, eventual consistency: cloud patterns

  • 20 min

By updated

Improve this page

In a cloud application, services communicate over a network that can go down, slow down or lose messages. You therefore have to choose between synchronous communication (I wait for the answer) and asynchronous communication (I publish a message and carry on), then decide which data consistency you accept.

This page presents the two communication modes, then the CAP theorem, which is often misunderstood: partition tolerance is not a choice on a distributed system, and the trade-off between strong consistency and availability only arises during a partition. It then covers eventual consistency and the cases where it is not enough, and details the Saga pattern, which replaces the ACID transaction when an operation spans several services.

What you will learn

  • Tell synchronous from asynchronous communication and their practical trade-offs
  • Read the CAP theorem and place cloud databases (DynamoDB, Spanner, Cosmos DB)
  • Recognise the use cases and the pitfalls of eventual consistency
  • Understand the Saga pattern for a distributed transaction without 2PC (two-phase commit, explained in section 4)
  • Choose synchronous or asynchronous depending on the scenario

Prerequisites: having understood loose coupling and stateless design, presented in the lesson Loose coupling and stateless design (in French).

This lesson takes the angle of managed cloud services (DynamoDB, Cosmos DB, Spanner, the provider's queues and buses). A neutral reading of an asynchronous exchange on an architecture diagram is in Synchronous, asynchronous and consistency (in French), how a queue works (acknowledgement, retries, dead-letter queue) in Message queues and brokers (in French), and the NoSQL database families in NoSQL databases and CAP (in French).

1. Synchronous vs asynchronous, the two communication modes

Synchronous communication

Service A calls service B and waits for the answer before continuing its processing. This is the classic REST model, gRPC, and most HTTP calls.

The synchronous mode has three advantages:

  • Simple to reason about: the logic reads linearly, like a function call.
  • Immediately visible error: if B is down, A gets an error right away.
  • No additional infrastructure: just two services and a network.

It also has three drawbacks:

  • Strong temporal coupling: A and B must be available at the same time.
  • Cumulative latency: if A calls B which calls C, the total latency is the sum.
  • Cascading saturation: if B slows down, A slows down too, and all of A's threads block.

Asynchronous communication

Service A publishes a message in a queue or an event in a bus, then continues its processing. Service B consumes the message later, at its own pace.

The asynchronous mode brings three advantages:

  • Temporal decoupling: B can be down without blocking A.
  • Resilience: the queue absorbs load spikes, B processes at its own pace.
  • Independent scaling: A and B scale according to their own profiles.

In return, it costs three things:

  • Harder to reason about: the logic is no longer linear, you have to follow the event flows.
  • Deferred error: if the processing fails, A does not know immediately.
  • Additional infrastructure: you need a queue (RabbitMQ, SQS, Service Bus, Pub/Sub) or a bus (Kafka, EventBridge).

Decision table

This table compares the two modes on temporal coupling, fault tolerance and complexity: asynchronous gains in resilience, but costs more to design.

CriterionSynchronousAsynchronous
Temporal couplingStrongWeak
Latency perceived by the userImmediateDeferred
Fault toleranceLowHigh
ComplexityLowMedium to high
Use caseReading data, immediate validationLong processing, notifications, integrations
Associated patternsTimeout, retry, circuit breaker (cuts calls to a failing service)Saga, outbox (section 4)

When to choose synchronous

Synchronous is the right choice when the user waits for the answer or the logic requires an immediate return, provided the called service is fast and reliable. The figures below are orders of magnitude: set your own according to your service.

  • The user waits for the answer in their interface (read, validation).
  • The business logic requires an immediate return (payment with confirmation).
  • The called service is fast (for example under 100 ms at p95, that is, for 95 % of requests) and reliable (for example an SLA, an availability commitment, above 99.9 %).

When to choose asynchronous

Asynchronous fits when the processing is long, when load spikes must be absorbed, when several services consume the same event or when resilience is critical.

  • The processing is long (for example over one second) and the user does not need the immediate answer.
  • The load is variable and spikes must be absorbed (queue = buffer).
  • Several services consume the same event (event-driven architecture).
  • Resilience is critical: if B is down, A must keep going.

2. CAP: do you really have to pick two properties out of three?

No, and that formula distorts the theorem

The "pick 2 out of 3" formula is wrong, or at least misleading, and it leads to bad architecture decisions. The CAP theorem (Eric Brewer, 2000, formally proved by Gilbert and Lynch in 2002) deals with the following three properties.

LetterPropertyDefinition
CConsistency (strong consistency)Every read returns the most recently written data
AAvailabilityEvery request receives a response (success or business error), no timeout
PPartition toleranceThe system keeps working if the network is partitioned

A partition is not an option, it is an event. You do not "choose" P: as soon as your system is distributed, the network eventually breaks, through a zone outage or a severed inter-region link. Giving up on P would amount to giving up on distribution itself.

The useful statement is therefore this one: when a partition occurs, the system must decide between staying consistent by refusing to serve some of the requests (CP) and staying available by agreeing to serve potentially stale data (AP). The rest of the time, without a partition, a system can perfectly well offer consistency and availability: this is even the nominal case. This is what the PACELC extension formalises, adding that outside partitions (Else) the trade-off moves between latency and consistency.

Practical consequence

When a distributed system suffers a network partition, it has two options.

The CP choice refuses writes on the side that cannot synchronise, so that all reads see up-to-date data. The service is then partially unavailable, but consistent.

The AP choice accepts writes everywhere, and reads may return slightly stale data. The service stays available, but consistency is only reached eventually: this is eventual consistency.

How do cloud databases sit on CAP?

DatabasePositioningTrade-off
DynamoDB, regional tableAP, with strongly consistent reads as an optioneventually consistent by default, strongly consistent on demand and at extra cost
DynamoDB, global tablesMREC (multi-Region eventual consistency) by default, MRSC (multi-Region strong consistency) as an optionthe mode is set at creation and cannot be changed afterwards
Cosmos DBConfigurable: 5 consistency levels, from Strong to Eventual, Session by defaultthe level is set on the account and can be relaxed for a request or a client: no single CAP classification
Cloud Spanner (GCP)CP with high availabilityGlobal strong consistency through a proprietary distributed atomic clock, higher latency
Aurora Global DatabaseSingle writer in the primary Region, fast cross-Region replication (latency typically under a second)Local reads in the secondary Regions, failover if the primary Region fails; it is not a multi-writer system
MongoDB AtlasConfigurableRead concerns from local to linearizable

A standalone database instance has no place in this table, and this is a frequent confusion. CAP describes the behaviour of a distributed system facing a partition: a single-instance PostgreSQL cannot be partitioned, it is simply unavailable when it goes down. Classifying it as "CA" gives the illusion of an advantageous compromise where there is only a single point of failure. The CAP question only arises once you replicate.

Spanner is the most interesting case: Google invested in a distributed atomic clock (a proprietary technology called TrueTime) to reach strong consistency while staying globally distributed, at the price of a higher latency per write, which depends on the chosen configuration (regional or multi-regional) and on the distance between replicas. It is a remarkable engineering compromise but unsuited to very high-frequency workloads.

3. What is eventual consistency?

Definition

Eventual consistency means that reads may return slightly stale data for a short delay (typically a few milliseconds to a few seconds), but that all replicas eventually converge to the same value.

If a user writes a value at 14:00:00, a read at 14:00:01 may return the old value. A read at 14:00:02 will have the new value. The system ends up consistent: this is what eventually consistent means.

When eventual consistency is enough

Many applications do not require strong consistency, and the criterion is not a proportion but a simple question: what happens if a user reads a value a few seconds old? If the answer is "nothing serious", eventual consistency is fine.

  • An e-commerce shopping cart that takes 500 ms to synchronise: invisible to the user.
  • A like counter that adjusts its number 1 second after the click: acceptable.
  • A product recommendation based on purchase history 5 minutes later: excellent.
  • An analytics dashboard with D-1 data: standard.

For these cases, eventual consistency allows higher availability and much better scalability. This is why DynamoDB, Cassandra and many NoSQL databases favour availability (AP by default), while offering stronger consistency as an option, at read time or in configuration.

When strong consistency is mandatory

Four categories of cases require strong consistency.

First case, financial transactions: the debit of one account and the credit of the other must be atomic and immediately consistent, otherwise a double spend becomes possible.

Second case, critical inventory. For booking the last plane ticket, if two customers see "1 available" at the same time, you cannot sell both.

Third case, security and authorisations. With an eventual consistency of 5 minutes, a revoked access remains usable for 5 minutes.

Last case, distributed locking: a single elected leader, a single worker processing a job, which is not compatible with eventual consistency.

For these cases, use Spanner, Aurora, PostgreSQL, or DynamoDB with strong reads (with their latency overhead). A strong read guarantees seeing the latest value, but is not enough to prevent overselling: two customers can read "1 available" then both write. You also need a conditional write (ConditionExpression in DynamoDB), a transaction or a lock, which rejects the second write.

What are the classic pitfalls of eventual consistency?

Pitfall 1, Reading your own write. A user writes, then reads immediately, and the read returns the old value because it landed on a replica that has not yet received the write. The remedy is the read-your-writes guarantee: a session always sees its own writes. Databases do not all offer it in the same way, and several do not apply it by default:

  • DynamoDB reads with eventual consistency by default. You must request a strongly consistent read (ConsistentRead), possible on a table or a local secondary index, but not on a global secondary index.
  • Azure Cosmos DB offers the Session consistency level, the default of an account: it ensures read-your-writes within a single client session.
  • Cassandra requires writing and reading at the QUORUM level. With W replicas acknowledging the write, R replicas queried on read and RF replicas in total, the condition R + W > RF guarantees that at least one replica read contains the write.

Pitfall 2, A read that goes back in time. A writes X1 then X2. B reads X2, then, reading again, gets X1 because the second read landed on a lagging replica: the state seems to go back in time. Solution: monotonic reads consistency, which guarantees that a read never returns an older value than the previous one.

Pitfall 3, Concurrent write conflict. Two writes arrive at the same time on the same key, on different nodes. Solution: an explicit conflict resolution strategy (timestamp, version vectors, or "last write wins").

4. The Saga pattern, distributed transactions without 2PC

The problem of distributed transactions

A classic ACID transaction (atomic, consistent, isolated, durable, detailed in Transactions: what ACID and BASE promise) is simple in a single-instance database: BEGIN, operations, then COMMIT or ROLLBACK atomically. On a system distributed across several services, this atomicity no longer exists naturally.

Take the validation of an e-commerce order, which requires three operations:

  1. Debit the customer's account (Payment service).
  2. Reserve the stock (Inventory service).
  3. Schedule the delivery (Shipping service).

If step 3 fails, steps 1 and 2 must be cancelled, but they live in different databases, on different services, perhaps at different cloud providers.

Two-Phase Commit (2PC), the classic solution that is avoided rather than abandoned

2PC is a distributed transaction protocol that coordinates several participants through a coordinator. It guarantees atomicity at the price of:

  • Strong temporal coupling: all participants must be available simultaneously.
  • Blocking: if the coordinator goes down at the wrong moment, the participants stay locked.
  • Poor performance: multiple round trips before being able to commit.

2PC is not dead, it has moved. It is still implemented and used, in relational engines through the XA standard, in some message brokers, and inside the distributed databases themselves, which use it for their internal commits. What has changed is the scope: a synchronous distributed transaction between services is now avoided when compensations can be accepted instead.

The right formulation is therefore this one: when your participants belong to different teams, databases or providers, the temporal coupling of 2PC costs more than the complexity of compensations. Most NoSQL databases do not offer it anyway, which settles the question for you.

The Saga pattern

The Saga pattern replaces 2PC with a sequence of local transactions, each with a compensating transaction that can undo it.

In a choreographed Saga, each service listens to events and publishes its own, with no central coordinator.

1. Order Service → emits "OrderCreated"
2. Payment Service → consumes, debits, emits "PaymentDone"
3. Inventory Service → consumes, reserves, emits "StockReserved"
4. Shipping Service → consumes, schedules, emits "Shipped"
If step 3 fails:
3'. Inventory Service → emits "StockReservationFailed"
2'. Payment Service → consumes, refunds, emits "PaymentRefunded"
1'. Order Service → consumes, marks order as cancelled

The following diagram runs through the choreography when the delivery fails: follow the crossed-out arrow, then the compensation events that travel back up the chain until the order is cancelled.

Sequence diagram of a choreographed Saga: the Order service emits OrderCreated, the Payment service debits and emits PaymentDone, Inventory reserves and emits StockReserved, Shipping fails. Inventory emits StockReservationFailed, Payment refunds and emits PaymentRefunded, the order is cancelled.ShippingserviceInventoryservicePaymentserviceOrder serviceOrderCreatedPaymentDone(customer charged)StockReservedShipping impossibleStockReservationFailedPaymentRefunded(compensation)Order cancelled
Source · SVG
Esc to close, + and − to zoom, scroll to move around. 100 %

In an orchestrated Saga, a central orchestrator (for example AWS Step Functions or Azure Durable Functions) coordinates the steps and handles the compensations. It is simpler to reason about, but it reintroduces a coordination point.

Trade-offs

This table compares the three ways of holding together a transaction that spans several services. 2PC keeps atomicity but strongly couples the participants; the two Saga variants accept eventual consistency in exchange for weak coupling.

Aspect2PCChoreographed SagaOrchestrated Saga
AtomicityStrongCompensationCompensation
CouplingStrongWeakMedium
PerformancePoorGoodGood
Debugging complexityLowHighMedium
ConsistencyImmediateEventualEventual

In practice, prefer an orchestrated Saga (Step Functions, Durable Functions) as soon as the workflow has several steps with compensations: the readability of the orchestrated code then outweighs the complexity of a central orchestrator.

A Saga only holds up in production with two safeguards.

  • Each step must be idempotent: replayed, it does not produce two effects, because messages are delivered at least once. The lesson Idempotence in the cloud (in French), which follows this page, covers this point.
  • Updating the local database and publishing the event must be atomic, which the outbox pattern guarantees: the event is written in a table, in the same transaction as the update, then published by a relay.

The chaining of compensations in a workflow engine is detailed in Background jobs, schedulers and workflows (in French).

5. How do you choose between synchronous and asynchronous?

The choice between synchronous and asynchronous, and between strong consistency and eventual consistency, depends on the business domain and on user constraints. Neither model is better in the absolute: each suits a type of use. Ask two questions, in this order.

The first question is about consistency: what happens if a user reads a value a few seconds old? If the answer is "nothing serious" (cart, likes, recommendations, dashboards, activity feed), choose eventual consistency by default: it simplifies the architecture, improves availability and stays invisible to the user. If the answer is "a real risk" (finance, critical inventory, access revocation, distributed lock), strong consistency is required, with its cost in latency and availability. The four cases of section 3 give the detailed list.

The second question is about the communication mode. Synchronous remains relevant when latency matters (a user call waits for the result) and when simplicity of reasoning comes first (few steps, few actors). Asynchronous becomes relevant as soon as the chain of events involves several services, spikes must be absorbed, or you want to decouple availabilities. On an architecture with many microservices, asynchronous by default limits the propagation of failures; on a monolith or a small application, direct synchronous is often simpler to reason about.

Finally, adopting a Saga requires a change of culture: the team must accept that some temporary inconsistencies are visible. An order can be created, paid, then cancelled 30 seconds later because the stock was missing, and the user then receives two successive notifications. This is the price of resilience on a distributed architecture, and teams used to a monolithic ACID transaction must adapt their UX design to make these compensations understandable.

Key points

  • Synchronous = I wait for the answer (REST, gRPC). Asynchronous = I publish a message, I carry on (queue, event bus).
  • CAP theorem: partition tolerance is not a choice. The trade-off between consistency and availability arises only during a partition; outside a partition, PACELC moves the compromise towards latency.
  • A single-instance database has no CAP position: it cannot be partitioned, it is simply unavailable when it goes down.
  • Eventual consistency is the default behaviour of many NoSQL databases; it fits when a stale read of a few seconds is harmless (cart, likes, recommendations, dashboards).
  • Strong consistency remains mandatory for finance, critical inventory, security and distributed locks, and a strong read is not enough: you also need a conditional write, a transaction or a lock.
  • Saga pattern replaces 2PC between services: local transactions and compensations, in choreography or orchestration. 2PC itself is still used inside an engine or through XA.
  • The synchronous/asynchronous choice depends on the number of services involved, latency sensitivity and tolerance to propagated failures: there is no universal default.