Categories
Cloud NiHao Cloud Documentation Products Sotfware & Developers & DevOps Storage Tools & How-Tos

Lazy Loading: Intro

This post has been updated on May 31, 2021.

“A picture is worth a thousand words”. Graphical representation has the ability to impact straight to us and can deliver the intention on point. Do we still need examples like the shinchan cartoon to prove that images can turn conscious human beings into crazy heads? The portion of images and videos in typical web content is significant. And there is no doubt at all of removing such content just for the sake of site performance. Luckily, we have solutions like lazy loading that in a certain context.

Well, there is a cost to having graphical content and soaks huge bandwidth. More importantly, it costs both users and servers in terms of data capturing and bandwidth consumption. And project managers may be unwillingly to cut media resources from applications and end up being in frustrated debates on the optimization of the application. Here comes the lazy load technique.

Lazy load animation
credits

What is Lazy Loading?

Simply, lazy loading means loading the content only when the users need them to display. This means we can delay the loading of the object until the point where we need it. The user won’t have to wait for the elements on the page to load and start working. This leads to potentially save gigabytes of data over high traffic. For example, if a webpage has an image that the user has to scroll down to see, you can display the placeholder initially and lazy load the full image when the user arrives at that location.

As the user scrolls down the page, the image placeholders start coming into the viewport (visible part of the webpage). We trigger the load for these images when they become visible. Google lighthouse is one useful tool to analyze and shortlist the candidate images to implement the approach of lazy loading.

Implementing lazy loading

Usually, lazy loading is implemented in two ways. One is to bind it with the “image” tag and the other is to add the “background” properties. In HTML, the “src” tag triggers the loading of the object at the instance of the request. So whether it is a cover image or footer image, it will be preloaded. As an alternative, use the “data-src” keyword to eliminate upfront loading.
Example: <img data-src="reference to image">

Now let’s move to another way which is using background attributes in CSS. For the “background-image” keyword in CSS, the browser needs to build the Document Object Model and CSS Model to detect that the style is to be applied to the current object or not. If the condition does not apply to an element, then the browser does not load the background image. So here we can put the condition of the viewport and enable loading of images only when it is inside the viewport.

How to know when to trigger the loading?

Now, we’ve to implement when to load the media content. For this, we can use JavaScript event listeners like on scroll, orientation change. When either of the events occurs, we can easily detect that the lazy-loaded images are not yet triggered for loading. So, using the viewport of the screen size we can set the condition that if the image has entered the viewport then invoke the “src” tag and eventually trigger the loading. And once the image is loaded, release the event listeners. Find the below example for reference:

Intersection Observer API is also an efficient and simple way to detect when an element enters the viewport and take actions accordingly. We bind the observer on all images and once the API detects the object in the parameter of the viewport, using the “isIntersecting” property apply the “src” tag on the images to be loaded. Once done, remove the observer from the image. Find below pen for reference:

If we compare both the Event method and Intersection Observer method, we would find that the Intersection method is much more flexible and quicker and gives better performance. While the event method may come with slight variations. However, the intersection observer API is not yet available in all the mediums.

Lazy loading presents a great performance benefit for larges sites like e-commerce vendors where they may need to load thousands of product images. In the latest updates, chromes browser has started supporting lazy loading as a native feature. So, the developer only has to put the relevant attributes and embed them with images.

Benefits of implementing lazy loading in your app:

Improving initial loading time: This approach decreases the wright of payload and allows quicker response

Bandwidth conservation: Only the requested data is loaded that implies saving of data.

Resource conservation: We can save server and use resources on a very large scale because we enable rendering of only required content

jQuery lazy, lazysizes, yall.js, Magento lazy loader, WordPress lazy load, Shopify lazy plugin are some of the popular plugins/libraries which we can use and implement this approach.

The simplest way to test lazy loading is working or not is to analyze the loading order. In Chrome, navigate to Inspect site => Network => images and refresh the page to fetch the whole timeline. Test the lazy loading by scrolling and observing the loading time.

It’s time to get lazy, folks! Lazy load is seriously uncontroversial in the concern of performance. Many customers at Sesame Disk have fairly adopted such an approach in their environments and have got considerably good results.

Hits: 30

Categories
General Topics and tips Products Sotfware & Developers & DevOps Storage Tools & How-Tos

