15 Isolating timelines

 
Image

In this chapter

  • Learn how to draw timeline diagrams from code.
  • Understand how to read timeline diagrams to find bugs.
  • Discover how to improve code design by reducing resources shared between timelines.

In this chapter, we will start using timeline diagrams to represent sequences of actions over time. They help us understand how our software runs. They are particularly useful in a distributed system, like when a web client talks to a web server. Timeline diagrams help us diagnose and predict bugs. We can then develop a solution.

Image There’s a bug!

MegaMart support is getting a lot of phone calls about the shopping cart showing the wrong total. The customers add stuff to their cart, it tells them it will cost $X, but when they check out, they are charged $Y. That’s no good, and the customers are not happy. Let’s see if we can debug this for them.

We can’t reproduce it when we click through slowly

Image
Image
Image

The slow clickthrough seems to work. But clicking things quickly will result in a different outcome. Let’s check it out.

Image Now we can try to click twice fast

Customers told us the bug happened when they clicked quickly

We can reproduce the bug by clicking on the buy button twice very quickly. Let’s see that:

Image
Image

We tested it a few more times and got a variety of results

We ran the same scenario (add shoes twice fast) several times. We got these answers:

  • $14**
  • $16
  • $22 ** the correct answer

The timeline diagram shows what happens over time

The two fundamentals of timeline diagrams

Two tricky details about the order of actions

Drawing the add-to-cart timeline: Step 1

Asynchronous calls require new timelines

Different languages, different threading models

Summary

sitemap