ByteByteGo 2024年07月11日
EP118: What are the differences among database locks?
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文回顾了数据库锁的类型、API 分页的技术以及常见的架构模式,并解释了 URL 输入浏览器后的处理流程和数字钱包扫描支付的原理。

🤔 数据库锁:用于防止并发访问数据以确保数据完整性和一致性的机制。常见的数据库锁类型包括: - 共享锁 (S 锁):允许多个事务同时读取资源,但不允许修改。其他事务也可以获取同一资源的共享锁。 - 排他锁 (X 锁):允许事务读取和修改资源。在持有排他锁期间,其他事务无法获取同一资源的任何类型的锁。 - 更新锁 (U 锁):用于防止事务在打算更新资源时出现死锁。 - 模式锁:用于保护数据库对象的结构。 - 批量更新锁 (BU 锁):在批量插入操作期间使用,通过减少所需的锁数量来提高性能。 - 键范围锁:用于索引数据,以防止幻读(将新行插入事务已读取的范围内)。 - 行级锁:锁定表中的特定行,允许同时访问其他行。 - 页面级锁:锁定数据库中的特定页面(固定大小的数据块)。 - 表级锁:锁定整个表。这易于实现,但会显着降低并发性。

💻 API 分页:在 API 设计中处理大型数据集并提高性能至关重要。常见的分页技术包括: - 偏移量分页:使用偏移量和限制参数来定义起点和要返回的记录数。 - 游标分页:使用游标(唯一标识符)来标记数据集中位置。 - 页面分页:指定页码和每页的大小。 - 密钥集分页:使用密钥过滤数据集,通常是主键或其他索引列。 - 基于时间分页:使用时间戳或日期对记录进行分页。 - 混合分页:结合多种分页技术来利用它们的优势。

🏛️ 架构模式:MVC、MVP、MVVM、MVVM-C 和 VIPER 架构模式是应用程序开发中最常用的模式之一。它们用于克服早期模式的局限性。 - MVC(模型-视图-控制器)是最古老的模式,已有近 50 年的历史。 - 每个模式都有一个“视图”(V)负责显示内容和接收用户输入。 - 大多数模式都包含一个“模型”(M)来管理业务数据。 - “控制器”、“演示者”和“视图模型”是介于视图和模型(在 VIPER 模式中为“实体”)之间的翻译器。 - 这些翻译器可能非常复杂,因此已经提出了各种模式来使其更易于维护。

🌐 URL 输入浏览器后的处理流程: 1. 用户输入 URL 并按下回车键。 2. 浏览器使用 DNS 查找来查找域名的 IP 地址。 3. 浏览器与服务器建立 TCP 连接。 4. 浏览器向服务器发送 HTTP 请求。 5. 服务器处理请求并发送响应。 6. 浏览器呈现 HTML 内容。

💰 数字钱包扫描支付原理: - 商户生成二维码并显示在屏幕上。 - 消费者扫描二维码并支付。 生成二维码的步骤: 1. 收银员计算总金额。 2. 收银员的计算机将订单 ID 和金额发送到支付服务提供商 (PSP)。 3. PSP 将信息保存到数据库并生成二维码 URL。 4. PSP 的支付网关服务读取二维码 URL。 5. 支付网关将二维码 URL 返回到商家的计算机。 6. 商家的计算机将二维码 URL(或图像)发送到收银台。 7. 收银台显示二维码。 消费者使用数字钱包扫描二维码支付的步骤: 1. 消费者打开他们的数字钱包应用程序来扫描二维码。 2. 消费者确认金额无误后,点击“支付”按钮。 3. 数字钱包应用程序通知 PSP 消费者已支付给定的二维码。 4. PSP 支付网关将此二维码标记为已支付,并将成功消息返回到消费者的数字钱包应用程序。 5. PSP 支付网关通知商家消费者已支付给定的二维码。

This week’s system design refresher:


Ship every native SaaS integration your users need (Sponsored)

Is product asking your team to build native product integrations with 3rd party services (ie. Salesforce, Slack, etc.)?

Save 70% of the engineering effort with Paragon, so you can stay focused on your core competencies.

Offload the plumbing around integrations with:

See how 100+ SaaS companies orchestrate ingestion jobs, real-time sync, and event-driven automations with 3rd party SaaS apps in weeks, not months.

Learn more


What are the differences among database locks?

In database management, locks are mechanisms that prevent concurrent access to data to ensure data integrity and consistency.

