查看: 4288| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

[经验总结] Designing Data-Intensive Applications - Chapter 11 摘录读后感

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
在坛子里也混迹一段时间了,看到各路大神在分享经验和学习心得,最近开始研究System Design 通过这个帖子https://www.1point3acres.com/bbs ... read&tid=559285  后


开始研读《Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems》https://www.amazon.com/Designing ... nable/dp/1449373321


决定每读一个Chapter,就把自己的读书摘录和笔记分享到坛子里供大家一起探讨学习.


同时推荐这个Blog: https://www.jyt0532.com/toc/designing_data_intensive-application/
文章同步发到我自己的Blog: https://comeshare.net/category/study/system-design/



A complex system that works is invariably found to have evolved from a simple system that works. The inverse proposition also appears to be true: A complex system designed from scratch never works and cannot be made to work.—John Gall, Systemantics (1975)

  • Batch process is under the assumption all the data is Bounded, which means we know the finite size of the data we are dealing with, so it is know when the job is finished.
  • In reality, a lot of the data is unbounded. (because data keeps generated every second). This issue will force the batch process to divide data into “chunks”.
    • Which means the result/derived data is delayed based on the interval you are chosen.  (this is too slow for impatient users)
  • Continuously data process without interruptions(break into chunks) is the idea behind streaming processing.
    • Think of “Stream” as a never stop flow of water/river that keep feeding data in;
    • E.g. (stdin/stdout, file inputstream, TCP stream etc.)
    • “Event Stream” as a data management mechanism
Transmitting Event Streams
  • Batch processing: Input are Files  vs. Streaming Processing: Input are Events;
  • What is Event: a small, self-contained, immutable object containing the details of something that happened at some point in time.
  • Event generated by a producer(Publisher/Sender) and then processed by multiple consumers(subscribers/recipients).
    • Related “events” are grouped together by Topic/Stream.
  • Batch vs. Stream processing is kind like the difference between Pull and Push.
    • Batch: is the consumer keep Pull event from DB.
    • Stream: is the DB keep push Event to Consumer.
  • Traditional DB/(RDMS) is not designed for “Stream/Event” processing
  • Message Systems:
    • a producer sends a message containing the event, which is then pushed to consumers.
    • MQ vs. Unix Pipe or TCP
      • MQ allows many-to-many relationships (Producer vs. Consumer)
      • As Unix Pipe & TCP is usually one-to-one
    • What happens if the producers send messages faster than the consumers can process them?  Three options
      • Drop message;  Queue ; backpressure (flow control)
      • What if Queue is full ?
    • What happens if nodes crash or temporarily go offline—are any messages lost?
      • Still a trade off between C & A (Consistency and Availability)
    • Direct messaging from producers to consumers: (prone to loss data)
      • UDP multicast: used in financial industry for streams such as stock market feeds;
      • Brokerless message library: ZeroMQ.
      • StatsD & Brubeck: UDP messaging.
    • Message brokers: (aka. Message Queue, e.g. ActiveMQ, RabbitMQ)
      • A special type of DB that optimized for Message Streams;
      • Producer → Broker → Consumer
      • Better fault tolerance; Message could be persist to disk;
      • It all asynchronously;
    • Message brokers compared to databases: (JMS, AMQP)
      • In MQ Some even support 2PC (two-phase commit);
      • In MQ, data is deleted right-after message is been consumed;
      • In MQ, usually assume the Queue is short.
      • DB Secondary Indexes vs. MQ subset of topics
      • MQ has no support for queries, but notify clients when data change
      • E.g. RabbitMQ, ActiveMQ, HornetQ, Qpid, TIBCO Enterprise Message Service, IBM MQ, Azure Service Bus, and Google Cloud Pub/Sub.
    • Multiple consumers:
      • Load balancing:  arbitrarily assigned to worker/consumer; Good for parallel processing expensive work-load.
      • Fan-out: Each message is sent to all consumers/worker; (topic subscription in JMS, exchange bindings in AMQP)
  • Two patterns could be combined.
  • Acknowledgments and redelivery:
    • acknowledgments: a confirmation from a client that it has finished processing a message so that the broker can remove it from the queue.
      • Note: due to network issues, the ack. Could be lost, then cause the ordering of the message change (C: In general, when you using Queue, you shouldn’t have cared about the order at first place)
  • Partitioned Logs:
    • Transient messaging mindset: transient operation that leaves no permanent trace.  (Which is totally opposite than DB or FileSystem)
    • MQ is NOT idempotent: receiving a message is destructive if the acknowledgment causes it to be deleted from the broker;
    • Why can we not have a hybrid, combining the durable storage approach of databases with the low-latency notification facilities of messaging?
      • Yes, log-based message brokers.
    • Using logs for message storage:
      • Log: is simply an append-only sequence of records on disk.
      • Log-based Message broker: A producer sends a message by appending it to the end of the log, and a consumer receives messages by reading the log sequentially.
      • This log can be partitioned.
      • Each message is offset by a sequence number. (totally ordered)
        • Note: no ordering guarantee across partitions tho.

  • E.g. Apache Kafka, Amazon Kinesis Streams, and Twitter’s DistributedLog. (millions of MPS by partitioning)
  • Logs compared to traditional messaging*:
    • the broker can assign entire partitions to nodes in the consumer group, Then each client then consumes all the messages in the partitions it has been assigned.
    • JMS/AMQP style of message broker is preferable: Message is expensive, parallel processing, order doesn’t matter.
    • Log-based approach: high message throughput, where each message is fast to process and where message ordering is important.
  • Consumer offsets:
    • Similar to “log sequence number” in single-leader DB replication;
    • The message broker behaves like a leader database, and the consumer like a follower.
  • Disk space usage:
    • To prevent run out of storage, log is divided into segments, old segments are deleted or archived.
    • “Log” is kind of like a bounded-size buffer, if the consumer can’t keep up, the old message will be discarded. (aka. Circular buffer, ring buffer)
      • E.g. 6T HDD with 150MB/s write speed can buffer up to 11 hrs of messages.
  • When consumers cannot keep up with producers:
    • Three choices: dropping, buffering or backpressure(flow-control)
    • Consumers are independent from each other;
  • Replaying old messages:
    • it is a read-only operation that does not change the log.
    • Offset is under the consumer’s control. So, it have the freedom to go back to previous data/offset.
    • This made it easier for integration with other dataflows.
Databases and Streams
  • DB and Stream are correlated deeper; (e.g. write is an event)
  • The replication log is a stream of database write events, produced by the leader as it processes transactions.
  • Keeping Systems in Sync:
    • Often need to combine several different technologies in order to satisfy their requirements. (Write/Read/Search/Analytics etc.)
    • It is essential to keep all the data in-sync. if an item is updated in the database, it also needs to be updated in the cache, search indexes, and data warehouse. (usually through ETL processes.)
      • Or “Dual writes”, which prone to issue like race-conditiotion;

  • The key is to determine if we can only one “Source of Truth” (aka. Leader)
  • Change Data Capture:
    • CDC(Change Data Capture): is a process that extracts DB changes and puts it into other systems.
      • E.g. in the form of “Stream”

  • Implementing change data capture:
    • We call any “log consumer” a “derived data system”. The idea behind CDC is to ensure all those “derived data systems” got the up-to-date changes.
    • Essentially, CDC makes one database the leader (the one from which the changes are captured), and turns the others into followers.
    • Potential Mechanism: DB Triggers(Con:performance overheads), Parsing replication log(Con:schema changes);
    • Usually done asynchronously → replication lag
  • Initial Snapshot:
  • Log compaction: (e.g. Apache Kafka)
    • This is used when need add a new “derived data system”
    • This allows the message broker to be used for durable storage, not just for transient messaging.
  • API support for change streams: (e.g. RethinkDB, FIreBase, CouchDB, Meteor, VoltDB)
    • DBs engine started to support change streams;
    • A table will hold transactions but can’t be queried.
  • Event Sourcing:
    • event sourcing: a technique that was developed in the domain-driven design (DDD) community.
    • CDC vs. ES(Event Sourcing):  different level of abstraction.
      • CDC: application isn’t aware of CDC occurring, so it happens at a lower level.
      • ES: reflect things that happened at the application level.
    • ES is a powerful technique for data modeling, because it makes more sense to record a user’s action as immutable events, rather than the effect of the actions on a mutable DB.
    • Event sourcing is similar to the chronicle data model. (or “fact table”).
    • Deriving current state from the event log:
      • Applications need to take the events and transform it to an application state that is suitable for the user to view. (deterministic)
    • Commands and events:
      • First it comes as “Command” and then after it successfully executed, it becomes “Event” which is durable and immutable.
        • when the event is generated, it becomes a fact.
      • A consumer of the event stream is not allowed to reject an event;
      • any validation of a command needs to happen synchronously, before it becomes an event.
  • State, Streams, and Immutability:
    • Immutability is also what makes event sourcing and change data capture powerful.
    • Whenever you have a state that changes, that state is the result of the events that mutated it over time.
    • mutable state and an append-only log of immutable events do not contradict each other: they are two sides of the same coin.
      • the changelog, represents the evolution of state over time.
    • In terms of mathematical:
      • application state is what you get when you integrate an event stream over time;
      • a change stream is what you get when you differentiate the state by time;
    • Quote from Pat Helland:
      • Transaction logs record all the changes made to the database. High-speed appends are the only way to change the log. From this perspective, the contents of the database hold a caching of the latest record values in the logs. The truth is the log. The database is a cache of a subset of the log. That cached subset happens to be the latest value of each record and index value from the log.
    • Log compaction: bridging the distinction between log and DB state.  it retains only the latest version of each record, and discards overwritten versions.
    • Advantages of immutable events: (e.g. Account ledger)
      • particularly important in financial systems, it is also beneficial for many other systems.
      • capture more information than just the current state.
      • E.g. Shopping cart history with append-only event could help analytic in the future;
    • Deriving several views from the same event log:
      • You can derive several different read-oriented representations from the same log of events. (C: This idea is similar to the talk about Apache Kafka,built application around the Kafka stream)
      • Having an explicit translation step from an event log to a database makes it easier to evolve your application over time.  (C: enable old & new system running side by side)
      • Command Query Responsibility Segregation (CQRS): you gain a lot of flexibility by separating the form in which data is written from the form it is read.
    • Concurrency control:
      • The biggest downside of event sourcing and change data capture is asynchronous. → cause delay.
      • Potential Solutions:
        • “Reading your own writes”
        • “Implementing linearizable storage using total order broadcast”
    • Limitations of immutability:
      • Truly deleting data could be difficult because data liv in many places.
      • Deletion is more a matter of “making it harder to retrieve the data” than actually “making it impossible to retrieve the data.”
Processing Streams
  • Where streams come from (user activity events, sensors, and writes to databases).
  • How streams are transported (through direct messaging, via message brokers, and in event logs).
  • Last question is What can you do with Stream ? three major options
    • 1, write it to a database, cache, search index, or similar storage system so it can be used by other applications/clients/systems.
      • Kind like maintaining materialized views.
    • 2, push the events to users directly
    • 3, process one or more input streams to produce one or more output streams. (pipelining); processing streams to produce other, derived streams.
  • A block of Code that processes streams so called “Operator” or “Job”. (kind like Unique processes or MapReduce job)
    • Since the Stream never ends(unbounded), so sorting doesn’t make sense here, neither does sort-merge joins will be used.
    • Fault-tolerance mechanisms also need to be revised.
  • Use of Stream Processing
    • Monitoring System: Fraud detection, Trading System, Manufacturing System, Military and Intelligence systems.
    • Complex event processing(CEP): emerged from the 90s
      • CEP allows you to specify rules to search for certain patterns of events in a stream.
    • Stream analytics: (e.g. Apache Storm, Spark Streaming, Flink, Concord, Samza, and Kafka Streams, Google Cloud Dataflow and Azure Stream Analytics)
      • More oriented toward aggregations and statistical metrics over a large number of events
      • Stream analytics systems sometimes use probabilistic algorithms, such as Bloom filters.
    • Maintaining materialized views:
      • Derived data systems can be treated as maintaining materialized views.
    • Search on streams:
      • The percolator feature of Elasticsearch is one option for implementing this kind of stream search.
    • Message passing and RPC:
  • Reasoning About Time
    • Time “window”
    • Using the timestamps in the events allows the processing to be deterministic.
    • Event time versus processing time: (e.g. Star War movies)
      • Processing may be delayed.
      • Confusing event time and processing time leads to bad data.

    • Knowing when you’re ready:
      • need to be able to handle such straggler events that arrive after the window has already been declared complete.
        • 1, Ignore the straggler events;
        • 2, Publish a correction;
    • Whose clock are you using, anyway?
      • Need address Incorrect device clocks, log three timestamps:
        • The time at which the event occurred, according to the device clock
        • The time at which the event was sent to the server, according to the device clock
        • The time at which the event was received by the server, according to the server clock
    • Types of windows:
      • Tumbling window: fixed length, and every event belongs to exactly one window.
      • Hopping window: fixed length, but allows windows to overlap in order to provide some smoothing.
      • Sliding window: contains all the events that occur within some interval of each other.
      • Session window: has no fixed duration. But, grouping together all events relative to the same user that occur closely together in time. (e.g. website analytics)
  • Stream Joins
    • Similar to batch jobs; However, since new events can appear anytime on a stream makes joins on streams more challenging than in batch jobs.
    • three different types of joins: stream-stream joins, stream-table joins, and table-table joins.
    • Stream-stream join (window join):
      • a stream processor needs to maintain state.
    • Stream-table join (stream enrichment):
      • enriching the activity events with information from the database.
      • Instead of performing remote SQL queries, we can cache up a copy of DB. (In Memory hashtable or local disk index)
        • Need CDC to ensure the stream data is up-to-date;
      • A stream-table join is actually very similar to a stream-stream join, but in this case we have “table changelog stream” involved.
    • Table-table join (materialized view maintenance): (e.g. Tweets)
      • it maintains a materialized view for a query that joins two tables.
    • Time-dependence of joins:
      • Common: they all require the stream processor to maintain some state based on one join input, and query that state on messages from the other join input.
      • If the state changes over time, and you join with some state, what point in time do you use for the join ? (e.g. sales Tax calculation)
      • If the ordering of events across streams is undetermined, the join becomes nondeterministic;
      • slowly changing dimension (SCD):  addressed by using a unique identifier for a particular version of the joined record. (but this approach made log compaction impossible, because we need retain all version of the records)
  • Fault Tolerance
    • You can’t wait until a stream is finished to validate its output/result, since all the stream is unbounded and will never really finish/complete.
    • Microbatching and checkpointing:
      • Microbatching: break the stream into small blocks, and treat each block like a miniature batch process.  (e.g. Spark Streaming)  usually one second interval.
        • Smaller the batches size the greater overhead.
        • Larger batches size means longer delay of results.
        • implicitly provides a tumbling window equal to the batch size
      • Checkpointing: triggered by barriers in the message stream, similar to the boundaries between microbatches, but without forcing a particular window size.  (e.g. Apache Flink)
      • Both approaches won’t prevent external side effects after the results have been written into External Systems.
    • Atomic commit revisited:
      • Achieve “Exactly-Once” processing without transactions across heterogeneous technologies.
      • Idempotence:
        • Distributed transactions are one way of achieving that goal, but another way is to rely on idempotence.
        • if an operation is not naturally idempotent, it can often be made idempotent with a bit of extra metadata. (e.g. Kafka with some offset value)
      • Rebuilding state after a failure:
        • keep state local to the stream processor, and replicate it periodically.
        • sometimes the state can be rebuilt from the input streams.
Summary
  • discussed event streams, what purposes they serve, and how to process them.
    • Similar to “batch processing” but unbounded.
    • message brokers and event logs serve as the streaming equivalent of a filesystem.
  • Two types of Message brokers:
    • AMQP/JMS-style message broker: exact order is not important
    • Log-based message broker: order is kept.
      • Similar to log-structured storage engines
  • Where streams come from ?
    • user activity events, sensors providing periodic readings, and data feeds (e.g., market data in finance)
    • writes to a database as a stream: capture the changelog
      • Change Data Capture
      • Event Sourcing
  • DB as streams is very useful for integrating different systems
    • E.g.   search indexes, caches, and analytics systems.
  • Stream joins and fault tolerance:  By maintaining state as streams and replaying messages
    • searching for event patterns (complex event processing),
    • computing windowed aggregations (stream analytics),
    • keeping derived data systems up to date (materialized views).
  • three types of joins:
    • Stream-stream joins
    • Stream-table joins
    • Table-table joins
  • fault tolerance and exactly-once semantics
  • microbatching,
  • checkpointing,
  • transactions,
  • idempotent writes.


Designing Data-Intensive Applications - Chapter 1 摘录读后
https://www.1point3acres.com/bbs/thread-617831-1-1.html

Designing Data-Intensive Applications - Chapter 2 摘录读后感
https://www.1point3acres.com/bbs/thread-619627-1-1.html

Designing Data-Intensive Applications - Chapter 3 摘录读后感
https://www.1point3acres.com/bbs/thread-621149-1-1.html

Designing Data-Intensive Applications - Chapter 4 摘录读后感
https://www.1point3acres.com/bbs/thread-622790-1-1.html

Designing Data-Intensive Applications - Chapter 5 摘录读后感
https://www.1point3acres.com/bbs/thread-623896-1-1.html

Designing Data-Intensive Applications - Chapter 6 摘录读后感
https://www.1point3acres.com/bbs/thread-624433-1-1.html

Designing Data-Intensive Applications - Chapter 7 摘录读后感
https://www.1point3acres.com/bbs/thread-625587-1-1.html

Designing Data-Intensive Applications - Chapter 8 摘录读后感
https://www.1point3acres.com/bbs/thread-626563-1-1.html

Designing Data-Intensive Applications - Chapter 9 摘录读后感
https://www.1point3acres.com/bbs/thread-628100-1-1.html

Designing Data-Intensive Applications - Chapter 10 摘录读后感
https://www.1point3acres.com/bbs/thread-629666-1-1.html






补充内容 (2020-5-27 07:56):
最新补充在6楼: (以后更新修正不再发布这,而是直接更新到我Blog了)
https://www.1point3acres.com/bbs ... 21&pid=11635890

评分

参与人数 1大米 +1 收起 理由
litJordan + 1 很有用的信息!

查看全部评分


上一篇:github系统设计资料
下一篇:多读经典paper,来提高System Design的实力
🔗
jyt0532 2020-5-2 13:05:05 | 只看该作者
全局:
我發現你念得有點快
回复

使用道具 举报

🔗
 楼主| comeshare 2020-5-2 13:16:41 | 只看该作者
全局:
jyt0532 发表于 2020-5-2 13:05
我發現你念得有點快

你是在说真的么,还是在反讽我太慢了.😂.
回复

使用道具 举报

🔗
jyt0532 2020-5-2 13:43:52 | 只看该作者
全局:
comeshare 发表于 2020-5-2 13:16
你是在说真的么,还是在反讽我太慢了.😂.

我通常一章要讀一個月...
回复

使用道具 举报

🔗
 楼主| comeshare 2020-5-2 21:02:34 | 只看该作者
全局:
jyt0532 发表于 2020-5-2 13:43
我通常一章要讀一個月...

我的天,  一章读这么久?  一天2-3页的速度?
我好奇你是怎么个读法的. 我的approach是,快速读完一遍,做好摘录和笔记,然后接下来就开始只看摘录和笔记,再进行精华/总结/清理...争取把书越读越薄.  我现在第一轮摘录的笔记我觉得似乎已经太厚了, 600多页的书,现在笔记总共就有100+页了.
回复

