Skip to content

Instantly share code, notes, and snippets.

@Ammar-64
Forked from Kishimoto96/cronandsockets.md
Last active June 10, 2023 12:19
Show Gist options
  • Save Ammar-64/9463e95e3d288e93d4d9569bbd98ec3c to your computer and use it in GitHub Desktop.
Save Ammar-64/9463e95e3d288e93d4d9569bbd98ec3c to your computer and use it in GitHub Desktop.

Discussion about Cron and Websockets:

  1. What is a cron job?
  2. Is it possible to specify a time zone when scheduling a task with cron?
  3. What happens if a cron job takes longer than expected to complete its execution? Will another instance start running while the first one is still executing?
  4. What is a websocket?
  5. What is the advantage of using WebSockets over traditional HTTP requests?
  6. What is Socket.IO?
  7. What do we mean by websocket handshake?
  8. What is redis?
  9. What is the purpose of using redis?
  10. Give 3 examples of applications that use redis?
  11. What is Design pattern in programming?
  12. what are the main three categories of design pattern?
  13. What are some benefits of using design pattern?
@omikay
Copy link

omikay commented Jun 10, 2023

@omikay | @baraah-berra | @ramin

  1. a cron job is a repetitive program run periodically based on a specific schedule.
  2. Yes. To use cron with timezone, you can:
  • Set your system's time zone to the intended time zone and then state the time for that zone in the cron job.
  • Specify a timezone for your whole cron file by setting a timezone as CRON_TZ=UTC, or any other timezone listed by timedatectl list-timezones.
  1. If a job takes longer than expected, then the next one is delayed. Quoting from Wiki If your function execution takes longer than the timer interval, another execution won't be triggered until after the current invocation completes. The next execution is scheduled after the current execution completes.
  2. WebSocket is a computer communications protocol providing full-duplex (A full-duplex (FDX) system allows communication in both directions and, unlike half-duplex, allows this to happen simultaneously.) communication channels over a single TCP connection. It is a stateful protocol, which means the connection between client and server will stay alive until it is terminated by either party (client or server). After closing the connection by either the client or server, the connection is terminated from both ends.
  3. HTTP is unidirectional, where the client sends the request, and the server sends the response. Almost all real-time applications like (trading, gaming, monitoring, and notification) services use WebSocket to receive the data on a single communication channel. All the frequently updated applications used WebSocket because it is faster than HTTP Connection.

If we want to fetch old data or want to get the data only once to process it with an application, we should go with the HTTP protocol. These old data are not required very frequently or fetched only once and can be queried by a simple HTTP request, so in this scenario, it’s better not to use WebSocket.
6. socket.io, a widely adopted JavaScript library, provides a seamless and efficient solution for implementing real-time communication in web applications.
7. The handshake is the "Web" in WebSockets. It's the bridge from HTTP to WebSockets. In the handshake, details of the connection are negotiated, and either party can back out before completion if the terms are unfavorable. The server must be careful to understand everything the client asks for; otherwise, security issues can occur.
8. Redis is an open-source, in-memory, key-value data store most commonly used as a primary database, cache, message broker, and queue. Redis delivers sub-millisecond response times, enabling fast and powerful real-time applications in industries such as gaming, fintech, ad-tech, social media, healthcare, and IoT.
9.

  • Performance: Redis runs in-memory and enables low-latency and high throughput. It supports millions of operations per second.
  • Replication: Redis offers asynchronous replication and Active-Active Geo-Distribution to ensure that data is available for both reads and writes. It also supports point-in-time backups.
  • Caching: Redis is used as a cache in almost every application to reduce the load on a relational or NoSQL database.
  • Simplicity: Redis makes complex applications easier to write and maintain. It provides clients for almost every popular language, making it easy to build applications that can run on any platform.
  • Flexible data structures​: Strings, Hashes, Lists, Sets, Sorted Sets, Bitmaps, HyperLogLogs, Geospatial, Streams are all data types that can be used in Redis. They can be used for queues, latest updates, and geosearching.
  • Caching Systems: Redis is often used as an in-memory cache due to its high performance and low latency. Applications that handle large volumes of data or frequently accessed data can utilize Redis to store the most frequently accessed data in memory, reducing the load on backend databases and improving overall system performance. For example, popular content management systems like WordPress use Redis for object caching to speed up page rendering.
  • Real-time Data Streaming: Redis has features that make it suitable for real-time data streaming applications. It offers publish/subscribe messaging capabilities, allowing multiple clients to subscribe to channels and receive real-time updates. This makes it useful for building applications like chat systems, real-time analytics, and live streaming platforms. For instance, popular messaging platforms like Slack use Redis for real-time message delivery.
  • Job Queues and Background Processing: Redis provides data structures and commands that are well-suited for building job queues and managing background tasks. It allows applications to add tasks to a queue, process them asynchronously, and track the status of tasks. This is particularly useful in scenarios where time-consuming or resource-intensive tasks need to be offloaded from the main application flow. A popular example of an application using Redis for job queuing is Resque, a background job framework for Ruby on Rails.
  1. A design pattern in programming is a reusable solution to a commonly occurring problem in software design. It is a general, proven approach that provides a template for solving a particular design or implementation issue. Design patterns typically address specific concerns within the overall architecture of an application, such as object creation, structuring relationships between objects, managing communication between components, or organizing the flow of control. They provide a higher-level abstraction and promote loose coupling between components, making the code more flexible and adaptable to changes.
  • Creational Patterns: These patterns deal with object creation mechanisms, providing flexibility in creating objects without directly coupling the code to their specific classes or implementation. Examples include the Singleton pattern, Factory pattern, and Builder pattern.
  • Structural Patterns: Structural patterns focus on how objects are composed to form larger structures or entities while keeping the relationships between objects flexible. Patterns such as Adapter, Decorator, and Composite fall into this category.
  • Behavioral Patterns: Behavioral patterns are concerned with communication and interaction between objects, defining common communication patterns, and the assignment of responsibilities. Examples include Observer, Strategy, and Template Method patterns.
  1. Reusability: Design patterns provide reusable solutions to common problems. Once a pattern is established and well-documented, it can be applied across different projects and scenarios. This saves development time and effort by avoiding the need to reinvent solutions to known problems.

Maintainability: Design patterns promote modular and organized code structures. They provide clear guidelines for separating concerns, establishing relationships between components, and defining responsibilities. This improves code readability and maintainability, as it becomes easier to understand and modify the codebase when following a well-known pattern.

Scalability: Design patterns help in building scalable applications. They provide abstraction and decoupling, allowing individual components to be modified or replaced without affecting the entire system. This modular approach facilitates the addition of new features or the adaptation to changing requirements, making the software more flexible and scalable.

Code Quality: Design patterns embody best practices and proven solutions to common problems. By following these patterns, developers can create more robust and reliable code. Design patterns promote concepts such as loose coupling, high cohesion, and separation of concerns, leading to cleaner, more understandable, and better-structured code.

Collaboration and Communication: Design patterns provide a common language and shared vocabulary among developers. They serve as a means of communication, allowing team members to understand and discuss the system's design using recognized patterns. This improves collaboration, eases knowledge transfer, and enhances teamwork.

Performance Optimization: Some design patterns, such as the Flyweight pattern or Proxy pattern, are specifically aimed at optimizing performance. These patterns provide techniques to manage resources efficiently, reduce memory usage, minimize network requests, or avoid unnecessary computations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment