DEV Community

yanjun qiu
yanjun qiu

Posted on

Day 3: Understanding L2 Caching and the Oracle Callback Pattern

Core Concept

In this stage of my learning, I have focused on organizing the most critical components of the blockchain's evolution from a "ledger" to a "world computer." These concepts are categorized into the Application Layer, the Architecture Layer, and the Connectivity Layer.

Application Layer: The Three Pillars of Web3

  • DeFi (Decentralized Finance): A set of financial protocols running on public blockchains, where the core principle is "Code is Law." It replaces traditional financial intermediaries like banks, stock exchanges, and brokers with smart contracts.

  • NFT (Non-Fungible Token): Unlike Bitcoin, which is divisible and interchangeable, every NFT is unique. It serves as the underlying proof of "scarcity" and "ownership" in the digital world, with use cases extending from artwork to in-game assets and Real World Assets (RWA).

  • DAO (Decentralized Autonomous Organization): A new form of collective organization. Members exercise voting rights by holding tokens, and the organization’s rules and treasury management are executed automatically by on-chain code rather than traditional administrative mandates.

Architecture Layer: The Evolution of L1 and L2

  • L1 (Layer 1 - The Base Layer): Refers to the primary blockchain network, such as Ethereum or Bitcoin. It is responsible for final settlement, security, and decentralization. However, due to performance constraints, L1s often face congestion and high transaction fees.

  • L2 (Layer 2 - The Scaling Layer): Secondary protocols built on top of L1 (such as Arbitrum or Optimism). L2s process thousands of transactions off-chain and then bundle the results to be sent back to L1 for verification. The goal is to achieve high scalability without sacrificing security.

Connectivity Layer: Oracle

Oracle: Blockchain is a closed, deterministic system; it cannot perceive the outside world (such as tomorrow's weather or current stock prices). Oracles act as "middleware," securely feeding real-world data into smart contracts and breaking the "silo effect" of the blockchain.

Mental Model / Abstraction

To quickly grasp these Web3 concepts, we can draw analogies from traditional backend architectures:

L1 & L2: The Main DB and "Write-Back Cache"

  • L1 as the "Global, Single-Source Production Main Database": Think of L1 as an extremely expensive financial-grade database with very low write TPS but the highest levels of consistency and physical security. Every write (transaction) requires consensus across tens of thousands of nodes globally, making it costly and slow.

  • L2 as a "Write-Back Cache / Buffer with Batching": L2 acts like a high-performance caching layer sitting in front of the main database.

    • Fast Response: User requests are first completed within the L2 cache, reducing response times from minutes to milliseconds.
    • Asynchronous Submission: L2 does not write every single transaction to L1 immediately. Instead, it aggregates (Rolls up) and compresses thousands of transactions over a period of time, then performs an Asynchronous Batch Commit to the L1 main DB.
    • Eventual Consistency: Just like syncing a cache to a database, L2 ensures that the final state recorded on L1—the "Source of Truth"—is verifiable and immutable.

Oracle: The "Deterministic Input Adapter" in Distributed Systems

  • Mental Model: Tainted External Data Sources and Consensus Consistency. In backend development, if we need to execute logic based on "weather," we simply call weather_api.get() in our code. In the "deterministic state machine" of a blockchain, however, this would be disastrous.

  • The Analogy: Imagine a distributed computing cluster with 1,000 nodes.

  • The Conflict Scenario: If a smart contract says "Refund if it rains," and every node calls a weather API at different times or from different locations, some might get "Sunny" while others get "Rainy." The cluster would never reach consensus, and the ledger would halt or fork.

  • The Solution: An Oracle acts as the cluster’s only authoritative, digitally signed "Deterministic Input Gateway." It packages a fact—such as "Temp in Shanghai at 12:00 is 25°C"—into a signed snapshot and feeds it onto the chain. This ensures every node sees the exact same input, thereby guaranteeing deterministic calculation results.

Top comments (0)