0%

IQ

What is the IQ Framework?

The IQ framework is a solution designed for Cache-Augmented SQL (CASQL) systems, which combine relational databases (RDBMS) and key-value stores (KVS) to boost performance by caching database query results. However, CASQL systems often face challenges related to stale data and race conditions. The IQ framework ensures strong consistency while maintaining high performance.

Challenges in CASQL Systems

  1. Stale Data in Cache:

    • Cached data in the KVS can become outdated if updates to the RDBMS are not properly synchronized.

    • For example, if a record in the database is modified, but the corresponding cache entry isn’t updated, subsequent reads might return incorrect values.

  2. Concurrency Issues:

    • Multiple sessions accessing and modifying the same key in KVS concurrently can lead to inconsistent results.

    • Example:

      • One session updates a value while another session modifies it based on outdated data.
  3. RDBMS and Cache Coordination:

    • While RDBMS ensures transactional consistency, KVS often lacks this capability, making it difficult to synchronize their states.

Key Features of the IQ Framework

  1. Lease Mechanism: Inhibit (I) and Quarantine (Q):
    1. I Lease (for reads):
      1. Ensures that only one session can query the RDBMS for a cache miss and update the KVS.
      2. Other sessions attempting to read the same key must “back off” and wait.
    2. Q Lease (for writes):
      1. Required for modifying, deleting, or incrementally updating keys in the KVS.
      2. If an I lease exists, the Q lease invalidates it to ensure the write operation’s integrity.
      3. The KVS ignores I’s write operation because this I lease is no longer valid.
  2. Lease Expiry:
    1. A lease for a key has a fixed life time and is granted to one KVS connection (thread) at a time.
    2. Expired leases are automatically released, ensuring system availability.
    3. The finite life time enables the KVS to release the lease and continue processing operations in the presence of node failures hosting the application.
  3. Session-based Model:
    1. The framework operates through sessions, similar to the two-phase locking protocol.
    2. Leases can be acquired either before or during an RDBMS transaction, providing flexibility.

Implementing ACID Properties

原子性 (Atomicity): IQ 框架确保事务的操作同时在数据库 (RDBMS) 和缓存 (KVS) 中执行。也就是说,操作不会只在数据库中完成而没有更新缓存。这种设计假设 KVS 中的数据是 RDBMS 数据的一部分,因此如果遇到问题,可以直接删除 KVS 中的数据来保持一致。

一致性 (Consistency): IQ 框架保证事务在数据库和缓存中的数据状态从一个有效状态变为另一个有效状态。如果数据库的事务回滚 (abort),那么缓存中的操作也不会被应用,确保不会留下无效的缓存数据。

隔离性 (Isolation): 即使有多个会话 (session) 同时执行,IQ 框架也让每个会话看起来像是独立执行的,避免了并发问题。例如,即使两个用户同时读写相同的数据,他们看到的结果也是正确且一致的。

持久性 (Durability): 持久性是由数据库 (RDBMS) 提供的,而缓存 (KVS) 则作为数据库的一部分镜像。KVS 存储的数据是在内存中的副本,但一旦数据库中的事务提交,数据就会被持久保存。

CAS 操作只能保证单一操作的原子性,但无法在多个并发会话中保证强一致性。 由于数据库和缓存系统中的操作顺序可能不一致,会导致数据不同步。

在并发场景下:CAS 无法感知其他会话在其读取后对数据的更改。 多个会话同时执行 CAS 操作时,可能导致更新丢失或顺序混乱,如本例中 S2 的更新被 S1 覆盖。

Q 租约用于写操作,确保某一时刻只有一个会话能够修改目标键值。如果某个键值已有 Q 租约,其他会话(如 S1)会被要求退避(back off)或中止操作。

img

Invalidate

What is Snapshot Isolation?

Snapshot isolation is a multi-version concurrency control mechanism commonly used in RDBMS to allow concurrent transactions to execute efficiently. It guarantees:

  1. Consistent Snapshot: All reads in a transaction observe the same consistent state of the database, as it existed at the transaction’s start.
  2. Conflict Detection: A transaction can only commit if its updates do not conflict with updates made by other transactions since its snapshot was taken.

The Problem

Snapshot isolation can cause a race condition between a write session (S1) and a read session (S2) when KVS is involved. The issue unfolds as follows:

  1. Write Session (S1):

    • S1 modifies the RDBMS and triggers a delete operation in the KVS to invalidate outdated key-value pairs.

    • S1 commits the transaction after completing its changes in the RDBMS.

  2. Read Session (S2):

    • S2 starts after S1’s delete operation in the KVS. It observes a KVS miss for a key-value pair because S1 has invalidated it.

    • S2 queries the RDBMS to recompute the key-value pair. However, because snapshot isolation allows S2 to read an older snapshot of the database, it retrieves outdated (stale) data.

    • S2 inserts this stale data back into the KVS before S1 commits its changes to the RDBMS.

  3. Inconsistency:

    • After both sessions complete, the KVS contains a stale key-value pair inconsistent with the RDBMS, leading to incorrect results for future reads.

Solution

img

I Lease (Inhibit Lease):

  • Used by read sessions (e.g., S2).
  • When a read session observes a KVS miss, it requests an I lease for the key (k_j) from the KVS server.
  • The I lease allows the read session to query the RDBMS, compute a value, and insert the computed key-value pair into the KVS.
  • If a Q lease is already in place, the I lease is denied, and the read session is told to back off and retry later.

Q Lease (Quarantine Lease):

  • Used by write sessions (e.g., S1).
  • When a write session plans to invalidate a key in the KVS, it requests a Q lease for the key (k_j).
  • The Q lease prevents other sessions (including those holding I leases) from modifying or inserting the key in the KVS.
  • Multiple Q leases can be granted for the same key since deleting a key is idempotent (doesn’t create conflicts).

Optimization

The Problem

  • In the original scenario, write sessions (e.g., S1) immediately delete key-value pairs in the KVS as soon as they acquire a Q lease (e.g., Step 1.3 in Figure 3).
  • This can cause read sessions (e.g., S2) to encounter KVS misses, triggering redundant operations like querying the RDBMS, recalculating values, and reinserting them into the KVS.

The Proposed Optimization

Deferring Key Deletion Until Write Commit

  1. Key Changes:

    • Instead of deleting the key immediately in Step 1.3, the write session (S1) holds the Q lease and defers the deletion until the write session commits (Step 1.5).

    • While S1 is mid-flight, the invalidated key-value pair remains in the KVS for other read sessions (S2) to observe.

  2. Handling KVS Hits:

    • Read sessions like S2 that encounter a KVS hit consume the “stale” key-value pair, treating it as valid.

    • This is acceptable because S2’s actions can be serialized to occur before S1, which is still in progress and has not yet committed its RDBMS changes.

  3. Handling Write Aborts:

    • If a write session (S1) encounters an exception and aborts, the Q lease is released without deleting the key.

    • The current key-value pair in the KVS remains valid and accessible to other sessions.

Implementation Details

  1. Versioning Concept:

    • The optimization can be conceptualized as maintaining a temporary version of the key-value pair for use by all sessions except the one currently invalidating it (S1).

    • Once S1 commits, the temporary version is removed.

  2. Abort Command:

    • If a write session (S1) aborts due to constraints or exceptions, an abort command releases all Q leases held by S1 without deleting the key-value pair.

    • Without this command, Q leases would expire naturally after a timeout, during which no other session could modify or access the key.

Re-Arrangement Window:

  • With this optimization, S2 and S1 can be re-arranged in a serializable schedule where S2 logically occurs before S1.
  • Without the optimization, the re-arrangement window shrinks to zero because S2 would have already queried the RDBMS for stale data, violating consistency.

Refresh and Incremental Update