使用道具 举报

🔗
 楼主| comeshare 2020-5-27 07:55:12 | 只看该作者
全局:
在坛子里也混迹一段时间了,看到各路大神在分享经验和学习心得,最近开始研究System Design
通过这个帖子https://www.1point3acres.com/bbs ... read&tid=559285  后


开始研读<Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems>https://amzn.to/2WYphy6


决定每读一个Chapter,就把自己的读书摘录和笔记分享到坛子里供大家一起探讨学习.


同时推荐这个Blog: https://www.jyt0532.com/toc/designing_data_intensive-application/
文章同步发到我自己的Blog: https://comeshare.net/category/study/system-design/ 
(以后修正更新都会发布到blog)

A complex system that works is invariably found to have evolved from a simple system that works. The inverse proposition also appears to be true: A complex system designed from scratch never works and cannot be made to work.—John Gall, Systemantics (1975)

  • Batch process is under the assumption all the data is Bounded, which means we know the finite size of the data we are dealing with, so it is known when the job is finished.
  • In reality, a lot of the data is unbounded. (because data keeps generated every second). This issue will force the batch process to divide data into “chunks”.
    • Which means the result/derived data is delayed based on the interval you are chosen.  (this is too slow for impatient users)
  • Continuously data processing without interruptions(break into chunks) is the idea behind streaming processing.
    • Think of “Stream” as a never stop flow of water/river that keep feeding data in;
    • E.g. (stdin/stdout, file inputstream, TCP stream etc.)
    • “Event Stream” as a data management mechanism
Transmitting Event Streams
  • Batch processing: Input are Files  vs. Streaming Processing: Input are Events;
  • What is Event: a small, self-contained, immutable object containing the details of something that happened at some point in time.
  • Event generated by a producer(Publisher/Sender) and then processed by multiple consumers(subscribers/recipients).
    • Related “events” are grouped together by Topic/Stream.
  • Batch vs. Stream processing is kind like the difference between Pull and Push.
    • Batch: is the consumer keep Pull event from DB.
    • Stream: is the DB keep push Event to Consumer.
  • Traditional DB/(RDMS) is not designed for “Stream/Event” processing
  • Message Systems:
    • A producer sends a message containing the event, which is then pushed to consumers.
    • MQ vs. Unix Pipe or TCP
      • MQ allows many-to-many relationships (Producer vs. Consumer)
      • As Unix Pipe & TCP is usually one-to-one
    • What happens if the producers send messages faster than the consumers can process them?  Three options
      • Drop message;  Queue ; backpressure (flow control)
      • What if Queue is full ?
    • What happens if nodes crash or temporarily go offline—are any messages lost?
      • Still a trade off between C & A (Consistency and Availability)
    • Direct messaging from producers to consumers: (prone to loss data)
      • UDP multicast: used in financial industry for streams such as stock market feeds;
      • Brokerless message library: ZeroMQ.
      • StatsD & Brubeck: UDP messaging.
    • Message brokers: (aka. Message Queue, e.g. ActiveMQ, RabbitMQ)
      • A special type of DB that optimized for Message Streams;
      • Producer → Broker → Consumer
      • Better fault tolerance; Message could be persist to disk;
      • It all asynchronously;
    • Message brokers compared to databases: (JMS, AMQP)
      • In MQ Some even support 2PC (two-phase commit);
      • In MQ, data is deleted right-after message is been consumed;
      • In MQ, usually assume the Queue is short.
      • DB Secondary Indexes vs. MQ subset of topics
      • MQ has no support for queries, but notify clients when data change
      • E.g. RabbitMQ, ActiveMQ, HornetQ, Qpid, TIBCO Enterprise Message Service, IBM MQ, Azure Service Bus, and Google Cloud Pub/Sub.
    • Multiple consumers:
      • Load balancing:  arbitrarily assigned to worker/consumer; Good for parallel processing expensive work-load.
      • Fan-out: Each message is sent to all consumers/worker; (topic subscription in JMS, exchange bindings in AMQP)
  • Two patterns could be combined.
  • Acknowledgments and redelivery:
    • acknowledgments: a confirmation from a client that it has finished processing a message so that the broker can remove it from the queue.
      • Note: due to network issues, the ack. Could be lost, then cause the ordering of the message change (C: In general, when you using Queue, you shouldn’t have cared about the order at first place)
  • Partitioned Logs:
    • Transient messaging mindset: transient operation that leaves no permanent trace.  (Which is totally opposite than DB or FileSystem)
    • MQ is NOT idempotent: receiving a message is destructive if the acknowledgment causes it to be deleted from the broker;
    • Why can we not have a hybrid, combining the durable storage approach of databases with the low-latency notification facilities of messaging?
      • Yes, log-based message brokers.
    • Using logs for message storage:
      • Log: is simply an append-only sequence of records on disk.
      • Log-based Message broker: A producer sends a message by appending it to the end of the log, and a consumer receives messages by reading the log sequentially.
      • This log can be partitioned.
      • Each message is offset by a sequence number. (totally ordered)
        • Note: no ordering guarantee across partitions tho.

  • E.g. Apache Kafka, Amazon Kinesis Streams, and Twitter’s DistributedLog. (millions of MPS by partitioning)
  • Logs compared to traditional messaging*:
    • the broker can assign entire partitions to nodes in the consumer group, Then each client consumes all the messages in the partitions it has been assigned.
    • JMS/AMQP style of message broker is preferable: Message is expensive, parallel processing, order doesn’t matter.
    • Log-based approach: high message throughput, where each message is fast to process and where message ordering is important.
  • Consumer offsets:
    • Similar to “log sequence number” in single-leader DB replication;
    • The message broker behaves like a leader database, and the consumer like a follower.
  • Disk space usage:
    • To prevent run out of storage, log is divided into segments, old segments are deleted or archived.
    • “Log” is kind of like a bounded-size buffer, if the consumer can’t keep up, the old message will be discarded. (aka. Circular buffer, ring buffer)
      • E.g. 6T HDD with 150MB/s write speed can buffer up to 11 hrs of messages.
  • When consumers cannot keep up with producers:
    • Three choices: dropping, buffering or backpressure(flow-control)
    • Consumers are independent from each other;
  • Replaying old messages:
    • it is a read-only operation that does not change the log.
    • Offset is under the consumer’s control. So, it has the freedom to go back to previous data/offset.
    • This made it easier for integration with other dataflows.
