ruby
43 lines · 9 steps
Atomic money transfers with Rails transactions
A service object moves funds between two accounts atomically using row locks and a database transaction.
Explained by
highlit
1class FundsTransfer
2 class InsufficientFundsError < StandardError; end
3
4 def initialize(source:, destination:, amount:)
5 @source = source
6 @destination = destination
7 @amount = amount
8 end
9
10 def call
11 ActiveRecord::Base.transaction do
12 @source.lock!
13 @destination.lock!
14
15 raise InsufficientFundsError if @source.balance < @amount
16
17 @source.update!(balance: @source.balance - @amount)
18 @destination.update!(balance: @destination.balance + @amount)
19
20 transfer = Transfer.create!(
21 source_account: @source,
22 destination_account: @destination,
23 amount: @amount,
24 status: :completed
25 )
26
27 LedgerEntry.create!(account: @source, transfer: transfer, change: -@amount)
28 LedgerEntry.create!(account: @destination, transfer: transfer, change: @amount)
29
30 transfer
31 end
32 rescue InsufficientFundsError
33 errors << "Source account has insufficient funds"
34 false
35 rescue ActiveRecord::RecordInvalid => e
36 errors << e.record.errors.full_messages.to_sentence
37 false
38 end
39
40 def errors
41 @errors ||= []
42 end
43end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Wrapping mutations in a transaction guarantees that a partial failure rolls everything back.
- 2Pessimistic row locks serialize concurrent transfers so two requests can't double-spend the same balance.
- 3Rescuing specific exceptions lets a service return a clean false-or-result instead of leaking raw errors.
Related explainers
ruby
class MetricSeries def initialize(readings, window: 5) @readings = readings @window = window
Rolling averages with each_cons in Ruby
sliding-window
enumerable
data-smoothing
Intermediate
7 steps
javascript
async function mapWithConcurrency(items, limit, worker) { const results = new Array(items.length); let nextIndex = 0;
Bounded-concurrency async map in JavaScript
concurrency
async-await
promises
Intermediate
7 steps
go
package config import ( "encoding/json"
Parsing untyped JSON config in Go
json parsing
type assertions
error handling
Intermediate
7 steps
java
public final class Debouncer<T> { private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(runnable -> {
How a debouncer collapses bursts in Java
debounce
concurrency
scheduling
Intermediate
9 steps
ruby
class Api::V1::ArticlesController < Api::V1::BaseController def index articles = Article .published
Building a JSON:API endpoint in Rails
serialization
eager-loading
pagination
Intermediate
7 steps
ruby
class Money include Comparable CURRENCY_SYMBOLS = { usd: "$", eur: "€", gbp: "£" }.freeze
Building an immutable Money value object in Ruby
value-object
immutability
comparable
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/atomic-money-transfers-with-rails-transactions-explained-ruby-3aee/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.