Implementing Distributed Transactions โšก [in a gist]

Distributed Transactions are not theoretical; they are very well used in many systems. An example of it is 10-min food/grocery delivery.

๐Ÿ‘‡
โœจ The UX we want is: Users should see orders placed only when we have one food item and a delivery agent available to deliver.

A key feature we want from our databases is atomicity. Our storage layer can choose to provide it through atomic operations or transactions.

๐Ÿ‘‡
We will have 3 microservices: Order, Store, and Delivery.

Important decision: Store services have food, and every food has packets that can be purchased and assigned.

Hence, instead of just playing with the count, we will play with the granular food packets while ordering.

๐Ÿ‘‡
โœจ Phase 1: Reservation

Order service calls the reservation API exposed on the store and the delivery services.

๐Ÿ‘‡
The individual services reserve the food packet (of the ordered food) and a delivery agent atomically (exclusive lock or atomic operation).

Upon reservation, the food packet and the agent become unavailable for any other transaction.

๐Ÿ‘‡
โœจ Phase 2:

Order service then calls the store and delivery services to atomically assign the reserved food packet and the delivery agent to the order.

Upon success assigning both to the order, the order is marked as successful, and the order service returns a 200 OK.

๐Ÿ‘‡
The end-user will only see "Order Placed" when the food packet is assigned, and the delivery agent is assigned to the order.

So, all 4 API calls should succeed for the order to be successfully placed.

๐Ÿ‘‡
Negative cases:

- If any reservation fails, the user will see "Order Not Placed"
- If the reservation is made but assigning fails, the user will see "Order Not Placed"

๐Ÿ‘‡
- If there is any transient issue in any service during the assignment phase, APIs will be retried by the order service.

- To not have a perpetual reservation, every reservation will have an expiration timer that will be large enough to cover transient outages.

๐Ÿ‘‡
Thus, in any case, an end-user will never experience a moment where we say that the order is placed, but it cannot be fulfilled in the backend.

๐Ÿ‘‡
This topic is extensively covered in my YT video; check it out. You can find the notes I used during the video attached ๐Ÿ‘‡โ€ and the link to the video is right here

Notes can be found here: drive.google.com/file/d/18q2ELrโ€ฆ
If you find this amusing, follow me and subscribe to my YT channel. I post 3 solid engineering videos every week.

Subscribe asliengineering.com

#AsliEngineering #SystemDesign #DistributedSystems
Thanks for reading :) Do RT if you found this helpful

Follow @arpit_bhayani for system design, engineering, outage dissections, and distributed systems.

Until next time, stay awesome :)

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Arpit Bhayani | #AsliEngineering

Arpit Bhayani | #AsliEngineering Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @arpit_bhayani

Mar 20
Harsh reality: It will take you 10 years to realize how great our college curriculum is and how critical the core Computer Science subjects are.

Do not get misguided by these "EdTech cum SalesTech" startups and influencers. Everything at scale just boils down to
- Operating system concepts: Mutex, Semaphores, Critical Sections, Thread, Sockets, etc

- Database Internals: Query optimization of Database

- Distributed Systems: Need I say anything here

- Computer Architecture: To understand why vertical scaling has a limit
- Networking: Securing infra, optimizing routing, BGP protocol, CDN, TCP, UDP, etc.

- Multimedia Processing: If you get to build a live streaming platform

to just name a few
Read 4 tweets
Aug 15, 2021
Ever wonder how replication happens between Master and Replica? How changes on Master propagates to Replica?

This is a short thread of how it happens

#systemdesign #distributedsystems

๐Ÿงต
Any write operation happening on the Master is logged in the Replication log file as an event. The format in which these events are logged in the Log file is called Replication Format.

The two common Replication formats:

- Statement-based format
- Row-based format
โœจ Statement-based Format

The Master records the operation as an event in its log, and when the Replica reads this log, it executes the same operation on its copy of data.

This way, the operation on the Master is executed on the Replica, which keeps it in sync with the Master.
Read 11 tweets
Aug 12, 2021
Just wrapped up my 1:1 call with one of my cohort-ian and we ended up building an infinitely scalable Distributed Task Scheduler, AWS CloudWatch Events, DKron, and Quartz Scheduler, in under 30 minutes.

When foundations are clear, no system is harder to design ๐Ÿ’ช

#systemdesign
The features we discussed and designed were:

- Infinite task ingestion
- 30 second SLA of execution
- Execution Framework that supports Binaries, Scripts, Remote Executions
- Fault tolerance of Scheduler Nodes
- Repeatability of tasks
- Exactly-once schedule and execution
The design we discussed did not just have random boxes of high-funda components but rather the actual tools and techs that we would be using, along with their pros and limitations. ๐Ÿ’ช
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(