Databases and Streams
  • DB and Stream are correlated deeper; (e.g. write is an event)
  • The replication log is a stream of database write events, produced by the leader as it processes transactions.
  • Keeping Systems in Sync:
    • Often need to combine several different technologies in order to satisfy their requirements. (Write/Read/Search/Analytics etc.)
    • It is essential to keep all the data in-sync. if an item is updated in the database, it also needs to be updated in the cache, search indexes, and data warehouse. (usually through ETL processes.)
      • Or “Dual writes”, which prone to issue like race-conditiotion;

  • The key is to determine if we can have only one “Source of Truth” (aka. Leader)
  • Change Data Capture:
    • CDC(Change Data Capture): is a process that extracts DB changes and puts it into other systems.
      • E.g. in the form of “Stream”

  • Implementing change data capture:
    • We call any “log consumer” a “derived data system”. The idea behind CDC is to ensure all those “derived data systems” got the up-to-date changes.
    • Essentially, CDC makes one database the leader (the one from which the changes are captured), and turns the others into followers.
    • Potential Mechanism: DB Triggers(Con:performance overheads), Parsing replication log(Con:schema changes);
    • Usually done asynchronously → replication lag
  • Initial Snapshot:
  • Log compaction: (e.g. Apache Kafka)
    • This is used when need add a new “derived data system”
    • This allows the message broker to be used for durable storage, not just for transient messaging.
  • API support for change streams: (e.g. RethinkDB, FIreBase, CouchDB, Meteor, VoltDB)
    • DBs engine started to support change streams;
    • A table will hold transactions but can’t be queried.
  • Event Sourcing:
    • Event Sourcing: a technique that was developed in the domain-driven design (DDD) community.
    • CDC vs. ES(Event Sourcing):  different level of abstraction.
      • CDC: application isn’t aware of CDC occurring, so it happens at a lower level.
      • ES: reflect things that happened at the application level.
    • ES is a powerful technique for data modeling, because it makes more sense to record a user’s action as immutable events, rather than the effect of the actions on a mutable DB.
    • Event sourcing is similar to the chronicle data model. (or “fact table”).
    • Deriving current state from the event log:
      • Applications need to take the events and transform it to an application state that is suitable for the user to view. (deterministic)
    • Commands and events:
      • First it comes as “Command” and then after it successfully executed, it becomes “Event” which is durable and immutable.
        • when the event is generated, it becomes a fact.
      • A consumer of the event stream is not allowed to reject an event;
      • Any validation of a command needs to happen synchronously, before it becomes an event.
  • State, Streams, and Immutability:
    • Immutability is also what makes event sourcing and change data capture powerful.
    • Whenever you have a state that changes, that state is the result of the events that mutated it over time.
    • mutable state and an append-only log of immutable events do not contradict each other: they are two sides of the same coin.
      • the changelog, represents the evolution of state over time.
    • In terms of mathematical:
      • application state is what you get when you integrate an event stream over time;
      • a change stream is what you get when you differentiate the state by time;

    • Quote from Pat Helland:
      • Transaction logs record all the changes made to the database. High-speed appends are the only way to change the log. From this perspective, the contents of the database hold a caching of the latest record values in the logs. The truth is the log. The database is a cache of a subset of the log. That cached subset happens to be the latest value of each record and index value from the log.
    • Log compaction: bridging the distinction between log and DB state.  it retains only the latest version of each record, and discards overwritten versions.
    • Advantages of immutable events: (e.g. Account ledger)
      • Particularly important in financial systems, it is also beneficial for many other systems.
      • Capture more information than just the current state.
      • E.g. Shopping cart history with append-only event could help analytic in the future;
    • Deriving several views from the same event log:
      • You can derive several different read-oriented representations from the same log of events. (C: This idea is similar to the talk about Apache Kafka,built application around the Kafka stream)
      • Having an explicit translation step from an event log to a database makes it easier to evolve your application over time.  (C: enable old & new system running side by side)
      • Command Query Responsibility Segregation (CQRS): you gain a lot of flexibility by separating the form in which data is written from the form it is read.
    • Concurrency control:
      • The biggest downside of event sourcing and change data capture is asynchronous. → cause delay.
      • Potential Solutions:
        • “Reading your own writes”
        • “Implementing linearizable storage using total order broadcast”
    • Limitations of immutability:
      • Truly deleting data could be difficult because data live in many places.
      • Deletion is more a matter of “making it harder to retrieve the data” than actually “making it impossible to retrieve the data.”
