🐝
Mess around software system design
  • README
  • ArchitectureTradeOffAnalysis
    • Estimation
    • Middleware
    • Network
    • Server
    • Storage
  • Conversion cheat sheet
  • Scenarios
    • TinyURL
      • Estimation
      • Flowchart
      • Shortening mechanisms
      • Rest API
      • Performance
      • Storage
      • Follow-up
    • TaskScheduler
      • JDK delay queue
      • Timer based
      • RabbitMQ based
      • Kafka-based fixed delay time
      • Redis-based customized delay time
      • MySQL-based customized delay time
      • Timer TimingWheel
      • Industrial Scheduler
      • Workflow Engine
      • Airflow Arch
    • GoogleDrive
      • Estimation
      • Flowchart
      • Storage
      • Follow-up
    • Youtube
      • Estimation
      • Flowchart
      • Performance
      • Storage
      • Follow-up
      • Netflix
    • Uber
      • Estimation
      • Rest api
      • Flowchart
      • KNN algorithms
      • Geohash-based KNN mechanism
      • Redis implementation
      • Storage
    • Twitter
      • Estimation
      • Flowchart
      • Storage
      • Scalability
      • Follow-up
    • Instant messenger
      • Architecture overview
      • Presence
      • Unread count
      • Notifications
      • Read receipt
      • Large group chat
      • Storage-Offline 1:1 Chat
      • Storage-Offline group chat
      • Storage-Message roaming
      • NonFunc-Realtime
      • NonFunc-Reliability
      • NonFunc-Ordering
      • NonFunc-Security
      • Livecast-LinkedIn
    • Distributed Lock
      • Single machine
      • AP model based
      • CP model based
      • Chubby-TODO
    • Payment system
      • Resilience
      • Consistency
      • Flash sale
    • Key value store
      • Master-slave KV
      • Peer-to-peer KV
      • Distributed cache
  • Time series scenarios
    • Observability
      • TimeSeries data
      • Distributed traces
      • Logs
      • Metrics
      • NonFunc requirments
  • Search engine
    • Typeahead
    • Search engine
    • Distributed crawler
      • Estimation
      • Flowchart
      • Efficiency
      • Robustness
      • Performance
      • Storage
      • Standalone implementation
      • Python Scrapy framework
    • Stream search
  • Big data
    • GFS/HDFS
      • Data flow
      • High availability
      • Consistency
    • Map reduce
    • Big table/Hbase
    • Haystack
    • TopK
    • Stateful stream
    • Lambda architecture
    • storm架构
    • Beam架构
    • Comparing stream frameworks
    • Instagram-[TODO]
  • MicroSvcs
    • Service Registry
      • Flowchart
      • Data model
      • High availability
      • Comparison
      • Implementation
    • Service governance
      • Load balancing
      • Circuit breaker
      • Bulkhead
      • Downgrade
      • Timeout
      • API gateway
      • RateLimiter
        • Config
        • Algorithm comparison
        • Sliding window
        • Industrial impl
    • MicroSvcs_ConfigCenter-[TODO]
    • MicroSvcs_Security
      • Authentication
      • Authorization
      • Privacy
  • Cache
    • Typical topics
      • Expiration algorithm
      • Access patterns
      • Cache penetration
      • Big key
      • Hot key
      • Distributed lock
      • Data consistency
      • High availability
    • Cache_Redis
      • Data structure
      • ACID
      • Performance
      • Availability
      • Cluster
      • Applications
    • Cache_Memcached
  • Message queue
    • Overview
    • Kafka
      • Ordering
      • At least once
      • Message backlog
      • Consumer idempotency
      • High performance
      • Internal leader election
    • MySQL-based msg queue
    • Other msg queues
      • ActiveMQ-TODO
      • RabbitMQ-TODO
      • RocketMQ-TODO
      • Comparison between MQ
  • Traditional DB
    • Index data structure
    • Index categories
    • Lock
    • MVCC
    • Redo & Undo logs
    • Binlog
    • Schema design
    • DB optimization
    • Distributed transactions
    • High availability
    • Scalability
    • DB migration
    • Partition
    • Sharding
      • Sharding strategies
      • Sharding ID generator overview
        • Auto-increment key
        • UUID
        • Snowflake
        • Implement example
      • Cross-shard pagination queries
      • Non-shard key queries
      • Capacity planning
  • Non-Traditional DB
    • NoSQL overview
    • Rum guess
    • Data structure
    • MySQL based key value
    • KeyValueStore
    • ObjectStore
    • ElasticSearch
    • TableStore-[TODO]
    • Time series DB
    • DistributedAcidDatabase-[TODO]
  • Java basics
    • IO
    • Exception handling
  • Java concurrency
    • Overview
      • Synchronized
      • Reentrant lock
      • Concurrent collections
      • CAS
      • Others
    • Codes
      • ThreadLocal
      • ThreadPool
      • ThreadLifeCycle
      • SingletonPattern
      • Future
      • BlockingQueue
      • Counter
      • ConcurrentHashmap
      • DelayedQueue
  • Java JVM
    • Overview
    • Dynamic proxy
    • Class loading
    • Garbage collection
    • Visibility
  • Server
    • Nginx-[TODO]
  • Distributed system theories
    • Elementary school with CAP
    • Consistency
      • Eventual with Gossip
      • Strong with Raft
      • Tunable with Quorum
      • Fault tolerant with BFT-TODO
      • AutoMerge with CRDT
    • Time in distributed system
      • Logical time
      • Physical time
    • DDIA_Studying-[TODO]
  • Protocols
    • ApiDesign
      • REST
      • RPC
    • Websockets
    • Serialization
      • Thrift
      • Avro
    • HTTP
    • HTTPS
    • Netty-TODO
  • Statistical data structure
    • BloomFilter
    • HyperLoglog
    • CountMinSketch
  • DevOps
    • Container_Docker
    • Container_Kubernetes-[TODO]
  • Network components
    • CDN
    • DNS
    • Load balancer
    • Reverse proxy
    • 云中网络-TODO
  • Templates
    • interviewRecord
  • TODO
    • RecommendationSystem-[TODO]
    • SessionServer-[TODO]
    • Disk
    • Unix philosophy and Kafka
    • Bitcoin
    • Design pattern
      • StateMachine
      • Factory
    • Akka
    • GoogleDoc
      • CRDT