GFW; How does it work?

Great wall like the GFW
Chinese Great wall inspired the GFW.

GFW or the Great Firewall of the China is the subject of this article. As you might guess we will discuss the basics you need to understand of how it works. After reading this you should be able have a discussion about the GFW. Aside you will be able to continue a deeper research on how to avoid colliding with it head on.

Update: June 8, 2021

How do you know about this GFW thing?

If you are from the western world or anywhere but china mainland. Likes are that you never dealt much with anything similar to the GFW. Well unless your employer wanted to block social media or something like that. Saving the differences.

I as a “young and naive” engineer moved to China. Then the firs thing I did at landing time was try to message my family and friends using a regular western Social media App. To my surprise It was “not possible”. Then after a day or two I figured it out. The Chinese Governement created this BEAST they call the GFW (The great firewall of China), the job of this “machine” is to block stuff that they do not want Chinese people to access.

What is blocked by the Chinese GFW?

Well, if you had the control freak boss trying to get you not to use FB during work. You know more or less what the GFW is, it differs from the the regular little bit controlling employers wanting to block some website(s). Meaning that I later learned many things about how the Chinese networks and internet policies work. In fact it has the full power and resources of the Chinese government behind. There are researchers and security engineers working full-time, has current AI algorithms to adapt and figure out where people are going why and make decisions on the fly. Finally not to make it too long it’s a sophisticated machine.

What is blocked by the GFW?

A few million websites, but more most known include the following; Facebook, Google, Twitter, Youtube, Netflix, ALL The biggest international newspapers in the world that are not controlled and protected by the Chinese government. This includes part of our cloud storage, file sharing and collaboration systems and more even when we are no youtube and well OOOH!!! SO MUCH MORE…

This post

To be clear in this article, I would like to explain how Chinese Mainland is controlling their Internet. As I would call it, Chinese Mainland is running a “Mainland Intranet” which is protected by The Great Firewall of China (GFW).

At Sesame Disk / Nihao Cloud we have had a long history of dealing with this “Beast” called the GFW. Give us a try.

Mini Office Firewall

We can compare this with your office or home where your router protects your computers. Meaning the router has local “Parental” like settings that gives access to the Internet. Aside it also controls or blocks websites with possible harmful content for your kids. At least that’s how some companies block Facebook during office hours as well.

Bigger Router- Bigger Wall

The GFW works similar but on a much larger scale. Instead of just dealing with your small office, the GFW is filtering much of the traffic going in and out of Chinese Mainland.

here I will explain how Internet Traffic works in a short and simple way. By the way you don’t need to be a geek to understand it :).

Understanding Internet Traffic Rules.

Traffic on internet
Cities in China

Question like. So how does an actual E-Mail gets sent or received over the Internet? Furthermore, how can we browse websites or stream movies?

What happens is more or less the following. First when you request a site or service or movie to load; the data to travel fast back and forth it has to follow Internet traffic rules. Second, using these rules all the data gets divided then sent and delivered in “little” IP packets. Where IP is like a packaging mechanism for internet communications. For instance, lets imagine we want to look at a funny cat video on Youtube. Right away we click to load the video. Internally Youtube server dissembles that video into thousands of little packets and sends it to your laptop, PC or mobile. Then these packets get reassembled back into a video again. This happens very fast, that is why you normally do not notice.

All you need to know from this, that all Internet Traffic travels in little packets.

IP Address
local host joke
IP Addresses

So how did Youtube know where to send those little packets? On the Internet every destination: computer, website or cellphone has to have an IP Address to send and receive data on the Internet. Google for “my IP” and you can see your own IP address. IP addresses are a bunch of numbers because computers can then convert them into 1’s and 0’s but we need domain names so we can memorise it.

Otherwise, instead of sesamedisk.com, you would see 52.79.32.36. Not the most memorable web address right?

Whats is DNS?

DNS systems
DNS – Domain Name Server

For the “Internet” to understand those Domains names, we have Domain Name Service (DNS). DNS servers translate domain names back into an IP addresses.

Meaning (sesamedisk.com → 13.124.52.38). There are thousands of those DNS Servers that translate Domain Names into IP Addresses. How it works internally is not that important now.

OK, enough of dry and boring theory and back to how does the Great Firewall of China and how it works.

