DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Java and Low Latency
  • Implementing WOPI Protocol For Office Integration
  • The Curious Case of Dead-End-Lock(Deadlock) | MySql
  • Dynamic Schedulers and Custom Cross-Server Schedule Lock

Trending

  • How To Use ChatGPT API in Python for Your Real-Time Data
  • Memory Management in Java: An Introduction
  • Apache Flink
  • DevSecOps: Integrating Security Into Your DevOps Workflow

Transaction merging, locks, background threads and performance

Oren Eini user avatar by
Oren Eini
·
Jan. 02, 14 · Interview
Like (0)
Save
Tweet
Share
8.63K Views

Join the DZone community and get the full member experience.

Join For Free

the following code is taken from the leveldb codebase :

image

this code is responsible for merging concurrent transactions. it operates using a mutex and conditional variables. and the idea really impressed me when i first saw it.

i implemented that in voron because i think that this is a great way to gain higher concurrency rates while still having a single writer mode (which is much easier to work with).

however, as it turned out, this isn’t a really good idea in practice. the way conditional variables work, you lock the mutex, check your condition, and then wait on the conditional variable. waking up from a conditional variable means that you have re-acquired the mutex.

what i want to happen:

  • multiple concurrent threads will try to write at the same time.
  • one of them goes through and start writing, it takes all the pending writes and write them to disk.
  • it then releases all the waiting threads whose work it already completed.
  • they all move on from there without having to wait on one another.

however, because of the way conditional variables are implemented, what will actually happen is that they each will wake up one at a time, acquiring the mutex lock, then releasing it. however, while they are being released, they compete with one another, and they also compete with the next write .

we profiled this approach, and the result was that we could see how we were spending pretty much all of our time in just synchronizing threads.

instead, we moved to a different approach, in which the first write will actually start off a new thread, dedicated to writing batches. the way it works, when a thread want to write a batch, it will add that to a queue and wake up the background writer thread, then wait on an event. the background writer will read all the current batches and merge them into a single write transaction.

when it is done, it will wake up all the sleeping writers. we tried to use taskcompletionsource for that, initially, but we found that the inline nature of tasks made that too expensive to use. instead, we use manualreseteventslim, and we explicitly wait / wake them. we even reuse events, so we don’t have to keep creating and disposing them.

the end result is that we have a pretty sequential process for actually doing concurrent writes, which turn it into a simple producers/consumer problem from threading perspective, and the actual writes into a simple write things out as fast as you can process them.

this also gives us the chance to do some re-ordering of operations to get better performance overall.

Lock (computer science)

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Java and Low Latency
  • Implementing WOPI Protocol For Office Integration
  • The Curious Case of Dead-End-Lock(Deadlock) | MySql
  • Dynamic Schedulers and Custom Cross-Server Schedule Lock

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: