Locks, Queues and business workflows processing
Last week, we discussed Distributed Locking. Today, we’ll continue with locking but doing it differently: with a full backflip. We’ll discuss how to implement a simple in-process lock. What’s special about it? We’ll use the queue broker we delivered in Ordering, Grouping and Consistency in Messaging systems. And explain why. Then, we’ll take it back to a higher level, showing you how your business application can benefit from it. Does it sound intriguing? I hope it does, as we aim to learn and have fun. A quick recap about lockingLast week, we learned that locking is the simplest way to handle exclusive access to the resource. Using it, the actor says, “Until I finish, no other actor can access this resource!“. Others will need to wait or fail immediately. Who’s the actor? Think: user, service, node, processor, etc. Without locks, you can get unpredictable states - like a read model flipping from correct to incorrect or a file partially overwritten by multiple workers. Locks sacrifice a bit of parallelism and performance for the certainty that no two actors update the same resource simultaneously. In many cases, that’s the safest trade-off, especially if data correctness is a key factor. The basic flow looks like this: Lock definitionLet’s now define a high-level API for locking. I’ll use TypeScript again because I like it, and I have to choose some language. It can look as follows:
It has four methods:
All methods take options with a specific lock identifier. It can represent the locked resource id or any other arbitrary text format. Of course, we could be more creative and add more options, such as timeouts, but let’s keep it focused. Challenges with implementing lockingWhen implementing locking, we need to be sure we’ll acquire a specific lock for the single resource. That’s vulnerable to race conditions. We must also make it thread-safe, ensuring safety when multi-threads try to lock the same record. The last part is simpler in TypeScript, as it uses a JavaScript event loop and is single-threaded. Node.js authors thought handling tasks sequentially (if done well) could be faster than handling and managing multiple threads. Per MDN:
So if you’re doing a more classical approach like C#, Java, be aware that you’d need to use thread-safe constructions like ConcurrentDictionary, ConcurrentHashMap. Luckily, in Ordering, Grouping and Consistency in Messaging systems, we implemented the Queue Broker capable of handling a single task per group of tasks. We used it to handle idempotency, which is a similar, but slightly different goal. To recap, our Queue Broker allows multiple tasks to be enqueued and handled asynchronously. It works similarly as Amazon SQS FIFO ensures task with the same task group ID are processed in the strict order. Groups are independent, so tasks for one group don’t block tasks in another. The other tasks from the same group will wait in the queue to be processed. Our Queue Broker uses a single-writer pattern to ensure that scheduling happens sequentially. We can enqueue multiple tasks, but they’re not processed immediately; the background queue processing processes them. Thanks to that, we can control the concurrency. Let’s use it in our case! Implementing LocksLet’s use what we learn so far, starting with the Lock setup:
We defined the InProcessLock function that will generate a new Lock. It has a queue broker without any limits on parallel processing. The only concurrency limit that we want to enforce is for the specific lock id. We’re also keeping the currently held locks in map cache. Acquiring LockNow, let’s implement the acquire function:... Continue reading this post for free in the Substack app |
Older messages
Distributed Locking: A Practical Guide
Monday, December 23, 2024
If you're wondering how and when distributed locking can be useful, here's the practical guide. I explained why distributed locking is needed in real-world scenarios. Explored how popular tools
On getting the meaningful discussions, and why that's important
Thursday, December 19, 2024
To put our design into practice, we need to be able to persuade our colleagues, stakeholders, and other peers. Without the ability to explain and persuade, even the best design will not be applied. And
The Write-Ahead Log: The underrated Reliability Foundation for Databases and Distributed systems
Tuesday, December 10, 2024
The write-ahead log (WAL) is everywhere. Yet, many people miss it and are not aware of it. The simple idea powers reliability in databases, messaging systems, and distributed systems. Let's discuss
Applying Observability: From Strategy to Practice with Hazel Weakly
Monday, December 2, 2024
Have you considered applying observability but struggled to match the strategy with the tooling? Or maybe you were lost on how to do it? I have something for you!I had a great discussion with Hazel
Get full 30-day access to all Architecture Weekly content for free
Friday, November 29, 2024
Hey! Thank you for being a subscriber; that's much appreciated. ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏
You Might Also Like
Re: This took me 10 minutes and protects my privacy
Thursday, January 2, 2025
My New Year's resolution is to do a better job of protecting my identity online. Last year, billions of personal records were compromised due to data breaches. That's why I wanted to tell you
Edge 462: What is Fast-LLM. The New Popular Framework for Pretraining your Own LLMs
Thursday, January 2, 2025
Created by ServiceNow, the framework provides the key building blocks for pretraining AI models. ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏
The Evolution of Android Architecture Patterns
Thursday, January 2, 2025
View in browser 🔖 Articles The Evolution of Android Architecture Patterns As we welcome the New Year, dive into the transformative journey of Android architecture patterns. From MVC to MVI, explore how
🤖 Here’s How Robots are Farming Your Food — My 5 Video Game Resolutions for 2025
Wednesday, January 1, 2025
Also: Facebook Is Too Good at Suggesting Ads, and More! How-To Geek Logo January 1, 2025 Did You Know After the 1982 film ET: The Extra-Terrestrial featured Reese's Pieces prominently as a treat
Daily Coding Problem: Problem #1655 [Medium]
Wednesday, January 1, 2025
Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Google. What will this code print out? def make_functions(): flist = [] for i in [1, 2,
Ranked | The Most Popular Visualizations on Voronoi in 2024 🏆
Wednesday, January 1, 2025
We round up the most popular, most discussed, and most liked visualizations of 2024 on Voronoi, our data storytelling platform. View Online | Subscribe In 2024, there were over 3000 visualizations
Top Tech Deals 👀 Cheap TVs, Gaming Headphones, Apple Watch, and More!
Wednesday, January 1, 2025
Score an indoor smart camera, soundbar, and more at a big discount. How-To Geek Logo January 1, 2025 Top Tech Deals: Cheap TVs, Gaming Headphones, Apple Watch, and More! Score an indoor smart camera,
New U.S. DoJ Rule Halts Bulk Data Transfers to Adversarial Nations to Protect Privacy
Wednesday, January 1, 2025
THN Daily Updates Newsletter cover Full Stack Web Development ($54.99 Value) FREE for a Limited Time This book offers a comprehensive guide to full stack web development, covering everything from core
Detecting Parasites 🪲
Wednesday, January 1, 2025
A newsletter publisher takes on parasite SEO. Here's a version for your browser. Hunting for the end of the long tail • December 31, 2024 Detecting Parasites Our year-end award for best blog post
Final Chance to Claim Your Bonus Gift 🎁
Tuesday, December 31, 2024
Just sign up to VC+ by January 1st and we'll include a free gift. View email in browser OFFER ENDS JANUARY 1ST Last Chance to Claim Your Free Gift! The Global Forecast Series, presented by Inigo,