Key Issues with Compare-and-Swap (CAS)

  • CAS Limitation:

    • CAS alone cannot ensure strong consistency. It provides atomic updates to a single key-value pair but does not coordinate these updates with RDBMS transactions.
  • Example (Figure 2):

    • KVS writes can occur either:

      1. Prior to the RDBMS transaction, or

      2. As part of the RDBMS transaction.

    • Problem: If the RDBMS transaction aborts, the KVS will retain the modified key-value pair, potentially exposing dirty reads to other sessions.

  • Figure 6 (Dirty Read Problem):

    • Write session S1 modifies a key-value pair in KVS.
    • S1’s transaction later aborts, but the intermediate KVS value is consumed by a read session S2 before the rollback, leading to inconsistencies.
  • Developer Responsibility:

    • Without additional mechanisms, developers must implement complex logic to restore KVS key-value pairs to their original values when RDBMS transactions abort.

Race Conditions with Incremental Updates (δ Operations)

  • Figure 7 (Snapshot Isolation with δ Operations):

    • Write session S1 updates the RDBMS and KVS using an incremental update (e.g., appending to a value).
    • Concurrently, read session S2 queries the RDBMS and overwrites the key-value pair in the KVS.
    • Result: The KVS reflects inconsistent state, as S2’s overwrite may invalidate S1’s incremental change.
  • Figure 8 (Reordering KVS Operations):

    • Delaying KVS updates until after the RDBMS transaction doesn’t solve the problem.
  • Example in Figure 8:

    • S1 appends a change to a value based on its RDBMS view.
    • S2 modifies the RDBMS during S1’s execution, which S1 unknowingly incorporates into its KVS update.
    • Problem: S2’s modifications are reflected twice in the KVS, introducing inconsistencies.

Solution

Key Concepts in the Solution

img

  1. Q Leases for Write Sessions:

    • A Q lease must be obtained for each key-value pair that a session intends to update.

    • This prevents race conditions by locking the key-value pair until the session completes its operations.

  2. Steps for Write Sessions:

    • Step 1: Obtain Q leases for the keys to be updated before committing the RDBMS transaction. This can happen:

      • Before starting the RDBMS transaction.

      • As part of the RDBMS transaction.

    • Step 2: Write the updated key-value pairs to the KVS after committing the RDBMS transaction.

    • Step 3: Release the Q leases once the KVS is updated.

    • Automatic Cleanup: If a Q lease expires, the KVS deletes the associated key-value pair to avoid stale data.

  3. Command Design for Write Operations:

    • QaRead (Quarantine-and-Read):

      • Acquires a Q lease on the referenced key and reads its value from the KVS.

      • If a Q lease for the same key is already held by another session, the requesting session receives an abort message, must roll back its RDBMS transaction, release all leases, back off, and retry later.

      • If no value exists in the KVS (a KVS miss), the application can:

        • Skip updating the key, or
        • Query the RDBMS, compute a new value, and insert it using SaR (below).
      • If a QaRead lease encounters an I lease held by a read session, it invalidates the I lease to prevent race conditions.

    • SaR (Swap-and-Release):

      • Updates the value of a key in the KVS with the new value and releases the Q lease.
      • If the new value is null, the Q lease is simply released without updating the KVS.

Handling Race Conditions

  1. Q Leases for Concurrent Write Sessions:

    • If two write sessions request Q leases for the same key, the KVS resolves the conflict by:

      • Aborting one session.
      • Ensuring the aborted session retries later, serializing its updates after the session holding the Q lease.
    • This guarantees a valid serial schedule in the RDBMS and KVS.

  2. Read Sessions and I Leases:

    • Read sessions use I leases to avoid race conditions when querying the KVS.

    • If a write session issues a QaRead that encounters an existing I lease, the I lease is invalidated to ensure the KVS reflects the latest updates from the RDBMS.

