Delving into the world of React Native, we explore the intricacies of handling offline mode. A crucial aspect for any application, we'll unravel the techniques to ensure seamless user experience, even without internet connectivity.
Conclusion
We have discussed all scenarios of handling data locally, as well as Redux and Redux-Persist that help a lot with developing the offline mode of the mobile app.
Data Sync
Syncing is a concept used to make sure that both the local app and the server’s data are the same at all times
- Save a timestamp on local app data when the most recent server data is synced
- If some movie information is updated, syncing the info locally i.e. name changed, etc.
How to Implement the HPA?
Example: Create a docker container that runs Apache and PHP
- Create a Kubernetes deployment and run the container
- Use kubernetes configuration: apiVersion: apps/v1 kind: Deployment metadata: hpa-demo-pod
- Labels: “hpa”, “app:hpa-deployment,” “spec:”
- Creates a Docker container with a custom Docker image containing the PHP script
- Connect to the cluster via kubectl using a configuration file
- KUBECONFIG environmental variable to the path of the kubeconfig file
Advantages and disadvantages of HPA
Reduced cost: You don’t have to guess the number of nodes or pods needed to run your workloads
- Scaling up or down dynamically based on resource utilization: This saves you money
- Disadvantages: It may happen that very few pods are running on one node and still scale down to some extent
Architecture in React Native
To implement an architecture that checks all the concepts discussed above, we need some help from libraries like Redux-Persist and NetInfo
- Redux Persist is a JavaScript library for persisting and rehydrating data
- Asyncstorage is a key-value-based, unencrypted, asynchronous storage system that is global and can be used as the local storage with redux-persist for the app
Fetch Data Request Flow
In case the internet is not working, the movies list will be populated from locally-saved data using redux-persist.
- If the server receives new data that is not synced with the mobile app, it will be appended in local data to persist for the future
- It is up to the developer to decide the structure of API for smooth local operation
Offline Mode
As app developers, we strive to eliminate as many inconveniences on the user’s side as possible
- One such inconvenience can be losing Internet connection in the midst of using an app
- To avoid this, we must design for offline mode
- Strategies that serve as a base for designing offline applications in React Native
Conclusion
The main advantage of autoscaling is that resources are not over-provisioned.
Authorization in Offline Mode
Normally, auth token & refresh token are used for authorization. If the token is not refreshed at a specific time, it expires.
- When the app is not authorized, you can hit the refresh_token API to get authorization again. The app flow can be moved to the login screen or it should show the message ‘login back to sync data’.
Horizontal Pod Autoscaler (HPA) in Kubernetes
Let’s try to understand autoscaling and why we need it
Types of Autoscaling in Kubernetes
Horizontal Pod Autoscaler
- Adjusts the number of replicas of an application.
- Vertical Pod Autoscaler
- Adapts the resource requests and limits of a container;
- Cluster Autoscarcerator
- Configures number of nodes.
Migration
With redux-persist, whenever there is a structural change, the version number will be incremented.
- If no version is specified, the library recognizes reducer as minus 1 version. The key is to configure your persist configuration in your rootReducer.cfg.
Post Data Request Flow
If the internet is not working, the request data will be stored in sync_queue and at the same time persisted in redux-persist
- Whenever the internet reconnects, the app will dispatch the jobs from sync-queue one by one and remove them from the queue
What is HPA and where does it fit in the Kubernetes ecosystem?
HPA is a controller that lies in one of the components called ‘kube-controller-manager’ and is configured by HorizontalPodAutoscaler resource objects.
- scales the number of pods in a replication controller, deployment, replica set, or stateful set based on CPU utilization and can be configured to make scaling decisions based on custom or external metrics.
Metrics Server
A scalable, efficient source of container resource usage metrics for Kubernetes’ built-in autoscaling pipelines
- There are three types of metrics server APIs
- Resource Metrics API: Predefined resource metrics (CPU and memory) of pods and nodes
- Custom Met metrics API: Custom metrics associated with a kube-netes object
- External metrics API
- Horizontal scaling means increasing and decreasing the number of replicas. Vertical scaling means decreasing and increasing the compute resources of a single replica
How to develop a mobile app if the internet is not working
First, the UI/UX should reflect the fact that we are in offline mode.
- Then, at the data level, the features that can be handled offline have to be synced with the server. e.g. A queue should be maintained for all API requests, for executing requests when the internet connection will work.