Concurrency
183 bites tagged Concurrency — interview questions with model answers, and 60-second explainers.
Write skew under snapshot isolation
Two transactions read an overlapping set, each writes disjoint rows, jointly violating an invariant. subtle anomaly snapshot isolation misses. thinking snapshot isolation equals serializable or that row locks alone fix it.
Two-Phase Locking and serializability
A growing phase only acquires locks, a shrinking phase only releases, no lock taken after one is freed. how locking enforces serial-equivalent schedules. confusing 2PL with the two-phase commit protocol.
How MVCC enables non-blocking reads
Writers create new row versions instead of overwriting, readers see a consistent snapshot, so readers never block writers. grasp of versioned rows and snapshots. claiming MVCC eliminates all locking including write conflicts.
Database deadlocks and how engines resolve them
Define a deadlock as mutual waiting on locks, name detection plus victim rollback, and prevention by consistent lock ordering. understanding circular lock waits. confusing a deadlock with a slow query or simple lock wait.
The lost update anomaly explained
Both transactions read the same value, each adds one, the second overwrite erases the first. read-modify-write race awareness. thinking the database auto-serializes plain reads, or that the final value is always correct.
Read Committed versus Serializable isolation levels
Name the four levels, map each anomaly (dirty read, non-repeatable read, phantom) to the level that blocks it. grasp of concurrency anomalies versus consistency cost. claiming Serializable blocks only phantoms.
Object store vs NFS consistency models
S3 gives strong read-after-write per object with no partial updates; NFS offers close-to-open with shared mutable files. distributed consistency depth.
Generating Video Thumbnails with AVAssetImageGenerator
Load AVAsset, configure AVAssetImageGenerator, request the time off the main thread, hop back to update UI. AVFoundation usage plus async UI hygiene. doing the decode synchronously on the main thread.
dispatch_async versus Task.detached for offloading work
Dispatch_async submits a closure to a queue; Task.detached starts an unstructured async task off the current actor without inheriting context. GCD versus structured concurrency.
Apply a CIFilter to a live camera feed
Use AVCaptureSession with AVCaptureVideoDataOutput, receive CMSampleBuffers in captureOutput via the sample buffer delegate on a serial queue, filter with CIImage, drop late frames. Capturing raw frames and the queue discipline.
What is an actor and how does it prevent data races
Actors serialize access to mutable state, reachable only via await; compiler blocks unsafe access. Token-refresh actor coalesces concurrent refreshes. Actor isolation as compiler-enforced safety.
How task cancellation works with async/await
Cancellation is a flag, not a kill; check Task.isCancelled or call checkCancellation, URLSession throws CancellationError automatically. Cooperative cancellation in Swift Concurrency.
What is @MainActor and why does it matter?
@MainActor is a global actor guaranteeing code runs on the main thread; annotate UI-updating types or methods so post-network state changes are main-thread safe. thread safety for UI updates.
Run two API calls concurrently with async let
Use async let to start two independent fetches that run concurrently, then await both, so total time approaches the slower call rather than the sum. structured concurrency for parallel work.
Design a unique referral code system
A unique DB constraint as the source of truth, generation via random retry or an encoded counter, and collision handling. uniqueness under concurrency and code-space sizing.
Design a referral feature's lifecycle and races
A referral entity with explicit states, a unique constraint on the invited user, and atomic transactions plus idempotency to prevent double credits. modeling a stateful flow with idempotency and concurrency safety.
Design a graceful worker pool in Go
Buffered job channel, fixed worker goroutines, WaitGroup to await in-flight work, context cancellation to stop intake. concurrency coordination with goroutines, channels, and context.
Cancellation and cleanup: Go context/errgroup vs Tokio
Go propagates cancellation via context.Context that goroutines must poll, with errgroup canceling siblings on first error; Tokio cancels by dropping futures, which stops them at await… structured-concurrency cancellation knowledge.
Go scheduler work-stealing and blocking syscalls
The GMP model runs goroutines (G) on OS threads (M) attached to logical processors (P); idle P's steal half of another P's run queue; on a blocking syscall the M detaches with its G. knowledge of the Go runtime scheduler internals.
Goroutines and channels versus ownership-based concurrency
Go uses cheap goroutines and CSP-style channels to coordinate by communication; Rust uses ownership plus Send/Sync to make data races a compile error. understanding of two concurrency philosophies.
Fearless concurrency: Rust compile-time vs Go runtime
Rust uses ownership plus Send/Sync to reject data races at compile time; Go encourages channels but still allows races, with the runtime race detector catching them at test… understanding of where each language catches concurrency bugs.
Rust Send and Sync marker traits explained
Send means a value can move across threads, Sync means a reference can be shared; Rc uses non-atomic refcounts, Arc uses atomic ones. understanding of compile-time thread-safety guarantees. confusing the two traits.
In-memory rate limiter middleware in Go
Use a token-bucket limiter (golang.org/x/time/rate), guard a per-client map with sync.Mutex, wrap http.Handler so requests over the limit get 429. rate limiting and middleware design.
Cancellation: Go context vs Rust sync stdlib
Go's context.Context threads a Done channel and deadline through call chains; Rust std has no built-in cancellation, so you wire an AtomicBool or channel and check it. cancellation propagation models.
Get Concurrency bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.