Processing Streams
  • Where streams come from (user activity events, sensors, and writes to databases).
  • How streams are transported (through direct messaging, via message brokers, and in event logs).
  • Last question is What can you do with Stream ? three major options
    • 1, write it to a database, cache, search index, or similar storage system so it can be used by other applications/clients/systems.
      • Kind like maintaining materialized views.
    • 2, push the events to users directly;
    • 3, process one or more input streams to produce one or more output streams. (pipelining); processing streams to produce other, derived streams.
  • A block of Code that processes streams so called “Operator” or “Job”. (kind like Unique processes or MapReduce job)
    • Since the Stream never ends(unbounded), so sorting doesn’t make sense here, neither does sort-merge joins will be used.
    • Fault-tolerance mechanisms also need to be revised.
  • Use of Stream Processing
    • Monitoring System: Fraud detection, Trading System, Manufacturing System, Military and Intelligence systems.
    • Complex event processing(CEP): emerged from the 90s
      • CEP allows you to specify rules to search for certain patterns of events in a stream.
    • Stream analytics: (e.g. Apache Storm, Spark Streaming, Flink, Concord, Samza, and Kafka Streams, Google Cloud Dataflow and Azure Stream Analytics)
      • More oriented toward aggregations and statistical metrics over a large number of events;
      • Stream analytics systems sometimes use probabilistic algorithms, such as Bloom filters.
    • Maintaining materialized views:
      • Derived data systems can be treated as maintaining materialized views.
    • Search on streams:
      • The percolator feature of Elasticsearch is one option for implementing this kind of stream search.
    • Message passing and RPC:
  • Reasoning About Time
    • Time “window”
    • Using the timestamps in the events allows the processing to be deterministic.
    • Event time versus processing time: (e.g. Star War movies)
      • Processing may be delayed.
      • Confusing event time and processing time leads to bad data.

    • Knowing when you’re ready:
      • need to be able to handle such straggler events that arrive after the window has already been declared complete.
        • 1, Ignore the straggler events;
        • 2, Publish a correction;
    • Whose clock are you using, anyway?
      • Need address Incorrect device clocks, log three timestamps:
        • The time at which the event occurred, according to the device clock
        • The time at which the event was sent to the server, according to the device clock
        • The time at which the event was received by the server, according to the server clock
    • Types of windows:
      • Tumbling window: fixed length, and every event belongs to exactly one window.
      • Hopping window: fixed length, but allows windows to overlap in order to provide some smoothing.
      • Sliding window: contains all the events that occur within some interval of each other.
      • Session window: has no fixed duration. But, grouping together all events relative to the same user that occur closely together in time. (e.g. website analytics)
  • Stream Joins
    • Similar to batch jobs; However, since new events can appear anytime on a stream makes joins on streams more challenging than in batch jobs.
    • three different types of joins: stream-stream joins, stream-table joins, and table-table joins.
    • Stream-stream join (window join):
      • a stream processor needs to maintain state.
    • Stream-table join (stream enrichment):
      • Enriching the activity events with information from the database.
      • Instead of performing remote SQL queries, we can cache up a copy of DB. (In Memory hashtable or local disk index)
        • Need CDC to ensure the stream data is up-to-date;
      • A stream-table join is actually very similar to a stream-stream join, but in this case we have “table changelog stream” involved.
    • Table-table join (materialized view maintenance): (e.g. Tweets)
      • it maintains a materialized view for a query that joins two tables.
    • Time-dependence of joins:
      • Common: they all require the stream processor to maintain some state based on one join input, and query that state on messages from the other join input.
      • If the state changes over time, and you join with some state, what point in time do you use for the join ? (e.g. sales Tax calculation)
      • If the ordering of events across streams is undetermined, the join becomes nondeterministic;
      • slowly changing dimension (SCD):  addressed by using a unique identifier for a particular version of the joined record. (but this approach made log compaction impossible, because we need retain all version of the records)
  • Fault Tolerance
    • You can’t wait until a stream is finished to validate its output/result, since all the stream is unbounded and will never really finish/complete.
    • Microbatching and checkpointing:
      • Microbatching: break the stream into small blocks, and treat each block like a miniature batch process.  (e.g. Spark Streaming)  usually one second interval.
        • Smaller the batches size the greater overhead.
        • Larger batches size means longer delay of results.
        • implicitly provides a tumbling window equal to the batch size
      • Checkpointing: triggered by barriers in the message stream, similar to the boundaries between microbatches, but without forcing a particular window size.  (e.g. Apache Flink)
      • Both approaches won’t prevent external side effects after the results have been written into External Systems.
    • Atomic commit revisited:
      • Achieve “Exactly-Once” processing without transactions across heterogeneous technologies.
      • Idempotence:
        • Distributed transactions are one way of achieving that goal, but another way is to rely on idempotence.
        • if an operation is not naturally idempotent, it can often be made idempotent with a bit of extra metadata. (e.g. Kafka with some offset value)
      • Rebuilding state after a failure:
        • keep state local to the stream processor, and replicate it periodically.
        • sometimes the state can be rebuilt from the input streams.