DNS Spoofing

The first and most efficient way to block websites is by DNS spoofing or “Domain name system poisoning”.

DNS poisoning
DNS Poisoning

So let’s go back to our cat video. When we type youtube.com on the browser, DNS Server receives a request to check what youtube.com means and send you to the right IP address. This process happens to any Internet communication like web browsing etc.

Now, DNS requests for websites outside of Chinese Mainland will be taken to a Deep Package Inspection (DPI) by the GFW. This means, that each little IP Package will be opened and checked for the content like a customs control.

If any pattern found in that Package matches unwanted content, domains like the list of services provided before. Then the GFW returns a bogus IP Address and therefore the Website will not open.

That’s why if you are in China and try to reach Youtube.com you will receive this website:

Result of the GFW in action
Result of the DNS poisoning done by the GFW.

What they do in many cases is that they take your request for let say nihaocloud.com and resend it to another IP that is also in their list of DNS poisoning like Facebook, then the FB site tells you that this nihaocloud.com is not there.
This is a very simplified way to explain it, but it has the basics.

VPN & Encrypted Traffic

VPN technology
VPN Technology

Even though GFW knows that this traffic is encrypted but it can only can guess what is inside. Here lies one of the biggest opportunity to get the traffic across the GFW. Now, GFW is UNCERTAIN, whether this is a simple Youtube access which can be blocked, or this is the most important connection for the financial transactions where by cutting this connection could cause huge damage to the economy. Sometimes they get happy trigger and just block much of what is encrypted, but then they have to relax it afterwards for the economy. This tends to happen when there are protests, commemorations of historical events that are not to be talked about in China, etc.

In other words you need an encryption that is good enough not to be penetrated by current algorithms, as the GFW has deep inspection algorithms. Uses AI to “guess” what is inside packages, etc.

UNCERTAINTY- that’s the biggest headache for the GFW.

The keyword here is “Collateral Damage” which could be caused by blocking encrypted packets. This Collateral Damage has to be kept to a minimum.

  • understand collateral damage as the term invented by the Americans to describe questionable things that happened under their watch in the Middle East.

So what is the GFW is doing with this?

As we can imagine the staff behind the GFW are not stupid and came up with an “Active Probing” mechanism. The GFW is now looking for the encrypted traffic and guessing the encryption / VPN Protocols.

Then a Probing Servers at GFW takes encrypted packets and forwards the Package to the receiver then waits for the reply. From those replies, the GFW can decide what to do with that Package, drop it or let it go through. This decisions are sometimes evaluated with historical data, response data, volumes and many other metrics. Later on all the data is taken and analyzed in some cases by an AI algorithm that decides what is to be done ultimately.

So to sum it up the GFW is working in 2 ways:

DNS Poisoning

Active Probing

One more interesting thing to know is that different networks in Chinese Mainland are running through different filtering. While public Networks like China Unicom or Telecom have strongest filters, Networks for Universities (eg the CERNet – China Education and Research Network) is filtered less harshly.

We should keep in mind that the GFW is a very dynamic Beast. It is always trying to find the optimal balance between Collateral Damage and efficiently blocking the Internet. So some Services can be blocked today, but not tomorrow and again blocked the day after.

After word

Those who live in Chinese Mainland know that during big public events or holidays the GFW is controlling much tougher than usual. Also, the GFW is learning and being constantly developed and improved. So what we understand today might change tomorrow. The Chinese Government invests on it as if their lives dependents on it. At the end… Who knows?

All we know can be found by research and testing but nobody will explain how it really works. I would also like to mention that this article is not only based on our accumulated years IT experience in Chinese Mainland but also on a Research, presented at the annual Conference of the CCC –Chaos Computer Club Hamburg

So there will be always a battle between the GFW and VPN Providers and there will always be the problem of Collateral Damage and nobody wants to hurt the economy. Notice that is valid to mention that the GFW has also created an alternative economy that grows year after year.

The GFW is well prepared and will always be as far as the Chinese government is concerned. Also, do not forget that Chinese Mainland also has placed legal notices to fight this battle offline. 

UPDATE: Sesame Disk / NiHao Cloud was interviewed by BBC Business daily, about China’s Internet Privacy Clampdown. You can listen to the full episode here

Hits: 74