Here are the common types of locks used in databases:

    Shared Lock (S Lock)
    It allows multiple transactions to read a resource simultaneously but not modify it. Other transactions can also acquire a shared lock on the same resource.

    Exclusive Lock (X Lock)
    It allows a transaction to both read and modify a resource. No other transaction can acquire any type of lock on the same resource while an exclusive lock is held.

    Update Lock (U Lock)
    It is used to prevent a deadlock scenario when a transaction intends to update a resource.

    Schema Lock
    It is used to protect the structure of database objects.

    Bulk Update Lock (BU Lock)
    It is used during bulk insert operations to improve performance by reducing the number of locks required.

    Key-Range Lock
    It is used in indexed data to prevent phantom reads (inserting new rows into a range that a transaction has already read).

    Row-Level Lock
    It locks a specific row in a table, allowing other rows to be accessed concurrently.

    Page-Level Lock
    It locks a specific page (a fixed-size block of data) in the database.

    Table-Level Lock
    It locks an entire table. This is simple to implement but can reduce concurrency significantly.


How do we Perform Pagination in API Design?

Pagination is crucial in API design to handle large datasets efficiently and improve performance. Here are six popular pagination techniques:


Latest articles

If you’re not a paid subscriber, here’s what you missed.

    A Crash Course in Database Sharding

    A Crash Course on Microservice Communication Patterns

    A Crash Course on Cell-based Architecture

    A Crash Course on Content-Delivery Networks (CDN)

    A Crash Course on REST APIs

To receive all the full articles and support ByteByteGo, consider subscribing:

Subscribe now


What distinguishes MVC, MVP, MVVM, MVVM-C, and VIPER architecture patterns from each other?

These architecture patterns are among the most commonly used in app development, whether on iOS or Android platforms. Developers have introduced them to overcome the limitations of earlier patterns. So, how do they differ?


What happens when you type a URL into your browser?

The diagram below illustrates the steps.

    Bob enters a URL into the browser and hits Enter. In this example, the URL is composed of 4 parts:
    ? scheme - ????://. This tells the browser to send a connection to the server using HTTP.
    ? domain - ???????.???. This is the domain name of the site.
    ? path - ???????/????????. It is the path on the server to the requested resource: phone.
    ? resource - ?????. It is the name of the resource Bob wants to visit.

    The browser looks up the IP address for the domain with a domain name system (DNS) lookup. To make the lookup process fast, data is cached at different layers: browser cache, OS cache, local network cache, and ISP cache.

    2.1 If the IP address cannot be found at any of the caches, the browser goes to DNS servers to do a recursive DNS lookup until the IP address is found (this will be covered in another post).

    Now that we have the IP address of the server, the browser establishes a TCP connection with the server.

    The browser sends an HTTP request to the server. The request looks like this:

    ??? /????? ????/1.1
    ????: ???????.???

    The server processes the request and sends back the response. For a successful response (the status code is 200). The HTML response might look like this:

    ????/1.1 200 ??
    ????: ???, 30 ??? 2022 00:01:01 ???
    ??????: ??????
    ???????-????: ????/????; ???????=???-8

    <!??????? ????>
    <???? ????="??">
    ????? ?????
    </????>

    The browser renders the HTML content.


How do you pay from your digital wallet, such as Paypal, Venmo, Paytm, by scanning the QR code?

To understand the process involved, we need to divide the “scan to pay” process into two sub-processes:

    Merchant generates a QR code and displays it on the screen

    Consumer scans the QR code and pays

Here are the steps for generating the QR code:

    When you want to pay for your shopping, the cashier tallies up all the goods and calculates the total amount due, for example, $123.45. The checkout has an order ID of SN129803. The cashier clicks the “checkout” button.

    The cashier’s computer sends the order ID and the amount to PSP.

    The PSP saves this information to the database and generates a QR code URL.

    PSP’s Payment Gateway service reads the QR code URL.

    The payment gateway returns the QR code URL to the merchant’s computer.

    The merchant’s computer sends the QR code URL (or image) to the checkout counter.

    The checkout counter displays the QR code.

These 7 steps complete in less than a second. Now it’s the consumer’s turn to pay from their digital wallet by scanning the QR code:

    The consumer opens their digital wallet app to scan the QR code.

    After confirming the amount is correct, the client clicks the “pay” button.

    The digital wallet App notifies the PSP that the consumer has paid the given QR code.

    The PSP payment gateway marks this QR code as paid and returns a success message to the consumer’s digital wallet App.

    The PSP payment gateway notifies the merchant that the consumer has paid the given QR code.


SPONSOR US

Get your product in front of more than 500,000 tech professionals.

Our newsletter puts your products and services directly in front of an audience that matters - hundreds of thousands of engineering leaders and senior engineers - who have influence over significant tech decisions and big purchases.

Space Fills Up Fast - Reserve Today

Ad spots typically sell out about 4 weeks in advance. To ensure your ad reaches this influential audience, reserve your space now by emailing hi@bytebytego.com

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

系统设计 数据库 API 架构模式 数字钱包 支付
相关文章