Summary
  • Discussed event streams, what purposes they serve, and how to process them.
    • Similar to “batch processing” but unbounded.
    • message brokers and event logs serve as the streaming equivalent of a filesystem.
  • Two types of Message brokers:
    • AMQP/JMS-style message broker: exact order is not important
    • Log-based message broker: order is kept.
      • Similar to log-structured storage engines
  • Where streams come from ?
    • user activity events, sensors providing periodic readings, and data feeds (e.g., market data in finance)
    • writes to a database as a stream: capture the changelog
      • Change Data Capture
      • Event Sourcing
  • DB as streams is very useful for integrating different systems
    • E.g.   search indexes, caches, and analytics systems.
  • Stream joins and fault tolerance:  By maintaining state as streams and replaying messages
    • searching for event patterns (complex event processing),
    • computing windowed aggregations (stream analytics),
    • keeping derived data systems up to date (materialized views).
  • three types of joins:
    • Stream-stream joins
    • Stream-table joins
    • Table-table joins
  • fault tolerance and exactly-once semantics
    • microbatching,
    • checkpointing,
    • transactions,
    • idempotent writes.

Part 1: Foundation of Data Systems

Designing Data-Intensive Applications - Chapter 1 摘录读后感
(更新) https://www.1point3acres.com/bbs ... 617831&pid=11484152
(原帖) https://www.1point3acres.com/bbs/thread-617831-1-1.html

Designing Data-Intensive Applications - Chapter 2 摘录读后感
(更新) https://www.1point3acres.com/bbs ... 619627&pid=11484533
(原帖) https://www.1point3acres.com/bbs/thread-619627-1-1.html

Designing Data-Intensive Applications - Chapter 3 摘录读后感
(更新) https://www.1point3acres.com/bbs ... 621149&pid=11494109
(原帖)https://www.1point3acres.com/bbs/thread-621149-1-1.html

Designing Data-Intensive Applications - Chapter 4 摘录读后感
(更新)https://www.1point3acres.com/bbs ... 622790&pid=11517080
(原帖)https://www.1point3acres.com/bbs/thread-622790-1-1.html

Part 2: Distributed Data

Designing Data-Intensive Applications - Chapter 5 摘录读后感
(更新)https://www.1point3acres.com/bbs ... 623896&pid=11535000
(原帖)https://www.1point3acres.com/bbs/thread-623896-1-1.html

Designing Data-Intensive Applications - Chapter 6 摘录读后感
(更新)https://www.1point3acres.com/bbs ... 624433&pid=11548351  
(原帖)https://www.1point3acres.com/bbs/thread-624433-1-1.html  

Designing Data-Intensive Applications - Chapter 7 摘录读后感
(更新)https://www.1point3acres.com/bbs ... 625587&pid=11563943  
(原帖)https://www.1point3acres.com/bbs/thread-625587-1-1.html  

Designing Data-Intensive Applications - Chapter 8 摘录读后感
(更新)https://www.1point3acres.com/bbs ... 626563&pid=11578767
(原帖)https://www.1point3acres.com/bbs/thread-626563-1-1.html

Designing Data-Intensive Applications - Chapter 9 摘录读后感
(更新)https://comeshare.net/2020/04/18 ... ency-and-consensus/
(原帖)https://www.1point3acres.com/bbs/thread-628100-1-1.html  

Part 3: Derived Data

Designing Data-Intensive Applications - Chapter 10 摘录读后感
(更新)https://www.1point3acres.com/bbs ... 629666&pid=11599287  
(原帖)https://www.1point3acres.com/bbs/thread-629666-1-1.html

Designing Data-Intensive Applications - Chapter 11 摘录读后感
https://www.1point3acres.com/bbs/thread-632821-1-1.html

Designing Data-Intensive Applications - Chapter 12 摘录读后感
https://www.1point3acres.com/bbs/thread-636762-1-1.html


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表