Powered by GitBook
On this page
  • Cache aside
  • Flowchart
  • Cons: Data inconsistency
  • Double delete
  • Flowchart
  • Pros: Data consistency
  • Cons: Low cache hit rate
  • Read through
  • Flowchart
  • Cons: Data inconsistency
  • Asynchronous version
  • Write through
  • Flowchart
  • Cons: Data inconsistency
  • Asynchronous version
  • Write back
  • Flowchart
  • Pros
  • Cons: Data loss
  • Refresh ahead
  • Flowchart
  • Pros
  • Cons: Additional components

Was this helpful?

  1. Cache
  2. Typical topics

Access patterns

PreviousExpiration algorithmNextCache penetration

Last updated 1 year ago

Was this helpful?

Cache aside

Flowchart

  • Doing nothing special when using cache. Treat the database and cache as independent data sources.

  • First write DB, then cache; read from cache, then DB.

  • Most widely used pattern in distributed applications. Popular cache frameworks such as Redis / Memcached opt this approach by default.

Cons: Data inconsistency

  • Data inconsistency could happen in multiple cases:

    • Two parallel write

    • One read and one write

  • Possible solution: Add one delayed delete after the immediate delete

Double delete

Flowchart

Pros: Data consistency

  • Since the second delete is scheduled a bit long after the first one, setting cache operation should already be eliminated.

Cons: Low cache hit rate

Read through

Flowchart

  • In read path, cache will act on behalf of client; on write path, it is the same as cache aside.

Cons: Data inconsistency

  • Since it shares the same write path as cache aside, it has the same data inconsistency issue with cache aside.

Asynchronous version

  • For the step to update cache, put it in an asynchronous job. This step will only improve the perf a lot when the cached item is large.

Write through

  • In the read path, it is the same as cache aside; in the write path, cache will act on behalf of client.

Flowchart

Cons: Data inconsistency

  • It has similar problem.

Asynchronous version

  • For the step to update cache, put it in an asynchronous job. This step will only improve the perf a lot when the cached item is large.

Write back

  • Suitable for high read & write throughput system. Used more often in operating system's write to cache

    • Linux page cache algorithm

    • Asynchronously write message to disk in message queue

Flowchart

  • When updating data, only update cache. When cache keys expire, these entries will be persisted to DB.

Pros

Data consistency

  • This pattern could greatly reduce the inconsistency problem if handled properly.

    • When there is no data in DB, then cache is source of truth

    • When there is data in DB, as long as SETNX is used, the "write back" operation on read path will not result in cache & DB inconsistency.

  • SETNX: Set if not exist

    • Update cache if there is no entry

    • Skip is there is already an entry

Performance

  • Since the application writes only to the caching service, it does not need to wait till data is written to the underlying data source. Read and write both happens at the caching side. Thus it improves performance.

Cons: Data loss

  • If cache suddenly crashes, then the data cached inside will all be lost.

Refresh ahead

  • This pattern is actually similar to write back. But it writes to DB instead of cache, and it will watch DB binlog for updating cache.

Flowchart

Pros

Data consistency

  • Please refer to the SETNX solution in "Write back" approach.

Performance

  • Please refer to the explanation in "Write back" approach.

Cons: Additional components

  • Requires an additional components for watch binlogs

Cache aside
Flowchart
Cons: Data inconsistency
Double delete
Flowchart
Pros: Data consistency
Cons: Low cache hit rate
Read through
Flowchart
Cons: Data inconsistency
Asynchronous version
Write through
Flowchart
Cons: Data inconsistency
Asynchronous version
Write back
Flowchart
Pros
Data consistency
Performance
Cons: Data loss
Refresh ahead
Flowchart
Pros
Data consistency
Performance
Cons: Additional components
Singleflight
Flowchart
Cache aside pattern
Cache aside data inconistency
Cache aside data inconistency
Cache read through
Cache read through
Cache write through
Cache read through
write back pattern
write back pattern
write back pattern