DEV Community

Alex Aslam
Alex Aslam

Posted on

Event Sourcing vs. Blockchain: Where Immutability Matters

"Immutability isn’t magic—it’s a tradeoff."

Both event sourcing and blockchain promise:
🔒 Data that can’t be deleted
Full history tracking
🔍 Transparent auditing

But they solve very different problems. Use the wrong one, and you’ll pay—in complexity, performance, or coffee-fueled all-nighters.

Here’s how to choose.


1. Core Philosophies

Event Sourcing

  • What it is:
    • Append-only log of domain events (e.g., OrderPlaced, UserBanned)
    • Projections rebuild current state
  • Where it shines:
    • Debugging ("Why is this user’s balance $0?")
    • Temporal queries ("Show me yesterday’s dashboard")
    • Undo/redo workflows

Blockchain

  • What it is:
    • Decentralized ledger of transactions
    • Consensus-driven (no single owner)
  • Where it shines:
    • Distrustful environments (e.g., crypto, supply chains)
    • Provenance tracking (e.g., NFT ownership)
    • Censorship resistance

2. Key Differences

Event Sourcing Blockchain
Authority Centralized (your DB) Decentralized
Performance 100K+ events/sec 3-100 TPS (varies by chain)
Storage Cost $$ (DB storage) $$$$ (global replication)
Data Privacy Private by default Public by default
Use Case Internal systems Cross-organization

3. When to Use Event Sourcing

Ideal Scenarios

Audit-heavy domains (banking, healthcare)
Complex workflows (e.g., order cancellations)
Debugging production issues

Example

# Replay to debug a pricing bug
events = EventStore.for(order_id).up_to("2023-05-10")
PricingCalculator.replay(events) # Find where math broke
Enter fullscreen mode Exit fullscreen mode

4. When to Use Blockchain

Ideal Scenarios

Distrustful parties (e.g., supplier tracking)
Asset ownership (e.g., deeds, IP rights)
Censorship resistance (e.g., activist archives)

Example

// NFT ownership on Ethereum
function transfer(address to, uint256 tokenId) public {
  require(ownerOf(tokenId) == msg.sender);
  _transfer(msg.sender, to, tokenId); // Immutable record
}
Enter fullscreen mode Exit fullscreen mode

5. The Hybrid Approach

Case Study: Supply Chain

  • Blockchain:
    • Record cross-company transactions (e.g., "Shipment left factory")
  • Event Sourcing:
    • Power internal logistics (e.g., warehouse routing)

Implementation

# When shipment departs
event = ShipmentDispatched.new(tracking_id: "123")
EventStore.publish(event)
Blockchain.record(event) # Write to Hyperledger
Enter fullscreen mode Exit fullscreen mode

6. Pitfalls to Avoid

🚫 Using blockchain as a database:

  • 10K RPM writes will kill your wallet (and sanity) 🚫 Ignoring privacy in event sourcing:
  • GDPR requests become nightmares without redaction 🚫 Overengineering:
  • Not every app needs provable immutability

"But Our CTO Loves Blockchain!"

Ask:

  1. Do we need decentralization? (If not, event sourcing is cheaper)
  2. Who’s paying for gas fees? (Spoiler: Your ops team will revolt)
  3. Can we fake it? (A cryptographically signed event store often suffices)

Have you used either (or both)? Share your war stories below.

Top comments (0)