Skip to content

Instance Metadata Caching

Ray Luo edited this page Dec 11, 2019 · 6 revisions

Instance Metadata Caching

"I feel the need - the need for speed!" -- Quoted from the movie Top Gun

Every developer wants his/her program runs faster. In this essay, we are going to show you how you can add a one-liner to make your MSAL Python powered app to acquire a token roughly 50%~100% faster!

Preparation

  1. First let's pick an existing sample script to demonstrate this experiment. We choose the username password sample, because it would require no user interaction, so that our time measurement would not be affected by slow human interaction.

  2. Configure a config.json for your sample as you normally would.

  3. In order to time it, you would want to add this snippet at the very beginning of your sample:

    import time; t = time.time()

    and then you add this line at the very end of your sample:

    print("It took", time.time() - t, "second(s)")

    Ideally, you would want to comment out these lines of actual Microsoft Graph API call, because it is not part of token acquisition. Lastly, you can optionally also uncomment these two lines to enable logging for you to see what happens under the hood.

  4. Now run it several times. You will like see an output like this:

    It took 0.9160797595977783 second(s)
    
    It took 0.7800290584564209 second(s)
    
    It took 0.8385567665100098 second(s)
    

    So it took roughly 0.8x seconds to acquire a token.

Actual Magic

Now let's have some fun.

Install a 3rd-party module "requests-cache" by running this once: pip install requests-cache, and then add this line at the very beginning of your sample:

import requests_cache; requests_cache.install_cache()

Now run it several times. You will like see an output like this:

It took 0.41900205612182617 second(s)

It took 0.480008602142334 second(s)

It took 0.44204020500183105 second(s)

So it took roughly 0.4x seconds to acquire a token.

What is going on?

Before acquiring a token, MSAL would need to hit several discovery endpoint to find some metadata. These metadata are infrequently changed, so they can be cached and reused. All these are standard practice happening on the HTTP layer, that's why MSAL Python does not have to implement such behavior by itself, and a generic HTTP cache library would do this job for us.

In fact, this trick works not just for MSAL Python, it works for the other parts of your app.

Conclusion

Now you have it, a whopping 50% to 100% performance gain with only one single line of code. And it is universally applicable to all your apps utilizing HTTP.

Happy Hacking!