Integration with Two-Phase Locking

  • The Q lease mechanism resembles two-phase locking:

    1. Growing Phase: The session acquires all necessary Q leases using QaRead before committing its RDBMS transaction.

    2. Shrinking Phase: The session releases all Q leases using SaR after committing its RDBMS transaction.

  • Flexibility:

    • A session can issue QaRead commands either before starting the RDBMS transaction or as part of the transaction.

Key Concepts of Incremental Updates

  1. Incremental Update Command: IQ-δ:

    • Purpose: Allows a write session to perform an incremental update, such as appending data to an existing key-value pair.

    • Syntax: IQ-δ(ki, δi)

      • ki: The key to be updated.
      • δi: The incremental change to apply (e.g., the value to append).
  2. Similarities to QaRead:

    • Q Lease Requirement: Before issuing the IQ-δ command, the session must obtain a Q lease for the key ki to ensure exclusive access.

    • Abort on Conflict:

      • If another session already holds a Q lease on the same key (ki), the KVS returns an abort message.

      • The write session must:

        1. Release all its leases.

        2. Abort its ongoing RDBMS transaction (if any).

        3. Retry the operation later.

优化关键点总结

  1. 保留旧版本(Older Version):

    • 当写会话(S1)更新某键值对 (ki-vi) 时,KVS 暂时保留该键值对的旧版本 (ki-vi_old),直到 S1 提交。

    • 这避免了读会话在写会话更新期间遇到 KVS miss。

  2. 写会话的更新视图:

    • 写会话(S1)在更新期间必须能够看到自己的修改结果(ki-vi_new)。

    • KVS 确保为 S1 提供其最新的更新视图。

Questions

Why is it acceptable for invalidate to delete cache entries?

Consistency Assurance: The cache entry being invalidated represents stale data that is no longer consistent with the current state of the RDBMS. Deleting it prevents read sessions from accessing outdated information.

How is a lease different than a lock?

  • Lease: Has a fixed lifetime and expires automatically after a certain duration. This makes leases useful in distributed systems where failures or delays could otherwise cause indefinite blocking.
  • Lock: Typically remains active until explicitly released, which can lead to deadlocks or indefinite resource contention if not managed properly.

True or False: IQ leases require changes to the RDBMS software.

False:

IQ leases do not require changes to the RDBMS software.

Instead, they extend the functionality of the Key-Value Store (KVS) by introducing new lease-based commands (e.g., QaRead and SaR) to coordinate operations between the KVS and the RDBMS. This design leverages existing RDBMS features without altering its underlying implementation.

What factors does CAMP consider when selecting a victim?

H(p) = L + size(p) / cost(p)

What is the definition of cost? Provide an example.

  • Computation Time: The time required to regenerate or recompute the data if it is evicted from memory.
  • Access Latency: The time it would take to fetch the data from disk or another slower storage tier.
  • Importance: The priority or weight assigned to the data based on how frequently or critically it is used.

How does CAMP insert a key-value pair in memory?

When a new key-value pair p needs to be inserted into memory, CAMP performs the following steps:

1. Check Cache Capacity

  • If there is enough memory to store the new key-value pair:
    • The pair is inserted directly into the appropriate priority group based on its cost-to-size ratio.
    • L is not updated.
  • If the cache is full:
    • CAMP selects one or more key-value pairs to evict based on their H(p) values.
    • It removes the pair(s) with the lowest H(p) values until there is sufficient space for the new pair.

2. Insert the New Pair

  • The new key-value pair p is added to the cache, and its H(p) value is computed and recorded.
  • The pair is placed in the appropriate priority queue based on its cost-to-size ratio.

With BG, what is the definition of Service Level Agreement, SLA?

SLA, e.g., 95% of requests to observe a response time equal to or faster than 100 msec with at most 0.1% of requests observing unpredictable data for 10 minutes.

Name one reason why a system may produce unpredictable data?

Eventual consistency. Or multiple threads are updating the same data item.

Reference: https://dl.acm.org/doi/abs/10.1145/2663165.2663318